-
Notifications
You must be signed in to change notification settings - Fork 7
/
tg.sh
3132 lines (2910 loc) · 95.6 KB
/
tg.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
#!/bin/sh
# TopGit - A different patch queue manager
# Copyright (C) 2008 Petr Baudis <[email protected]>
# Copyright (C) 2014-2021 Kyle J. McKay <[email protected]>
# All rights reserved.
# GPLv2
TG_VERSION="0.19.13"
# Update in Makefile if you add any code that requires a newer version of git
GIT_MINIMUM_VERSION="@mingitver@"
## SHA-1 pattern
hexch='[0-9a-f]'
octet="$hexch$hexch"
octet4="$octet$octet$octet$octet"
octet19="$octet4$octet4$octet4$octet4$octet$octet$octet"
octet20="$octet4$octet4$octet4$octet4$octet4"
octet32="$octet20$octet4$octet4$octet4"
tab=' '
lf='
'
## Auxiliary functions
# some ridiculous sh implementations require 'trap ... EXIT' to be executed
# OUTSIDE ALL FUNCTIONS to work in a sane fashion. Always trap it and eval
# "${TRAPEXIT_:-exit}" as a substitute.
TRAPEXIT_=
trapexit_()
{
EXITCODE_=${1:-$?}
trap - EXIT
eval "${TRAPEXIT_:-exit $EXITCODE_}"
exit $EXITCODE_
}
trap 'trapexit_ $?' EXIT
# unset that ignores error code that shouldn't be produced according to POSIX
unset_()
{
{ unset "$@"; } >/dev/null 2>&1 || :
}
# Preserves current $? value while triggering a non-zero set -e exit if active
# This works even for shells that sometimes fail to correctly trigger a -e exit
check_exit_code()
{
return $?
}
# This is the POSIX equivalent of which
cmd_path()
(
{ "unset" -f command unset unalias "$1"; } >/dev/null 2>&1 || :
{ "unalias" -a || unalias -m "*"; } >/dev/null 2>&1 || :
command -v "$1"
)
# helper for wrappers
# note deliberate use of '(' ... ')' rather than '{' ... '}'
exec_lc_all_c()
(
{ "unset" -f "$1" || :; } >/dev/null 2>&1 &&
shift &&
LC_ALL="C" &&
export LC_ALL &&
exec "$@"
)
# These tools work better for us with LC_ALL=C and by using these little
# convenience functions LC_ALL=C does not have to appear in the code but
# any Git translations will still appear for Git commands
awk() { exec_lc_all_c awk @AWK_PATH@ "$@"; }
cat() { exec_lc_all_c cat cat "$@"; }
cmp() { exec_lc_all_c cmp cmp "$@"; }
cut() { exec_lc_all_c cut cut "$@"; }
find() { exec_lc_all_c find find "$@"; }
grep() { exec_lc_all_c grep grep "$@"; }
join() { exec_lc_all_c join join "$@"; }
paste() { exec_lc_all_c paste paste "$@"; }
sed() { exec_lc_all_c sed sed "$@"; }
sort() { exec_lc_all_c sort sort "$@"; }
tr() { exec_lc_all_c tr tr "$@"; }
wc() { exec_lc_all_c wc wc "$@"; }
xargs() { exec_lc_all_c xargs xargs "$@"; }
# Output arguments without any possible interpretation
# (Avoid misinterpretation of '\' characters or leading "-n", "-E" or "-e")
echol()
{
printf '%s\n' "$*"
}
info()
{
echol "${TG_RECURSIVE}${tgname:-tg}: $*"
}
warn()
{
info "warning: $*" >&2
}
err()
{
info "error: $*" >&2
}
fatal()
{
info "fatal: $*" >&2
}
die()
{
fatal "$@"
exit 1
}
# shift off first arg then return "$*" properly quoted in single-quotes
# if $1 was '' output goes to stdout otherwise it's assigned to $1
# the final \n, if any, is omitted from the result but any others are included
v_quotearg()
{
_quotearg_v="$1"
shift
set -- "$_quotearg_v" \
"sed \"s/'/'\\\\\\''/g;1s/^/'/;\\\$s/\\\$/'/;s/'''/'/g;1s/^''\\(.\\)/\\1/\"" "$*"
unset_ _quotearg_v
if [ -z "$3" ]; then
if [ -z "$1" ]; then
echo "''"
else
eval "$1=\"''\""
fi
else
if [ -z "$1" ]; then
printf "%s$4" "$3" | eval "$2"
else
eval "$1="'"$(printf "%s$4" "$3" | eval "$2")"'
fi
fi
}
# same as v_quotearg except there's no extra $1 so output always goes to stdout
quotearg()
{
v_quotearg '' "$@"
}
vcmp()
{
# Compare $1 to $3 each of which must match ^[^0-9]*\d*(\.\d*)*.*$
# where only the "\d*" parts in the regex participate in the comparison
# Since EVERY string matches that regex this function is easy to use
# An empty string ('') for $1 or $3 or any "\d*" part is treated as 0
# $2 is a compare op '<', '<=', '=', '==', '!=', '>=', '>'
# Return code is 0 for true, 1 for false (or unknown compare op)
# There is NO difference in behavior between '=' and '=='
# Note that "vcmp 1.8 == 1.8.0.0.0.0" correctly returns 0
set -- "$1" "$2" "$3" "${1%%[0-9]*}" "${3%%[0-9]*}"
set -- "${1#"$4"}" "$2" "${3#"$5"}"
set -- "${1%%[!0-9.]*}" "$2" "${3%%[!0-9.]*}"
while
vcmp_a_="${1%%.*}"
vcmp_b_="${3%%.*}"
[ "z$vcmp_a_" != "z" ] || [ "z$vcmp_b_" != "z" ]
do
if [ "${vcmp_a_:-0}" -lt "${vcmp_b_:-0}" ]; then
unset_ vcmp_a_ vcmp_b_
case "$2" in "<"|"<="|"!=") return 0; esac
return 1
elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
unset_ vcmp_a_ vcmp_b_
case "$2" in ">"|">="|"!=") return 0; esac
return 1;
fi
vcmp_a_="${1#$vcmp_a_}"
vcmp_b_="${3#$vcmp_b_}"
set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
done
unset_ vcmp_a_ vcmp_b_
case "$2" in "="|"=="|"<="|">=") return 0; esac
return 1
}
# true if "$1" is an existing dir and is empty except for
# any additional files given as extra arguments. If "$2"
# is the single character "." then all ".*" files will be
# ignored for the test (plus any further args, if any)
is_empty_dir() {
test -n "$1" && test -d "$1" || return 1
iedd_="$1"
shift
ieddnok_='\.?$'
if [ z"$1" = z"." ]; then
ieddnok_=
shift
while ! case "$1" in "."*) ! :; esac; do shift; done
fi
if [ $# -eq 0 ]; then
! \ls -a1 "$iedd_" | grep -q -E -v '^\.'"$ieddnok_"
else
# we only handle ".git" right now for efficiency
[ z"$*" = z".git" ] || {
fatal "[BUG] is_empty_dir not implemented for arguments: $*"
exit 70
}
! \ls -a1 "$iedd_" | grep -q -E -v -i -e '^\.\.?$' -e '^\.git$'
fi
}
precheck() {
if ! git_version="$(git version)"; then
die "'git version' failed"
fi
case "$git_version" in [Gg]"it version "*);;*)
die "'git version' output does not start with 'git version '"
esac
vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
die "git version >= $GIT_MINIMUM_VERSION required but found $git_version instead"
}
case "$1" in version|--version|-V)
echo "TopGit version $TG_VERSION"
exit 0
esac
[ $# -eq 1 ] && [ "$1" = "--make-empty-blob" ] || precheck
[ $# -ne 1 ] || [ "$1" != "precheck" ] || exit 0
cat_depsmsg_internal()
{
v_ref_exists_rev _rev "refs/heads/$1" || return 0
if [ -s "$tg_cache_dir/refs/heads/$1/.$2" ]; then
if read _rev_match && [ "$_rev" = "$_rev_match" ]; then
_line=
while IFS= read -r _line || [ -n "$_line" ]; do
printf '%s\n' "$_line"
done
return 0
fi <"$tg_cache_dir/refs/heads/$1/.$2"
fi
[ -d "$tg_cache_dir/refs/heads/$1" ] || mkdir -p "$tg_cache_dir/refs/heads/$1" 2>/dev/null || :
if [ -d "$tg_cache_dir/refs/heads/$1" ]; then
rm -f "$tg_cache_dir/refs/heads/$1/.$2" "$tg_cache_dir/refs/heads/$1/.$2-$$"
printf '%s\n' "$_rev" >"$tg_cache_dir/refs/heads/$1/.$2-$$"
_line=
git cat-file blob "$_rev:.$2" 2>/dev/null |
while IFS= read -r _line || [ -n "$_line" ]; do
printf '%s\n' "$_line" >&3
printf '%s\n' "$_line"
done 3>>"$tg_cache_dir/refs/heads/$1/.$2-$$"
mv -f "$tg_cache_dir/refs/heads/$1/.$2-$$" "$tg_cache_dir/refs/heads/$1/.$2"
rm -f "$tg_cache_dir/refs/heads/$1/.$2-$$"
else
git cat-file blob "$_rev:.$2" 2>/dev/null
fi
}
# cat_deps BRANCHNAME
# Caches result
cat_deps()
{
cat_depsmsg_internal "$1" topdeps
}
# cat_msg BRANCHNAME
# Caches result
cat_msg()
{
cat_depsmsg_internal "$1" topmsg
}
# cat_file TOPIC:PATH [FROM]
# cat the file PATH from branch TOPIC when FROM is empty.
# FROM can be -i or -w, than the file will be from the index or worktree,
# respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
cat_file()
{
path="$1"
case "$2" in
-w)
cat "$root_dir/${path#*:}"
;;
-i)
# ':file' means cat from index
git cat-file blob ":${path#*:}" 2>/dev/null
;;
'')
case "$path" in
refs/heads/*:.topdeps)
_temp="${path%:.topdeps}"
cat_deps "${_temp#refs/heads/}"
;;
refs/heads/*:.topmsg)
_temp="${path%:.topmsg}"
cat_msg "${_temp#refs/heads/}"
;;
*)
git cat-file blob "$path" 2>/dev/null
;;
esac
;;
*)
die "Wrong argument to cat_file: '$2'"
;;
esac
}
# if use_alt_temp_odb and tg_use_alt_odb are true try to write the object(s)
# into the temporary alt odb area instead of the usual location
git_temp_alt_odb_cmd()
{
if [ -n "$use_alt_temp_odb" ] && [ -n "$tg_use_alt_odb" ] &&
[ -n "$TG_OBJECT_DIRECTORY" ] &&
[ -f "$TG_OBJECT_DIRECTORY/info/alternates" ]; then
(
GIT_ALTERNATE_OBJECT_DIRECTORIES="$TG_PRESERVED_ALTERNATES"
GIT_OBJECT_DIRECTORY="$TG_OBJECT_DIRECTORY"
unset_ TG_OBJECT_DIRECTORY TG_PRESERVED_ALTERNATES
export GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_OBJECT_DIRECTORY
git "$@"
)
else
git "$@"
fi
}
git_write_tree() { git_temp_alt_odb_cmd write-tree "$@"; }
git_mktree() { git_temp_alt_odb_cmd mktree "$@"; }
make_mtblob() {
use_alt_temp_odb=1
tg_use_alt_odb=1
git_temp_alt_odb_cmd hash-object -t blob -w --stdin </dev/null >/dev/null 2>&1
}
# short-circuit this for speed
[ $# -ne 1 ] || [ "$1" != "--make-empty-blob" ] || { make_mtblob || :; exit 0; }
# get tree for the committed topic (second arg)
# store result in variable named by first arg
v_get_tree_()
{
eval "$1="'"refs/heads/$2"'
}
# get tree for the base (second arg)
# store result in variable named by first arg
v_get_tree_b()
{
eval "$1="'"refs/$topbases/$2"'
}
# get tree for the index
# store result in variable named by first arg
v_get_tree_i()
{
eval "$1="'"$(git_write_tree)"'
}
# get tree for the worktree
# store result in variable named by first arg
v_get_tree_w()
{
eval "$1="'"$(
i_tree="$(git_write_tree)"
# the file for --index-output needs to sit next to the
# current index file
cd "$root_dir"
: ${GIT_INDEX_FILE:="$git_dir/index"}
TMP_INDEX="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX")"
git read-tree -m "$i_tree" --index-output="$TMP_INDEX" &&
GIT_INDEX_FILE="$TMP_INDEX" &&
export GIT_INDEX_FILE &&
git diff --name-only -z HEAD |
git update-index -z --add --remove --stdin &&
git_write_tree &&
rm -f "$TMP_INDEX"
)"'
}
# get tree for arbitrary ref (second arg)
# store result in variable named by first arg
v_get_tree_r()
{
eval "$1="'"$2"'
}
# v_strip_ref answer "$(git symbolic-ref HEAD)"
# Output will have a leading refs/heads/ or refs/$topbases/ stripped if present
# store result in variable named by first arg
v_strip_ref()
{
case "$2" in
refs/"$topbases"/*)
eval "$1="'"${2#refs/$topbases/}"'
;;
refs/heads/*)
eval "$1="'"${2#refs/heads/}"'
;;
*)
eval "$1="'"$2"'
esac
}
# v_pretty_tree answer [-t] NAME [-b | -i | -w | -r]
# Output tree ID of a cleaned-up tree without tg's artifacts.
# NAME will be ignored for -i and -w, but needs to be present
# With -r NAME must be a full ref name to a treeish (it's used as-is)
# If -t is used the tree is written into the alternate temporary objects area
# store result in variable named by first arg
v_pretty_tree()
{
_vname="$1"
shift
use_alt_temp_odb=
[ "$1" != "-t" ] || { shift; use_alt_temp_odb=1; }
eval "v_get_tree_${2#?}" _tree '"$1"'
eval "$_vname=\"\$(
git ls-tree --full-tree \"\$_tree\" |
sed -ne '/ \.top.*\$/!p' |
git_mktree
)\""
}
# return an empty-tree root commit -- date is either passed in or current
# If passed in "$*" must be epochsecs followed by optional hhmm offset (+0000 default)
# An invalid secs causes the current date to be used, an invalid zone offset
# causes +0000 to be used
make_empty_commit()
(
# the empty tree is guaranteed to always be there even in a repo with
# zero objects, but for completeness we force it to exist as a real object
SECS=
read -r SECS ZONE JUNK <<-EOT || :
$*
EOT
case "$SECS" in *[!0-9]*) SECS=; esac
if [ -z "$SECS" ]; then
MTDATE="$(date '+%s %z')"
else
case "$ZONE" in
-[01][0-9][0-5][0-9]|+[01][0-9][0-5][0-9])
;;
[01][0-9][0-5][0-9])
ZONE="+$ZONE"
;;
*)
ZONE="+0000"
esac
MTDATE="$SECS $ZONE"
fi
EMPTYID="- <-> $MTDATE"
EMPTYTREE="$(git mktree < /dev/null)"
printf '%s\n' "tree $EMPTYTREE" "author $EMPTYID" "committer $EMPTYID" '' |
git hash-object -t commit -w --stdin
)
# standard input is a diff
# standard output is the "+" lines with leading "+ " removed
# beware that old lines followed by the dreaded '\ No newline at end of file'
# will appear to be new lines if lines are added after them
# the git diff --ignore-space-at-eol option can be used to prevent this
diff_added_lines()
{
awk '
BEGIN { in_hunk = 0; }
/^@@ / { in_hunk = 1; }
/^\+/ { if (in_hunk == 1) printf("%s\n", substr($0, 2)); }
!/^\\ No newline at end of file/ &&
/^[^@ +-]/ { in_hunk = 0; }
'
}
# $1 is name of new branch to create locally if all of these are true:
# a) exists as a remote TopGit branch for "$base_remote"
# b) the branch "name" does not have any invalid characters in it
# c) neither of the two branch refs (branch or base) exist locally
# returns success only if a new local branch was created (and dumps message)
auto_create_local_remote()
{
case "$1" in ""|*[" $tab$lf~^:\\*?["]*|.*|*/.*|*.|*./|/*|*/|*//*) return 1; esac
[ -n "$base_remote" ] &&
git update-ref --stdin <<-EOT >/dev/null 2>&1 &&
verify refs/remotes/$base_remote/${topbases#heads/}/$1 refs/remotes/$base_remote/${topbases#heads/}/$1
verify refs/remotes/$base_remote/$1 refs/remotes/$base_remote/$1
create refs/$topbases/$1 refs/remotes/$base_remote/${topbases#heads/}/$1^0
create refs/heads/$1 refs/remotes/$base_remote/$1^0
EOT
{ init_reflog "refs/$topbases/$1" || :; } &&
info "topic branch '$1' automatically set up from remote '$base_remote'"
}
is_writable_hook()
{
if [ -n "$1" ] && [ -e "$1" ] && [ ! -L "$1" ] && [ -f "$1" ] && [ -r "$1" ] && [ -w "$1" ] && [ -x "$1" ]; then
hook_links="$(ls -ld "$1" 2>/dev/null | awk '{print $2}')" || :
[ "$hook_links" != "1" ] || return 0
fi
return 1
}
# setup_hook NAME
setup_hook()
{
setup_git_dir_is_bare
[ -z "$git_dir_is_bare" ] || return 0
setup_git_hooks_dir
tgname="${0##*/}"
hook_call="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
if [ -f "$git_hooks_dir/$1" ] && grep -Fq "$hook_call" "$git_hooks_dir/$1"; then
# Another job well done!
return
fi
# Prepare incantation
hook_chain=
if [ -e "$git_hooks_dir/$1" ] || [ -L "$git_hooks_dir/$1" ]; then
hook_call="$hook_call"' || exit $?'
if
! is_writable_hook "$git_hooks_dir/$1" ||
! sed -n 1p <"$git_hooks_dir/$1" | grep -Fqx "#!@SHELL_PATH@"
then
chain_num=
while [ -e "$git_hooks_dir/$1-chain$chain_num" ] || [ -L "$git_hooks_dir/$1-chain$chain_num" ]; do
chain_num=$(( $chain_num + 1 ))
done
mv -f "$git_hooks_dir/$1" "$git_hooks_dir/$1-chain$chain_num"
hook_chain=1
fi
else
hook_call="exec $hook_call"
[ -d "$git_hooks_dir" ] || mkdir -p "$git_hooks_dir" || :
fi
# Don't call hook if tg is not installed
hook_call="if command -v \"$tgname\" >/dev/null 2>&1; then $hook_call; fi"
# Insert call into the hook
{
echol "#!@SHELL_PATH@"
echol "$hook_call"
if [ -n "$hook_chain" ]; then
echol "test -f \"\$0-chain$chain_num\" &&"
echol "test -x \"\$0-chain$chain_num\" &&"
echol "exec \"\$0-chain$chain_num\" \"\$@\" || :"
else
[ ! -s "$git_hooks_dir/$1" ] || cat "$git_hooks_dir/$1"
fi
} >"$git_hooks_dir/$1+"
chmod a+x "$git_hooks_dir/$1+"
mv "$git_hooks_dir/$1+" "$git_hooks_dir/$1"
}
# setup_ours (no arguments)
setup_ours()
{
setup_git_dir_is_bare
[ -z "$git_dir_is_bare" ] || return 0
if [ ! -s "$git_common_dir/info/attributes" ] || ! grep -q topmsg "$git_common_dir/info/attributes"; then
[ -d "$git_common_dir/info" ] || mkdir "$git_common_dir/info"
{
echo ".topmsg merge=ours"
echo ".topdeps merge=ours"
} >>"$git_common_dir/info/attributes"
fi
if ! git config merge.ours.driver >/dev/null; then
git config merge.ours.name '"always keep ours" merge driver'
git config merge.ours.driver 'touch %A'
fi
}
# measure_branch NAME [BASE] [EXTRAHEAD...]
measure_branch()
{
_bname="$1"; _base="$2"
shift; shift
if [ -z "$_base" ]; then
v_strip_ref _base "$_bname"
_base="refs/$topbases/$_base"
fi
# The caller should've verified $name is valid
_commits="$(git rev-list --count "$_bname" "$@" ^"$_base" --)"
_nmcommits="$(git rev-list --count --no-merges "$_bname" "$@" ^"$_base" --)"
if [ $_commits -ne 1 ]; then
_suffix="commits"
else
_suffix="commit"
fi
echo "$_commits/$_nmcommits $_suffix"
}
# true if $1 is contained by (or the same as) $2
# this is never slower than merge-base --is-ancestor and is often slightly faster
contained_by()
{
[ "$(git rev-list --count --max-count=1 "$1" --not "$2" --)" = "0" ]
}
# branch_contains B1 B2
# Whether B1 is a superset of B2.
branch_contains()
{
v_ref_exists_rev _revb1 "$1" || return 0
v_ref_exists_rev _revb2 "$2" || return 0
if [ -s "$tg_cache_dir/$1/.bc/$2/.d" ]; then
if read _result _rev_matchb1 _rev_matchb2 &&
[ "$_revb1" = "$_rev_matchb1" ] && [ "$_revb2" = "$_rev_matchb2" ]; then
return $_result
fi <"$tg_cache_dir/$1/.bc/$2/.d"
fi
[ -d "$tg_cache_dir/$1/.bc/$2" ] || mkdir -p "$tg_cache_dir/$1/.bc/$2" 2>/dev/null || :
_result=0
contained_by "$_revb2" "$_revb1" || _result=1
if [ -d "$tg_cache_dir/$1/.bc/$2" ]; then
echo "$_result" "$_revb1" "$_revb2" >"$tg_cache_dir/$1/.bc/$2/.d"
fi
return $_result
}
create_ref_dirs()
{
[ ! -s "$tg_tmp_dir/tg~ref-dirs-created" ] && [ -s "$tg_ref_cache" ] || return 0
mkdir -p "$tg_tmp_dir/cached/refs"
awk '{x=$1; sub(/^refs\//,"",x); if (x != "") {gsub(/[^A-Za-z0-9\/_.+-]/,"\\\\&",x); print x;}}' <"$tg_ref_cache" |
{
cd "$tg_tmp_dir/cached/refs" &&
xargs mkdir -p
}
awk -v p="$tg_tmp_dir/cached/" '
NF == 2 &&
$1 ~ /^refs\/./ &&
$2 ~ /^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+$/ {
fn = p $1 "/.ref"
print "0 " $2 >fn
close(fn)
}
' <"$tg_ref_cache"
echo 1 >"$tg_tmp_dir/tg~ref-dirs-created"
}
# If the first argument is non-empty, stores "1" there if this call created the cache
v_create_ref_cache()
{
[ -n "$tg_ref_cache" ] && ! [ -s "$tg_ref_cache" ] || return 0
_remotespec=
[ -z "$base_remote" ] || _remotespec="refs/remotes/$base_remote"
[ -z "$1" ] || eval "$1=1"
git for-each-ref --format='%(refname) %(objectname)' \
refs/heads "refs/$topbases" $_remotespec >"$tg_ref_cache"
create_ref_dirs
}
remove_ref_cache()
{
[ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ] || return 0
>"$tg_ref_cache"
>"$tg_ref_cache_br"
>"$tg_ref_cache_rbr"
>"$tg_ref_cache_ann"
>"$tg_ref_cache_dep"
}
core_abbrev_val=
core_abbrev_is_setup=
v_get_core_abbrev()
{
if [ -z "$core_abbrev_is_setup" ]; then
core_abbrev_val="$(git config --int --get core.abbrev 2>/dev/null)" || :
: "${core_abbrev_val:=7}"
[ "$core_abbrev_val" -ge 4 ] || core_abbrev_val=4
core_abbrev_is_setup=1
fi
eval "$1="'"$core_abbrev_val"'
}
# setting tg_ref_cache_only to non-empty will force non-$tg_ref_cache lookups to fail
rev_parse()
{
rev_parse_code_=1
if [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ]; then
rev_parse_code_=0
awk -v r="$1" 'BEGIN {e=1}; $1 == r {print $2; e=0; exit}; END {exit e}' <"$tg_ref_cache" ||
rev_parse_code_=$?
fi
[ $rev_parse_code_ -ne 0 ] && [ -z "$tg_ref_cache_only" ] || return $rev_parse_code_
git rev-parse --quiet --verify "$1^0" -- 2>/dev/null
}
# v_ref_exists_rev answer REF
# Whether REF (second arg) is a valid ref name
# REF must be fully qualified and start with refs/heads/, refs/$topbases/
# or, if $base_remote is set, refs/remotes/$base_remote/
# Caches result if $tg_read_only and outputs HASH on success
# store result in variable named by first arg
v_ref_exists_rev()
{
case "$2" in
refs/*)
;;
$octethl)
eval "$1="'"$2"'
return;;
*)
die "v_ref_exists_rev requires fully-qualified ref name (given: $2)"
esac
[ -n "$tg_read_only" ] || { eval "$1="'"$(git rev-parse --quiet --verify "$2^0" -- 2>/dev/null)"'; return; }
_result=
_result_rev=
{ read -r _result _result_rev <"$tg_tmp_dir/cached/$2/.ref"; } 2>/dev/null || :
[ -z "$_result" ] || { eval "$1="'"$_result_rev"'; return $_result; }
_result=0
_result_rev="$(rev_parse "$2")" || _result=$?
[ -d "$tg_tmp_dir/cached/$2" ] || mkdir -p "$tg_tmp_dir/cached/$2" 2>/dev/null
[ ! -d "$tg_tmp_dir/cached/$2" ] ||
echo $_result $_result_rev >"$tg_tmp_dir/cached/$2/.ref" 2>/dev/null || :
eval "$1="'"$_result_rev"'
return $_result
}
# Same as v_ref_exists_rev but output is abbreviated hash
# Optional third argument defaults to --short but may be any --short=.../--no-short option
v_ref_exists_rev_short()
{
case "$2" in
refs/*)
;;
$octethl)
;;
*)
die "v_ref_exists_rev_short requires fully-qualified ref name (given: $2)"
esac
if [ "${3:---short}" = "--short" ]; then
v_get_core_abbrev _shortval
set -- "$1" "$2" "--short=$_shortval"
fi
[ -n "$tg_read_only" ] || { eval "$1="'"$(git rev-parse --quiet --verify ${3:---short} "$2^0" -- 2>/dev/null)"'; return; }
_result=
_result_rev=
_result_arg=
{ read -r _result _result_rev _result_arg <"$tg_tmp_dir/cached/$2/.rfs"; } 2>/dev/null || :
[ -z "$_result" ] || [ "${_result_arg:-missing}" != "${3:---short}" ] || { eval "$1="'"$_result_rev"'; return $_result; }
_result=0
_result_rev="$(rev_parse "$2")" || _result=$?
if [ $_result -eq 0 ]; then
_result_rev="$(git rev-parse --verify ${3:---short} --quiet "$_result_rev^0" --)"
_result=$?
fi
[ -d "$tg_tmp_dir/cached/$2" ] || mkdir -p "$tg_tmp_dir/cached/$2" 2>/dev/null
[ ! -d "$tg_tmp_dir/cached/$2" ] ||
echo $_result $_result_rev "${3:---short}" >"$tg_tmp_dir/cached/$2/.rfs" 2>/dev/null || :
eval "$1="'"$_result_rev"'
return $_result
}
# ref_exists REF
# Whether REF is a valid ref name
# REF must be fully qualified and start with refs/heads/, refs/$topbases/
# or, if $base_remote is set, refs/remotes/$base_remote/
# Caches result
ref_exists()
{
v_ref_exists_rev _dummy "$1"
}
# v_rev_parse_tree answer REF
# Runs git rev-parse REF^{tree}
# Caches result if $tg_read_only
# store result in variable named by first arg
v_rev_parse_tree()
{
[ -n "$tg_read_only" ] || { eval "$1="'"$(git rev-parse --verify "$2^{tree}" -- 2>/dev/null)"'; return; }
if [ -f "$tg_tmp_dir/cached/$2/.rpt" ]; then
if IFS= read -r _result <"$tg_tmp_dir/cached/$2/.rpt" && [ -n "$_result" ]; then
eval "$1="'"$_result"'
return 0
fi
return 1
fi
[ -d "$tg_tmp_dir/cached/$2" ] || mkdir -p "$tg_tmp_dir/cached/$2" 2>/dev/null || :
if [ -d "$tg_tmp_dir/cached/$2" ]; then
git rev-parse --verify "$2^{tree}" -- >"$tg_tmp_dir/cached/$2/.rpt" 2>/dev/null || :
if IFS= read -r _result <"$tg_tmp_dir/cached/$2/.rpt" && [ -n "$_result" ]; then
eval "$1="'"$_result"'
return 0
fi
return 1
fi
eval "$1="'"$(git rev-parse --verify "$2^{tree}" -- 2>/dev/null)"'
}
# has_remote BRANCH
# Whether BRANCH has a remote equivalent (accepts ${topbases#heads/}/ too)
has_remote()
{
[ -n "$base_remote" ] && ref_exists "refs/remotes/$base_remote/$1"
}
# Return the verified TopGit branch name for "$2" in "$1" or die with an error.
# If -z "$1" still set return code but do not return result
# As a convenience, if HEAD or @ is given and HEAD is a symbolic ref to
# refs/heads/... then ... will be verified instead.
# if "$3" = "-f" (for fail) then return an error rather than dying.
v_verify_topgit_branch()
{
if [ "$2" = "HEAD" ] || [ "$2" = "@" ]; then
_verifyname="$(git symbolic-ref HEAD 2>/dev/null)" || :
[ -n "$_verifyname" ] || [ "$3" = "-f" ] || die "HEAD is not a symbolic ref"
case "$_verifyname" in refs/"$topbases"/*|refs/heads/*);;*)
[ "$3" != "-f" ] || return 1
die "HEAD is not a symbolic ref to the refs/heads namespace"
esac
set -- "$1" "$_verifyname" "$3"
fi
case "$2" in
refs/"$topbases"/*)
_verifyname="${2#refs/$topbases/}"
;;
refs/heads/*)
_verifyname="${2#refs/heads/}"
;;
*)
_verifyname="$2"
;;
esac
if ! ref_exists "refs/heads/$_verifyname"; then
[ "$3" != "-f" ] || return 1
die "no such branch: $_verifyname"
fi
if ! ref_exists "refs/$topbases/$_verifyname"; then
[ "$3" != "-f" ] || return 1
die "not a TopGit-controlled branch: $_verifyname"
fi
[ -z "$1" ] || eval "$1="'"$_verifyname"'
}
# Caches result
# $1 = branch name (i.e. "t/foo/bar")
# $2 = optional result of rev-parse "refs/heads/$1"
# $3 = optional result of rev-parse "refs/$topbases/$1"
branch_annihilated()
{
_branch_name="$1"
_rev="$2"
[ -n "$_rev" ] || v_ref_exists_rev _rev "refs/heads/$_branch_name"
_rev_base="$3"
[ -n "$_rev_base" ] || v_ref_exists_rev _rev_base "refs/$topbases/$_branch_name"
_result=
_result_rev=
_result_rev_base=
{ read -r _result _result_rev _result_rev_base <"$tg_cache_dir/refs/heads/$_branch_name/.ann"; } 2>/dev/null || :
[ -z "$_result" ] || [ "$_result_rev" != "$_rev" ] || [ "$_result_rev_base" != "$_rev_base" ] || return $_result
# use the merge base in case the base is ahead.
_mb="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)" || :
_result=0
if [ -n "$_mb" ]; then
v_rev_parse_tree _mbtree "$_mb"
v_rev_parse_tree _revtree "$_rev"
test "$_mbtree" = "$_revtree" || _result=1
fi
[ -d "$tg_cache_dir/refs/heads/$_branch_name" ] || mkdir -p "$tg_cache_dir/refs/heads/$_branch_name" 2>/dev/null
[ ! -d "$tg_cache_dir/refs/heads/$_branch_name" ] ||
echo $_result $_rev $_rev_base >"$tg_cache_dir/refs/heads/$_branch_name/.ann" 2>/dev/null || :
return $_result
}
non_annihilated_branches()
{
refscacheopt="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ]; then
refscacheopt="$refscacheopt"'-r="$tg_ref_cache" "refs/$topbases"'
fi
eval run_awk_topgit_branches -n "$refscacheopt" '"refs/$topbases" "$@"'
}
# Make sure our tree is clean
# if optional "$1" given also verify that a checkout to "$1" would succeed
# Note that the empty tree can always be used as a diff target even if it's not
# actually a real, concrete object in the repository (since Git v1.5.5)
ensure_clean_tree()
{
check_status
[ -z "$tg_state$git_state" ] || { do_status; exit 1; }
git update-index --ignore-submodules --refresh ||
die "the working directory has uncommitted changes (see above) - first commit or reset them"
_ectHEAD="$(git rev-parse --verify --quiet "HEAD^{tree}" -- 2>/dev/null)" && [ -n "$_ectHEAD" ] ||
_ectHEAD="$mttree"
[ -z "$(git diff-index --cached --name-status -r --ignore-submodules "$_ectHEAD" -- 2>/dev/null)" ] ||
die "the index has uncommited changes"
[ -z "$1" ] || git read-tree -n -u -m "$1" ||
die "git checkout \"$1\" would fail"
}
# Make sure .topdeps and .topmsg are "clean"
# They are considered "clean" if each is identical in worktree, index and HEAD
# With "-u" as the argument skip the HEAD check (-u => unborn)
# untracked .topdeps and/or .topmsg files are always considered "dirty" as well
# with -u them just existing constitutes "dirty"
ensure_clean_topfiles()
{
_dirtw=0
_dirti=0
_dirtu=0
_check="$(git diff-files --ignore-submodules --name-only -- :/.topdeps :/.topmsg)" &&
[ -z "$_check" ] || _dirtw=1
if [ "$1" != "-u" ]; then
_check="$(git diff-index --cached --ignore-submodules --name-only HEAD -- :/.topdeps :/.topmsg)" &&
[ -z "$_check" ] || _dirti=1
fi
if [ "$_dirti$_dirtw" = "00" ]; then
v_get_show_cdup
if [ -e "${git_cdup_result}.topdeps" ] || [ -e "${git_cdup_result}.topmsg" ]; then
[ "$1" != "-u" ] &&
_check="$(git status --porcelain --ignored --untracked-files --ignore-submodules -- :/.topdeps :/.topmsg)" &&
[ -z "$_check" ] || _dirtu=1
fi
fi
if [ "$_dirtu$_dirti$_dirtw" != "000" ]; then
git status --ignored --untracked-files --ignore-submodules -- :/.topdeps :/.topmsg || :
case "$_dirtu$_dirti$_dirtw" in
001) die "the working directory has uncommitted changes (see above) - first commit or reset them";;
010) die "the index has uncommited changes (see above)";;
011) die "the working directory and index have uncommitted changes (see above) - first commit or reset them";;
100) die "the working directory has untracked files that would be overwritten (see above)";;
esac
fi
}
# is_sha1 REF
# Whether REF is a SHA1 (compared to a symbolic name).
is_sha1()
{
case "$1" in $octethl) return 0;; esac
return 1
}
# navigate_deps <run_awk_topgit_navigate options and arguments>
# all options and arguments are passed through to run_awk_topgit_navigate
# except for a leading -td= option, if any, which is picked off for deps
# after arranging to feed it a suitable deps list
navigate_deps()
{
dogfer=
dorad=1
userc=
tmpdep=
ratd_opts="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
ratn_opts=
if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
userc=1
tmprfs="$tg_ref_cache"
tmptgbr="$tg_ref_cache_br"
tmpann="$tg_ref_cache_ann"
tmpdep="$tg_ref_cache_dep"
[ -s "$tg_ref_cache" ] || dogfer=1
[ -n "$dogfer" ] || ! [ -s "$tmptgbr" ] || ! [ -f "$tmpann" ] || ! [ -s "$tmpdep" ] || dorad=
else
ratd_opts="${ratd_opts}-rmr"
ratn_opts="-rma -rmb"
tmprfs="$tg_tmp_dir/refs.$$"
tmpann="$tg_tmp_dir/ann.$$"
tmptgbr="$tg_tmp_dir/tgbr.$$"
dogfer=1
fi
refpats="\"refs/heads\" \"refs/\$topbases\""
[ -z "$base_remote" ] || refpats="$refpats \"refs/remotes/\$base_remote\""
[ -z "$dogfer" ] ||
eval git for-each-ref '--format="%(refname) %(objectname)"' "$refpats" >"$tmprfs"
depscmd="run_awk_topgit_deps $ratd_opts"
case "$1" in -td=*)
userc=
depscmd="$depscmd $1"
shift
esac
depscmd="$depscmd"' -a="$tmpann" -b="$tmptgbr" -r="$tmprfs" -s "refs/$topbases"'
if [ -n "$userc" ]; then
if [ -n "$dorad" ]; then
eval "$depscmd" >"$tmpdep"
fi
depscmd='<"$tmpdep" '
else
depscmd="$depscmd |"
fi
eval "$depscmd" run_awk_topgit_navigate '-a="$tmpann" -b="$tmptgbr"' "$ratn_opts" '"$@"'
}
# recurse_deps_internal NAME [BRANCHPATH...]
# get recursive list of dependencies with leading 0 if branch exists 1 if missing
# followed by a 1 if the branch is "tgish" (2 if it also has a remote); 0 if not
# followed by a 0 for a non-leaf, 1 for a leaf or 2 for annihilated tgish
# (but missing and remotes are always "0")
# followed by a 0 for no excess visits or a positive number of excess visits
# then the branch name followed by its depedency chain (which might be empty)
# An output line might look like this:
# 0 1 1 0 t/foo/leaf t/foo/int t/stage
# If no_remotes is non-empty, exclude remotes