-
Notifications
You must be signed in to change notification settings - Fork 2
/
register-git-cache.sh
executable file
·1170 lines (1080 loc) · 51.9 KB
/
register-git-cache.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
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
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# NOTE: bash syntax used, e.g. `;&` to fall through case statements
# Does not work in other /bin/sh handlers like dash!
# This script manages registration of remote git repositories
# in the current single workspace, to help faster CI clones
# with a git-reference. It shoud be instantiated on each worker
# or shared via NFS. The full pathname of the script should be
# available (as a copy or symlink) in the directory it would
# manage as the git reference/cache repository. After you
# register the repositories you want to track, you can call
# this script from a crontab to occasionally update the cache.
#
# For background, see e.g.
# https://support.cloudbees.com/hc/en-us/articles/115001728812-Using-a-Git-reference-repository
# https://randyfay.com/content/reference-cache-repositories-speed-clones-git-clone-reference
# https://randyfay.com/content/git-clone-reference-considered-harmful for some caveats
# In particular, note that usage of reference repositories does its magic by
# having a newly cloned (and later updated?) repository refer to bits of code
# present in some filesystem path, rather than make a copy from the original
# remote repository again - so saving network, disk space and maybe time.
# Corollaries:
# * the reference repo should be in the filesystem (maybe over NFS) and
# at the same locally resolved FS path if shared across build agents
# * the reference repo should be at least readable to the build account
# when used in Jenkins
# * files (and contents inside) should not disappear or be renamed over
# time (garbage collection, pruning, etc. do that) or the cloned repo
# will become invalid (not a big issue for workspaces that made a build
# in the past and are not reused as such, but may be a problem to remake
# the same run without extra rituals to create a coherent checkout;
# not sure if this is also a problem for reusing the workspace for later
# runs of a job, with same or other commits)
# ** there is a "git disassociate" command for making a workspace standalone
# again, by copying into it the data from a reference repo - forfeiting
# the disk savings, but keeping the network/time improvements probably;
# this is not integrated into Jenkins Git client side, AFAIK
# * the advanced option to use a reference repo is only applied during
# cloning - existing workspaces must be remade to try it out
#
# This script also aims to help maintain a fanout of reference repositories
# hosted under a single directory, as proposed for improving performance in
# Jenkins jobs using a single configuration (e.g. generated from OrgFolders):
# https://issues.jenkins.io/browse/JENKINS-64383
# https://github.com/jenkinsci/git-client-plugin/pull/644
# For this support, it adds the following environment variables to fork and
# work in a determined sub-directory, applied if REFREPODIR_MODE is not empty:
# * REFREPODIR_BASE - location of the top-level refrepo (where this script
# or symlink to it is located, by default)
# * REFREPODIR - location of the refrepo determined for the Git URL
# * REFREPODIR_MODE using a suffix defined in PR#644 above:
# GIT_URL_BASENAME = subdir named by "basename" of normalized repo URL
# GIT_URL_SHA256 = subdir named by hash of normalized repo URL
# GIT_SUBMODULES = sha256 if present, or basename otherwise (here we
# use bare repo on top, so real submodules are not handled directly)
# GIT_URL = subdir tree named verbatim by normalized repo URL (not portable)
# GIT_*_FALLBACK = if dir named above is not present, use REFREPODIR_BASE
# The new method get_subrepo_dir() can be used to determine in script code
# whether such forking should be used for adding or updating a Git URL: if
# it returns a success and not-empty string, that is the dir to (make and)
# change into for the actual git operations for that one Git URL.
#
# Also can be used in Windows with the Linux-like environment provided by
# e.g. builds of Git for Windows: https://git-scm.com/download/win :
# C:> set REFREPODIR_MODE=GIT_SUBMODULES
# C:> register-git-cache.sh add-recursive https://github.com/org/repo.git
#
# Copyright 2018-2021 (C) Jim Klimov <[email protected]>
# Shared on the terms of MIT license.
# Original development tracked at https://github.com/jimklimov/git-scripts
#
LC_ALL=C
LANG=C
TZ=UTC
export LC_ALL LANC TZ
# These chars passed via shell as escape sequences confuse grep/sed/...
# sometimes, so better pass them "as is":
TABCHAR="`printf '\t'`"
EOLCHAR="`printf '\n'`"
# Just prepend $CI_TIME to a command line to optionally profile it:
case "${CI_TIME-}" in
time|time_wrapper|*/time) ;;
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) CI_TIME="time_wrapper" ;;
*) CI_TIME="" ;;
esac
# Should we dig into loops (more data, more impact from collecting it)?
case "${CI_TIME_LOOPS-}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) CI_TIME_LOOPS="true" ;;
*) CI_TIME_LOOPS="false" ;;
esac
if [ "${DEBUG-}" = true ]; then
set -x
else
DEBUG=false
fi
# Match handled parameters of do_fetch()
case "${DO_FETCH-}" in
false|-vs|-vp|-v|-p|-s) ;; # Keep the value we might pass as argument or use in conditionals
true)
if [ -n "${REFREPODIR_MODE-}" ]; then
DO_FETCH="-p" # We handle '-p' for default parallelized fetching safely across different dirs
else
DO_FETCH="-vs"
fi
;;
default|"") DO_FETCH="" ;; # Use default non-verbose handling
*) DO_FETCH="" ;; # ditto
esac
# NOTE: Currently all the support happens without extra recursion,
# so maybe this varname will be dropped.
if [ -n "${REFREPODIR-}" ]; then
# Up to the caller (including recursion with REFREPODIR_MODE options)
# to make sure the request is valid (especially for relative paths!)
cd "${REFREPODIR}" || { echo "FATAL: REFREPODIR='$REFREPODIR' was specified but not usable" >&2 ; exit 1; }
if [ -z "${REFREPODIR_BASE-}" ] ; then
echo "WARNING: REFREPODIR_BASE for the parent is not specified, would use REFREPODIR as the top level" >&2
REFREPODIR_BASE="`pwd`"
fi
else
# With empty REFREPODIR, this script instance is not recursing now.
# If a valid REFREPODIR_MODE is not empty, it will recurse for git ops.
cd "`dirname $0`" || exit 1
REFREPODIR_BASE="`pwd`"
export REFREPODIR_BASE
fi
# This file can list line by line shell-glob (case) patterns to avoid addition
# of certain URLs (e.g. by automated jobs parsing a history of build setups,
# including references to SCM server instances that no longer exist).
EXCEPT_PATTERNS_FILE="${REFREPODIR_BASE}/.except"
case "${QUIET_SKIP-}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]) QUIET_SKIP=true ;;
*) QUIET_SKIP=false ;;
esac
# Throttling inspired by https://stackoverflow.com/a/8735146/4715872
# TODO? Detect from CPU count
[ -n "$MAXJOBS" ] && [ "$MAXJOBS" -gt 0 ] \
|| MAXJOBS=8
throttle_running_child_count() {
local SLEPT=false
while [ "`jobs -pr | wc -l`" -ge $MAXJOBS ]; do
sleep 0.1
SLEPT=true
done
if $SLEPT && ( $DEBUG || [ -n "$CI_TIME" ] ) ; then
echo "[D] `date`: Was blocked due to child subprocessess, now proceeding" >&2
fi
}
# Detect a time-stamping capable (GNU) date in the current system
for GDATE in ${GDATE-} gdate date false ; do
D="`$GDATE -u +%s 2>/dev/null`" && [ -n "$D" ] && [ "$D" -gt 0 ] && break
done
if [ "$GDATE" = false ]; then GDATE=""; fi
time_wrapper() {
local TS_START=0
local TS_END=0
local SUB_RES=0
local TS_TEXT=''
[ -z "$GDATE" ] || TS_START="`$GDATE -u +%s`"
time "$@" || SUB_RES=$?
[ -z "$GDATE" ] || { TS_END="`$GDATE -u +%s`" ; TS_TEXT=" after $(($TS_END - $TS_START)) whole seconds"; }
echo "[D] `date`: Completed command with code ${SUB_RES}${TS_TEXT} in dir '`pwd`': $*" >&2
return $SUB_RES
}
declare -A KNOWN_EXCLUDED
is_repo_not_excluded() {
# Returns 0 if we can go on registering/processing; 1 to skip this repo
local REPO
REPO="$1"
if [ "${KNOWN_EXCLUDED["$REPO"]-}" = 1 ] ; then
return 1
fi
if [ ! -s "$EXCEPT_PATTERNS_FILE" ] || [ "$SKIP_EXCEPT_PATTERNS_FILE" = true ] ; then
# No exceptions
return 0
fi
while read PAT ; do
[ -n "$PAT" ] || continue
case "$REPO" in
"#"*) continue ;;
$PAT|$PAT.git)
$QUIET_SKIP || echo "SKIP: Repo '$REPO' excluded by pattern '$PAT(.git)'" >&2
KNOWN_EXCLUDED["$REPO"]=1
return 1
;;
esac
case "$REPO.git" in
$PAT|$PAT.git)
$QUIET_SKIP || echo "SKIP: Repo '$REPO(.git)' excluded by pattern '$PAT(.git)'" >&2
KNOWN_EXCLUDED["$REPO"]=1
return 1
;;
esac
done < "$EXCEPT_PATTERNS_FILE"
# None of defined exceptions matched this repo
return 0
}
# We have a number of lookups that per original logic anyhow listed
# all repos and then filter the data. In loops like recursive addition
# we tend to re-request this info - so better not hit the disk if we
# can. This array is populated from routine below (called e.g. from
# do_list_repoids() routine) and by registration of new repo URLs.
# While probably wasteful for smaller operations aiming to only act
# on a few repos and exit (and so was slated for arguments-dependent
# optimization in do_list_repoids() routine), this should help with
# the heavier updates.
declare -a KNOWN_REPOIDS
cache_list_repoids() {
if [ "${#KNOWN_REPOIDS[@]}" -eq 0 ]; then
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Caching repoid's and locations for all known repo URL(s)" >&2
fi
IFSOLD="$IFS"
IFS="${EOLCHAR}"
# Store tab-separated (multi-token) strings: "REPOID URL URL_LOWERCASED SUBDIRNAME"
# For items in main REFREPODIR_BASE the SUBDIRNAME is empty, trailing tab optional
KNOWN_REPOIDS=( $(IFS="$IFSOLD"; ($CI_TIME git remote -v || echo "FAILED to 'git remote -v' in '`pwd`'">&2 ; \
if [ -n "${REFREPODIR_MODE-}" ] ; then \
for DG in `ls -1d "${REFREPODIR_BASE-}"/*/.git "${REFREPODIR_BASE-}"/*/objects 2>/dev/null` ; do \
( D="`dirname "$DG"`" && cd "$D" && { $CI_TIME git remote -v || echo "FAILED to 'git remote -v' in '`pwd`'">&2 ; } | sed 's,$,\t'"`basename "$D"`," ) ; \
done; \
fi; \
) | sed -e '/(fetch)/!d' -e 's,^\([^ '"${TABCHAR}"']*\)[ '"${TABCHAR}"']*\([^ ]*\)[ '"${TABCHAR}"']*(fetch),\1\t\2\t\L\2,' ) )
# TODO: Make an efficient fix for non-GNU sed (\L\2 for lowercasing \2 is an extension)
IFS="$IFSOLD"
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Cached repoid's and locations for all known repo URL(s)" >&2
fi
fi
}
# Track unique repos we have registered now
declare -A REGISTERED_NOW
declare -A REGISTERED_EARLIER
do_register_repo() {
# REPO is a substring from `git remote` listing,
# so technically can be an ID or part of URL (but
# only a single complete URL makes sense for adding)
local REPO
REPO="$1"
[ "${REGISTERED_NOW["$REPO"]}" = 1 ] \
&& { $QUIET_SKIP || echo "SKIP: Repo '$REPO' already registered during this run" ; } \
&& return 42
[ "${REGISTERED_EARLIER["$REPO"]}" = 1 ] \
&& { $QUIET_SKIP || echo "SKIP: Repo '$REPO' was registered earlier and already seen during this run" ; } \
&& return 0
is_repo_not_excluded "$REPO" || return 0 # being excluded is not a fatal error, just a skip (reported there)
local REFREPODIR_REPO
[ -n "${REFREPODIR_MODE-}" ] && REFREPODIR_REPO="`get_subrepo_dir "$REPO"`" \
&& { mkdir -p "${REFREPODIR_REPO}" && pushd "${REFREPODIR_BASE}/${REFREPODIR_REPO}" >/dev/null && trap 'popd >/dev/null ; trap - RETURN' RETURN || exit $? ; }
[ -e .git ] || [ -s HEAD ] || \
( echo "[I] `date`: === Initializing bare repository for git references at `pwd` ..." ; \
$CI_TIME git init --bare && $CI_TIME git config gc.auto 0 ) || exit $? # fatal error
# Print entries line by line. Note that the hits above for REGISTERED_NOW
# or REGISTERED_EARLIER could only happen in another iteration below this
# point, so KNOWN_REPOIDS would be updated there as well.
if IFS="${EOLCHAR}" eval 'echo "${KNOWN_REPOIDS[*]}"' | egrep -i '[ '"${TABCHAR}"']'"$REPO"'($|[ '"${TABCHAR}"']'"$REFREPODIR_REPO"'$)' >/dev/null ; then
echo "SKIP: Repo '$REPO' already registered in `pwd` per KNOWN_REPOIDS" >&2
REGISTERED_EARLIER["${REPO}"]=1
REGISTERED_EARLIER["${REPO} `pwd`"]=1
return 0
fi
local OUT REPOID
OUT="`$CI_TIME git remote -v | grep -i "$REPO"`" \
&& REPOID="`echo "$OUT" | head -1 | awk '{print $1}'`" \
&& echo "SKIP: Repo '$REPO' already registered as '$REPOID' in `pwd`" \
&& KNOWN_REPOIDS+=( "$(printf '%s\t%s\t%s\t%s' "$REPOID" "$REPO" "`lc "$REPO"`" "$REFREPODIR_REPO")" ) \
&& REGISTERED_EARLIER["${REPO}"]=1 \
&& REGISTERED_EARLIER["${REPO} `pwd`"]=1 \
&& return 0
sleep 1 # ensure unique ID
REPOID="repo-`date -u +%s`"
$CI_TIME git remote add "$REPOID" "$REPO" \
&& $CI_TIME git remote set-url --push "$REPOID" no_push \
&& echo "[I] `date`: OK: Registered repo '$REPOID' => '$REPO' in `pwd`" \
&& KNOWN_REPOIDS+=( "$(printf '%s\t%s\t%s\t%s' "$REPOID" "$REPO" "`lc "$REPO"`" "$REFREPODIR_REPO")" ) \
&& REGISTERED_NOW["$REPO"]=1
#?# && REGISTERED_NOW["$REPOID"]="$REPO"
}
do_list_remotes() {
# For each arg, do git-ls-remote - List references in a remote repository
# (NOT listing of known remote repo IDs - see do_list_repoids() for that)
local TS_START=0
local TS_END=0
local TS_TEXT=''
if [ -n "$CI_TIME" ]; then
[ -z "$GDATE" ] || TS_START="`$GDATE -u +%s`"
echo "[D] `date`: Discovering references from any tip commit of repo(s): $*" >&2
fi
( TEMPDIR_REMOTES="`mktemp -d --tmpdir rgc.XXXXXX`" && [ -n "$TEMPDIR_REMOTES" ] && [ -d "$TEMPDIR_REMOTES" ] || TEMPDIR_REMOTES=""
if [ -n "$TEMPDIR_REMOTES" ] ; then
# Absolutize to be sure
TEMPDIR_REMOTES="$(cd "$TEMPDIR_REMOTES" && pwd)"
#trap 'echo "do_list_remotes(): REMOVING TEMPDIR_REMOTES=$TEMPDIR_REMOTES">&2 && rm -rf "$TEMPDIR_REMOTES"' 0
trap 'rm -rf "$TEMPDIR_REMOTES"' 0
else
echo "do_list_remotes(): Failed to create TEMPDIR_REMOTES" >&2
exit 1
fi
# Temp files proved crucial to not mix up stdout's from parallel child
# processes which happened sometimes in the original implementation
for REPO in "$@" ; do
echo "[I] `date`: ===== Listing remotes of '$REPO'..." >&2
is_repo_not_excluded "$REPO" || continue # not a fatal error, just a skip (reported there)
(
local REFREPODIR_REPO=''
[ -n "${REFREPODIR_MODE-}" ] && REFREPODIR_REPO="`get_subrepo_dir "$REPO"`" \
&& { pushd "${REFREPODIR_BASE}/${REFREPODIR_REPO}" >/dev/null || exit $? ; }
{ $CI_TIME git ls-remote "$REPO" || echo "[I] `date`: FAILED to 'git ls-remote $REPO' in '`pwd`'">&2 ; } \
| awk -v REPODIR="${REFREPODIR_REPO}" -v REPOURL="${REPO}" '{print $1"\t"$2"\t"REPOURL"\t"REPODIR}' \
> "`mktemp --tmpdir="$TEMPDIR_REMOTES" remote-refs.XXXXXXXXXXXX`"
# Note: the trailing column is empty for discoveries/runs without REFREPODIR
# And we ignore here faults like absent remotes... or invalid Git dirs...
) &
throttle_running_child_count
done
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Waiting for subprocesses for discovery of references from any tip commit of repo(s): $*" >&2
fi
wait
$CI_TIME sync
if [ -n "`ls -1 "${TEMPDIR_REMOTES}/"`" ]; then
cat "$TEMPDIR_REMOTES"/* || true
if $CI_TIME_LOOPS || $DEBUG; then
echo "[D] `date`: Dumping raw discovery of git-references data:" >&2
cat "${TEMPDIR_REMOTES}/"* >&2
fi
fi
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Completed raw discovery of references from any tip commit of repo(s): $*" >&2
fi
) | sort | uniq
if [ -n "$CI_TIME" ]; then
[ -z "$GDATE" ] || { TS_END="`$GDATE -u +%s`" ; TS_TEXT=" after $(($TS_END - $TS_START)) whole seconds"; }
echo "[D] `date`: Finished discovering and filtering references from any tip commit of repo(s)${TS_TEXT}: $*" >&2
fi
}
do_list_subrepos() {
local TS_START=0
local TS_END=0
local TS_TEXT=''
[ -z "$GDATE" ] || TS_START="`$GDATE -u +%s`"
echo "[I] `date`: Discovering submodules (if any) referenced from any tip commit of repo(s): $*" >&2
( # List all unique branches/tags etc. known in the repo(s) from argument,
# and from each branch, get a .gitmodules if any and URLs from it:
TEMPDIR_SUBURLS="`mktemp -d --tmpdir="$TEMPDIR_BASE" subrepos.$$.XXXXXXXX`" && [ -n "$TEMPDIR_SUBURLS" ] && [ -d "$TEMPDIR_SUBURLS" ] || TEMPDIR_SUBURLS=""
if [ -n "$TEMPDIR_SUBURLS" ] ; then
# Absolutize to be sure
TEMPDIR_SUBURLS="$(cd "$TEMPDIR_SUBURLS" && pwd)"
#trap 'echo "do_list_subrepos(): REMOVING TEMPDIR_SUBURLS=$TEMPDIR_SUBURLS">&2 && rm -rf "$TEMPDIR_SUBURLS"' 0
trap 'rm -rf "$TEMPDIR_SUBURLS"' 0
else
echo "do_list_subrepos(): Failed to create TEMPDIR_SUBURLS" >&2
exit 1
fi
do_list_remotes "$@" | while IFS="${TABCHAR}" read HASH GITREF REPOURL REFREPODIR_REPO ; do
echo "===== Will check submodules (if any) under tip hash '$HASH' => '$GITREF' in '$REFREPODIR_REPO' for '$REPOURL'..." >&2
# After pretty reporting, constrain the list to unique items for inspection
echo "$HASH $REFREPODIR_REPO"
done | sort | uniq | \
( local FIRSTLOOP=true
declare -A PREEXISTING_MODDATA
for F in $(cd ${TEMPDIR_BASE}/ && ls -1) ; do
PREEXISTING_MODDATA["$F"]=1
done
while read HASH REFREPODIR_REPO ; do
# This should fire only after stdin pours in - when sort|uniq
# pipes are done and "under tip" log above no longer streams
$FIRSTLOOP && echo "[D] `date`: Searching commits listed above (if any) for unique URLs from .gitmodules (if any)..." >&2
FIRSTLOOP=false
# Avoid forking thousands of subshells if we can:
if [[ -v PREEXISTING_MODDATA["${HASH}:.gitmodules-urls"] ]] ; then
# Already existed before
$CI_TIME_LOOPS && echo "[D] ${HASH} was pre-existing" >&2
if [ -s "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls" ] && [ ! -e "${TEMPDIR_SUBURLS}/${HASH}:.gitmodules-urls" ] ; then
# Do not link to empty files to cat them below
ln "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls" "${TEMPDIR_SUBURLS}/"
fi
else
( $CI_TIME_LOOPS && echo "[D] ${HASH} not pre-existing" >&2
# Note the 'test -e': here we assume that a file creation
# and population attempt was successful as an atomic operation
# and even if it is empty, that is a definitive final status
if \
[ -e "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls" ] \
|| [ -e "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls.tmp" ] \
; then
# Made recently, or is being parsed by another thread now
if $CI_TIME_LOOPS; then
echo "[D] `date`: ======= NOT Checking submodules (if any) under tip hash '$HASH' '`pwd`' / '$REFREPODIR_REPO' - results already filed" >&2
fi
else
# Not existing before, not made recently nor being made now by another thread (.tmp)
trap 'rm -f "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls.tmp" || true' 0
[ -n "${REFREPODIR_REPO}" ] \
&& { pushd "${REFREPODIR_BASE}/${REFREPODIR_REPO}" >/dev/null || exit $? ; }
if $CI_TIME_LOOPS ; then
echo "[D] `date`: ======= Checking submodules (if any) under tip hash '$HASH' '`pwd`' / '$REFREPODIR_REPO'..." >&2
$CI_TIME git show "${HASH}:.gitmodules"
else
git show "${HASH}:.gitmodules" 2>/dev/null
fi \
| sed -e 's,[ '"${TABCHAR}"'\r\n]*,,g' -e '/^url=/!d' -e 's,^url=,,' \
> "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls.tmp" \
&& mv -f "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls.tmp" "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls"
# If we did not succeed for whatever reason, no final file should appear
rm -f "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls.tmp" || true
fi
if [ -s "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls" ] && [ ! -e "${TEMPDIR_SUBURLS}/${HASH}:.gitmodules-urls" ] ; then
# Do not link to empty files to cat them below
ln "${TEMPDIR_BASE}/${HASH}:.gitmodules-urls" "${TEMPDIR_SUBURLS}/"
fi
) &
throttle_running_child_count
fi
done
)
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Waiting for subprocesses for discovery of submodules (if any) referenced from any tip commit of repo(s): $*" >&2
fi
wait
$CI_TIME sync
if [ -n "`ls -1 "${TEMPDIR_SUBURLS}/"`" ]; then
cat "${TEMPDIR_SUBURLS}/"*:.gitmodules-urls
if $CI_TIME_LOOPS || $DEBUG; then
echo "[D] `date`: Dumping raw discovery of submodules data:" >&2
cat "${TEMPDIR_SUBURLS}/"*:.gitmodules-urls >&2
fi
fi
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Completed raw discovery of submodules (if any) referenced from any tip commit of repo(s): $*" >&2
fi
) | sort | uniq
[ -z "$GDATE" ] || { TS_END="`$GDATE -u +%s`" ; TS_TEXT=" after $(($TS_END - $TS_START)) whole seconds"; }
echo "[I] `date`: Finished discovering and filtering submodules (if any) referenced from any tip commit of repo(s)${TS_TEXT}: $*" >&2
# ...in the end, return all unique Git URLs registered as git submodules
}
# Track which repos (URLs) we have inspected during the recursion -
# whether new registrations or previously existing population, here:
declare -A REGISTERED_RECURSIVELY_NOW
do_register_repos_recursive() {
# Register each repo URL and dig into all branches' `.gitmodules` file to recurse
# Note a REPO may be something already registered, then we just look for submodules
# Note: If recursing for nested refrepos (not-empty REFREPODIR_MODE),
# we may need to only fork to process each one child repo - but go back
# into parent to track all URLs we have processed to avoid doing them
# more than once. Maybe also can need to avoid the LOCK on parent dir?..
local REPO SUBREPO
local RES=0
local _RES=0
local RECURSE_MODE="all"
local TS_START=0
local TS_END=0
local TS_TEXT=''
[ -z "$GDATE" ] || TS_START="`$GDATE -u +%s`"
# Exit recursion; for call to refresh all already registered URLs pass $1=="all"
if [ $# = 0 ]; then return 0; fi
case "$1" in
all|new) RECURSE_MODE="$1"; shift ;;
esac
local REPO_LIST TOPREPO_LIST
declare -a REPO_LIST
declare -a TOPREPO_LIST
if [ $# = 0 ]; then
# A special case for top-level recursive handler from CLI:
# recursive calls would have a RECURSE_MODE and another arg
# (even if that would be an empty token).
echo "Caller specified a RECURSE_MODE as the only argument, so list all known Git URLs and refresh submodules that they might reference" >&2
if [ "$DO_FETCH" = false ] ; then
echo "Caller asked to not re-fetch Git URLs already registered - probably they were recently refreshed in a separate call" >&2
else
echo "[I] `date`: === Fetch all known repositories' contents for recursion analysis..." >&2
do_fetch_repos $DO_FETCH || RES=$?
DO_FETCH=false
echo "[I] `date`: === Completed a fetch of all known repositories' contents for recursion analysis" >&2
fi
REPO_LIST+=( `QUIET_SKIP=true do_list_repoids | awk '{print $2}' | sort | uniq` )
echo "Discovered the following currently-known Git URLs for further recursion: ${REPO_LIST[*]}" >&2
if [ "${#REPO_LIST[@]}" = 0 ]; then
echo "FAILED: No Git URLs found under `pwd`, aborting the recursive inspection" >&2
return 1
fi
# Prevent recursing into Git URLs we have just confirmed are known
# (same as for register-or-check in the args-named repos below)
for REPO in "${REPO_LIST[@]}" ; do
REGISTERED_RECURSIVELY_NOW["$REPO"]=1
done
else
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Recursing into possible submodules of repo URL(s): $*" >&2
fi
for REPO in "$@" ; do
[ "${REGISTERED_RECURSIVELY_NOW["$REPO"]}" = 1 ] \
&& { $QUIET_SKIP || echo "SKIP: '$REPO' was already inspected recursively during this run" >&2 ; } \
|| { is_repo_not_excluded "$REPO" && TOPREPO_LIST+=( "$REPO" ) ; }
# Note: is_repo_not_excluded() returns 0 to go on processing the repo
done
# First register the nearest-level repos
for REPO in "${TOPREPO_LIST[@]}" ; do
# Repos disliked by exclude pattern were filtered away above, as
# well as repos already visited in other recursion codepaths.
# Other repos that existed earlier we want to dig into, except for
# code 42 (means skip because already registered during this run,
# as a double failsafe precaution - e.g. listed twice in CLI args):
echo "[I] `date`: === Register '$REPO' or see if it is already here..." >&2
do_register_repo "$REPO" || { _RES=$?; [ "${_RES}" = 42 ] || RES="${_RES}"; continue ; }
REGISTERED_RECURSIVELY_NOW["$REPO"]=1
case "$RECURSE_MODE" in
new) # we would only recurse into repo URLs previously not known
if [ "${REGISTERED_NOW["$REPO"]}" = 1 ] ; then
REPO_LIST+=( "$REPO" )
fi
;;
all) # will recurse into all known repos to check for new branches etc.
REPO_LIST+=( "$REPO" ) ;;
esac
if [ "$DO_FETCH" = false ] && [ "${REGISTERED_NOW["$REPO"]}" != 1 ] ; then
echo "[I] `date`: === Not fetching '$REPO' contents (it existed and caller says it was recently refreshed)..." >&2
else
# We need the (recent) contents to look into .gitmodules files later
echo "[I] `date`: === Fetch '$REPO' contents for recursion analysis..." >&2
if [ "$DO_FETCH" = false ] ; then
do_fetch_repos true "$REPO" || RES=$?
else
do_fetch_repos $DO_FETCH "$REPO" || RES=$?
fi
fi
done
fi
# Above we have selected some Git URLs that whose contents we did
# not investigate yet; here we look inside for unique submodule URLs
# and recurse this routine into such URLs. Note that if there are
# no new URLs referenced by submodules since last iteration, there
# should be no recursion for a parameterless run that checks "all".
#
# TODO?: For REFREPODIR_MODE, group repos in same directory to
# check and dedup their likely-crossing hashes once and for all
# (currently dropped from do_list_repoids()|awk... lookup above)
for SUBREPO in `do_list_subrepos "${REPO_LIST[@]}"`; do
# Avoid recursing to speed up - lots of operations there we know we would do in vain
if [ "${REGISTERED_RECURSIVELY_NOW["$SUBREPO"]}" = 1 ] ; then
$QUIET_SKIP || echo "SKIP: '$SUBREPO' was already inspected recursively during this run and got requested again" >&2
continue
fi
echo "[I] `date`: ===== Recursively register '$SUBREPO'..." >&2
do_register_repos_recursive "$RECURSE_MODE" "$SUBREPO" || RES=$?
# Now we've certainly handled this URL:
REGISTERED_RECURSIVELY_NOW["$SUBREPO"]=1
done
if [ -n "$CI_TIME" ]; then
[ -z "$GDATE" ] || { TS_END="`$GDATE -u +%s`" ; TS_TEXT=" after $(($TS_END - $TS_START)) whole seconds"; }
echo "[D] `date`: Finished ($RES) recursing into possible submodules of repo URL(s)${TS_TEXT}: $*" >&2
fi
return $RES
}
do_unregister_repo() {
# REPO is a substring from `git remote` listing,
# so can be part of an ID or URL
local REPO REPO_IDS REPO_ID
REPO="$1"
local REFREPODIR_REPO
[ -n "${REFREPODIR_MODE-}" ] && REFREPODIR_REPO="`get_subrepo_dir "$REPO"`" \
&& { pushd "${REFREPODIR_BASE}/${REFREPODIR_REPO}" >/dev/null && trap 'popd >/dev/null ; trap - RETURN' RETURN || return $? ; }
# There may happen to be several registrations for same URL
REPO_IDS="`$CI_TIME git remote -v | GREP_OPTIONS= grep -i "$REPO" | awk '{print $1}' | sort | uniq`" || REPO_IDS=""
[ -z "$REPO_IDS" ] && echo "SKIP: Repo '$REPO' not registered in `pwd`" && return 0
RES=0
for REPO_ID in $REPO_IDS ; do
echo "[I] `date`: === Unregistering repository ID '$REPO_ID' from `pwd`..."
$CI_TIME git remote remove "$REPO_ID" || RES=$?
done
return $RES
}
lc() {
echo "$*" | tr 'A-Z' 'a-z'
}
lower_priority() {
# Do not bring the system down by bursting dozens (or more) of
# git clients... Though not a critical failure if we can not.
# TODO : Use GNU parallel or somesuch?
renice -n +5 $$ || true
}
do_list_repoids() {
# Optional arguments are a list of URLs that must match exactly
# (.git extension included) though case-insensitively to be listed.
# Returns the git remote repo names (e.g. repo-1536929947 here) and
# the original URL.
# TODO: Cleaner handling of REFREPODIR_* cases (e.g. match only $@
# provided dirs, if any)?..
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Listing repoid's and locations for repo URL(s): $*" >&2
fi
cache_list_repoids
# Print entries line by line:
### for LINE in "${KNOWN_REPOIDS[@]}" ; do echo "$LINE" ; done | \
IFS="${EOLCHAR}" eval 'echo "${KNOWN_REPOIDS[*]}"' | \
if [ $# = 0 ]; then cat ; else
# Maybe `echo "$@" | tr ' ' '|' is even better?
# This could however fall victim to whitespaces in URLs,
# double-whitespaces, etc. :\
RE="`printf '%s' "$1"; shift; while [ $# -gt 0 ]; do printf '%s' "|${1}"; shift; done`"
grep -E "$RE"
fi | \
while IFS="${TABCHAR}" read R U U_LC D ; do
is_repo_not_excluded "$U" || continue # not a fatal error, just a skip (reported there)
is_repo_not_excluded "$U_LC" || continue # not a fatal error, just a skip (reported there)
if [ $# = 0 ]; then
printf '%s\t%s\t%s\n' "$R" "$U" "$D"
else
for UU in "$@" ; do
if [ "`lc "$UU"`" = "$U_LC" ]; then
printf '%s\t%s\t%s\n' "$R" "$U" "$D"
fi
done
fi
done
if [ -n "$CI_TIME" ]; then
echo "[D] `date`: Finished listing repoid's and locations for repo URL(s): $*" >&2
fi
}
do_fetch_repos_verbose_seq() (
# Fetches repos listed on stdin and reports, sequentially
# -f allows to force-update references to remote current state (e.g. floating tags)
RES=0
while read R U D; do
[ -n "$U" ] || U="$R"
echo "=== (fetcher:verbose:seq) $U ($R):" >&2
is_repo_not_excluded "$U" || continue # not a fatal error, just a skip (reported there)
( local REFREPODIR_REPO="$D"
{ [ -n "${REFREPODIR_REPO}" ] || \
{ [ -n "${REFREPODIR_MODE-}" ] && REFREPODIR_REPO="`get_subrepo_dir "$U"`" ; } ; } \
&& { pushd "${REFREPODIR_BASE}/${REFREPODIR_REPO}" >/dev/null || exit $? ; }
echo "[I] `date`: === (fetcher:verbose:seq) Starting $U ($R) in `pwd` :" >&2
$CI_TIME git fetch -f --progress "$R" '+refs/heads/*:refs/remotes/'"$R"'/*' \
&& $CI_TIME git fetch -f --tags --progress "$R" \
&& echo "[I] `date`: ===== (fetcher:verbose:seq) Completed $U ($R) in `pwd`" >&2
) || { RES=$? ; echo "[I] `date`: (fetcher:verbose:seq) FAILED TO FETCH : $U ($R)" >&2 ; }
echo ""
done
exit $RES
)
do_fetch_repos_verbose_par() (
# Fetches repos listed on stdin and reports, in parallel. NOTE:
# * can complete faster than seq, but with messier output
# * no job control for multiple children so far
# -f allows to force-update references to remote current state (e.g. floating tags)
RES=0
while read R U D ; do
[ -n "$U" ] || U="$R"
is_repo_not_excluded "$U" || continue # not a fatal error, just a skip (reported there)
( local REFREPODIR_REPO="$D"
{ [ -n "${REFREPODIR_REPO}" ] || \
{ [ -n "${REFREPODIR_MODE-}" ] && REFREPODIR_REPO="`get_subrepo_dir "$U"`" ; } ; } \
&& { pushd "${REFREPODIR_BASE}/${REFREPODIR_REPO}" >/dev/null || exit $? ; }
echo "[I] `date`: === (fetcher:verbose:par) Starting $U ($R) in `pwd` in background..." >&2
$CI_TIME git fetch -f --progress "$R" '+refs/heads/*:refs/remotes/'"$R"'/*' \
&& $CI_TIME git fetch -f --tags --progress "$R" \
|| { RES=$? ; echo "[I] `date`: (fetcher:verbose:par) FAILED TO FETCH : $U ($R) in `pwd` in background" >&2 ; exit $RES; }
echo "[I] `date`: ===== (fetcher:verbose:par) Completed $U ($R) in `pwd` in background" >&2
) &
throttle_running_child_count
echo ""
done
wait || RES=$?
exit $RES
)
do_fetch_repos() {
local TS_START=0
local TS_END=0
local TS_TEXT=''
local RES=0
[ -z "$GDATE" ] || TS_START="`$GDATE -u +%s`"
FETCHER="do_fetch_repos_verbose_seq"
case "$1" in
-vp) FETCHER="do_fetch_repos_verbose_par"
lower_priority
;& # fall through
-vs|-v)
shift
if [ $# = 0 ] && [ -z "${REFREPODIR_MODE-}" ] ; then
$CI_TIME git remote -v | GREP_OPTIONS= grep fetch | awk '{print $1" "$2}'
else
if [ -z "${REFREPODIR_MODE-}" ] ; then
do_list_repoids "$@"
else
# Reverse sort, to prioritize presumed-smaller-scope (faster) repos in subdirs
do_list_repoids "$@" | sort -k3r
fi
fi | $FETCHER || RES=$?
if [ -n "$CI_TIME" ]; then
[ -z "$GDATE" ] || { TS_END="`$GDATE -u +%s`" ; TS_TEXT=" after $(($TS_END - $TS_START)) whole seconds"; }
echo "[D] `date`: Finished ($RES) fetching of repo URL(s)${TS_TEXT}: $*" >&2
fi
return $RES
;;
false)
echo "SKIP: Fetching disabled by argument or DO_FETCH envvar" >&2
return 0
;;
true|-s) shift; FETCHER='' ;; # fall through
-p) shift; FETCHER="-j8" ;; # MAXJOBS instead of 8?
*) FETCHER='' ;; # no shift, URLs incoming (maybe)
esac
# Non-verbose default mode:
# TODO: Can we pass a refspec to fetch all branches here?
# Or should we follow up with another fetch (like verbose)?
if [ -z "${REFREPODIR_MODE-}" ] ; then
echo "[I] `date`: === (fetcher:default:seq) Processing refrepo dir '`pwd`': $*" >&2
$CI_TIME git fetch -f $FETCHER --multiple --tags `do_list_repoids "$@" | awk '{print $1}'` || RES=$?
else
# We handle a '-p' for non-verbose parallel mode that can
# stretch across subdirs to run more git's (parallelized
# internally to natively cleanly handle single dir contents)
local R U D
local D_='.'
local R_=''
# Reverse sort, to prioritize presumed-smaller-scope (faster) repos in subdirs
( do_list_repoids "$@" | sort -k3r | uniq ; echo '. . .' ) | \
( RESw=0
# Track which repo URLs we have already fetched in specific-scoped
# subdirs, to not refetch those URLs in the root-dir wad (we keep
# it for history for now, but it is very slow to manage, even for
# its own updating fetches). So unconverted consumers might still
# use this directory, but if we deal with REFREPODIR mode then no
# point maintaining it for URLs handled by subdirs (no consumers
# expected really). It can be fully updated separately, by a run
# without REFREPODIR_MODE setting from caller.
declare -A FETCHED_REPO
while read R U D ; do
if [ "$D" = "$D_" ] ; then
if [ -z "${FETCHED_REPO["$U"]-}" ]; then
R_="$R_ $R"
FETCHED_REPO["$U"]="$D"
else
echo "===== (fetcher:default) SKIP: Git URL '$U' was already considered for subdirectory '${FETCHED_REPO["$U"]}' - not re-fetching into '$D'" >&2
fi
else
# Hit a new value in directory column, fetch the list collected
# for previous dir if any ('.' here is the starting value of D_)
if [ "$D_" != '.' ]; then
sub_fetcher() ( [ -n "$D_" ] || D_="${REFREPODIR_BASE}"
if [ -z "$R_" ]; then
echo "===== (fetcher:default) SKIP: Git URL list is empty after selection - not re-fetching into '$D_'" >&2
exit 0 # just exiting a subprocess here
fi
# TODO: Change reporting depending on FETCHER value?
echo "[I] `date`: ===== (fetcher:default:par) Processing refrepo dir '$D_': $R_" >&2
cd "$D_" || exit
$CI_TIME git fetch -f $FETCHER --multiple --tags $R_ || \
{ echo "[I] `date`: ======= (fetcher:default:seq) Retry sequentially refrepo dir '$D_': $R_" >&2 ;
$CI_TIME git fetch -f --multiple --tags $R_ ; }
)
if [ -n "$FETCHER" ]; then
# Assume parallel setting
sub_fetcher &
throttle_running_child_count
else
sub_fetcher || RESw=$?
fi
fi
if [ "$D" = '.' ]; then
# Sentinel entry '. . .' was hit
break
fi
# Initialize next loop (or the first loop ever)
echo "[I] `date`: === (fetcher:default) Preparing filtered list of Git URLs for dir '$D'..." >&2
D_="$D"
if [ -z "${FETCHED_REPO["$U"]-}" ]; then
R_="$R"
FETCHED_REPO["$U"]="$D"
else
R_=''
echo "===== (fetcher:default) SKIP: Git URL '$U' was already considered for subdirectory '${FETCHED_REPO["$U"]}' - not re-fetching into '$D'" >&2
fi
fi
done
if [ -n "$FETCHER" ]; then
# Assume parallel setting
wait || RESW=$?
fi
exit $RESw
) || RES=$?
fi
if [ -n "$CI_TIME" ]; then
[ -z "$GDATE" ] || { TS_END="`$GDATE -u +%s`" ; TS_TEXT=" after $(($TS_END - $TS_START)) whole seconds"; }
echo "[D] `date`: Finished ($RES) fetching of repo URL(s)${TS_TEXT}: $*" >&2
fi
return $RES
}
normalize_git_url() {
# Perform Git URL normalization similar to that in JENKINS-64383 solution
local REPO="$1"
local REPONORM="`echo "$REPO" | tr 'A-Z' 'a-z' | sed -e 's,\.git$,,'`"
case "${REPONORM}" in
*://*) ;;
/*) REPONORM="file://`echo "${REPONORM}" | sed -e 's,/\./,/,g' -e 's,//*,/,g'`" ;;
*) REPONORM="file://$(echo "`pwd`/${REPONORM}" | sed -e 's,/\./,/,g' -e 's,//*,/,g')" ;;
esac
echo "$REPONORM"
}
get_subrepo_dir() {
# Returns a sub-directory name (relative to parent workspace) determined
# by rules similar to those for JENKINS-64383 solution for hosting several
# reference repositories with smaller scopes under one common configured
# location. The caller should check if the directory exists before using
# it; non-zero return codes are for errors determining the path name.
# A currently non-existent name return in some contexts may be something
# to `mkdir` for example.
local REPO="$1"
local REPONORM="`normalize_git_url "$REPO"`"
local SUBREPO_DIR=""
# Compatibility with JENKINS-64383 solution
case "${REFREPODIR_MODE}" in
"") return 2 ;; # Standalone run
GIT_URL|'${GIT_URL}'|GIT_URL_FALLBACK|'${GIT_URL_FALLBACK}')
# Note this can include non-portable FS characters like ":"
SUBREPO_DIR="${REPONORM}"
;;
GIT_URL_BASENAME|'${GIT_URL_BASENAME}'|GIT_URL_BASENAME_FALLBACK|'${GIT_URL_BASENAME_FALLBACK}')
SUBREPO_DIR="`basename "$REPONORM"`"
;;
GIT_URL_SHA256|'${GIT_URL_SHA256}'|GIT_URL_SHA256_FALLBACK|'${GIT_URL_SHA256_FALLBACK}')
SUBREPO_DIR="`echo "$REPONORM" | sha256sum | cut -d' ' -f1`"
;;
GIT_SUBMODULES|'${GIT_SUBMODULES}'|GIT_SUBMODULES_FALLBACK|'${GIT_SUBMODULES_FALLBACK}')
# Simplified matcher logic for best expectations from JENKINS-64383
SUBREPO_DIR="`echo "$REPONORM" | sha256sum | cut -d' ' -f1`"
[ -d "$SUBREPO_DIR" ] || [ -d "$SUBREPO_DIR.git" ] \
|| SUBREPO_DIR="`basename "$REPONORM"`"
;;
*) echo "WARNING: Unsupported mode REFREPODIR_MODE='$REFREPODIR_MODE'" >&2
return 3
;;
esac
if [ -n "${SUBREPO_DIR}" ] ; then
[ -e "${SUBREPO_DIR}/.git" -o -e "${SUBREPO_DIR}/objects" ] || SUBREPO_DIR="${SUBREPO_DIR}.git"
fi
case "${REFREPODIR_MODE}" in
GIT_*_FALLBACK|'${GIT_'*'_FALLBACK}')
if [ -n "${SUBREPO_DIR}" ] && [ -e "${SUBREPO_DIR}/.git" -o -e "${SUBREPO_DIR}/objects" ] ; then
: # No fallback needed
else
# Exported below if this script is recursing or running at top level
SUBREPO_DIR="${REFREPODIR_BASE-}"
fi
;;
esac
[ -n "${SUBREPO_DIR}" ] \
&& echo "${SUBREPO_DIR}" \
|| return 1
}
BIG_RES=0
DID_UPDATE=false
# Note: assumes the script running user may write to the refrepo dir tree
if [ -n "${REFREPODIR-}" ] && [ -d "${REFREPODIR-}" ]; then
LOCK="${REFREPODIR}/.gitcache.lock"
else
LOCK="`dirname $0`/.gitcache.lock"
fi
if [ -z "$SKIP_LOCK" ] && [ "$1" != "--dev-test" ] ; then
# Skipping is reserved for RO operations like listing, or for debugging the script
while [ -s "$LOCK" ] ; do
OLDPID="`head -1 "$LOCK"`"
OLDHOST="`head -2 "$LOCK" | tail -1`"
if [ "$OLDPID" = "admin-lock" ] ; then
if [ "$1" = "unlock" ]; then
echo "`date -u`: [$$]: WAS LOCKED by administrator on $OLDHOST, unlocking now..." >&2
rm -f "$LOCK"
shift
else
echo "`date -u`: [$$]: LOCKED by administrator on $OLDHOST, use '$0 unlock' to clear this lock" >&2
sleep 1
fi
else
if [ -n "$OLDPID" ] && [ "$OLDPID" -gt 0 ] ; then
echo "`date -u`: [$$]: LOCKED by PID $OLDPID on $OLDHOST, waiting (export SKIP_LOCK=true to bypass in safe conditions)..." >&2
if [ "$OLDHOST" = "`hostname`" ]; then
if [ ! -d "/proc/$OLDPID" ]; then
echo "I am `hostname` and '/proc/$OLDPID' is absent, removing lock and waiting for up to 15 sec (maybe other copies will kick in)..."
rm -f "$LOCK" ; sleep "`expr 5 + $$ % 10`"
fi
fi
sleep 1
fi
fi
done
( echo "$$" ; hostname ) > "$LOCK"
trap 'rm -rf "$LOCK"' 0 1 2 3 15
fi
# We actually want to retain data in TEMPDIR_BASE to avoid discovering
# it from same commit hashes again and again
# TODO: Garbage-collection in TEMPDIR_BASE as we would change HEADs,
# delete repos, known old pulls, tags and/or branches etc. over time?
TEMPDIR_BASE="${REFREPODIR_BASE}/.git.cache.rgc"
# Absolutize to be sure
mkdir -p "$TEMPDIR_BASE"
TEMPDIR_BASE="$(cd "$TEMPDIR_BASE" && pwd)"
rm -f "${TEMPDIR_BASE}"/*.tmp || true
ACTIONS=""
while [ $# -gt 0 ]; do
[ -z "$ACTIONS" ] && ACTIONS="$1" || ACTIONS="$ACTIONS $1"
case "$1" in
help|-h|--help)
cat << EOF
Usage:
$0 [add] REPO_URL [REPO_URL...]
$0 add-recursive [new|all] REPO_URL [REPO_URL...] => register repo (if not yet),
fetch its contents, and do same for submodules (if any)
$0 { list | ls | ls-recursive } [REPO_URL...]
$0 up [-v|-vs|-vp] [REPO_URL...] => fetch new commits
$0 up-all => fetch new commits for all registered
repos (including those URLs normally
skipped by .exclude patterns if any)
$0 co REPO_URL => register + fetch
$0 del REPO_GLOB => unregister
$0 dedup-references [REPO_URL...] => unregister URLs that are listed many
times (e.g. when converting to fanout)