-
Notifications
You must be signed in to change notification settings - Fork 18
/
mgit
executable file
·603 lines (540 loc) · 14.5 KB
/
mgit
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/bin/bash
shopt -s nullglob
IFS=$'\n\b'
usage() {
[ "$1" ] && {
echo
echo "ERROR: $1"
echo
exit
}
echo
echo " multigit 3.6.1 - git wrapper for working with overlaid repos."
echo " Cosmin Apreutesei | public domain | https://github.com/capr/multigit"
echo
echo " USAGE: mgit ..."
echo
echo " ls list cloned repos"
echo " ls-all list all known repos"
echo " ls-uncloned list all known but not cloned repos"
echo
echo " ls-modified|st[atus] list modified files across all repos"
echo " ls-unpushed list repos that are ahead of origin"
echo " ls-untracked list files untracked by any repo"
echo " ls-double-tracked list files tracked by multiple repos"
echo " ls-tracked list files and which repos are tracking them"
echo " which FILENAME list which repo(s) are tracking a file"
echo
echo " init REPO create a local repo"
echo " clone [REMOTE/]REPO|URL[=VERSION] ... clone one ore more repos"
echo " clone-all clone all known uncloned repos"
echo " clone-release REL|RELFILE clone all repos from a release file"
echo " remove [--dry] REPO ... remove repos from disk (!)"
echo
echo " baseurl [REMOTE [URL|-]] get/set/delete the baseurl of a remote"
echo " origin [REPO [REMOTE|URL|-]] get/set/delete the known origin of a repo"
echo
echo " [-] REPO1,... start a shell for using git on a repo"
echo " [-] REPO1,...|--all command ... execute any git command on a repo"
echo " [-] REPO1,...|--all exec ... execute a shell command in a repo context"
echo " [-] REPO1,...|--all ver[sion] [tag] show repo version or tag (as enum or list)"
echo " [-] REPO1,...|--all make-symlinks make symbolic links in .mgit/REPO"
echo " [-] REPO1,...|--all make-hardlinks make hard links in .mgit/REPO"
echo
echo " release [REL] show a release or list releases"
echo " release REL update [tag] create/update a release based on HEADs"
echo " release REL clone clone all repos from a release"
echo " release REL remove remove a release file"
echo
echo " [help|--help] show this screen"
echo
# append any plugin help files
for f in .mgit/*.help; do
cat "$f"
done
echo
exit
}
check_root() { [ -d .mgit ] || usage "'.mgit' dir not found."; }
list_known() {
check_root
(cd .mgit && \
for f in *.origin; do
echo "${f%.origin}"
done)
}
list_cloned() {
check_root
(cd .mgit && \
for f in *; do
[ -d "$f/.git" ] && echo "$f"
done)
}
list_uncloned() {
check_root
(cd .mgit && \
for f in *.origin; do
f="${f%.origin}"
[ ! -d "$f/.git" ] && echo $f
done)
}
list_modified() {
check_root
"$0" --all status -s
}
list_unpushed() {
check_root
for repo in `list_cloned`; do
[ "$(GIT_DIR=".mgit/$repo/.git" \
git rev-list HEAD...origin/master --count 2>/dev/null)" \
!= "0" ] && echo "$repo"
done
}
tracked_files() {
check_root
([ -d .git ] && git ls-files
for repo in `list_cloned`; do
GIT_DIR=".mgit/$repo/.git" git ls-files
done) | sort | uniq $1
}
which_tracks() {
[ "$1" ] || usage "Filename expected."
list_tracked | while IFS=" " read repo file; do
[ "$file" == "$1" ] && echo "$repo"
done
}
existing_files() {
find * -type f | grep -v "^$1$" | sort
}
list_tracked() {
check_root
local MGIT_DIR="$PWD"
for repo in `list_cloned`; do
(cd "$PWD0"; GIT_DIR="$MGIT_DIR/.mgit/$repo/.git" git ls-files) | while read f; do
printf "%-16s %s\n" "$repo" "$f"
done
done
}
list_untracked() {
check_root
# NOTE: using a temp file because bash on Windows can't do <(tracked_files)
tracked_files > $$.tmp
if [ "$OSTYPE" != "msys" ]; then
# NOTE: not using grep because it's buggy on OSX (doesn't match all).
existing_files $$.tmp | comm -23 - $$.tmp
else
# NOTE: using grep because Git for Windows doesn't have comm,
# and because if MSYS is in PATH, comm from MSYS can't be used with
# Git's bash (it crashes).
existing_files $$.tmp | grep -v -F -x -f $$.tmp
fi
rm $$.tmp
}
list_double_tracked() {
check_root
# NOTE: using a temp file because bash on Windows can't do <(tracked_files -d)
tracked_files -d > $$.tmp
[ "$(cat $$.tmp)" ] && \
PWD0="$PWD" list_tracked | grep -F -w -f $$.tmp
rm $$.tmp
}
clone_all() {
for repo in `list_uncloned`; do
"$0" clone "$repo"
done
}
init() {
local name="$1"
name="${name//[^\.\-_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]/}"
[ "$name" = "$1" ] || \
usage "Invalid name: '$1'. Only letters, numbers, '.', '-' and '_' allowed."
# check that the repo is not already cloned
[ -d ".mgit/$name" ] && {
echo "SKIPPING: Already exists: '$name'."
return 1
}
mkdir -p ".mgit/$name"
export GIT_DIR=".mgit/$name/.git"
git init $MULTIGIT_INIT_OPTS
git config core.worktree ../../..
git config core.excludesfile ".mgit/$name.exclude"
make_exclude_file "$name"
return 0
}
make_exclude_file() {
[ "$no_exclude_file" ] && return
[ -f ".mgit/$1.exclude" ] && return
# make a default "exclude-all" exclude file
echo '*' > ".mgit/$1.exclude"
}
clone_one() {
local arg="$1"
local name
local origin
local rorigin
local url
local ver
arg="${arg//[[:blank:]]/}" # spaces not allowed
[ "$arg" = "$1" ] || usage "Invalid name '$name'."
# extract `=version` if any
ver="${arg##*=}"
[ "$ver" = "$arg" ] && ver=""
arg="${arg%=*}"
# check if the arg is a full url or just `[origin/]name`
if [ "${arg#*:}" != "$arg" ]; then
url="$arg"
origin="$arg"
name="${url##*/}"
name="${name%.git}"
else
name="${arg##*/}"
origin="${arg%/*}"
[ "$origin" = "$arg" ] && origin=""
fi
# check that arg is not `/` or `origin/`
[ "$name" ] || usage "Invalid repo name '$1'."
# check for a registered origin
[ -f ".mgit/$name.origin" ] && \
rorigin=$(cat .mgit/$name.origin)
# decide the origin
if [ "$origin" ]; then
[ "$rorigin" -a "$origin" != "$rorigin" ] && \
echo "NOTE: Using different origin for '$name': '$origin' (was '$rorigin')."
else
origin="$rorigin"
[ "$origin" ] || {
echo "SKIPPING: Unknown repo '$name'."
return
}
fi
# find the origin url
if [ ! "$url" ]; then
if [ -f ".mgit/$origin.baseurl" ]; then
local baseurl=$(cat .mgit/$origin.baseurl)
url="$baseurl$name"
else
# assume the origin on file is a full url: check if it is
if [ "${origin#*:}" != "$origin" ]; then
url="$origin"
else
echo "ERROR: Unknown origin: '$origin' for '$name'."
echo "HINT: To register '$origin' to be used as an origin, type, eg.:"
echo "HINT: "$(basename "$0")" baseurl $origin https://github.com/$origin/"
return
fi
fi
fi
# finally, clone the repo
no_exclude_file=1 init "$name" || return
git remote add origin "$url"
git fetch $MULTIGIT_FETCH_OPTS || {
rm -rf ".mgit/$name/"
rm ".mgit/$name.exclude"
echo "ERROR: Fetch failed. Repo removed."
return
}
git branch --track master origin/master
git checkout $ver
# make an exclude file if one wasn't checked out already.
make_exclude_file "$name"
# (re)register the repo's origin.
if [ "$origin" != "$rorigin" ]; then
if [ "$rorigin" ]; then
echo "NOTE: Updating origin for '$name': $origin"
echo " (was: $rorigin)"
else
echo "NOTE: Adding origin for '$name': $origin"
echo " (url: $url)"
fi
mkdir -p .mgit
echo "$origin" > ".mgit/$name.origin"
fi
}
clone() {
[ "$1" ] || usage "Repo name expected."
if [ $# = 1 ]; then
clone_one "$@"
else
while [ $# != 0 ]; do
clone_one "$1"
shift
done
fi
}
run() { if [ "$DRY_RUN" ]; then echo "$@"; else "$@"; fi; }
remove_one() {
[ "$1" ] || usage "Invalid name '$1'."
[ -d ".mgit/$1/" ] || {
echo "ERROR: Repo not found '$1'."
return
}
# don't remove from a subshell
[ "$MULTIGIT_REPO" = "$1" ] && \
usage "Refusing to remove '$1' from a subshell."
# get tracked files for this repo
files="$(GIT_DIR=".mgit/$1/.git" git ls-files)" || {
echo "ERROR: Could not get the list of files for '$1'."
echo "HINT: If you know that there are no checked out files,"
echo "HINT: feel free to \`rm -rf .mgit/$1/ .mgit/$1.exclude\`."
return
}
# ask for confirmation if there are files to delete
[ "$files" ] && {
local n=$(echo "$files" | wc -l)
echo "Remove ALL $((n)) files of '$1'? You can't undo this [yes/N]"
read yes
[ "$yes" = "yes" ] || { echo "Canceled."; return; }
}
# remove files
for file in $files; do
run rm "$file"
done
# remove empty directories
for file in $files; do
echo "$(dirname "$file")"
done | uniq | while read dir; do
[ "$dir" != "." ] && run /bin/rmdir -p "$dir" 2>/dev/null
done
# remove the git dir
run rm -rf ".mgit/$1/"
run rm -f ".mgit/$1.exclude"
echo "Removed: '$1'."
}
remove() {
[ "$1" = "--dry" ] && { DRY_RUN=1; shift; }
[ "$1" ] || usage "Repo name expected."
if [ $# = 1 ]; then
remove_one "$@"
else
while [ $# != 0 ]; do
remove_one "$1"
shift
done
fi
}
baseurl() {
[ "$1" ] || {
for f in .mgit/*.baseurl; do
f="${f#.mgit/}"
f="${f%.baseurl}"
printf "%-20s %s\n" "$f" "$(baseurl "$f")"
done
return
}
local origin="$1"
local url="$2"
# spaces not allowed
origin="${origin//[[:blank:]]/}"
url="${url//[[:blank:]]/}"
[ -z "$1" -o "$1" != "$origin" ] && usage "Invalid origin name '$1'."
[ "$2" -a "$2" != "$url" ] && usage "Invalid baseurl '$2'."
if [ "X$url" = "X-" ]; then
rm ".mgit/$origin.baseurl"
elif [ "$url" ]; then
[ "${url##*/}" != "" ] && usage "A base URL must end with a '/'."
mkdir -p .mgit
echo "$url" > ".mgit/$origin.baseurl"
else
cat ".mgit/$origin.baseurl"
fi
}
origin() {
[ "$1" ] || {
for f in .mgit/*.origin; do
f="${f#.mgit/}"
f="${f%.origin}"
printf "%-20s %s\n" "$f" "$(origin "$f")"
done
return
}
local repo="$1"
local origin="$2"
# spaces not allowed
repo="${repo//[[:blank:]]/}"
origin="${origin//[[:blank:]]/}"
[ -z "$1" -o "$1" != "$repo" ] && usage "Invalid repo name '$1'."
[ "$2" -a "$2" != "$origin" ] && usage "Invalid origin '$2'."
if [ "X$origin" = "X-" ]; then
rm ".mgit/$repo.origin"
elif [ "$origin" ]; then
mkdir -p .mgit
echo "$origin" > ".mgit/$repo.origin"
else
cat ".mgit/$repo.origin"
fi
}
list_releases() {
for f in .mgit/*.release; do
f="${f#.mgit/}"
f="${f%.release}"
echo "$f"
done
}
show_release() {
[ -f ".mgit/$1.release" ] || usage "Unknown release '$1'."
cat ".mgit/$1.release"
}
update_release() {
"$0" --all version $2 > ".mgit/$1.release"
}
remove_release() {
rm ".mgit/$1.release"
}
clone_release() {
local rel="$1"
[ -f "$rel" ] || rel=".mgit/$rel.release"
[ -f "$rel" ] || usage "Release not found: '$1'."
cat "$rel" | (IFS=" "; while read repo ver; do
"$0" clone "$repo=$ver"
done)
}
release() {
local rel="$1"
local cmd="$2"
rel="${rel//[[:blank:]]/}" # spaces not allowed
[ "$rel" -a "$1" != "$rel" ] && usage "Invalid release name '$1'."
[ "$rel" ] || { list_releases; return; }
shift 2
case "$cmd" in
"") show_release "$rel" "$@" ;;
update) update_release "$rel" "$@" ;;
clone) clone_release "$rel" "$@" ;;
remove) remove_release "$rel" "$@" ;;
*) usage "Invalid release command '$cmd'."
esac
}
git_shell() {
cd "$PWD0" || return
echo "Entering subshell: git commands will affect the repo '$MULTIGIT_REPO'."
echo "Type \`exit' to exit subshell."
git status -s
echo
if [ "$OSTYPE" = "msys" ]; then
export PROMPT="[$MULTIGIT_REPO] \$P\$G"
"$COMSPEC" /k
else
export PS1="[$MULTIGIT_REPO] \u@\h:\w\$ "
"$SHELL" --norc -i
fi
}
git_ver() {
if [ "$1" = "tag" ]; then
printf "$MULTIGIT_REPO=$(git describe --tags --abbrev=0) "
else
printf "$MULTIGIT_REPO=$(git describe --tags --long --always) "
fi
}
git_version() {
printf "%-20s" "$MULTIGIT_REPO"
if [ "$1" = "tag" ]; then
git describe --tags --abbrev=0 || echo ""
else
git describe --tags --long --always || echo ""
fi
}
git_remove_links() {
[ "$OSTYPE" = "msys" ] && usage "Not for Windows."
([ "$MULTIGIT_REPO" ] && cd ".mgit/$MULTIGIT_REPO" || exit 1
find . ! -path './.git/*' ! -path './.git' ! -path '.' -exec rm -rf {} \; 2>/dev/null)
}
git_make_hardlinks() {
git_remove_links
git ls-files | while read f; do
mkdir -p "$(dirname ".mgit/$MULTIGIT_REPO/$f")"
ln -f "$f" ".mgit/$MULTIGIT_REPO/$f"
done
}
git_make_symlinks() {
git_remove_links
git ls-files | while read f; do
mkdir -p "$(dirname ".mgit/$MULTIGIT_REPO/$f")"
ln -sf "$PWD/$f" ".mgit/$MULTIGIT_REPO/$f"
done
}
git_cmd_one() {
local repo="$1"
local cmd="$2"
shift 2
[ "$VERBOSE" ] && echo "@$repo:"
export GIT_DIR="$PWD/.mgit/$repo/.git"
export MULTIGIT_REPO="$repo"
[ -d "$GIT_DIR" ] || usage "Unknown repo: '$repo'."
case "$cmd" in
exec) cd "$PWD0" && "$@" ;;
ver) git_ver "$@" ;;
version) git_version "$@" ;;
make-symlinks) git_make_symlinks "$@" ;;
make-hardlinks) git_make_hardlinks "$@" ;;
"") git_shell "$@" ;;
*)
# look for and execute a git plugin command
if [ -f ".mgit/git-$cmd.sh" ]; then
PWD0="$PWD0" ".mgit/git-$cmd.sh" "$@"
else
(cd "$PWD0" && git "$cmd" "$@")
fi
;;
esac
export GIT_DIR=
export MULTIGIT_REPO=
}
git_cmd() {
local repos="$1"; shift
if [ "$repos" = "--all" ]; then
[ "$1" ] || usage "Refusing to start a subshell for each repo."
[ "$1" == "-v" ] && { VERBOSE=1; shift; }
for repo in `list_cloned`; do
git_cmd_one "$repo" "$@"
done
else
(IFS=","; for repo in $repos; do
git_cmd_one "$repo" "$@"
done)
fi
[ "$1" = "ver" ] && echo
}
cd_root() {
local pwd1
while [ "$PWD" != "$pwd1" ]; do
[ -d .mgit ] && return
pwd1="$PWD"
cd .. || usage "Could not cd to '$PWD/..'."
done
cd "$PWD0" # root dir not found, go back to initial dir
}
PWD0="$PWD"
cd_root
cmd="$1"; shift
case "$cmd" in
"") usage ;;
help) usage ;;
--help) usage ;;
ls) list_cloned ;;
ls-all) list_known ;;
ls-uncloned) list_uncloned ;;
ls-modified) list_modified ;;
st) list_modified ;;
status) list_modified ;;
which) which_tracks "$@" ;;
ls-unpushed) list_unpushed ;;
ls-untracked) list_untracked ;;
ls-double-tracked) list_double_tracked ;;
ls-tracked) list_tracked ;;
init) init "$@" ;;
clone) clone "$@" ;;
clone-all) clone_all "$@" ;;
clone-release) clone_release "$@" ;;
remove) remove "$@" ;;
baseurl) baseurl "$@" ;;
origin) origin "$@" ;;
release) release "$@" ;;
-) git_cmd "$@" ;;
*)
# look for and execute a plugin command
if [ -f ".mgit/$cmd.sh" ]; then
".mgit/$cmd.sh" "$@"
else
git_cmd "$cmd" "$@"
fi
;;
esac