This repository has been archived by the owner on Oct 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit-shadow
executable file
·344 lines (269 loc) · 8.22 KB
/
git-shadow
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/bin/bash
# set -x
upsearch () {
slashes=${PWD//[^\/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/$1" && echo "$directory/$1" && return
directory=$(dirname "$directory")
done
}
GITUNDERCONTROL=$(git rev-parse --is-inside-work-tree 2> /dev/null)
GITTOPLEVELWORKTREE=$(git rev-parse --show-toplevel 2> /dev/null)
GITKEEP='.gitkeep'
GITSHADOWCONFIG='config'
GITSHADOWUNDERCONTROL=true
GITUSETOPLEVEL=false
LINUXTOPLEVELWORKTREE=$(echo /$GITTOPLEVELWORKTREE|sed 's/://g'|sed 's/\/\//\//g')
GITKEEPWORKTREETOPLEVEL="$LINUXTOPLEVELWORKTREE/$GITKEEP"
GITKEEPWORKTREE=$(upsearch "$GITKEEP")
LINUXWORKTREE=$(dirname "$GITKEEPWORKTREE")
if [[ -z $GITKEEPWORKTREE ]]; then
GITKEEPWORKTREE="$(pwd)/$GITKEEP"
GITSHADOWUNDERCONTROL=false
fi
CONFIGFILETOPLEVEL="$LINUXTOPLEVELWORKTREE/$GITSHADOWCONFIG"
CONFIGFILE="$GITKEEPWORKTREE/$GITSHADOWCONFIG"
loadShadowEnvironment() {
getShadowRepoList
getBinPath
getCurrentRepo
}
SHADOWREPOLIST=""
getShadowRepoList(){
# SHADOWREPOLIST
if [[ -f $CONFIGFILE ]]; then
#statements
SHADOWREPOLIST=$(<"$CONFIGFILE")
fi
}
BINPATH=""
getBinPath(){
# BINPATH
# user script should be installed to local/bin on linux/unix
case `uname` in
Darwin )
platform="MacOSX"
BINPATH="/usr/local/bin"
;;
Linux )
;;
MINGW32_NT-* )
platform="Windows"
BINPATH="/usr/bin"
;;
esac
}
# call from gitShadowCreate
# save current shadow repo to $currentRepo
CURRENTREPO=""
getCurrentRepo(){
for repo in $SHADOWREPOLIST; do
CURRENTREPO=$repo
break
done
}
setCurrentRepo() {
# set current repo
# set <empty string> if not exist
# $1: repo
CURRENTREPO=""
for repo in $SHADOWREPOLIST; do
if [[ $1 == $repo ]]; then
CURRENTREPO=$1
break
fi
done
}
# todo redo the code
printUsage() {
echo -e "Install: ./git-shadow install"
echo -e " Usage: git shadow [shadow command]"
echo -e " or: git shadow [git command]"
echo -e ""
echo -e "Git shadow commands:"
echo -e "install install this script"
echo -e "uninstall remove this script from system"
echo -e "clone [repo] [url]"
echo -e "current list all shadow repos"
echo -e "current [repo] set default repo"
}
# private
gitShadowCreate() {
# find if repo user inputed exists
# if not create
# else do nothing
# $1 repo
# $2 git keep path
# if .gitkeep not exit create it
if [[ ! -d $2 ]]; then
mkdir $2
fi
setCurrentRepo $1
if [[ -z $CURRENTREPO ]]; then
if [[ ! -f $2/$1.gitignore ]]; then
echo ".git-*" > "$2/$1.gitignore"
echo ".gitkeep" >> "$2/$1.gitignore"
fi
echo $1 $SHADOWREPOLIST > "$CONFIGFILE"
CURRENTREPO=$1
echo $1 created and set as default repo.
fi
}
#######################################
# main entry
#######################################
gitShadowInstall() {
# tip: mingw32 ln.exe cannot create symbolic link only copy it to dest
# so you should run `./git-shadow install` after update
ln -f $(dirname $0)/$(basename $0) $BINPATH/git-shadow
echo "installed/updated."
}
gitShadowUninstall() {
rm $BINPATH/git-shadow
echo "removed."
}
gitShadowClone() {
gitShadowCreate $1
git --work-tree="$LINUXWORKTREE/tmp/" clone $2 "$LINUXWORKTREE/tmp/.git-$CURRENTREPO"
echo merge files to main repo.
cp -rf ./tmp/* .
echo copy .git-$CURRENTREPO to main repo-dir.
mv tmp/.git-$CURRENTREPO .
rm -rf ./tmp/
git --work-tree="$LINUXWORKTREE/$GITKEEP" --git-dir="$LINUXWORKTREE/.git-$CURRENTREPO" checkout master -- .gitignore
mv "$LINUXWORKTREE/$GITKEEP/.gitignore" "$LINUXWORKTREE/$GITKEEP/$CURRENTREPO.gitignore"
}
gitInvoke() {
if [[ -z $CURRENTREPO ]]; then
echo "Can not found current repo."
echo "Type 'git shadow init' to init git shadow."
exit
fi
# echo $LINUXWORKTREE/$GITKEEP/$CURRENTREPO.gitignore
# create empty .gitignore if origin repo/.gitignore not exists
if [[ ! -f "$LINUXWORKTREE/.gitignore" ]]; then
echo "" > "$LINUXWORKTREE/.gitignore"
fi
if [[ $GITUSETOPLEVEL == true ]]; then
if [[ -f "$LINUXTOPLEVELWORKTREE/$GITKEEP/$CURRENTREPO.gitignore" ]]; then
mv "$LINUXTOPLEVELWORKTREE/.gitignore" "$GITKEEPWORKTREETOPLEVEL/git.gitignore"
cp "$GITKEEPWORKTREETOPLEVEL/$CURRENTREPO.gitignore" "$LINUXTOPLEVELWORKTREE/.gitignore"
fi;
git --work-tree="$LINUXTOPLEVELWORKTREE" --git-dir="$LINUXTOPLEVELWORKTREE/.git-$CURRENTREPO" "$@"
if [[ -f "$GITKEEPWORKTREETOPLEVEL/$CURRENTREPO.gitignore" ]]; then
mv "$GITKEEPWORKTREETOPLEVEL/git.gitignore" "$LINUXTOPLEVELWORKTREE/.gitignore"
fi;
else
if [[ -f "$LINUXWORKTREE/$GITKEEP/$CURRENTREPO.gitignore" ]]; then
mv "$LINUXWORKTREE/.gitignore" "$GITKEEPWORKTREE/git.gitignore"
cp "$GITKEEPWORKTREE/$CURRENTREPO.gitignore" "$LINUXWORKTREE/.gitignore"
fi;
git --work-tree="$LINUXWORKTREE" --git-dir="$LINUXWORKTREE/.git-$CURRENTREPO" "$@"
if [[ -f "$GITKEEPWORKTREE/$CURRENTREPO.gitignore" ]]; then
mv "$GITKEEPWORKTREE/git.gitignore" "$LINUXWORKTREE/.gitignore"
fi;
fi
}
gitShadowCurrent() {
if [[ -z $1 ]]; then
# echo shadow repo list:
firstTag="\e[32m*(current)\e[0m"
shadowRepoExists=false
for repo in $SHADOWREPOLIST; do
echo -e $i $repo$firstTag
firstTag=""
shadowRepoExists=true
done
if [[ $shadowRepoExists == false ]]; then
echo -e "Nothing found in shadow repo list."
fi
else
setCurrentRepo $1
if [[ $CURRENTREPO == $1 ]]; then
echo $1 > $CONFIGFILE
for repo in $SHADOWREPOLIST; do
if [[ $1 != $repo ]]; then
echo $repo >> $CONFIGFILE
fi
done
getShadowRepoList
getCurrentRepo
echo git-shadow: current repo set to $CURRENTREPO
else
echo git-shadow: shadow repo not found, have you create before?
fi
fi
}
gitShadowInit() {
# para => init(1) --toplevel(2) deploy-repo(3)
if [[ $# == 3 ]]; then
if [[ $2 == "--toplevel" ]]; then
# CONFIGFILE="$LINUXWORKTREE/$GITKEEP/$GITSHADOWCONFIG"
echo "CONFIGFILETOPLEVEL"
echo $CONFIGFILETOPLEVEL
echo $3
echo "under git control"
echo $GITUNDERCONTROL
if [[ $GITUNDERCONTROL != true ]]; then
echo "This directory is not under git control."
echo "Try remove '--toplevel' option to init git shadow repo in current directory."
exit
fi
gitShadowCreate $3 $GITKEEPWORKTREETOPLEVEL
GITUSETOPLEVEL=true
else
echo "'$2' is not a vaild option, do you mean '--toplevel' ?"
exit
fi
else
gitShadowCreate $2 $GITKEEPWORKTREE
GITUSETOPLEVEL=false
fi
gitInvoke "init"
}
gitShadowDebug() {
VAR=''
echo ${VAR-"defalult"}
echo $VAR
}
#######################################
# bootstrapper
#######################################
echo -e "git-shadow-repos 0.36.1"
# echo -e $(dirname $0) ** find script path
if [[ -z "$1" ]]; then
if [[ "$BASH_SOURCE" = *\.* ]]; then
#statements
echo -e "Please install this script first."
echo -e `basename "$0"` install
else
printUsage
fi
else
loadShadowEnvironment
case $1 in
debug )
gitShadowDebug
;;
install )
gitShadowInstall
;;
uninstall )
gitShadowUninstall
;;
clone* )
gitShadowCopy $2 $3
;;
current* )
gitShadowCurrent $2
;;
init* )
gitShadowInit $@
;;
*)
gitInvoke "$@"
;;
esac
fi