-
Notifications
You must be signed in to change notification settings - Fork 1
/
clone-subdir.sh
executable file
·93 lines (69 loc) · 1.87 KB
/
clone-subdir.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
set -e
# Colorize terminal
red='\e[0;31m'
no_color='\033[0m'
# Defaults
DELETE_GIT_DIR="false"
GIT_BRANCH="main"
OUTPUT_DIR="$(pwd)"
# Declare script helper
TEXT_HELPER="\nThis script aims to clone a git subdirectory.
Following flags are available:
-b Git branch to clone.
Default is '$GIT_BRANCH'.
-d Delete git directory after clone.
Default is '$DELETE_GIT_DIR'.
-o Output directory to clone the repository.
Default is '$OUTPUT_DIR'.
-s Sub-directory to clone.
-u Url of the target repository.
-h Print script help.\n\n"
print_help() {
printf "$TEXT_HELPER"
}
# Parse options
while getopts hb:do:s:u: flag; do
case "${flag}" in
b)
GIT_BRANCH="${OPTARG}";;
d)
DELETE_GIT_DIR="true";;
o)
OUTPUT_DIR="${OPTARG}";;
s)
SUB_DIR="${OPTARG}";;
u)
REPO_URL="${OPTARG}";;
h | *)
print_help
exit 0;;
esac
done
# Script conditions
if [ -z "$REPO_URL" ]; then
printf "\n${red}Error.${no_color} Argument missing : repo url name (flag -u)".
exit 1
fi
if [ -z "$SUB_DIR" ]; then
printf "\n${red}Error.${no_color} Argument missing : subdirectory to clone (flag -s)".
exit 1
fi
# init git repository in the output dir
printf "\n\n${red}[clone subdir]${no_color} Init git repository\n\n"
[ ! -d "$OUPUT_DIR" ] && mkdir -p "$OUTPUT_DIR"
cd "$OUTPUT_DIR"
git init
git remote add origin "$REPO_URL"
git config core.sparsecheckout true
# Clone sub directory from the target repository
printf "\n\n${red}[clone subdir]${no_color} Clone repository\n\n"
echo "$SUB_DIR/*" >> .git/info/sparse-checkout
git pull origin "$GIT_BRANCH"
cp -aR "$SUB_DIR/." . && rm -rf "$SUB_DIR"
cd - > /dev/null
# Delete git artifacts in the fresh cloned repo
if [ "$DELETE_GIT_DIR" == "true" ]; then
printf "\n\n${red}[clone subdir]${no_color} ungit cloned repoitory\n\n"
rm -rf "${OUTPUT_DIR%/}/.git"
fi