-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-gets
executable file
·954 lines (904 loc) · 31.4 KB
/
git-gets
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
#!/bin/bash
GIT_GET_VERSION=
if [ -z "$RAW" ]; then
RAW=
else
RAW="$RAW"
fi
if [ -z "$GH_USE_HTTPS" ]; then
GH_USE_HTTPS=
else
GH_USE_HTTPS="$GH_USE_HTTPS"
fi
if [ -z "$GH_USE_SSH" ]; then
GH_USE_SSH=
else
GH_USE_SSH="$GH_USE_SSH"
fi
if [ -z "$GIT_DIR" ]; then
GIT_DIR=
else
GIT_DIR="$GIT_DIR"
fi
if [ -z "$GIT_WORK_TREE" ]; then
GIT_WORK_TREE=
else
GIT_WORK_TREE="$GIT_WORK_TREE"
fi
set -euo pipefail
usage()
{
exec man git-gets
}
git_new() {
V="$(git version)"
[[ "$V" =~ ^git[[:space:]]version[[:space:]]2\..\. ]] && return 1
[[ "$V" =~ ^git[[:space:]]version[[:space:]]2\.1.\. ]] && return 1
return 0
}
fakerealpath() {
if [[ "$1" =~ ^/ ]]; then
printf '%s' "$1"
else
printf '%s/%s' "$PWD" "$1"
fi
}
got_dir() {
set -e
if [ "$OUTPUT" = "-" ]; then
[ -n "$VERBOSE" ] && (cd "$(dirname "$1")" && ls -lhAR "$(basename "$1")" >&2)
(cd "$(dirname "$1")" && tar c "$(basename "$1")")
elif [ -d "$OUTPUT" ]; then
if [ -n "$FORCE_DIR" ]; then
[ -z "$QUIET" ] && printf "${C_WARN}Warning: Overriding directory %q${C_RST}\n" "$OUTPUT" >&2
rm -rf "$OUTPUT" && mv "$1" "$OUTPUT"
else
printf "${C_ERR}Error: Directory %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
fi
elif [ -f "$OUTPUT" ]; then
if [ -n "$FORCE_DIR" ]; then
[ -z "$QUIET" ] && printf "${C_WARN}Warning: Overriding file %q${C_RST}\n" "$OUTPUT" >&2
rm -f "$OUTPUT" && mv "$1" "$OUTPUT"
else
printf "${C_ERR}Error: File %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
fi
elif [ -e "$OUTPUT" ]; then
printf "${C_ERR}Error: Weird file %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
else
mv "$1" "$OUTPUT"
fi
[ -n "$VERBOSE" ] && printf "${C_DBG}size used in /tmp = %s${C_RST}\n" "$(du -sh "$WORK_DIR")" >&2
T="$2"
if [ -z "$TAG_FILE" ]; then
[ -z "$QUIET" ] && printf "${C_NOTICE}tag = %q${C_RST}\n" "$T" >&2
else
if [[ "$TAG_FILE" =~ ^/ ]]; then
TG="$TAG_FILE"
elif [ "$OUTPUT" = "-" ]; then
TG="$OCWD/$TAG_FILE"
else
TG="$OUTPUT/$TAG_FILE"
fi
[ -z "$QUIET" ] && printf "${C_NOTICE}tag = %q -> %s${C_RST}\n" "$T" "$TG" >&2
if [ -e "$TG" ]; then
printf "${C_ERR}Error: Unable to create tag file %s because it exists; git-gets continues.${C_RST}" "$TG" >&2
else
printf "%s\n" "$T" >"$TG"
fi
fi
exit 0
}
config_remotes() {
if [ -z "$2" ]; then
git config --unset "remote.origin.$1" || true
[ -n "$UPSTREAM" ] && git config --unset "remote.upstream.$1" || true
else
git config "remote.origin.$1" "${2/_/origin}"
[ -n "$UPSTREAM" ] && git config "remote.upstream.$1" "${2/_/upstream}"
fi
true
}
got_repo() {
set -e
git config core.repositoryformatversion 0
if [ "$PRESERVE" = X ] && [ -z "$SUBMODULE_MODE" ]; then
git config --unset remote.origin.mirror || true
if [ -n "$UPSTREAM" ]; then
git config remote.upstream.url "$UPSTREAM"
git config remote.upstream.promisor true
git config remote.upstream.partialclonefilter tree:0
fi
if [ -z "$CL_BRANCHES" ]; then
config_remotes fetch '+refs/heads/*:refs/remotes/_/*'
elif git show-ref --verify "refs/heads/$BRANCH" >/dev/null 2>/dev/null; then
[ -n "$VERBOSE" ] && echo "${C_INFO}Set branch: refs/heads/$BRANCH${C_RST}" >&2
config_remotes fetch "+refs/heads/$BRANCH:refs/remotes/_/$BRANCH"
if [ -n "$UPSTREAM" ] && [ -z "$(git ls-remote "$UPSTREAM" "refs/heads/$BRANCH")" ]; then
UBRANCH="$(git ls-remote --symref "$UPSTREAM" HEAD | { grep '^ref:' || true; } | cut -d $'\t' -f 1)"
UBRANCH="${UBRANCH#ref: }"
[ -z "$QUIET" ] && echo "${C_WARN}Warning: refs/heads/$BRANCH not found on upstream $UPSTREAM, using default $UBRANCH instead.${C_RST}" >&2
git config remote.upstream.fetch "+$UBRANCH:refs/remotes/upstream/${UBRANCH#refs/heads/}"
fi
else
[ -z "$RAW" ] && [ -z "$QUIET" ] && echo "${C_WARN}Warning: $BRANCH is not a branch, so your clone will contain no branch.${C_RST}" >&2
config_remotes fetch ''
fi
config_remotes tagOpt "$([ -n "$CL_TAGS" ] && echo '--no-tags')"
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' git update-ref --stdin; printf "${C_RST}\n") >&2
git for-each-ref refs/remotes/ --format='delete %(refname)' | git update-ref --stdin
git for-each-ref --format='%(refname)' | \
while IFS= read -r line; do
if [ "$line" = "refs/heads/$BRANCH" ]; then
echo "create refs/remotes/origin/$BRANCH" "$line"
elif [[ "$line" =~ ^refs/heads/ ]]; then
[ -z "$CL_BRANCHES" ] && echo create "refs/remotes/origin/${line##refs/heads/}" "$line"
echo "delete $line"
elif [[ "$line" =~ ^refs/tags/ ]]; then
[ -n "$CL_TAGS" ] && echo "delete $line"
true
else
echo "delete $line"
fi
done | git update-ref --stdin
if git show-ref --verify "refs/remotes/origin/$BRANCH" >/dev/null 2>/dev/null; then
ARGS=(git branch -f "$BRANCH")
ARGS+=("refs/remotes/origin/$BRANCH")
[ -n "$QUIET" ] && ARGS+=(-q)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && "${ARGS[@]}" >&2
[ -n "$QUIET" ] && "${ARGS[@]}" 2>/dev/null >&2
git symbolic-ref HEAD "refs/heads/$BRANCH"
fi
if [ -n "$UPSTREAM" ]; then
# --porcelain is the dark magic, don't touch
ARGS=(git fetch upstream --porcelain)
[ -n "$QUIET" ] && ARGS+=(-q)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && "${ARGS[@]}" >&2
[ -n "$QUIET" ] && "${ARGS[@]}" 2>/dev/null >&2
fi
fi
ARGS=(git reset --hard)
[ -n "$QUIET" ] && ARGS+=(-q)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && "${ARGS[@]}" >&2
[ -n "$QUIET" ] && "${ARGS[@]}" 2>/dev/null >&2
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}" >&2; printf '%q ' "${ARGS[@]}" >&2; printf "${C_RST}\n">&2)
if [ -n "$RAW" ] && [ -n "$NO_RECURSIVE" ]; then
if [ -z "$SUBMODULE_MODE" ]; then
[ -z "$PRESERVE" ] && mv "$GIT_DIR" ".git"
got_dir "$GIT_WORK_TREE" "$1"
else
exit 0
fi
fi
if [ -z "$NO_INIT" ]; then
ARGS=(git -C "$GIT_WORK_TREE" submodule)
[ -n "$QUIET" ] && ARGS+=(--quiet)
ARGS+=(init)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && "${ARGS[@]}" >&2
[ -n "$QUIET" ] && "${ARGS[@]}" 2>/dev/null >&2
fi
if [ -z "$RAW" ]; then
exec 4>&2
fi
CONFIRM_GOING_STATUS=
while IFS= read -r -u 3 line; do
PA="$(git -C "$GIT_WORK_TREE" config --file ".gitmodules" "submodule.$line.path" || echo '')"
if [ -z "$PA" ]; then
[ -z "$QUIET" ] && printf "${C_WARN}Skipping %s because it does not appear in .gitmodules${C_RST}\n" "$PA" >&2
continue
fi
URL="$(git config "submodule.$line.url")"
if [ -z "$URL" ]; then
[ -z "$QUIET" ] && printf "${C_WARN}Skipping %s because it has not been initialized${C_RST}\n" "$PA" >&2
continue
fi
SSHA1="$(git ls-files -s "$PA" | cut -d ' ' -f2)"
[ -n "$VERBOSE" ] && printf "${C_INFO}%s %s %s %s${C_RST}\n" "$line" "$PA" "$URL" "$SSHA1">&2
if [ -n "$(find "$GIT_WORK_TREE/$PA" -maxdepth 0 -type d -empty 2>/dev/null)" ]; then
true
else
[ -z "$QUIET" ] && printf "${C_WARN}Warning: Skipping %s because the folder is not empty${C_RST}\n" "$PA" >&2
continue
fi
if [ "$CONFIRM_GOING_STATUS" = A ]; then
GO=YES
elif [ "$CONFIRM_GOING_STATUS" = D ]; then
GO=
elif [ -z "$CONFIRM" ]; then
GO=YES
else
while true; do
[ -z "$RAW" ] && printf "${C_WARN}On the toplevel:${C_RST}\n" >&2
[ -n "$RAW" ] && printf "${C_WARN}Inside the submodule %s:${C_RST}\n" "$RAW" >&2
printf "${C_WARN}Will clone %s from %s, proceed? (Y/n/r/p/s/q/a/d/?) ... ${C_RST}" "$PA" "$URL" >&2
read -n 1 -r
case "$REPLY" in
"")
GO=YES
break
;;
y)
printf '\n' >&2
GO=YES
break
;;
n)
printf '\n' >&2
GO=
break
;;
r)
printf '\n' >&2
GO=R
break
;;
p)
printf '\n' >&2
GO=P
break
;;
s)
printf '\n' >&2
GO=S
break
;;
q)
printf '\n' >&2
exit 5
;;
a)
printf '\n' >&2
GO=YES
CONFIRM_GOING_STATUS=A
break
;;
d)
printf '\n' >&2
GO=
CONFIRM_GOING_STATUS=D
break
;;
?)
printf '\n' >&2
echo "${C_DBG}y - clone it${C_RST}" >&2
echo "${C_DBG}n - skip it${C_RST}" >&2
echo "${C_DBG}r - clone it, and all its own submodules${C_RST}" >&2
echo "${C_DBG}p - like r, but in parallel${C_RST}" >&2
echo "${C_DBG}s - clone it, but not its own submodules${C_RST}" >&2
echo "${C_DBG}q - quit git-gets immediately${C_RST}" >&2
echo "${C_DBG}a - clone it and all other submodules${C_RST}" >&2
echo "${C_DBG}d - skip it and all other submodules${C_RST}" >&2
;;
*)
printf '\n' >&2
;;
esac
done
fi
if [ -n "$GO" ]; then
[ -z "$QUIET" ] && printf "${C_NOTICE}+++ %s %s %s${C_RST}\n" "$PA" "$URL" "$SSHA1">&2
ARGS=("$0")
[ -n "$VERBOSE" ] && ARGS+=(--verbose)
[ -n "$QUIET" ] && ARGS+=(--quiet)
[ -n "$COLOR" ] && ARGS+=(--color=always) || ARGS+=(--color=never)
[ "$PRESERVE" = X ] && ARGS+=(-x)
[ "$PRESERVE" = G ] && ARGS+=(-g)
[ -n "$GH_USE_HTTPS" ] && ARGS+=(--https)
[ -n "$GH_USE_SSH" ] && ARGS+=(--ssh)
[ -n "$CL_BRANCHES" ] && ARGS+=(--single-branch)
[ -n "$CL_TAGS" ] && ARGS+=(--no-tags)
[ -n "$PARALLEL" ] && ARGS+=(--parallel)
case "$GO" in
R)
# overrides --no-recursive
# overrides --confirm
;;
P)
ARGS+=(--parallel)
;;
S)
ARGS+=(--no-recursive)
# --confirm is unnecessary here
;;
*)
[ -n "$NO_RECURSIVE" ] && ARGS+=(--no-recursive)
[ -z "$PARALLEL" ] && [ -n "$CONFIRM" ] && ARGS+=(--confirm)
;;
esac
ARGS+=(-o "$GIT_WORK_TREE/$PA" "$URL" "$SSHA1")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}" >&2; printf '%q ' "${ARGS[@]}" >&2; printf "${C_RST}\n">&2)
rmdir "$GIT_WORK_TREE/$PA"
if [ -n "$PARALLEL" ]; then
if [ -n "$CONFIRM" ]; then
RAW="$RAW/$PA" "${ARGS[@]}" 2>&1 | sed 's/^/ /' >>"$TMP_LOG" &
else
RAW="$RAW/$PA" "${ARGS[@]}" 2>&1 | sed 's/^/ /' >&2 &
fi
else
if [ -n "$CONFIRM" ]; then
RAW="$RAW/$PA" "${ARGS[@]}"
else
RAW="$RAW/$PA" "${ARGS[@]}" 2>&1 | sed 's/^/ /' >&2
fi
fi
else
[ -z "$QUIET" ] && printf "${C_DBG}%s %s %s %s${C_RST}\n" '---' "$PA" "$URL" "$SSHA1">&2
fi
done 3< <(git config --local --get-regexp 'submodule.*.url' | cut -d '.' -f 2)
[ -z "$RAW" ] && [ -n "$PARALLEL" ] && [ -n "$CONFIRM" ] && [ -z "$QUIET" ] && echo "${C_INFO}Waiting for sub-processes to finish, please be patient ...${C_RST}" >&2
wait
[ -z "$RAW" ] && [ -n "$PARALLEL" ] && [ -n "$CONFIRM" ] && cat "$TMP_LOG" >&2
if [ -z "$RAW" ] && [ -n "$PRESERVE" ]; then
ARGS=(git -C "$GIT_WORK_TREE" submodule)
[ -n "$QUIET" ] && ARGS+=(--quiet)
ARGS+=(absorbgitdirs)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && "${ARGS[@]}" >&2
[ -n "$QUIET" ] && "${ARGS[@]}" 2>/dev/null >&2
fi
if [ -z "$SUBMODULE_MODE" ]; then
[ -z "$PRESERVE" ] && mv "$GIT_DIR" ".git"
got_dir "$GIT_WORK_TREE" "$1"
else
exit 0
fi
}
COLOR=
[ -t 2 ] && COLOR=YES
SUBMODULE_MODE=
QUIET=
VERBOSE=
FORCE_DIR=
OUTPUT=
PRESERVE=
TAG=
TAG_FILE=
PARALLEL=
CONFIRM=
NO_INIT=
NO_RECURSIVE=
UPSTREAM=
CL_BRANCHES=
CL_TAGS=
POSITIONAL=()
while [ $# -gt 0 ]; do
case "$1" in
-X)
shift
set -- -x -u -B -T -P "$@"
;;
-Y)
shift
set -- -t -P "$@"
;;
-V|--version)
echo "git-gets(1) $GIT_GET_VERSION"
exit
;;
-h|--help)
usage
exit
;;
-q|--quiet)
QUIET=--quiet
shift
;;
-v|--verbose)
VERBOSE=YES
shift
;;
--color=never|--colour=never)
COLOR=
shift
;;
--color=always|--colour=always)
COLOR=YES
shift
;;
--color=auto|--colour=auto|--color|--colour)
COLOR=
[ -t 2 ] && COLOR=YES
shift
;;
-s|--ssh)
GH_USE_SSH=YES
shift
;;
-H|--https)
GH_USE_HTTPS=YES
shift
;;
-F|--rm-rf)
FORCE_DIR=YES
shift
;;
-o|--output)
OUTPUT="$2"
shift
shift
;;
-x)
if [ "$PRESERVE" = G ]; then
echo "Error: Conflict: -x and -g|--preserve-git" >&2
exit 1
fi
PRESERVE=X
shift
;;
-B|--single-branch)
CL_BRANCHES=YES
shift
;;
-T|--no-tags)
CL_TAGS=YES
shift
;;
-g|--preserve-git)
if [ "$PRESERVE" = X ]; then
echo "Error: Conflict: -x and -g|--preserve-git" >&2
exit 1
fi
PRESERVE=G
shift
;;
-u|--upstream)
UPSTREAM=YES
shift
;;
-t|--tag)
TAG=YES
shift
;;
--tag-file)
TAG=YES
TAG_FILE="$2"
shift
shift
;;
-P|--parallel)
PARALLEL=YES
shift
;;
-c|--confirm)
CONFIRM=YES
shift
;;
--no-init)
NO_INIT=YES
shift
;;
--no-recursive)
NO_RECURSIVE=YES
shift
;;
--flat)
echo "Error: --flat is no longer supported. Please see man git-gets for more information." >&2
exit 1
;;
*)
if [[ "$1" =~ ^-j ]]; then
PARALLEL="${1#-j}"
elif [[ "$1" =~ ^--jobs= ]]; then
PARALLEL="${1#--jobs=}"
elif [[ "$1" =~ ^--tag-file= ]]; then
TAG=YES
TAG_FILE="${1#--tag-file=}"
elif [[ "$1" =~ ^--output= ]]; then
OUTPUT="${1#--output=}"
elif [[ "$1" =~ ^-o= ]]; then
OUTPUT="${1#-o=}"
elif [[ "$1" =~ ^-o ]]; then
OUTPUT="${1#-o}"
elif [[ "$1" =~ ^-.. ]]; then
T="$1"
shift
set -- '' "${T:0:2}" "-${T:2}" "$@"
elif [[ "$1" =~ ^- ]]; then
echo "Error: Unrecognized argument $1" >&2
exit 1
else
POSITIONAL+=("$1")
fi
shift
;;
esac
done
if [ -n "$COLOR" ]; then
C_ERR=$'\e[37;41m'
C_WARN=$'\e[37;33m'
C_NOTICE=$'\e[35m'
C_INFO=$'\e[34m'
C_DBG=$'\e[36m'
C_RST=$'\e[0m'
else
C_ERR=
C_WARN=
C_NOTICE=
C_INFO=
C_DBG=
C_RST=
fi
if [ -n "$PRESERVE" ] && [ -n "$TAG" ]; then
echo "${C_ERR}Error: Conflict: -x|-g|--preserve-git and -t|--tag|--tag-file${C_RST}" >&2
exit 1
fi
if [ "${#POSITIONAL[@]}" -eq 0 ]; then
if git rev-parse HEAD >/dev/null 2>&1; then
SUBMODULE_MODE=YES
else
echo "${C_ERR}Error: Must specify <repo-url> (any git remote) or <user>/<repo> (GitHub)${C_RST}"
exit 1
fi
else
if [ -n "$SUBMODULE_MODE" ]; then
echo "${C_ERR}Error: Too many positional arguments.${C_RST}" >&2
exit 1
fi
if [ "${#POSITIONAL[@]}" -eq 1 ]; then
BRANCH="HEAD"
elif [ "${#POSITIONAL[@]}" -eq 2 ]; then
BRANCH="${POSITIONAL[1]}"
elif [ "${#POSITIONAL[@]}" -eq 3 ]; then
if [ -n "$OUTPUT" ]; then
echo "${C_ERR}Error: Too many positional arguments.${C_RST}" >&2
exit 1
fi
BRANCH="${POSITIONAL[1]}"
OUTPUT="${POSITIONAL[2]}"
else
echo "${C_ERR}Error: Too many positional arguments.${C_RST}" >&2
exit 1
fi
fi
if [ -z "$SUBMODULE_MODE" ]; then
if [ -n "$CL_BRANCHES" ] && [ "$PRESERVE" != X ]; then
echo "${C_ERR}Error: Conflict: --single-branch requires -x${C_RST}" >&2
exit 1
fi
if [ -n "$CL_TAGS" ] && [ "$PRESERVE" != X ]; then
echo "${C_ERR}Error: Conflict: --no-tags requires -x${C_RST}" >&2
exit 1
fi
if [ -n "$UPSTREAM" ] && [ -z "$PRESERVE" ]; then
echo "${C_ERR}Error: Conflict: --upstream requires -x${C_RST}" >&2
exit 1
fi
fi
if [ -n "$PARALLEL" ] && [ -n "$CONFIRM" ]; then
echo "${C_WARN}Warning: With --parallel, --confirm only works for first-level submodules.${C_RST}" >&2
PMT="${C_WARN}Proceed? (y/n) ... ${C_RST}"
while true; do
read -p "$PMT" -n 1 -r <&2
case "$REPLY" in
"")
;;
y|Y)
printf '\n' >&2
break
;;
n|N)
printf '\n' >&2
exit 2
;;
*)
printf '\n' >&2
;;
esac
done
fi
if ! which git >/dev/null; then
echo "${C_ERR}Error: git(1) not found${C_RST}" >&2
exit 66
fi
git_new || ([ -z "$QUIET" ] && printf "${C_WARN}Warning: You should upgrade your git (currently %s)${C_RST}\n" "$(git version)" >&2)
if [ -n "$TAG" ]; then
if [ -z "$TAG_FILE" ]; then
TAG_FILE="VERSION"
fi
else
TAG_FILE=
fi
OCWD="$PWD"
if [ -n "$SUBMODULE_MODE" ]; then
if [ -n "$OUTPUT" ]; then
echo "${C_ERR}Error: Conflict: --output cannot be used in submodule mode${C_RST}" >&2
exit 1
fi
if [ -n "$FORCE_DIR" ]; then
echo "${C_ERR}Error: Conflict: --rm-rf cannot be used in submodule mode${C_RST}" >&2
exit 1
fi
if [ "$PRESERVE" = X ] && [ -z "$RAW" ]; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: git-gets by default has -x enabled in submodule mode${C_RST}" >&2
fi
[ -z "$PRESERVE" ] && PRESERVE=X
if [ -n "$GIT_WORK_TREE" ]; then
GIT_WORK_TREE="$(realpath "$GIT_WORK_TREE")"
else
GIT_WORK_TREE="$(git -C "$GIT_WORK_TREE" rev-parse --show-toplevel)"
fi
if [ -n "$GIT_DIR" ]; then
GIT_DIR="$(realpath "$GIT_DIR")"
else
GIT_DIR="$(realpath "$(git rev-parse --git-dir)")"
fi
export GIT_WORK_TREE
export GIT_DIR
if [ -n "$VERBOSE" ]; then
printf "${C_DBG}RAW=%q${C_RST}\n" "$RAW" >&2
printf "${C_DBG}PRESERVE=%q${C_RST}\n" "$PRESERVE" >&2
printf "${C_DBG}TAG=%q${C_RST}\n" "$TAG" >&2
printf "${C_DBG}TAG_FILE=%q${C_RST}\n" "$TAG_FILE" >&2
printf "${C_DBG}PARALLEL=%q${C_RST}\n" "$PARALLEL" >&2
printf "${C_DBG}CONFIRM=%q${C_RST}\n" "$CONFIRM" >&2
printf "${C_DBG}NO_INIT=%q${C_RST}\n" "$NO_INIT" >&2
printf "${C_DBG}NO_RECURSIVE=%q${C_RST}\n" "$NO_RECURSIVE" >&2
printf "${C_DBG}GIT_DIR=%q${C_RST}\n" "$GIT_DIR" >&2
printf "${C_DBG}GIT_WORK_TREE=%q${C_RST}\n" "$GIT_WORK_TREE" >&2
fi
TMP_LOG="$(realpath "$(mktemp)")"
finish() {
cd /
rm -f "$TMP_LOG"
}
trap finish EXIT
cd "$GIT_WORK_TREE"
if [ -n "$(git status --porcelain --untracked-files=no --ignore-submodules=dirty --ignored=no)" ]; then
echo "${C_ERR}Error: Your stage / worktree is NOT clean. Too dangerous to proceed.${C_RST}" >&2
exit 1
fi
got_repo "$(git rev-parse HEAD)"
exit 0
fi
if [ -n "$NO_INIT" ]; then
echo "${C_ERR}Error: Conflict: --no-init cannot be used in regular mode, consider using git-get instead${C_RST}" >&2
exit 1
fi
resolve_gh_url() {
GITHUB=REPO
AMBI=
if grep -qE '^[^/]+/[^/]+$' <<<"$1"; then
GH_REPO="${1%.git}"
elif grep -qE '^[^/]+/[^/]+/commit/[0-9a-f]{4,40}$' <<<"$1"; then
if [ "${#POSITIONAL[@]}" -ne 1 ]; then
echo "${C_ERR}Error: Commit is already specified in the URL${C_RST}" >&2
exit 1
fi
GH_REPO="$(grep -Eo '^[^/]+/[^/]+/commit/' <<<"$1")"
GH_REPO="${GH_REPO%/commit/}"
BRANCH="$(sed -E 's_^[^/]+/[^/]+/commit/__' <<<"$1")"
elif grep -qE '^[^/]+/[^/]+/tree/' <<<"$1"; then
if [ "${#POSITIONAL[@]}" -ne 1 ]; then
echo "${C_ERR}Error: Branch is already specified in the URL${C_RST}" >&2
exit 1
fi
GH_REPO="$(grep -Eo '^[^/]+/[^/]+/tree/' <<<"$1")"
GH_REPO="${GH_REPO%/tree/}"
BRANCH="$(sed -E 's_^[^/]+/[^/]+/tree/__' <<<"$1")"
AMBI=VERY_MUCH
elif grep -qE '^[^/]+/[^/]+/blob/' <<<"$1"; then
if [ -n "$PRESERVE" ]; then
echo "${C_ERR}Error: Conflict: -x|-g|--preserve-git and -- <path> (in the URL)${C_RST}" >&2
exit 1
fi
if [ "${#POSITIONAL[@]}" -ne 1 ]; then
echo "${C_ERR}Error: Branch is already specified in the URL${C_RST}" >&2
exit 1
fi
GH_REPO="$(grep -Eo '^[^/]+/[^/]+/blob/' <<<"$1")"
GH_REPO="${GH_REPO%/blob/}"
BRANCH="$(sed -E 's_^[^/]+/[^/]+/blob/__' <<<"$1")"
AMBI=YES
else
echo "${C_ERR}Error: GitHub URL format not supported, only repo/tree/blob/commit will work${C_RST}" >&2
exit 1
fi
if [ -n "$GH_USE_HTTPS" ]; then
REPO="https://github.com/$GH_REPO.git"
else
REPO="git@github.com:$GH_REPO.git"
fi
if [ -n "$AMBI" ]; then
if [ -z "$(git ls-remote "$REPO" "$BRANCH")" ]; then
echo "${C_ERR}Error: git-gets only works on a whole repository, not a directory or file.${C_RST}" >&2
exit 1
fi
fi
unset AMBI
}
match_gh_url() {
[[ ! "${POSITIONAL[0]}" =~ ^$1 ]] && return 1
[ -n "$2" ] && [ -z "$QUIET" ] && echo "${C_WARN}Warning: $1 is an unencryped protocol${C_RST}" >&2
if [ -n "$3" ]; then
"$3" "${POSITIONAL[0]#${1}}" YES
else
GITHUB=REPO
GH_REPO="${POSITIONAL[0]#${1}}"
GH_REPO="${GH_REPO%/}"
GH_REPO="${GH_REPO%.git}"
if [ -n "$GH_USE_HTTPS" ]; then
REPO="https://github.com/$GH_REPO.git"
elif [ -n "$GH_USE_SSH" ]; then
REPO="git@github.com:$GH_REPO.git"
else
REPO="$1$GH_REPO.git"
fi
fi
}
match_gh_url "https://github.com/" '' resolve_gh_url ||
match_gh_url "http://github.com/" YES resolve_gh_url ||
match_gh_url "git://github.com/" YES '' ||
match_gh_url "ssh://git@github.com/" '' '' ||
match_gh_url "git@github.com:" '' '' ||
if [[ "${POSITIONAL[0]}" =~ ^.*/.*/.*$ ]]; then
GITHUB=
GH_REPO=
REPO="${POSITIONAL[0]}"
else
GITHUB=REPO
GH_REPO="${POSITIONAL[0]}"
if [ -n "$GH_USE_HTTPS" ]; then
REPO="https://github.com/${POSITIONAL[0]}.git"
else
REPO="git@github.com:${POSITIONAL[0]}.git"
fi
fi
if [ -n "$UPSTREAM" ]; then
if [ "$GITHUB" != REPO ]; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: -u is ignored because of lack of support for $REPO.${C_RST}" >&2
UPSTREAM=
elif ! which curl >/dev/null 2>/dev/null; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: -u is ignored because curl(1) is not found in PATH.${C_RST}" >&2
UPSTREAM=
else
set +e
UPSTREAM="$(curl -fsL -H "Accept: application/vnd.github+json" "https://api.github.com/repos/$GH_REPO" \
| sed -nE '/^ "full_name": /{
s/^ "full_name": "(.*)",$/\1/
p
q
}')"
set -e
if [ -z "$UPSTREAM" ]; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: Failed to obtain the upstream of $REPO, maybe it's not a fork.${C_RST}" >&2
elif [ "$UPSTREAM" = "$GH_REPO" ]; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: -u is ignored because it .${C_RST}" >&2
UPSTREAM=
else
UPSTREAM="${REPO/"$GH_REPO"/"$UPSTREAM"}"
fi
fi
fi
BASENAME="$(basename "$REPO")"
BASENAME="${BASENAME%.git}"
if [ -z "$OUTPUT" ]; then
OUTPUT="$BASENAME"
fi
if [ ! "$OUTPUT" = '-' ]; then
OUTPUT="$(fakerealpath "$OUTPUT")"
fi
if [ "$BRANCH" = "HEAD" ]; then
[ -n "$VERBOSE" ] && echo "${C_INFO}Resolving HEAD...${C_RST}" >&2
BRANCH="$(git ls-remote --symref "$REPO" HEAD | { grep '^ref:' || true; } | cut -d $'\t' -f 1)"
if [ -z "$BRANCH" ]; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: Weird repo HEAD non-symbolic, still proceeding${C_RST}" >&2
BRANCH=HEAD
else
BRANCH="${BRANCH#ref: }"
if [ "${BRANCH#refs/heads/}" = "${BRANCH}" ]; then
[ -z "$QUIET" ] && echo "${C_WARN}Warning: Weird repo HEAD symbolic, still proceeding${C_RST}" >&2
else
BRANCH="${BRANCH#refs/heads/}"
fi
fi
fi
if [ -n "$VERBOSE" ]; then
SHA1_SELF="$(git hash-object -t blob --no-filters "$0" || true)"
printf "${C_DBG}GIT_GET_VERSION=%q${C_RST}\n" "$GIT_GET_VERSION" >&2
printf "${C_DBG}SHA1_SELF=%q${C_RST}\n" "$SHA1_SELF" >&2
printf "${C_DBG}RAW=%q${C_RST}\n" "$RAW" >&2
printf "${C_DBG}FORCE_DIR=%q${C_RST}\n" "$FORCE_DIR" >&2
printf "${C_DBG}OUTPUT=%q${C_RST}\n" "$OUTPUT" >&2
printf "${C_DBG}PRESERVE=%q${C_RST}\n" "$PRESERVE" >&2
printf "${C_DBG}TAG=%q${C_RST}\n" "$TAG" >&2
printf "${C_DBG}TAG_FILE=%q${C_RST}\n" "$TAG_FILE" >&2
printf "${C_DBG}PARALLEL=%q${C_RST}\n" "$PARALLEL" >&2
printf "${C_DBG}CONFIRM=%q${C_RST}\n" "$CONFIRM" >&2
printf "${C_DBG}NO_RECURSIVE=%q${C_RST}\n" "$NO_RECURSIVE" >&2
printf "${C_DBG}GITHUB=%q${C_RST}\n" "$GITHUB" >&2
printf "${C_DBG}GH_REPO=%q${C_RST}\n" "$GH_REPO" >&2
printf "${C_DBG}REPO=%q${C_RST}\n" "$REPO" >&2
printf "${C_DBG}UPSTREAM=%q${C_RST}\n" "$UPSTREAM" >&2
printf "${C_DBG}BRANCH=%q${C_RST}\n" "$BRANCH" >&2
printf "${C_DBG}BASENAME=%q${C_RST}\n" "$BASENAME" >&2
fi
if [ "$OUTPUT" = "-" ]; then
true
elif [ -d "$OUTPUT" ]; then
if [ -z "$FORCE_DIR" ]; then
printf "${C_ERR}Error: Directory %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
fi
elif [ -f "$OUTPUT" ]; then
if [ -z "$FORCE" ]; then
printf "${C_ERR}Error: File %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
fi
elif [ -e "$OUTPUT" ]; then
printf "${C_ERR}Error: Weird file %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
fi
WORK_DIR="$(realpath "$(mktemp -d)")"
finish() {
set +eu
cd /
if [ -n "$KEEP_WORK_DIR" ]; then
printf "${C_WARN}Notice: WORK_DIR located at %s${C_RST}\n" "$WORK_DIR"
else
rm -rf "$WORK_DIR"
fi
}
trap finish EXIT
cd "$WORK_DIR"
TMP_LOG="$WORK_DIR/parallel.log"
[ -n "$VERBOSE" ] && printf "${C_DBG}WORK_DIR=%q${C_RST}\n" "$WORK_DIR" >&2
# 0. git 2.20+ -> shallow + [filter] -> filter
if git_new; then
mkdir -p "$BASENAME"
if [ ! "$PRESERVE" = X ] && [ -n "$(git ls-remote "$REPO" "$BRANCH")" ]; then
ARGS=(git clone --bare)
[ -n "$QUIET" ] && ARGS+=(-q)
[ -z "$QUIET" ] && ARGS+=(--progress)
[ ! "$BRANCH" = "HEAD" ] && ARGS+=(--branch "$BRANCH")
ARGS+=(--depth 1 --no-tags)
ARGS+=(--filter blob:none)
ARGS+=("$REPO" "$BASENAME/.git")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
set +e
if "${ARGS[@]}"; then
set -e
GIT_WORK_TREE="$(realpath "$BASENAME")"
GIT_DIR="$(realpath "$BASENAME/.git")"
export GIT_WORK_TREE
export GIT_DIR
git config --bool core.bare false
got_repo "$(git rev-parse HEAD)"
fi
set -e
[ -z "$QUIET" ] && echo "${C_WARN}Warning: Failed git clone, will attempt another method${C_RST}"
fi
if true; then
ARGS=(git clone --mirror --filter tree:0)
[ -n "$QUIET" ] && ARGS+=(-q)
[ -z "$QUIET" ] && ARGS+=(--progress)
ARGS+=("$REPO" "$BASENAME/.git")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
set +e
if "${ARGS[@]}"; then
SHA1="$(git -C "$BASENAME" rev-parse "$BRANCH")"
if [ -n "$SHA1" ]; then
[ -z "$QUIET" ] && printf "${C_INFO}Got SHA1: %q${C_RST}\n" "$SHA1" >&2
set -e
NSHA1="$(git --git-dir="$BASENAME/.git" rev-parse "$SHA1^{commit}")"
[ ! "$SHA1" = "$NSHA1" ] && printf "${C_INFO}Got actual SHA1: %q${C_RST}\n" "$NSHA1" >&2
GIT_WORK_TREE="$(realpath "$BASENAME")"
GIT_DIR="$(realpath "$BASENAME/.git")"
export GIT_WORK_TREE
export GIT_DIR
git config --bool core.bare false
git update-ref --no-deref HEAD "$NSHA1"
got_repo "$NSHA1"
fi
fi
set -e
fi
rm -rf "$BASENAME"
fi
printf "${C_ERR}Error: No viable method.${C_RST}\n" >&2
exit 3