/
git-get
executable file
·906 lines (857 loc) · 30.4 KB
/
git-get
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
#!/bin/bash
GIT_GET_VERSION=
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 "$PREV_TAGS" ]; then
PREV_TAGS=
else
PREV_TAGS="$PREV_TAGS"
fi
if [ -z "$OCWD" ]; then
OCWD="$PWD"
else
OCWD="$OCWD"
fi
set -euo pipefail
usage()
{
exec man git-get
}
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
}
normalize() {
X="$1"
ODIR=
while [ ! "$X" = "$ODIR" ]; do
ODIR="$X"
# shellcheck disable=SC2016
X="$(sed -E 's_//_/_g; s_/\./_/_g; s_[^/]+/\.\./_/_g; s_/$__g; s_^/__g; s_/\.$__g; s_^\./__g' <<<"$X")"
done
printf '%s' "$X"
}
COLOR=
[ -t 2 ] && COLOR=YES
QUIET=
VERBOSE=
FORCE=
FORCE_DIR=
OUTPUT=
PRESERVE=
TAG=
TAG_FILE=
DIR=
SPARSE=
UPSTREAM=
CL_BRANCHES=
CL_TAGS=
POSITIONAL=()
while [ $# -gt 0 ]; do
case "$1" in
-X)
shift
set -- -x -u -B -T "$@"
;;
-Y)
shift
set -- -t "$@"
;;
-V|--version)
echo "git-get(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|--force)
FORCE=YES
shift
;;
-F|--rm-rf)
FORCE=YES
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
;;
--)
if [ "$#" -eq 2 ]; then
DIR="$2"
shift
shift
elif [ "$#" -eq 1 ]; then
DIR=
SPARSE=YES
shift
else
echo "Error: Only one <dir> allowed after --, I saw $*" >&2
exit 1
fi
break
;;
*)
if [[ "$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 [ -n "$PRESERVE" ] && [ -n "$DIR" ]; then
echo "${C_ERR}Error: Conflict: -x|-g|--preserve-git and -- <path>${C_RST}" >&2
echo "${C_NOTICE}Hint: You may consider git-get the whole repo with -- option first:" >&2
echo " git get [-x|-g] [<options>] <repo> --" >&2
echo " and then manually:" >&2
echo " git sparse-checkout add $DIR${C_RST}" >&2
exit 1
fi
if [ -z "$PRESERVE" ] && [ -n "$SPARSE" ]; then
echo "${C_ERR}Error: Conflict: -- requires -x|-g|--preserve-git${C_RST}" >&2
exit 1
fi
if [ -n "$GH_USE_HTTPS" ] && [ -n "$GH_USE_SSH" ]; then
echo "${C_ERR}Error: Conflict: GH_USE_HTTPS and GH_USE_SSH${C_RST}" >&2
exit 1
fi
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
if [ "$#" -gt 0 ]; then
echo "${C_ERR}Error: Only one path allowed!${C_RST}" >&2
exit 1
fi
if [ "${#POSITIONAL[@]}" -eq 0 ]; then
echo "${C_ERR}Error: Must specify <url> or <user>/<repo> (GitHub)${C_RST}"
exit 1
elif [ "${#POSITIONAL[@]}" -eq 1 ]; then
BRANCH="HEAD"
elif [ "${#POSITIONAL[@]}" -eq 2 ]; then
BRANCH="${POSITIONAL[1]}"
else
echo "${C_ERR}Error: Too many positional arguments, maybe you forgot -- before <path>?${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
while [[ "$BRANCH" =~ / ]]; do
if [ -n "$(git ls-remote "$REPO" "$BRANCH")" ]; then
break
fi
DIR="$(basename "$BRANCH")/$DIR"
BRANCH="$(dirname "$BRANCH")"
done
fi
unset AMBI
}
resolve_gist_url() {
GITHUB=GIST
BRANCH=master
DIR=
if [[ "$1" =~ \#file- ]]; then
DIR="${1#*#file-}"
GH_REPO="${1%%#*}"
else
GH_REPO="$1"
fi
if [[ "$GH_REPO" =~ / ]]; then
GH_REPO="${GH_REPO#*/}"
fi
if [[ "$GH_REPO" =~ / ]]; then
echo "${C_ERR}Error: GitHub Gist URL format not supported${C_RST}" >&2
exit 1
fi
if [ -n "$GH_USE_HTTPS" ]; then
REPO="https://gist.github.com/$GH_REPO.git"
else
REPO="git@gist.github.com:$GH_REPO.git"
fi
}
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_gist_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}}"
else
GITHUB=GIST
GH_REPO="${POSITIONAL[0]#${1}}"
GH_REPO="${GH_REPO%/}"
GH_REPO="${GH_REPO%.git}"
if [ -n "$GH_USE_HTTPS" ]; then
REPO="https://gist.github.com/$GH_REPO.git"
elif [ -n "$GH_USE_SSH" ]; then
REPO="git@gist.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:" '' '' ||
match_gist_url "https://gist.github.com/" '' resolve_gist_url ||
match_gist_url "http://gist.github.com/" YES resolve_gist_url ||
match_gist_url "git://gist.github.com/" YES '' ||
match_gist_url "ssh://git@gist.github.com/" '' '' ||
match_gist_url "git@gist.github.com:" '' '' ||
if [[ "${POSITIONAL[0]}" =~ : ]] || [[ "${POSITIONAL[0]}" =~ ^\. ]]; then
GITHUB=
GH_REPO=
REPO="${POSITIONAL[0]}"
else
resolve_gh_url "${POSITIONAL[0]}" ''
fi
if [ -n "$PRESERVE" ] && [ -n "$DIR" ]; then
echo "${C_ERR}Error: Can't use an URL (of a folder or file) with -x|-g|--preserve-git${C_RST}" >&2
echo "${C_NOTICE}Hint: You may consider git-get the whole repo with -- option first:" >&2
echo " git get [-x|-g] [<options>] <repo> --" >&2
echo " and then manually:" >&2
echo " git sparse-checkout add $DIR${C_RST}" >&2
exit 1
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
DIR="$(normalize "$DIR")"
if [ -n "$DIR" ]; then
BASENAME="$(basename "$DIR")"
elif [ "$GITHUB" = GIST ]; then
BASENAME="$GH_REPO"
else
BASENAME="$(basename "$REPO")"
BASENAME="${BASENAME%.git}"
fi
if [ -z "$OUTPUT" ]; then
OUTPUT="$BASENAME"
DEF_OUTPUT=YES
else
DEF_OUTPUT=
fi
if [ -n "$TAG" ]; then
if [ -z "$TAG_FILE" ]; then
TAG_FILE="VERSION"
fi
else
TAG_FILE=
fi
if ! which git >/dev/null; then
echo "${C_ERR}Error: git(1) not found${C_RST}"
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 [ "$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 [ ! "$OUTPUT" = '-' ]; then
OUTPUT="$(fakerealpath "$OUTPUT")"
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}OCWD=%q${C_RST}\n" "$OCWD" >&2
printf "${C_DBG}FORCE=%q${C_RST}\n" "$FORCE" >&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}DEF_OUTPUT=%q${C_RST}\n" "$DEF_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}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}BRANCH=%q${C_RST}\n" "$BRANCH" >&2
printf "${C_DBG}DIR=%q${C_RST}\n" "$DIR" >&2
printf "${C_DBG}SPARSE=%q${C_RST}\n" "$SPARSE" >&2
printf "${C_DBG}UPSTREAM=%q${C_RST}\n" "$UPSTREAM" >&2
printf "${C_DBG}BASENAME=%q${C_RST}\n" "$BASENAME" >&2
printf "${C_DBG}PREV_TAGS=%q${C_RST}\n" "$PREV_TAGS" >&2
fi
if [ -n "$PRESERVE" ] || [ -z "$DIR" ]; then # must result in a dir
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
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"
[ -n "$VERBOSE" ] && printf "${C_DBG}WORK_DIR=%q${C_RST}\n" "$WORK_DIR" >&2
got_file() {
set -e
if [ "$OUTPUT" = "-" ]; then
if [ "$3" = "LINK" ]; then
readlink "$1"
else
cat "$1"
fi
elif [ -f "$OUTPUT" ]; then
if [ -n "$FORCE" ]; then
[ -z "$QUIET" ] && printf "${C_WARN}Warning: Overriding file %q${C_RST}\n" "$OUTPUT" >&2
mv -f "$1" "$OUTPUT"
else
printf "${C_ERR}Error: File %q exists${C_RST}\n" "$OUTPUT" >&2
exit 1
fi
elif [ -d "$OUTPUT" ]; then
if [ -n "$DEF_OUTPUT" ]; then
printf "${C_ERR}Error: There is a directory %q${C_RST}\n" "$OUTPUT" >&2
exit 1
else
mv "$1" "$OUTPUT"
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
if [ -z "$PREV_TAGS" ]; then
T="$2"
else
T="$(printf '%s\n%s' "$PREV_TAGS" "$2")"
fi
if [ -z "$TAG" ]; then
[ -z "$QUIET" ] && printf "${C_NOTICE}tag = %q${C_RST}\n" "$T" >&2
else
if [[ "$TAG_FILE" =~ ^/ ]]; then
TG="$TAG_FILE"
else
TG="$OCWD/$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-get continues.${C_RST}" "$TG" >&2
else
printf "%s\n" "$T" >"$TG"
fi
fi
exit 0
}
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" ]; 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
if [ -z "$PREV_TAGS" ]; then
T="$2"
else
T="$(printf '%s\n%s' "$PREV_TAGS" "$2")"
fi
if [ -z "$TAG" ]; 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
printf "${C_NOTICE}%s${C_RST}\n" "$T" >"$TG"
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 --bool core.bare false
git config core.repositoryformatversion 0
if [ -z "$DIR" ]; then
if [ "$PRESERVE" = X ]; 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 "$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
unset GIT_DIR
if [ -n "$SPARSE" ]; then
ARGS=(git sparse-checkout set --sparse-index --cone)
[ -n "$QUIET" ] && ARGS+=(-q)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >&2)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >&2)
fi
ARGS=(git reset --hard)
[ -n "$QUIET" ] && ARGS+=(-q)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >&2)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >&2)
[ -z "$PRESERVE" ] && mv "$1/.git" ".git"
got_dir "$1" "$2" && exit 0
else
# This workaround fixes #14 (https://github.com/b1f6c1c4/git-get/issues/14)
# which is caused by https://github.com/git/git/commit/35a9f1e99c5d31635bb78a4f2d498e72c04fc471
P=""
while IFS= read -r -u 3 line; do
TP="$(cut -d $'\t' -f 2 <<<"$line")"
if [ "$TP" = "$DIR" ] || [ ! "${DIR#${TP}/}" = "$DIR" ]; then
P="$TP"
fi
done 3< <(git ls-tree HEAD -r | grep '^160000 commit ')
unset GIT_DIR
if [ -n "$P" ]; then
[ -n "$VERBOSE" ] && printf "${C_INFO}Checking %q -> %s (%q)${C_RST}\n" "$DIR" "commit" "$P" >&2
# To support legacy git, use git checkout here
# ARGS=(git restore --source=HEAD --staged --worktree)
ARGS=(git checkout HEAD)
[ -n "$QUIET" ] && ARGS+=(-q)
ARGS+=(-- ".gitmodules" "$P")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >&2)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >&2)
ARGS=(git submodule)
[ -n "$QUIET" ] && ARGS+=(--quiet)
ARGS+=(init)
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >&2)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >&2)
while IFS= read -r -u 3 line; do
PA="$(git config --file "$1/.gitmodules" "submodule.$line.path")"
URL="$(git config --file "$1/.git/config" "submodule.$line.url")"
SSHA1="$(git --git-dir="$1/.git" ls-files -s "$PA" | cut -d ' ' -f2)"
[ -n "$VERBOSE" ] && printf "${C_WARN}%s %s %s %s${C_RST}\n" "$line" "$PA" "$URL" "$SSHA1" >&2
[ ! "$PA" = "$P" ] && continue
ARGS=("$0")
[ -n "$VERBOSE" ] && ARGS+=(--verbose)
[ -n "$QUIET" ] && ARGS+=(--quiet)
[ -n "$COLOR" ] && ARGS+=(--color=always) || ARGS+=(--color=never)
[ -n "$GH_USE_HTTPS" ] && ARGS+=(--https)
[ -n "$GH_USE_SSH" ] && ARGS+=(--ssh)
[ -n "$CL_BRANCHES" ] && ARGS+=(--single-branch)
[ -n "$CL_TAGS" ] && ARGS+=(--no-tags)
ARGS+=("$URL" "$SSHA1")
ARGS+=(-o "$OUTPUT")
[ -n "$FORCE" ] && ARGS+=(--force)
[ -n "$FORCE_DIR" ] && ARGS+=(--rm-rf)
[ -n "$TAG_FILE" ] && ARGS+=(--tag-file "$TAG_FILE")
[ ! "$P" = "$DIR" ] && ARGS+=(-- "${DIR#$P/}")
[ -z "$QUIET" ] && printf "${C_NOTICE}+++ %s %s %s${C_RST}\n" "$PA" "$URL" "$SSHA1">&2
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}" >&2; printf '%q ' "${ARGS[@]}" >&2; printf "${C_RST}\n">&2)
if [ -z "$PREV_TAGS" ]; then
PREV_TAGS="$2"
else
PREV_TAGS="$(printf '%s\n%s' "$PREV_TAGS" "$2")"
fi
OCWD="$OCWD" PREV_TAGS="$PREV_TAGS" "${ARGS[@]}" 2>&1 | sed 's/^/ /' >&2
exit "$?"
done 3< <(sed -nE '/^\[submodule /{
s/^\[submodule "(.*)"\]$/\1/
p
}' "$1/.git/config")
fi
# git restore --worktree issue fetch-pack one-by-another
# which is super slow
# This workaround fixes #8 (https://github.com/b1f6c1c4/git-get/issues/8)
# which is caused by https://github.com/git/git/commit/95acf11a3dc3d18ec999f4913ec6c6a54545c6b7
ARGS=(git diff-index HEAD --cached -p)
ARGS+=(-- "$DIR")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >/dev/null)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >/dev/null)
ARGS=(git reset HEAD)
[ -n "$QUIET" ] && ARGS+=(-q)
ARGS+=(-- "$DIR")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >&2)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >&2)
# To support legacy git, use git checkout here
# ARGS=(git restore --worktree)
ARGS=(git checkout)
[ -n "$QUIET" ] && ARGS+=(-q)
ARGS+=(-- "$DIR")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}" >&2; printf '%q ' "${ARGS[@]}" >&2; printf "${C_RST}\n">&2)
[ -z "$QUIET" ] && (cd "$1" && "${ARGS[@]}" >&2)
[ -n "$QUIET" ] && (cd "$1" && "${ARGS[@]}" 2>/dev/null >&2)
[ -f "$1/$DIR" ] && got_file "$1/$DIR" "$2"
[ -d "$1/$DIR" ] && got_dir "$1/$DIR" "$2"
[ -L "$1/$DIR" ] && got_file "$1/$DIR" "$2" LINK
echo "${C_ERR}Error: File type not supported.${C_RST}" >&2
exit 2
fi
}
# 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)
if [ -n "$DIR" ] || [ -n "$SPARSE" ]; then
ARGS+=(--filter blob:none)
fi
ARGS+=("$REPO" "$BASENAME/.git")
[ -n "$VERBOSE" ] && (printf "${C_NOTICE}"; printf '%q ' "${ARGS[@]}"; printf "${C_RST}\n") >&2
set +e
if "${ARGS[@]}"; then
set -e
export GIT_DIR="$(realpath "$BASENAME/.git")"
got_repo "$BASENAME" "$(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
export GIT_DIR="$(realpath "$BASENAME/.git")"
SHA1="$(git rev-parse "$BRANCH")"
if [ -n "$SHA1" ]; then
[ -z "$QUIET" ] && printf "${C_INFO}Got SHA1: %q${C_RST}\n" "$SHA1" >&2
set -e
NSHA1="$(git rev-parse "$SHA1^{commit}")"
[ ! "$SHA1" = "$NSHA1" ] && printf "${C_INFO}Got actual SHA1: %q${C_RST}\n" "$NSHA1" >&2
git update-ref --no-deref HEAD "$NSHA1"
got_repo "$BASENAME" "$NSHA1"
fi
fi
set -e
fi
rm -rf "$BASENAME"
fi
printf "${C_ERR}Error: No viable method.${C_RST}\n" >&2
exit 3