-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedGit.sh
511 lines (425 loc) · 13.1 KB
/
LinkedGit.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/env bash
# LinkedGit --- A shell of git for multi-workspace support
# Refer to https://github.com/outofmemo/LinkedGit for introduction.
# Contact: [email protected]
self=$0
# Colors
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
plain='\033[0m'
print_error(){
echo -e "${red}Error: $1$plain" >&2
}
print_warning(){
echo -e "${yellow}Warning: $1$plain" >&2
}
print_info(){
echo "LinkedGit: $1"
}
remove_relative_symbol() {
local name=$1
local path=$2
path=$path/
path=`echo $path | sed "s/\/\//\//"`
path=`echo $path | sed "s/\/\.\//\//"`
path=`echo $path | sed "s/[0-9,a-w,A-W,-,_,.]*\/\.\.\///"`
if [[ "$path" = */ ]]; then
path=${path%/*}
fi
if [[ "$path" = ./* ]]; then
path=${path#*/}
fi
eval "$name=$path"
}
resolve_relative_path() {
# Full absolute destination path
local dest_path=$1
# Full absolute work path
local work_path=$2
local relative_path
local dest_name
local work_name
#remove_relative_symbol dest_path $dest_path
#remove_relative_symbol work_path $work_path
# Firstly, search the common parent directory
while [[ -n "$dest_path" ]]; do
dest_path=${dest_path#*/}
work_path=${work_path#*/}
dest_name=${dest_path%%/*}
work_name=${work_path%%/*}
if [[ "$dest_name" != "$work_name" ]]; then
break
fi
done
# Secondly, go to the common parent directory
if [[ -n "$work_path" && "$work_path" != */ ]]; then
work_path=$work_path/
fi
while [[ -n "$work_path" ]]; do
work_path=${work_path#*/}
relative_path=${relative_path}../
done
# Lastly, go to the destination directory
relative_path=${relative_path}$dest_path
eval "$3=$relative_path"
}
find_git_path(){
# Full absolute work path
local work_path=$1
while [[ -n "$work_path" ]] && [[ ! -d "$work_path/.git" ]]; do
work_path=${work_path%/*}
done
if [[ -n "$work_path" ]]; then
eval "$2=$work_path/.git"
else
eval "$2=''"
fi
}
Lgit_exec(){
local work_path
local git_dir
local work_git_path
local real_git_path
local link_prefix
local image_prefix
local args
local ref
local ret
if [[ -z "$org_git" ]]; then
print_error "LinkedGit is not installed, run '$self install' first."
exit 1
fi
work_path=`pwd`
find_git_path "$work_path" work_git_path
if [[ -z "$work_git_path" ]]; then
# Can't find .git, let orginal git solve it
"$org_git" "$@"
exit $?
fi
cd "$work_git_path/.."
if [[ -r .git/git_dir ]]; then
git_dir=`cat .git/git_dir`
real_git_path=$git_dir
else
git_dir=''
real_git_path=".git"
fi
if [[ -n "$git_dir" ]]; then
[[ -f "$real_git_path/org_HEAD" && -f "$real_git_path/org_index" ]] || {
print_error "It's a fake git repository, and the real git repository is not ready."
exit 1
}
fi
if [[ -n "$git_dir" ]] || [[ -f "$real_git_path/org_HEAD" && -f "$real_git_path/org_index" ]]; then
# Make it absolute
cd "$real_git_path"
real_git_path=`pwd`
if [ -f "$work_git_path/org_HEAD" ]; then
ref=`cat $work_git_path/org_HEAD | awk '{print $NF}'`
else
ref=`cat $work_git_path/HEAD | awk '{print $NF}'`
fi
if [[ ! -f "$real_git_path/$ref" ]]; then
print_error "Invalid HEAD '$ref'. Maybe you have renamed the branch. \
If you known the new name of this branch, run 'echo ref: refs/heads/NewName >$work_git_path/HEAD' to fix it."
rm "$real_git_path/HEAD"
"$org_git" --git-dir="$real_git_path" --work-tree="$work_git_path/.." "$@"
exit $?
fi
if [[ -n "$git_dir" ]]; then
# Fake git repository
if [[ "$git_dir" = /* ]]; then
# If "git-dir" is an absolute path, use absolute path to link
link_prefix=$work_git_path/
else
# If "git-dir" is an relative path, use relative path to link
resolve_relative_path "$work_git_path" "$real_git_path" link_prefix
link_prefix=$link_prefix/
fi
image_prefix=$work_git_path/
else
# Linked real git repository
link_prefix=./org_
image_prefix=$work_git_path/org_
fi
if [[ -f "$real_git_path/index.lock" ]]; then
print_error "'$real_git_path/index.lock' exists, maybe another instance is running."
exit 1
fi
rm index || {
print_error "Remove index failed."
exit 1
}
ln -s "${link_prefix}index" index || {
print_error "Link index failed."
exit 1
}
cat "${image_prefix}HEAD" > HEAD || {
print_error "Switch HEAD failed."
exit 1
}
cd "$work_path"
trap "cat \"$real_git_path/HEAD\" > \"${image_prefix}HEAD\"; exit 1" SIGTERM SIGINT SIGHUP SIGQUIT
if [[ -n "$git_dir" ]]; then
# Fake repository
for arg in "$@"; do
# Remove argument '--git-dir=xxx' and '--work-tree=xxx'
if [[ "$arg" != --git-dir=* ]] && [[ "$arg" != --work-tree=* ]]; then
args="$args \"$arg\""
fi
done
eval "\"$org_git\" --git-dir=\"$real_git_path\" --work-tree=\"$work_git_path/..\" $args"
else
# Real repository
"$org_git" "$@"
fi
ret=$?
cat "$real_git_path/HEAD" > "${image_prefix}HEAD" || {
print_error "Sync HEAD failed."
exit 1
}
exit $ret
else
# Original real git repository
"$org_git" "$@"
exit $?
fi
}
Lgit_help(){
echo "LinkedGit commands:"
echo " install Install LinkedGit."
echo " uninstall Uninstall LinkedGit."
echo " link <path> [branch [start_point]] Link this workspace to a git repository."
echo " unlink Unlink this workspace."
}
write_script(){
local Lgit_path=$1
local org_git=$2
local var_inserted=''
local IFS_old=$IFS
if [[ -f "$Lgit_path" ]]; then
rm "$Lgit_path" || {
print_error "Remove '$Lgit_path' failed."
}
fi
print_info "Writing LinkedGit to '$Lgit_path'."
IFS=''
while read -r line; do
if [[ -z "$var_inserted" ]]; then
if [[ -z "$line" || "$line" = \#* ]]; then
echo "$line" >>"$Lgit_path"
else
echo "org_git='$org_git'" >>"$Lgit_path"
echo "$line" >>"$Lgit_path"
var_inserted='y'
fi
else
echo "$line" >>"$Lgit_path"
fi
done < $self
IFS=$IFS_old
if [[ -z "$var_inserted" ]]; then
print_error "Write script failed."
exit 1
fi
}
Lgit_install(){
local exe_git_path
local bak_git_path
if [[ -n "$org_git" ]]; then
print_error "LinkedGit is already installed."
exit 1
fi
git --version || {
print_error "Git is not installed, install git first."
exit 1
}
exe_git_path=`which git`
if [[ -z "$exe_git_path" ]]; then
print_error "Can't find git."
exit 1
fi
cat "$exe_git_path" | grep -q "Lgit_link()" && {
print_info "An old LinkedGit is installed, uninstall '$exe_git_path'..."
"$exe_git_path" uninstall || {
print_error "Uninstall old LinkedGit failed."
exit 1
}
"$self" install
return
}
if [[ -f "${exe_git_path}_org" ]]; then
bak_git_path="${exe_git_path}_org-"`date "+%Y-%m-%d-%H-%M-%S"`
print_warning "'${exe_git_path}_org' exists, backup it to '$bak_git_path'"
mv "${exe_git_path}_org" "$bak_git_path" || {
print_error "Backup '${exe_git_path}_org' failed."
exit 1
}
fi
print_info "Move '${exe_git_path}' to '${exe_git_path}_org'"
mv "${exe_git_path}" "${exe_git_path}_org" || {
print_error "Move '${exe_git_path}' failed."
exit 1
}
write_script "${exe_git_path}" "${exe_git_path}_org"
chmod a+x "${exe_git_path}" || {
print_error "Change file mode failed."
exit 1
}
print_info "Install finished."
}
Lgit_uninstall(){
if [[ -z "$org_git" ]]; then
print_error "LinkedGit is not installed."
exit 1
fi
print_info "Remove '$self'"
rm "$self" || {
print_error "Remove '$self' failed."
exit 1
}
print_info "Move '$org_git' to '$self''"
mv "$org_git" "$self" || {
print_error "Move '$org_git' failed."
exit 1
}
print_info "Uninstall finished."
}
Lgit_link(){
local git_path=$2
local branch=$3
local start_point=$4
local git_dir
local content
if [[ -z "$org_git" ]]; then
print_error "LinkedGit is not installed, run '$self install' first."
exit 1
fi
if [[ -z "$git_path" ]]; then
print_error "Git path must be specified."
Lgit_help
exit 1
fi
if [[ -f .git/git_dir ]]; then
git_dir=`cat .git/git_dir`
print_error "This workspace is already linked to '$git_dir'."
print_error "Run 'git unlink' first."
exit 1
fi
if [[ -d .git ]]; then
print_error "This workspace seems to be a real git repository."
exit 1
fi
if [[ "$git_path" != */.git ]] && [[ "$git_path" != */.git/ ]]; then
if [[ "$git_path" = */ ]];then
git_path=${git_path}.git
else
git_path=$git_path/.git
fi
fi
if [[ ! -d "$git_path/objects" ]]; then
print_error "'$git_path' is not a git repository."
exit 1
fi
if [[ ! -f $git_path/org_HEAD ]]; then
cp "$git_path/HEAD" "$git_path/org_HEAD" || {
print_error "Backup HEAD failed."
exit 1
}
fi
if [[ ! -f $git_path/org_index ]]; then
cp "$git_path/index" "$git_path/org_index" || {
print_error "Backup index failed."
exit 1
}
fi
mkdir .git || {
print_error "Create .git failed."
exit 1
}
cp "$git_path/org_HEAD" .git/HEAD || {
print_error "Copy HEAD failed."
exit 1
}
cp "$git_path/org_index" .git/index || {
print_error "Copy index failed."
exit 1
}
echo "$git_path" > .git/git_dir || {
print_error "Write git-dir failed."
exit 1
}
print_info "Linked to '${git_path}'."
if [[ -n "$branch" ]]; then
content=`ls -A |grep -v .git`
if [[ -n "$content" ]]; then
print_warning "This workspace is not empty, the files will be overridden. Do you want to continue? [yes/no]"
while read answer; do
if [[ "$answer" = 'yes' ]] || [[ "$answer" = 'y' ]] || [[ "$answer" = 'Y' ]] || [[ "$answer" = 'Yes' ]]; then
break
fi
if [[ "$answer" = "no" ]] || [[ "$answer" = "n" ]] || [[ "$answer" = "N" ]] || [[ "$answer" = "No" ]]; then
print_warning "Abort. Run 'git checkout .' manually to checkout files."
return
fi
print_warning "Please type 'yes' or 'no':"
done
fi
print_info "Checkout '$branch'..."
if [[ -f "$git_path/refs/heads/$branch" ]]; then
"$self" checkout "$branch" "$start_point" >/dev/null
else
"$self" checkout -b "$branch" "$start_point" >/dev/null
fi
if [[ $? != '0' ]]; then
print_error "Checkout '$branch' failed."
exit 1
fi
print_info "Reset files..."
"$self" reset --hard HEAD || {
print_error "Checkout files failed."
exit 1
}
else
print_warning "It's better to keep workspaces on different branchs. \
Use 'git checkout [-b] <branch>' to switch branch, then use 'git checkout .' to checkout files."
fi
}
Lgit_unlink(){
if [[ ! -d .git ]]; then
print_error "Not a git workspace."
exit 1
fi
if [[ ! -f .git/git_dir ]] || [[ -d .git/objects ]]; then
if [[ ! -f .git/org_HEAD ]]; then
# Orginal git repository
print_info "This workspace seems to be a real git repository, nothing to do."
return
else
# Git repository but has been linked
"$self" --version >/dev/null || exit 1
fi
else
# Fake git workspace
rm -r .git || {
print_error "Remove .git failed!"
exit 1
}
fi
print_info "Unlinked."
}
action=$1
if [[ -z "$action" ]]; then
"$org_git"
Lgit_help
exit
fi
case "$action" in
install|uninstall|link|unlink)
Lgit_$action "$@"
;;
*)
Lgit_exec "$@"
;;
esac