-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsparse-checkout
executable file
·109 lines (85 loc) · 2.43 KB
/
sparse-checkout
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# FUNCTIONS
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function apply() {
local folder_names
folder_names=$1
IFS=', ' read -r -a folder_names_arr <<<"${folder_names}"
for folder_name in "${folder_names_arr[@]}"; do
if [ ! -d "$folder_name" ]; then
echo " :> [ERROR] Invalid parameter ($folder_name). Use a valid folder name instead."
exit 2
fi
done
pwd=$(pwd)
if [ ! -f "${pwd}/.sparse-checkout" ]; then
echo " :> [ERROR] .sparse-checkout file does not exist in project's root folder."
exit 3
fi
IFS=$'\n' read -d '' -r -a keep_folders_arr < "${pwd}/.sparse-checkout"
echo " :> Sparse checkout will be applied for ${folder_names}"
git config core.sparsecheckout true
echo "/*" >.git/info/sparse-checkout
pipe_delimited_keep_folders=$(
IFS=$'|'
echo "${keep_folders_arr[*]}"
)
pipe_delimited_folder_names=$(
IFS=$'|'
echo "${folder_names_arr[*]}"
)
INCLUDED_FOLDERS=".*(${pipe_delimited_keep_folders}|${pipe_delimited_folder_names}).*"
for d in */; do
if [[ ! "$d" =~ $INCLUDED_FOLDERS ]]; then
echo "!${d}" >>.git/info/sparse-checkout
fi
done
git read-tree -mu HEAD
echo " :> Removing irrelevant folders"
git clean -fd &>/dev/null
for d in */; do
if [[ ! "$d" =~ $INCLUDED_FOLDERS ]]; then
rm -rf "${d}"
fi
done
echo " :> Sparse checkout completed for ${folder_names}"
}
function reset() {
echo "/*" >.git/info/sparse-checkout
git read-tree -mu HEAD
git config core.sparseCheckout false
git reset --hard HEAD
echo " :> Sparse checkout is resetted. All folders are accessible for now."
}
function check_clean_state() {
output=$(git status --porcelain)
if [[ -n ${output} ]]; then
echo " :> [ERROR] Uncommitted changes exist. Terminating."
exit 4
fi
}
function usage() {
echo "Usage:"
echo "sparse-checkout help"
echo "sparse-checkout reset"
echo "sparse-checkout apply FOLDER_NAME_1 FOLDER_NAME_2 FOLDER_NAME_3 [FOLDER_NAME_N]"
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# MAIN
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OPERATION=$1
FOLDER_NAMES=${*:2}
check_clean_state
if [ "$OPERATION" == "apply" ]; then
echo " :> Sparse-checkout will be resetted before enabling"
reset
apply "$FOLDER_NAMES"
elif [ "$OPERATION" == "reset" ]; then
reset
elif [ "$OPERATION" == "help" ]; then
usage
else
echo "Invalid parameter ($OPERATION)."
usage
fi