gnomake: suppress unwanted gratuitous "Entering directory ..." lines
[topgit/pro.git] / tg.sh
blob02d8e46e73a9bba8598227e27c99e789f75d255d
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) 2008 Petr Baudis <pasky@suse.cz>
4 # Copyright (C) 2014-2017 Kyle J. McKay <mackyle@gmail.com>
5 # All rights reserved.
6 # GPLv2
8 TG_VERSION=0.19.8
10 # Update in Makefile if you add any code that requires a newer version of git
11 GIT_MINIMUM_VERSION="@mingitver@"
13 ## SHA-1 pattern
15 octet='[0-9a-f][0-9a-f]'
16 octet4="$octet$octet$octet$octet"
17 octet19="$octet4$octet4$octet4$octet4$octet$octet$octet"
18 octet20="$octet4$octet4$octet4$octet4$octet4"
19 nullsha="0000000000000000000000000000000000000000" # :|git mktree|tr 0-9a-f 0
20 mtblob="e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" # :|git hash-object --stdin -w
21 tab=' '
22 lf='
25 ## Auxiliary functions
27 # some ridiculous sh implementations require 'trap ... EXIT' to be executed
28 # OUTSIDE ALL FUNCTIONS to work in a sane fashion. Always trap it and eval
29 # "${TRAPEXIT_:-exit}" as a substitute.
30 trapexit_()
32 EXITCODE_=${1:-$?}
33 trap - EXIT
34 eval "${TRAPEXIT_:-exit $EXITCODE_}"
35 exit $EXITCODE_
37 trap 'trapexit_ $?' EXIT
39 # unset that ignores error code that shouldn't be produced according to POSIX
40 unset_()
42 unset "$@" || :
45 # Preserves current $? value while triggering a non-zero set -e exit if active
46 # This works even for shells that sometimes fail to correctly trigger a -e exit
47 check_exit_code()
49 return $?
52 # This is the POSIX equivalent of which
53 cmd_path()
55 { "unset" -f command unset unalias "$1"; } >/dev/null 2>&1 || :
56 { "unalias" -a || unalias -m "*"; } >/dev/null 2>&1 || :
57 command -v "$1"
60 # helper for wrappers
61 # note deliberate use of '(' ... ')' rather than '{' ... '}'
62 exec_lc_all_c()
64 { "unset" -f "$1" || :; } >/dev/null 2>&1 &&
65 shift &&
66 LC_ALL="C" &&
67 export LC_ALL &&
68 exec "$@"
71 # These tools work better for us with LC_ALL=C and by using these little
72 # convenience functions LC_ALL=C does not have to appear in the code but
73 # any Git translations will still appear for Git commands
74 awk() { exec_lc_all_c awk @AWK_PATH@ "$@"; }
75 cat() { exec_lc_all_c cat cat "$@"; }
76 cut() { exec_lc_all_c cut cut "$@"; }
77 find() { exec_lc_all_c find find "$@"; }
78 grep() { exec_lc_all_c grep grep "$@"; }
79 join() { exec_lc_all_c join join "$@"; }
80 paste() { exec_lc_all_c paste paste "$@"; }
81 sed() { exec_lc_all_c sed sed "$@"; }
82 sort() { exec_lc_all_c sort sort "$@"; }
83 tr() { exec_lc_all_c tr tr "$@"; }
84 wc() { exec_lc_all_c wc wc "$@"; }
85 xargs() { exec_lc_all_c xargs xargs "$@"; }
87 # Output arguments without any possible interpretation
88 # (Avoid misinterpretation of '\' characters or leading "-n", "-E" or "-e")
89 echol()
91 printf '%s\n' "$*"
94 info()
96 echol "${TG_RECURSIVE}${tgname:-tg}: $*"
99 warn()
101 info "warning: $*" >&2
104 err()
106 info "error: $*" >&2
109 fatal()
111 info "fatal: $*" >&2
114 die()
116 fatal "$@"
117 exit 1
120 # shift off first arg then return "$*" properly quoted in single-quotes
121 # if $1 was '' output goes to stdout otherwise it's assigned to $1
122 # the final \n, if any, is omitted from the result but any others are included
123 v_quotearg()
125 _quotearg_v="$1"
126 shift
127 set -- "$_quotearg_v" \
128 "sed \"s/'/'\\\\\\''/g;1s/^/'/;\\\$s/\\\$/'/;s/'''/'/g;1s/^''\\(.\\)/\\1/\"" "$*"
129 unset_ _quotearg_v
130 if [ -z "$3" ]; then
131 if [ -z "$1" ]; then
132 echo "''"
133 else
134 eval "$1=\"''\""
136 else
137 if [ -z "$1" ]; then
138 printf "%s$4" "$3" | eval "$2"
139 else
140 eval "$1="'"$(printf "%s$4" "$3" | eval "$2")"'
145 # same as v_quotearg except there's no extra $1 so output always goes to stdout
146 quotearg()
148 v_quotearg '' "$@"
151 vcmp()
153 # Compare $1 to $3 each of which must match ^[^0-9]*\d*(\.\d*)*.*$
154 # where only the "\d*" parts in the regex participate in the comparison
155 # Since EVERY string matches that regex this function is easy to use
156 # An empty string ('') for $1 or $3 or any "\d*" part is treated as 0
157 # $2 is a compare op '<', '<=', '=', '==', '!=', '>=', '>'
158 # Return code is 0 for true, 1 for false (or unknown compare op)
159 # There is NO difference in behavior between '=' and '=='
160 # Note that "vcmp 1.8 == 1.8.0.0.0.0" correctly returns 0
161 set -- "$1" "$2" "$3" "${1%%[0-9]*}" "${3%%[0-9]*}"
162 set -- "${1#"$4"}" "$2" "${3#"$5"}"
163 set -- "${1%%[!0-9.]*}" "$2" "${3%%[!0-9.]*}"
164 while
165 vcmp_a_="${1%%.*}"
166 vcmp_b_="${3%%.*}"
167 [ "z$vcmp_a_" != "z" -o "z$vcmp_b_" != "z" ]
169 if [ "${vcmp_a_:-0}" -lt "${vcmp_b_:-0}" ]; then
170 unset_ vcmp_a_ vcmp_b_
171 case "$2" in "<"|"<="|"!=") return 0; esac
172 return 1
173 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
174 unset_ vcmp_a_ vcmp_b_
175 case "$2" in ">"|">="|"!=") return 0; esac
176 return 1;
178 vcmp_a_="${1#$vcmp_a_}"
179 vcmp_b_="${3#$vcmp_b_}"
180 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
181 done
182 unset_ vcmp_a_ vcmp_b_
183 case "$2" in "="|"=="|"<="|">=") return 0; esac
184 return 1
187 precheck() {
188 if ! git_version="$(git version)"; then
189 die "'git version' failed"
191 case "$git_version" in [Gg]"it version "*);;*)
192 die "'git version' output does not start with 'git version '"
193 esac
195 vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
196 die "git version >= $GIT_MINIMUM_VERSION required but found $git_version instead"
199 case "$1" in version|--version|-V)
200 echo "TopGit version $TG_VERSION"
201 exit 0
202 esac
204 precheck
205 [ "$1" = "precheck" ] && exit 0
208 cat_depsmsg_internal()
210 _rev="$(ref_exists_rev "refs/heads/$1")" || return 0
211 if [ -s "$tg_cache_dir/refs/heads/$1/.$2" ]; then
212 if read _rev_match && [ "$_rev" = "$_rev_match" ]; then
213 _line=
214 while IFS= read -r _line || [ -n "$_line" ]; do
215 printf '%s\n' "$_line"
216 done
217 return 0
218 fi <"$tg_cache_dir/refs/heads/$1/.$2"
220 [ -d "$tg_cache_dir/refs/heads/$1" ] || mkdir -p "$tg_cache_dir/refs/heads/$1" 2>/dev/null || :
221 if [ -d "$tg_cache_dir/refs/heads/$1" ]; then
222 printf '%s\n' "$_rev" >"$tg_cache_dir/refs/heads/$1/.$2"
223 _line=
224 git cat-file blob "$_rev:.$2" 2>/dev/null |
225 while IFS= read -r _line || [ -n "$_line" ]; do
226 printf '%s\n' "$_line" >&3
227 printf '%s\n' "$_line"
228 done 3>>"$tg_cache_dir/refs/heads/$1/.$2"
229 else
230 git cat-file blob "$_rev:.$2" 2>/dev/null
234 # cat_deps BRANCHNAME
235 # Caches result
236 cat_deps()
238 cat_depsmsg_internal "$1" topdeps
241 # cat_msg BRANCHNAME
242 # Caches result
243 cat_msg()
245 cat_depsmsg_internal "$1" topmsg
248 # cat_file TOPIC:PATH [FROM]
249 # cat the file PATH from branch TOPIC when FROM is empty.
250 # FROM can be -i or -w, than the file will be from the index or worktree,
251 # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
252 cat_file()
254 path="$1"
255 case "$2" in
257 cat "$root_dir/${path#*:}"
260 # ':file' means cat from index
261 git cat-file blob ":${path#*:}" 2>/dev/null
264 case "$path" in
265 refs/heads/*:.topdeps)
266 _temp="${path%:.topdeps}"
267 cat_deps "${_temp#refs/heads/}"
269 refs/heads/*:.topmsg)
270 _temp="${path%:.topmsg}"
271 cat_msg "${_temp#refs/heads/}"
274 git cat-file blob "$path" 2>/dev/null
276 esac
279 die "Wrong argument to cat_file: '$2'"
281 esac
284 # if use_alt_temp_odb and tg_use_alt_odb are true try to write the object(s)
285 # into the temporary alt odb area instead of the usual location
286 git_temp_alt_odb_cmd()
288 if [ -n "$use_alt_temp_odb" ] && [ -n "$tg_use_alt_odb" ] &&
289 [ -n "$TG_OBJECT_DIRECTORY" ] &&
290 [ -f "$TG_OBJECT_DIRECTORY/info/alternates" ]; then
292 GIT_ALTERNATE_OBJECT_DIRECTORIES="$TG_PRESERVED_ALTERNATES"
293 GIT_OBJECT_DIRECTORY="$TG_OBJECT_DIRECTORY"
294 unset_ TG_OBJECT_DIRECTORY TG_PRESERVED_ALTERNATES
295 export GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_OBJECT_DIRECTORY
296 git "$@"
298 else
299 git "$@"
303 git_write_tree() { git_temp_alt_odb_cmd write-tree "$@"; }
304 git_mktree() { git_temp_alt_odb_cmd mktree "$@"; }
306 # get tree for the committed topic
307 get_tree_()
309 echo "refs/heads/$1"
312 # get tree for the base
313 get_tree_b()
315 echo "refs/$topbases/$1"
318 # get tree for the index
319 get_tree_i()
321 git_write_tree
324 # get tree for the worktree
325 get_tree_w()
327 i_tree=$(git_write_tree)
329 # the file for --index-output needs to sit next to the
330 # current index file
331 cd "$root_dir"
332 : ${GIT_INDEX_FILE:="$git_dir/index"}
333 TMP_INDEX="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX")"
334 git read-tree -m $i_tree --index-output="$TMP_INDEX" &&
335 GIT_INDEX_FILE="$TMP_INDEX" &&
336 export GIT_INDEX_FILE &&
337 git diff --name-only -z HEAD |
338 git update-index -z --add --remove --stdin &&
339 git_write_tree &&
340 rm -f "$TMP_INDEX"
344 # get tree for arbitrary ref
345 get_tree_r()
347 echo "$1"
350 # strip_ref "$(git symbolic-ref HEAD)"
351 # Output will have a leading refs/heads/ or refs/$topbases/ stripped if present
352 strip_ref()
354 case "$1" in
355 refs/"$topbases"/*)
356 echol "${1#refs/$topbases/}"
358 refs/heads/*)
359 echol "${1#refs/heads/}"
362 echol "$1"
363 esac
366 # pretty_tree [-t] NAME [-b | -i | -w | -r]
367 # Output tree ID of a cleaned-up tree without tg's artifacts.
368 # NAME will be ignored for -i and -w, but needs to be present
369 # With -r NAME must be a full ref name to a treeish (it's used as-is)
370 # If -t is used the tree is written into the alternate temporary objects area
371 pretty_tree()
373 use_alt_temp_odb=
374 [ "$1" != "-t" ] || { shift; use_alt_temp_odb=1; }
375 name="$1"
376 source="${2#?}"
377 git ls-tree --full-tree "$(get_tree_$source "$name")" |
378 sed -ne '/ \.top.*$/!p' |
379 git_mktree
382 # return an empty-tree root commit -- date is either passed in or current
383 # If passed in "$*" must be epochsecs followed by optional hhmm offset (+0000 default)
384 # An invalid secs causes the current date to be used, an invalid zone offset
385 # causes +0000 to be used
386 make_empty_commit()
388 # the empty tree is guaranteed to always be there even in a repo with
389 # zero objects, but for completeness we force it to exist as a real object
390 SECS=
391 read -r SECS ZONE JUNK <<-EOT || :
394 case "$SECS" in *[!0-9]*) SECS=; esac
395 if [ -z "$SECS" ]; then
396 MTDATE="$(date '+%s %z')"
397 else
398 case "$ZONE" in
399 -[01][0-9][0-5][0-9]|+[01][0-9][0-5][0-9])
401 [01][0-9][0-5][0-9])
402 ZONE="+$ZONE"
405 ZONE="+0000"
406 esac
407 MTDATE="$SECS $ZONE"
409 EMPTYID="- <-> $MTDATE"
410 EMPTYTREE="$(git hash-object -t tree -w --stdin < /dev/null)"
411 printf '%s\n' "tree $EMPTYTREE" "author $EMPTYID" "committer $EMPTYID" '' |
412 git hash-object -t commit -w --stdin
415 # standard input is a diff
416 # standard output is the "+" lines with leading "+ " removed
417 # beware that old lines followed by the dreaded '\ No newline at end of file'
418 # will appear to be new lines if lines are added after them
419 # the git diff --ignore-space-at-eol option can be used to prevent this
420 diff_added_lines()
422 awk '
423 BEGIN { in_hunk = 0; }
424 /^@@ / { in_hunk = 1; }
425 /^\+/ { if (in_hunk == 1) printf("%s\n", substr($0, 2)); }
426 !/^\\ No newline at end of file/ &&
427 /^[^@ +-]/ { in_hunk = 0; }
431 # $1 is name of new branch to create locally if all of these are true:
432 # a) exists as a remote TopGit branch for "$base_remote"
433 # b) the branch "name" does not have any invalid characters in it
434 # c) neither of the two branch refs (branch or base) exist locally
435 # returns success only if a new local branch was created (and dumps message)
436 auto_create_local_remote()
438 case "$1" in ""|*[" $tab$lf~^:\\*?["]*|.*|*/.*|*.|*./|/*|*/|*//*) return 1; esac
439 [ -n "$base_remote" ] &&
440 git update-ref --stdin <<-EOT >/dev/null 2>&1 &&
441 verify refs/remotes/$base_remote/${topbases#heads/}/$1 refs/remotes/$base_remote/${topbases#heads/}/$1
442 verify refs/remotes/$base_remote/$1 refs/remotes/$base_remote/$1
443 create refs/$topbases/$1 refs/remotes/$base_remote/${topbases#heads/}/$1^0
444 create refs/heads/$1 refs/remotes/$base_remote/$1^0
446 { init_reflog "refs/$topbases/$1" || :; } &&
447 info "topic branch '$1' automatically set up from remote '$base_remote'"
450 # setup_hook NAME
451 setup_hook()
453 setup_git_dir_is_bare
454 [ -z "$git_dir_is_bare" ] || return 0
455 tgname="${0##*/}"
456 hook_call="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
457 if [ -f "$git_hooks_dir/$1" ] && grep -Fq "$hook_call" "$git_hooks_dir/$1"; then
458 # Another job well done!
459 return
461 # Prepare incantation
462 hook_chain=
463 if [ -s "$git_hooks_dir/$1" -a -x "$git_hooks_dir/$1" ]; then
464 hook_call="$hook_call"' || exit $?'
465 if [ -L "$git_hooks_dir/$1" ] || ! sed -n 1p <"$git_hooks_dir/$1" | grep -Fqx "#!@SHELL_PATH@"; then
466 chain_num=
467 while [ -e "$git_hooks_dir/$1-chain$chain_num" ]; do
468 chain_num=$(( $chain_num + 1 ))
469 done
470 mv -f "$git_hooks_dir/$1" "$git_hooks_dir/$1-chain$chain_num"
471 hook_chain=1
473 else
474 hook_call="exec $hook_call"
475 [ -d "$git_hooks_dir" ] || mkdir -p "$git_hooks_dir" || :
477 # Don't call hook if tg is not installed
478 hook_call="if command -v \"$tgname\" >/dev/null 2>&1; then $hook_call; fi"
479 # Insert call into the hook
481 echol "#!@SHELL_PATH@"
482 echol "$hook_call"
483 if [ -n "$hook_chain" ]; then
484 echol "exec \"\$0-chain$chain_num\" \"\$@\""
485 else
486 [ ! -s "$git_hooks_dir/$1" ] || cat "$git_hooks_dir/$1"
488 } >"$git_hooks_dir/$1+"
489 chmod a+x "$git_hooks_dir/$1+"
490 mv "$git_hooks_dir/$1+" "$git_hooks_dir/$1"
493 # setup_ours (no arguments)
494 setup_ours()
496 setup_git_dir_is_bare
497 [ -z "$git_dir_is_bare" ] || return 0
498 if [ ! -s "$git_common_dir/info/attributes" ] || ! grep -q topmsg "$git_common_dir/info/attributes"; then
499 [ -d "$git_common_dir/info" ] || mkdir "$git_common_dir/info"
501 echo ".topmsg merge=ours"
502 echo ".topdeps merge=ours"
503 } >>"$git_common_dir/info/attributes"
505 if ! git config merge.ours.driver >/dev/null; then
506 git config merge.ours.name '"always keep ours" merge driver'
507 git config merge.ours.driver 'touch %A'
511 # measure_branch NAME [BASE] [EXTRAHEAD...]
512 measure_branch()
514 _bname="$1"; _base="$2"
515 shift; shift
516 [ -n "$_base" ] || _base="refs/$topbases/$(strip_ref "$_bname")"
517 # The caller should've verified $name is valid
518 _commits="$(git rev-list --count "$_bname" "$@" ^"$_base" --)"
519 _nmcommits="$(git rev-list --count --no-merges "$_bname" "$@" ^"$_base" --)"
520 if [ $_commits -ne 1 ]; then
521 _suffix="commits"
522 else
523 _suffix="commit"
525 echo "$_commits/$_nmcommits $_suffix"
528 # true if $1 is contained by (or the same as) $2
529 # this is never slower than merge-base --is-ancestor and is often slightly faster
530 contained_by()
532 [ "$(git rev-list --count --max-count=1 "$1" --not "$2" --)" = "0" ]
535 # branch_contains B1 B2
536 # Whether B1 is a superset of B2.
537 branch_contains()
539 _revb1="$(ref_exists_rev "$1")" || return 0
540 _revb2="$(ref_exists_rev "$2")" || return 0
541 if [ -s "$tg_cache_dir/$1/.bc/$2/.d" ]; then
542 if read _result _rev_matchb1 _rev_matchb2 &&
543 [ "$_revb1" = "$_rev_matchb1" -a "$_revb2" = "$_rev_matchb2" ]; then
544 return $_result
545 fi <"$tg_cache_dir/$1/.bc/$2/.d"
547 [ -d "$tg_cache_dir/$1/.bc/$2" ] || mkdir -p "$tg_cache_dir/$1/.bc/$2" 2>/dev/null || :
548 _result=0
549 contained_by "$_revb2" "$_revb1" || _result=1
550 if [ -d "$tg_cache_dir/$1/.bc/$2" ]; then
551 echo "$_result" "$_revb1" "$_revb2" >"$tg_cache_dir/$1/.bc/$2/.d"
553 return $_result
556 create_ref_dirs()
558 [ ! -s "$tg_tmp_dir/tg~ref-dirs-created" -a -s "$tg_ref_cache" ] || return 0
559 mkdir -p "$tg_tmp_dir/cached/refs"
560 awk '{x = $1; sub(/^refs\//, "", x); if (x != "") print x}' <"$tg_ref_cache" | tr '\n' '\0' | {
561 cd "$tg_tmp_dir/cached/refs" &&
562 xargs -0 mkdir -p
564 awk -v p="$tg_tmp_dir/cached/" '
565 NF == 2 &&
566 $1 ~ /^refs\/./ &&
567 $2 ~ /^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+$/ {
568 fn = p $1 "/.ref"
569 print "0 " $2 >fn
570 close(fn)
572 ' <"$tg_ref_cache"
573 echo 1 >"$tg_tmp_dir/tg~ref-dirs-created"
576 # If the first argument is non-empty, stores "1" there if this call created the cache
577 v_create_ref_cache()
579 [ -n "$tg_ref_cache" -a ! -s "$tg_ref_cache" ] || return 0
580 _remotespec=
581 [ -z "$base_remote" ] || _remotespec="refs/remotes/$base_remote"
582 [ -z "$1" ] || eval "$1=1"
583 git for-each-ref --format='%(refname) %(objectname)' \
584 refs/heads "refs/$topbases" $_remotespec >"$tg_ref_cache"
585 create_ref_dirs
588 remove_ref_cache()
590 [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ] || return 0
591 >"$tg_ref_cache"
592 >"$tg_ref_cache_br"
593 >"$tg_ref_cache_rbr"
594 >"$tg_ref_cache_ann"
595 >"$tg_ref_cache_dep"
598 # setting tg_ref_cache_only to non-empty will force non-$tg_ref_cache lookups to fail
599 rev_parse()
601 if [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ]; then
602 awk -v r="$1" 'BEGIN {e=1}; $1 == r {print $2; e=0; exit}; END {exit e}' <"$tg_ref_cache"
603 else
604 [ -z "$tg_ref_cache_only" ] || return 1
605 git rev-parse --quiet --verify "$1^0" -- 2>/dev/null
609 # ref_exists_rev REF
610 # Whether REF is a valid ref name
611 # REF must be fully qualified and start with refs/heads/, refs/$topbases/
612 # or, if $base_remote is set, refs/remotes/$base_remote/
613 # Caches result if $tg_read_only and outputs HASH on success
614 ref_exists_rev()
616 case "$1" in
617 refs/*)
619 $octet20)
620 printf '%s' "$1"
621 return;;
623 die "ref_exists_rev requires fully-qualified ref name (given: $1)"
624 esac
625 [ -n "$tg_read_only" ] || { git rev-parse --quiet --verify "$1^0" -- 2>/dev/null; return; }
626 _result=
627 _result_rev=
628 { read -r _result _result_rev <"$tg_tmp_dir/cached/$1/.ref"; } 2>/dev/null || :
629 [ -z "$_result" ] || { printf '%s' "$_result_rev"; return $_result; }
630 _result_rev="$(rev_parse "$1")"
631 _result=$?
632 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null
633 [ ! -d "$tg_tmp_dir/cached/$1" ] ||
634 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.ref" 2>/dev/null || :
635 printf '%s' "$_result_rev"
636 return $_result
639 # Same as ref_exists_rev but output is abbreviated hash
640 # Optional second argument defaults to --short but may be any --short=.../--no-short option
641 ref_exists_rev_short()
643 case "$1" in
644 refs/*)
646 $octet20)
649 die "ref_exists_rev_short requires fully-qualified ref name"
650 esac
651 [ -n "$tg_read_only" ] || { git rev-parse --quiet --verify ${2:---short} "$1^0" -- 2>/dev/null; return; }
652 _result=
653 _result_rev=
654 { read -r _result _result_rev <"$tg_tmp_dir/cached/$1/.rfs"; } 2>/dev/null || :
655 [ -z "$_result" ] || { printf '%s' "$_result_rev"; return $_result; }
656 _result_rev="$(rev_parse "$1")"
657 _result=$?
658 if [ $_result -eq 0 ]; then
659 _result_rev="$(git rev-parse --verify ${2:---short} --quiet "$_result_rev^0" --)"
660 _result=$?
662 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null
663 [ ! -d "$tg_tmp_dir/cached/$1" ] ||
664 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.rfs" 2>/dev/null || :
665 printf '%s' "$_result_rev"
666 return $_result
669 # ref_exists REF
670 # Whether REF is a valid ref name
671 # REF must be fully qualified and start with refs/heads/, refs/$topbases/
672 # or, if $base_remote is set, refs/remotes/$base_remote/
673 # Caches result
674 ref_exists()
676 ref_exists_rev "$1" >/dev/null
679 # rev_parse_tree REF
680 # Runs git rev-parse REF^{tree}
681 # Caches result if $tg_read_only
682 rev_parse_tree()
684 [ -n "$tg_read_only" ] || { git rev-parse --verify "$1^{tree}" -- 2>/dev/null; return; }
685 if [ -f "$tg_tmp_dir/cached/$1/.rpt" ]; then
686 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
687 printf '%s\n' "$_result"
688 return 0
690 return 1
692 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null || :
693 if [ -d "$tg_tmp_dir/cached/$1" ]; then
694 git rev-parse --verify "$1^{tree}" -- >"$tg_tmp_dir/cached/$1/.rpt" 2>/dev/null || :
695 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
696 printf '%s\n' "$_result"
697 return 0
699 return 1
701 git rev-parse --verify "$1^{tree}" -- 2>/dev/null
704 # has_remote BRANCH
705 # Whether BRANCH has a remote equivalent (accepts ${topbases#heads/}/ too)
706 has_remote()
708 [ -n "$base_remote" ] && ref_exists "refs/remotes/$base_remote/$1"
711 # Return the verified TopGit branch name for "$2" in "$1" or die with an error.
712 # If -z "$1" still set return code but do not return result
713 # As a convenience, if HEAD or @ is given and HEAD is a symbolic ref to
714 # refs/heads/... then ... will be verified instead.
715 # if "$3" = "-f" (for fail) then return an error rather than dying.
716 v_verify_topgit_branch()
718 if [ "$2" = "HEAD" ] || [ "$2" = "@" ]; then
719 _verifyname="$(git symbolic-ref HEAD 2>/dev/null)" || :
720 [ -n "$_verifyname" -o "$3" = "-f" ] || die "HEAD is not a symbolic ref"
721 case "$_verifyname" in refs/"$topbases"/*|refs/heads/*);;*)
722 [ "$3" != "-f" ] || return 1
723 die "HEAD is not a symbolic ref to the refs/heads namespace"
724 esac
725 set -- "$1" "$_verifyname" "$3"
727 case "$2" in
728 refs/"$topbases"/*)
729 _verifyname="${2#refs/$topbases/}"
731 refs/heads/*)
732 _verifyname="${2#refs/heads/}"
735 _verifyname="$2"
737 esac
738 if ! ref_exists "refs/heads/$_verifyname"; then
739 [ "$3" != "-f" ] || return 1
740 die "no such branch: $_verifyname"
742 if ! ref_exists "refs/$topbases/$_verifyname"; then
743 [ "$3" != "-f" ] || return 1
744 die "not a TopGit-controlled branch: $_verifyname"
746 [ -z "$1" ] || eval "$1="'"$_verifyname"'
749 # Return the verified TopGit branch name or die with an error.
750 # As a convenience, if HEAD or @ is given and HEAD is a symbolic ref to
751 # refs/heads/... then ... will be verified instead.
752 # if "$2" = "-f" (for fail) then return an error rather than dying.
753 verify_topgit_branch()
755 v_verify_topgit_branch _verifyname "$@" || return
756 printf '%s' "$_verifyname"
759 # Caches result
760 # $1 = branch name (i.e. "t/foo/bar")
761 # $2 = optional result of rev-parse "refs/heads/$1"
762 # $3 = optional result of rev-parse "refs/$topbases/$1"
763 branch_annihilated()
765 _branch_name="$1"
766 _rev="${2:-$(ref_exists_rev "refs/heads/$_branch_name")}"
767 _rev_base="${3:-$(ref_exists_rev "refs/$topbases/$_branch_name")}"
769 _result=
770 _result_rev=
771 _result_rev_base=
772 { read -r _result _result_rev _result_rev_base <"$tg_cache_dir/refs/heads/$_branch_name/.ann"; } 2>/dev/null || :
773 [ -z "$_result" -o "$_result_rev" != "$_rev" -o "$_result_rev_base" != "$_rev_base" ] || return $_result
775 # use the merge base in case the base is ahead.
776 mb="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)"
778 test -z "$mb" || test "$(rev_parse_tree "$mb")" = "$(rev_parse_tree "$_rev")"
779 _result=$?
780 [ -d "$tg_cache_dir/refs/heads/$_branch_name" ] || mkdir -p "$tg_cache_dir/refs/heads/$_branch_name" 2>/dev/null
781 [ ! -d "$tg_cache_dir/refs/heads/$_branch_name" ] ||
782 echo $_result $_rev $_rev_base >"$tg_cache_dir/refs/heads/$_branch_name/.ann" 2>/dev/null || :
783 return $_result
786 non_annihilated_branches()
788 refscacheopt="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
789 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ]; then
790 refscacheopt="$refscacheopt"'-r="$tg_ref_cache" "refs/$topbases"'
792 eval run_awk_topgit_branches -n "$refscacheopt" '"refs/$topbases" "$@"'
795 # Make sure our tree is clean
796 # if optional "$1" given also verify that a checkout to "$1" would succeed
797 ensure_clean_tree()
799 check_status
800 [ -z "$tg_state$git_state" ] || { do_status; exit 1; }
801 git update-index --ignore-submodules --refresh ||
802 die "the working directory has uncommitted changes (see above) - first commit or reset them"
803 [ -z "$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)" ] ||
804 die "the index has uncommited changes"
805 [ -z "$1" ] || git read-tree -n -u -m "$1" ||
806 die "git checkout \"$1\" would fail"
809 # Make sure .topdeps and .topmsg are "clean"
810 # They are considered "clean" if each is identical in worktree, index and HEAD
811 # With "-u" as the argument skip the HEAD check (-u => unborn)
812 # untracked .topdeps and/or .topmsg files are always considered "dirty" as well
813 # with -u them just existing constitutes "dirty"
814 ensure_clean_topfiles()
816 _dirtw=0
817 _dirti=0
818 _dirtu=0
819 _check="$(git diff-files --ignore-submodules --name-only -- :/.topdeps :/.topmsg)" &&
820 [ -z "$_check" ] || _dirtw=1
821 if [ "$1" != "-u" ]; then
822 _check="$(git diff-index --cached --ignore-submodules --name-only HEAD -- :/.topdeps :/.topmsg)" &&
823 [ -z "$_check" ] || _dirti=1
825 if [ "$_dirti$_dirtw" = "00" ]; then
826 v_get_show_cdup
827 if [ -e "${git_cdup_result}.topdeps" ] || [ -e "${git_cdup_result}.topmsg" ]; then
828 [ "$1" != "-u" ] &&
829 _check="$(git status --porcelain --ignored --untracked-files --ignore-submodules -- :/.topdeps :/.topmsg)" &&
830 [ -z "$_check" ] || _dirtu=1
833 if [ "$_dirtu$_dirti$_dirtw" != "000" ]; then
834 git status --ignored --untracked-files --ignore-submodules -- :/.topdeps :/.topmsg || :
835 case "$_dirtu$_dirti$_dirtw" in
836 001) die "the working directory has uncommitted changes (see above) - first commit or reset them";;
837 010) die "the index has uncommited changes (see above)";;
838 011) die "the working directory and index have uncommitted changes (see above) - first commit or reset them";;
839 100) die "the working directory has untracked files that would be overwritten (see above)";;
840 esac
844 # is_sha1 REF
845 # Whether REF is a SHA1 (compared to a symbolic name).
846 is_sha1()
848 case "$1" in $octet20) return 0;; esac
849 return 1
852 # navigate_deps <run_awk_topgit_navigate options and arguments>
853 # all options and arguments are passed through to run_awk_topgit_navigate
854 # except for a leading -td= option, if any, which is picked off for deps
855 # after arranging to feed it a suitable deps list
856 navigate_deps()
858 dogfer=
859 dorad=1
860 userc=
861 tmpdep=
862 ratd_opts="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
863 ratn_opts=
864 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
865 userc=1
866 tmprfs="$tg_ref_cache"
867 tmptgbr="$tg_ref_cache_br"
868 tmpann="$tg_ref_cache_ann"
869 tmpdep="$tg_ref_cache_dep"
870 [ -s "$tg_ref_cache" ] || dogfer=1
871 [ -n "$dogfer" ] || ! [ -s "$tmptgbr" ] || ! [ -f "$tmpann" ] || ! [ -s "$tmpdep" ] || dorad=
872 else
873 ratd_opts="${ratd_opts}-rmr"
874 ratn_opts="-rma -rmb"
875 tmprfs="$tg_tmp_dir/refs.$$"
876 tmpann="$tg_tmp_dir/ann.$$"
877 tmptgbr="$tg_tmp_dir/tgbr.$$"
878 dogfer=1
880 refpats="\"refs/heads\" \"refs/\$topbases\""
881 [ -z "$base_remote" ] || refpats="$refpats \"refs/remotes/\$base_remote\""
882 [ -z "$dogfer" ] ||
883 eval git for-each-ref '--format="%(refname) %(objectname)"' "$refpats" >"$tmprfs"
884 depscmd="run_awk_topgit_deps $ratd_opts"
885 case "$1" in -td=*)
886 userc=
887 depscmd="$depscmd $1"
888 shift
889 esac
890 depscmd="$depscmd"' -a="$tmpann" -b="$tmptgbr" -r="$tmprfs" -m="$mtblob" -s "refs/$topbases"'
891 if [ -n "$userc" ]; then
892 if [ -n "$dorad" ]; then
893 eval "$depscmd" >"$tmpdep"
895 depscmd='<"$tmpdep" '
896 else
897 depscmd="$depscmd |"
899 eval "$depscmd" run_awk_topgit_navigate '-a="$tmpann" -b="$tmptgbr"' "$ratn_opts" '"$@"'
902 # recurse_deps_internal NAME [BRANCHPATH...]
903 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
904 # followed by a 1 if the branch is "tgish" (2 if it also has a remote); 0 if not
905 # followed by a 0 for a non-leaf, 1 for a leaf or 2 for annihilated tgish
906 # but missing and remotes are always "0"
907 # then the branch name followed by its depedency chain (which might be empty)
908 # An output line might look like this:
909 # 0 1 1 t/foo/leaf t/foo/int t/stage
910 # If no_remotes is non-empty, exclude remotes
911 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
912 # If with_top_level is non-empty, include the top-level that's normally omitted
913 # any branch names in the space-separated recurse_deps_exclude variable
914 # are skipped (along with their dependencies)
915 recurse_deps_internal()
917 case " $recurse_deps_exclude " in *" $1 "*) return 0; esac
918 ratr_opts="${recurse_preorder:+-f} ${with_top_level:+-s}"
919 dogfer=
920 dorad=1
921 userc=
922 tmpdep=
923 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
924 userc=1
925 tmprfs="$tg_ref_cache"
926 tmptgbr="$tg_ref_cache_br"
927 tmpann="$tg_ref_cache_ann"
928 tmpdep="$tg_ref_cache_dep"
929 [ -s "$tg_ref_cache" ] || dogfer=1
930 [ -n "$dogfer" ] || ! [ -s "$tmptgbr" ] || ! [ -f "$tmpann" ] || ! [ -s "$tmpdep" ] || dorad=
931 else
932 ratr_opts="$ratr_opts -rmh -rma -rmb"
933 tmprfs="$tg_tmp_dir/refs.$$"
934 tmpann="$tg_tmp_dir/ann.$$"
935 tmptgbr="$tg_tmp_dir/tgbr.$$"
936 dogfer=1
938 refpats="\"refs/heads\" \"refs/\$topbases\""
939 [ -z "$base_remote" ] || refpats="$refpats \"refs/remotes/\$base_remote\""
940 tmptgrmtbr=
941 dorab=1
942 if [ -z "$no_remotes" ] && [ -n "$base_remote" ]; then
943 if [ -n "$userc" ]; then
944 tmptgrmtbr="$tg_ref_cache_rbr"
945 [ -n "$dogfer" ] || ! [ -s "$tmptgrmtbr" ] || dorab=
946 else
947 tmptgrmtbr="$tg_tmp_dir/tgrmtbr.$$"
948 ratr_opts="$ratr_opts -rmr"
950 ratr_opts="$ratr_opts -r=\"\$tmptgbr\" -u=\"refs/remotes/\$base_remote/\${topbases#heads/}\""
952 [ -z "$dogfer" ] ||
953 eval git for-each-ref '--format="%(refname) %(objectname)"' "$refpats" >"$tmprfs"
954 if [ -n "$tmptgrmtbr" ] && [ -n "$dorab" ]; then
955 run_awk_topgit_branches -n -h="refs/remotes/$base_remote" -r="$tmprfs" \
956 "refs/remotes/$base_remote/${topbases#heads/}" >"$tmptgrmtbr"
958 depscmd="run_awk_topgit_deps${TG_DEBUG:+ -p=\"\$tg_ref_cache.pre\"}"
959 depscmd="$depscmd"' -a="$tmpann" -b="$tmptgbr" -r="$tmprfs" -m="$mtblob" "refs/$topbases"'
960 if [ -n "$userc" ]; then
961 if [ -n "$dorad" ]; then
962 eval "$depscmd" >"$tmpdep"
964 depscmd='<"$tmpdep" '
965 else
966 depscmd="$depscmd |"
968 eval "$depscmd" run_awk_topgit_recurse '-a="$tmpann" -b="$tmptgbr"' \
969 '-c=1 -h="$tmprfs"' "$ratr_opts" '-x="$recurse_deps_exclude"' '"$@"'
972 # do_eval CMD
973 # helper for recurse_deps so that a return statement executed inside CMD
974 # does not return from recurse_deps. This shouldn't be necessary, but it
975 # seems that it actually is.
976 do_eval()
978 eval "$@"
981 # becomes read-only for caching purposes
982 # assigns new value to tg_read_only
983 # become_cacheable/undo_become_cacheable calls may be nested
984 become_cacheable()
986 _old_tg_read_only="$tg_read_only"
987 if [ -z "$tg_read_only" ]; then
988 ! [ -e "$tg_tmp_dir/cached" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created" ] ||
989 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
990 tg_read_only=1
992 _my_ref_cache=
993 v_create_ref_cache _my_ref_cache
994 _my_ref_cache="${_my_ref_cache:+1}"
995 tg_read_only="undo${_my_ref_cache:-0}-$_old_tg_read_only"
998 # restores tg_read_only and ref_cache to state before become_cacheable call
999 # become_cacheable/undo_bocome_cacheable calls may be nested
1000 undo_become_cacheable()
1002 case "$tg_read_only" in
1003 "undo"[01]"-"*)
1004 _suffix="${tg_read_only#undo?-}"
1005 [ "${tg_read_only%$_suffix}" = "undo0-" ] || remove_ref_cache
1006 tg_read_only="$_suffix"
1007 esac
1010 # just call this, no undo, sets tg_read_only= and removes ref cache and cached results
1011 become_non_cacheable()
1013 remove_ref_cache
1014 tg_read_only=
1015 ! [ -e "$tg_tmp_dir/cached" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created" ] ||
1016 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
1019 # call this to make sure the current Git repository has an associated work tree
1020 ensure_work_tree()
1022 setup_git_dir_is_bare
1023 [ -n "$git_dir_is_bare" ] || return 0
1024 die "This operation must be run in a work tree"
1027 # call this to make sure Git will not complain about a missing user/email
1028 # result is cached in TG_IDENT_CHECKED and a non-empty value suppresses the check
1029 ensure_ident_available()
1031 [ -z "$TG_IDENT_CHECKED" ] || return 0
1032 git var GIT_AUTHOR_IDENT >/dev/null &&
1033 git var GIT_COMMITTER_IDENT >/dev/null || exit
1034 TG_IDENT_CHECKED=1
1035 export TG_IDENT_CHECKED
1036 return 0
1039 # recurse_deps [-o=<options string>] CMD NAME [BRANCHPATH...]
1040 # Recursively eval CMD on all dependencies of NAME.
1041 # Dependencies are visited in topological order.
1042 # If <options string> is given, it's eval'd into the recurse_deps_internal
1043 # call just before the "--" that's passed just before NAME
1044 # CMD can refer to the following variables:
1046 # _ret starts as 0; CMD can change; will be final return result
1047 # _dep bare branch name or "refs/remotes/..." for a remote base
1048 # _name has $_dep in its .topdeps ("" for top and $with_top_level)
1049 # _depchain 0+ space-separated branch names forming a path to top
1050 # _dep_missing boolean "1" if no such $_dep ref; "" if ref present
1051 # _dep_is_leaf boolean "1" if leaf; "" if not
1052 # _dep_is_tgish boolean "1" if tgish; "" if not (which implies no remote)
1053 # _dep_has_remote boolean "1" if $_dep has_remote; "" if not
1054 # _dep_annihilated boolean "1" if $_dep annihilated; "" if not
1056 # CMD may use a "return" statement without issue; its return value is ignored,
1057 # but if CMD sets _ret to a negative value, e.g. "-0" or "-1" the enumeration
1058 # will stop immediately and the value with the leading "-" stripped off will
1059 # be the final result code
1061 # CMD can refer to $_name for queried branch name,
1062 # $_dep for dependency name,
1063 # $_depchain for space-seperated branch backtrace,
1064 # $_dep_missing boolean to check whether $_dep is present
1065 # and the $_dep_is_tgish and $_dep_annihilated booleans.
1066 # If recurse_preorder is NOT set then the $_dep_is_leaf boolean is also valid.
1067 # It can modify $_ret to affect the return value
1068 # of the whole function.
1069 # If recurse_deps() hits missing dependencies, it will append
1070 # them to space-separated $missing_deps list and skip them
1071 # after calling CMD with _dep_missing set.
1072 # remote dependencies are processed if no_remotes is unset.
1073 # any branch names in the space-separated recurse_deps_exclude variable
1074 # are skipped (along with their dependencies)
1076 # If no_remotes is non-empty, exclude remotes
1077 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
1078 # If with_top_level is non-empty, include the top-level that's normally omitted
1079 # any branch names in the space-separated recurse_deps_exclude variable
1080 # are skipped (along with their dependencies)
1081 recurse_deps()
1083 _opts=
1084 case "$1" in -o=*) _opts="${1#-o=}"; shift; esac
1085 _cmd="$1"; shift
1087 _depsfile="$(get_temp tg-depsfile)"
1088 eval recurse_deps_internal "$_opts" -- '"$@"' >"$_depsfile" || :
1090 _ret=0
1091 while read _ismissing _istgish _isleaf _dep _name _deppath; do
1092 _depchain="$_name${_deppath:+ $_deppath}"
1093 _dep_is_tgish=
1094 [ "$_istgish" = "0" ] || _dep_is_tgish=1
1095 _dep_has_remote=
1096 [ "$_istgish" != "2" ] || _dep_has_remote=1
1097 _dep_missing=
1098 if [ "$_ismissing" != "0" ]; then
1099 _dep_missing=1
1100 case " $missing_deps " in *" $_dep "*);;*)
1101 missing_deps="${missing_deps:+$missing_deps }$_dep"
1102 esac
1104 _dep_annihilated=
1105 _dep_is_leaf=
1106 if [ "$_isleaf" = "1" ]; then
1107 _dep_is_leaf=1
1108 elif [ "$_isleaf" = "2" ]; then
1109 _dep_annihilated=1
1111 do_eval "$_cmd" || :
1112 if [ "${_ret#-}" != "$_ret" ]; then
1113 _ret="${_ret#-}"
1114 break
1116 done <"$_depsfile"
1117 rm -f "$_depsfile"
1118 return ${_ret:-0}
1121 # find_leaves NAME
1122 # output (one per line) the unique leaves of NAME
1123 # a leaf is either
1124 # 1) a non-tgish dependency
1125 # 2) the base of a tgish dependency with no non-annihilated dependencies
1126 # duplicates are suppressed (by commit rev) and remotes are always ignored
1127 # if a leaf has an exact tag match that will be output
1128 # note that recurse_deps_exclude IS honored for this operation
1129 find_leaves()
1131 no_remotes=1
1132 with_top_level=1
1133 recurse_preorder=
1134 seen_leaf_refs=
1135 seen_leaf_revs=
1136 while read _ismissing _istgish _isleaf _dep _name _deppath; do
1137 [ "$_isleaf" = "1" ] && [ "$_ismissing" = "0" ] || continue
1138 if [ "$_istgish" != "0" ]; then
1139 fulldep="refs/$topbases/$_dep"
1140 else
1141 fulldep="refs/heads/$_dep"
1143 case " $seen_leaf_refs " in *" $fulldep "*);;*)
1144 seen_leaf_refs="${seen_leaf_refs:+$seen_leaf_refs }$fulldep"
1145 if fullrev="$(ref_exists_rev "$fulldep")"; then
1146 case " $seen_leaf_revs " in *" $fullrev "*);;*)
1147 seen_leaf_revs="${seen_leaf_revs:+$seen_leaf_revs }$fullrev"
1148 # See if Git knows it by another name
1149 if tagname="$(git describe --exact-match "$fullrev" 2>/dev/null)" && [ -n "$tagname" ]; then
1150 echo "refs/tags/$tagname"
1151 else
1152 echo "$fulldep"
1154 esac
1156 esac
1157 done <<-EOT
1158 $(recurse_deps_internal -l -o=1 -- "$1")
1160 with_top_level=
1163 # branch_needs_update
1164 # This is a helper function for determining whether given branch
1165 # is up-to-date wrt. its dependencies. It expects input as if it
1166 # is called as a recurse_deps() helper.
1167 # In case the branch does need update, it will echo it together
1168 # with the branch backtrace on the output (see needs_update()
1169 # description for details) and set $_ret to non-zero.
1170 branch_needs_update()
1172 if [ -n "$_dep_missing" ]; then
1173 echo "! $_dep $_depchain"
1174 return 0
1177 if [ -n "$_dep_is_tgish" ]; then
1178 [ -z "$_dep_annihilated" ] || return 0
1180 if [ -n "$_dep_has_remote" ]; then
1181 branch_contains "refs/heads/$_dep" "refs/remotes/$base_remote/$_dep" ||
1182 echo "refs/remotes/$base_remote/$_dep $_dep $_depchain"
1184 # We want to sync with our base first and should output this before
1185 # the remote branch, but the order does not actually matter to tg-update
1186 # as it just recurses regardless, but it does matter for tg-info (which
1187 # treats out-of-date bases as though they were already merged in) so
1188 # we output the remote before the base.
1189 branch_contains "refs/heads/$_dep" "refs/$topbases/$_dep" || {
1190 echo ": $_dep $_depchain"
1191 _ret=1
1192 return
1196 if [ -n "$_name" ]; then
1197 case "$_dep" in refs/*) _fulldep="$_dep";; *) _fulldep="refs/heads/$_dep";; esac
1198 if ! branch_contains "refs/$topbases/$_name" "$_fulldep"; then
1199 # Some new commits in _dep
1200 echo "$_dep $_depchain"
1201 _ret=1
1206 # needs_update NAME
1207 # This function is recursive; it outputs reverse path from NAME
1208 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
1209 # inner paths first. Innermost name can be refs/remotes/<remote>/<name>
1210 # if the head is not in sync with the <remote> branch <name>, ':' if
1211 # the head is not in sync with the base (in this order of priority)
1212 # or '!' if dependency is missing. Note that the remote branch, base
1213 # order is reversed from the order they will actually be updated in
1214 # order to accomodate tg info which treats out-of-date items that are
1215 # only in the base as already being in the head for status purposes.
1216 # It will also return non-zero status if NAME needs update.
1217 # If needs_update() hits missing dependencies, it will append
1218 # them to space-separated $missing_deps list and skip them.
1219 needs_update()
1221 recurse_deps branch_needs_update "$1"
1224 # branch_empty NAME [-i | -w]
1225 branch_empty()
1227 if [ -z "$2" ]; then
1228 _rev="$(ref_exists_rev "refs/heads/$1")" || return 0
1229 _result=
1230 _result_rev=
1231 { read -r _result _result_rev <"$tg_cache_dir/refs/heads/$1/.mt"; } 2>/dev/null || :
1232 [ -z "$_result" -o "$_result_rev" != "$_rev" ] || return $_result
1233 _result=0
1234 [ "$(pretty_tree -t "$1" -b)" = "$(pretty_tree -t "$1" $2)" ] || _result=$?
1235 [ -d "$tg_cache_dir/refs/heads/$1" ] || mkdir -p "$tg_cache_dir/refs/heads/$1" 2>/dev/null
1236 [ ! -d "$tg_cache_dir/refs/heads/$1" ] || echo $_result $_rev >"$tg_cache_dir/refs/heads/$1/.mt"
1237 return $_result
1238 else
1239 [ "$(pretty_tree -t "$1" -b)" = "$(pretty_tree -t "$1" $2)" ]
1243 v_get_tdmopt_internal()
1245 [ -n "$1" ] && [ -n "$3" ] || return 0
1246 [ "$2" = "-i" ] || [ "$2" = "-w" ] || return 0
1247 _optval=
1248 if v_verify_topgit_branch _tghead "HEAD" -f; then
1249 if [ "$2" = "-w" ] && [ -f "$root_dir/$3" ] && [ -r "$root_dir/$3" ]; then
1250 _opthash=
1251 if _opthash="$(git hash-object -w -t blob --stdin <"$root_dir/$3")" && [ -n "$_opthash" ]; then
1252 _optval="$4\"$_tghead:$_opthash\""
1254 elif [ "$2" = "-i" ]; then
1255 if _opthash="$(git rev-parse --quiet --verify ":0:$3" --)" && [ -n "$_opthash" ]; then
1256 _optval="$4\"$_tghead:$_opthash\""
1260 eval "$1="'"$_optval"'
1263 # set var $1 to the correct -td= option for use in an eval for $2 -i or -w mode
1264 v_get_tdopt() { v_get_tdmopt_internal "$1" "$2" ".topdeps" "-td="; }
1266 # set var $1 to the correct -tm= option for use in an eval for $2 -i or -w mode
1267 v_get_tmopt() { v_get_tdmopt_internal "$1" "$2" ".topmsg" "-tm="; }
1269 # checkout_symref_full [-f] FULLREF [SEED]
1270 # Just like git checkout $iowopt -b FULLREF [SEED] except that FULLREF MUST start with
1271 # refs/ and HEAD is ALWAYS set to a symref to it and [SEED] (default is FULLREF)
1272 # MUST be a committish which if present will be used instead of current FULLREF
1273 # (and FULLREF will be updated to it as well in that case)
1274 # Any merge state is always cleared by this function
1275 # With -f it's like git checkout $iowopt -f -b FULLREF (uses read-tree --reset
1276 # instead of -m) but it will clear out any unmerged entries
1277 # As an extension, FULLREF may also be a full hash to create a detached HEAD instead
1278 checkout_symref_full()
1280 _mode=-m
1281 _head="HEAD"
1282 if [ "$1" = "-f" ]; then
1283 _mode="--reset"
1284 _head=
1285 shift
1287 _ishash=
1288 case "$1" in
1289 refs/?*)
1291 $octet20)
1292 _ishash=1
1293 [ -z "$2" ] || [ "$1" = "$2" ] ||
1294 die "programmer error: invalid checkout_symref_full \"$1\" \"$2\""
1295 set -- HEAD "$1"
1298 die "programmer error: invalid checkout_symref_full \"$1\""
1300 esac
1301 _seedrev="$(git rev-parse --quiet --verify "${2:-$1}^0" --)" ||
1302 die "invalid committish: \"${2:-$1}\""
1303 # Clear out any MERGE_HEAD kruft
1304 rm -f "$git_dir/MERGE_HEAD" || :
1305 # We have to do all the hard work ourselves :/
1306 # This is like git checkout -b "$1" "$2"
1307 # (or just git checkout "$1"),
1308 # but never creates a detached HEAD (unless $1 is a hash)
1309 git read-tree -u $_mode $_head "$_seedrev" &&
1311 [ -z "$2" ] && [ "$(git cat-file -t "$1")" = "commit" ] ||
1312 git update-ref ${_ishash:+--no-deref} "$1" "$_seedrev"
1313 } && {
1314 [ -n "$_ishash" ] || git symbolic-ref HEAD "$1"
1318 # switch_to_base NAME [SEED]
1319 switch_to_base()
1321 checkout_symref_full "refs/$topbases/$1" "$2"
1324 # run editor with arguments
1325 # the editor setting will be cached in $tg_editor (which is eval'd)
1326 # result non-zero if editor fails or GIT_EDITOR cannot be determined
1327 # just in case, noalt_setup will be in effect while the editor is running
1328 run_editor()
1330 tg_editor="$GIT_EDITOR"
1331 [ -n "$tg_editor" ] || tg_editor="$(git var GIT_EDITOR)" || return $?
1333 noalt_setup
1334 eval "$tg_editor" '"$@"'
1338 # Show the help messages.
1339 do_help()
1341 _www=
1342 if [ "$1" = "-w" ]; then
1343 _www=1
1344 shift
1346 if [ -z "$1" ] ; then
1347 # This is currently invoked in all kinds of circumstances,
1348 # including when the user made a usage error. Should we end up
1349 # providing more than a short help message, then we should
1350 # differentiate.
1351 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
1353 ## Build available commands list for help output
1355 cmds=
1356 sep=
1357 for cmd in "$TG_INST_CMDDIR"/tg-[!-]*; do
1358 ! [ -r "$cmd" ] && continue
1359 # strip directory part and "tg-" prefix
1360 cmd="${cmd##*/}"
1361 cmd="${cmd#tg-}"
1362 [ "$cmd" != "migrate-bases" ] || continue
1363 [ "$cmd" != "summary" ] || cmd="st[atus]|$cmd"
1364 cmds="$cmds$sep$cmd"
1365 sep="|"
1366 done
1368 echo "TopGit version $TG_VERSION - A different patch queue manager"
1369 echo "Usage: $tgname [-C <dir>] [-r <remote> | -u]" \
1370 "[-c <name>=<val>] [--no-pager] ($cmds) ..."
1371 echo " Or: $tgname help [-w] [<command>]"
1372 echo "Use \"$tgdisplaydir$tgname help tg\" for overview of TopGit"
1373 elif [ -r "$TG_INST_CMDDIR"/tg-$1 -o -r "$TG_INST_SHAREDIR/tg-$1.txt" ] ; then
1374 if [ -n "$_www" ]; then
1375 nohtml=
1376 if ! [ -r "$TG_INST_SHAREDIR/topgit.html" ]; then
1377 echo "${0##*/}: missing html help file:" \
1378 "$TG_INST_SHAREDIR/topgit.html" 1>&2
1379 nohtml=1
1381 if ! [ -r "$TG_INST_SHAREDIR/tg-$1.html" ]; then
1382 echo "${0##*/}: missing html help file:" \
1383 "$TG_INST_SHAREDIR/tg-$1.html" 1>&2
1384 nohtml=1
1386 if [ -n "$nohtml" ]; then
1387 echo "${0##*/}: use" \
1388 "\"${0##*/} help $1\" instead" 1>&2
1389 exit 1
1391 git web--browse -c help.browser "$TG_INST_SHAREDIR/tg-$1.html"
1392 exit
1394 output()
1396 if [ -r "$TG_INST_CMDDIR"/tg-$1 ] ; then
1397 "$TG_INST_CMDDIR"/tg-$1 -h 2>&1 || :
1398 echo
1399 elif [ "$1" = "help" ]; then
1400 echo "Usage: ${tgname:-tg} help [-w] [<command>]"
1401 echo
1402 elif [ "$1" = "status" ] || [ "$1" = "st" ]; then
1403 echo "Usage: ${tgname:-tg} @tgsthelpusage@"
1404 echo
1406 if [ -r "$TG_INST_SHAREDIR/tg-$1.txt" ] ; then
1407 cat "$TG_INST_SHAREDIR/tg-$1.txt"
1410 page output "$1"
1411 else
1412 echo "${0##*/}: no help for $1" 1>&2
1413 do_help
1414 exit 1
1418 check_status()
1420 git_state=
1421 git_remove=
1422 tg_state=
1423 tg_remove=
1424 tg_topmerge=
1425 setup_git_dir_is_bare
1426 [ -z "$git_dir_is_bare" ] || return 0
1428 if [ -e "$git_dir/MERGE_HEAD" ]; then
1429 git_state="merge"
1430 elif [ -e "$git_dir/rebase-apply/applying" ]; then
1431 git_state="am"
1432 git_remove="$git_dir/rebase-apply"
1433 elif [ -e "$git_dir/rebase-apply" ]; then
1434 git_state="rebase"
1435 git_remove="$git_dir/rebase-apply"
1436 elif [ -e "$git_dir/rebase-merge" ]; then
1437 git_state="rebase"
1438 git_remove="$git_dir/rebase-merge"
1439 elif [ -e "$git_dir/CHERRY_PICK_HEAD" ]; then
1440 git_state="cherry-pick"
1441 elif [ -e "$git_dir/BISECT_LOG" ]; then
1442 git_state="bisect"
1443 elif [ -e "$git_dir/REVERT_HEAD" ]; then
1444 git_state="revert"
1446 git_remove="${git_remove#./}"
1448 if [ -e "$git_dir/tg-update" ]; then
1449 tg_state="update"
1450 tg_remove="$git_dir/tg-update"
1451 ! [ -s "$git_dir/tg-update/merging_topfiles" ] || tg_topmerge=1
1453 tg_remove="${tg_remove#./}"
1456 # Show status information
1457 do_status()
1459 do_status_result=0
1460 do_status_verbose=
1461 do_status_help=
1462 abbrev=refs
1463 pfx=
1464 while [ $# -gt 0 ] && case "$1" in
1465 --help|-h)
1466 do_status_help=1
1467 break;;
1468 -vv)
1469 # kludge in this common bundling option
1470 abbrev=
1471 do_status_verbose=1
1472 pfx="## "
1474 --verbose|-v)
1475 [ -z "$do_status_verbose" ] || abbrev=
1476 do_status_verbose=1
1477 pfx="## "
1479 --exit-code)
1480 do_status_result=2
1483 die "unknown status argument: $1"
1485 esac; do shift; done
1486 if [ -n "$do_status_help" ]; then
1487 echo "Usage: ${tgname:-tg} @tgsthelpusage@"
1488 return
1490 check_status
1491 symref="$(git symbolic-ref --quiet HEAD)" || :
1492 headrv="$(git rev-parse --quiet --verify ${abbrev:+--short} HEAD --)" || :
1493 if [ -n "$symref" ]; then
1494 uprefpart=
1495 if [ -n "$headrv" ]; then
1496 upref="$(git rev-parse --symbolic-full-name @{upstream} 2>/dev/null)" || :
1497 if [ -n "$upref" ]; then
1498 uprefpart=" ... ${upref#$abbrev/remotes/}"
1499 mbase="$(git merge-base HEAD "$upref")" || :
1500 ahead="$(git rev-list --count HEAD ${mbase:+--not $mbase})" || ahead=0
1501 behind="$(git rev-list --count "$upref" ${mbase:+--not $mbase})" || behind=0
1502 [ "$ahead$behind" = "00" ] || uprefpart="$uprefpart ["
1503 [ "$ahead" = "0" ] || uprefpart="${uprefpart}ahead $ahead"
1504 [ "$ahead" = "0" ] || [ "$behind" = "0" ] || uprefpart="$uprefpart, "
1505 [ "$behind" = "0" ] || uprefpart="${uprefpart}behind $behind"
1506 [ "$ahead$behind" = "00" ] || uprefpart="$uprefpart]"
1509 echol "${pfx}HEAD -> ${symref#$abbrev/heads/} [${headrv:-unborn}]$uprefpart"
1510 else
1511 echol "${pfx}HEAD -> ${headrv:-?}"
1513 if [ -n "$tg_state" ]; then
1514 extra=
1515 if [ "$tg_state" = "update" ]; then
1516 IFS= read -r uname <"$git_dir/tg-update/name" || :
1517 [ -z "$uname" ] ||
1518 extra="; currently updating branch '$uname'"
1520 echol "${pfx}tg $tg_state in progress$extra"
1521 if [ -s "$git_dir/tg-update/fullcmd" ] && [ -s "$git_dir/tg-update/names" ]; then
1522 printf "${pfx}You are currently updating as a result of:\n${pfx} "
1523 cat "$git_dir/tg-update/fullcmd"
1524 bcnt="$(( $(wc -w < "$git_dir/tg-update/names") ))"
1525 if [ $bcnt -gt 1 ]; then
1526 pcnt=0
1527 ! [ -s "$git_dir/tg-update/processed" ] ||
1528 pcnt="$(( $(wc -w < "$git_dir/tg-update/processed") ))"
1529 echo "${pfx}$pcnt of $bcnt branches updated so far"
1532 if [ "$tg_state" = "update" ]; then
1533 echol "${pfx} (use \"$tgdisplayac update --continue\" to continue)"
1534 echol "${pfx} (use \"$tgdisplayac update --skip\" to skip this branch and continue)"
1535 echol "${pfx} (use \"$tgdisplayac update --stop\" to stop and retain changes so far)"
1536 echol "${pfx} (use \"$tgdisplayac update --abort\" to restore pre-update state)"
1539 [ -z "$git_state" ] || echo "${pfx}git $git_state in progress"
1540 if [ "$git_state" = "merge" ]; then
1541 ucnt="$(( $(git ls-files --unmerged --full-name --abbrev :/ | wc -l) ))"
1542 if [ $ucnt -gt 0 ]; then
1543 echo "${pfx}"'fix conflicts and then "git commit" the result'
1544 else
1545 echo "${pfx}"'all conflicts fixed; run "git commit" to record result'
1548 if [ -z "$git_state" ]; then
1549 setup_git_dir_is_bare
1550 [ -z "$git_dir_is_bare" ] || return 0
1551 gsp="$(git status --porcelain 2>/dev/null)" || return 0 # bare repository???
1552 gspcnt=0
1553 [ -z "$gsp" ] ||
1554 gspcnt="$(( $(printf '%s\n' "$gsp" | sed -n '/^??/!p' | wc -l) ))"
1555 untr=
1556 if [ "$gspcnt" -eq 0 ]; then
1557 [ -z "$gsp" ] || untr="; non-ignored, untracked files present"
1558 echo "${pfx}working directory is clean$untr"
1559 [ -n "$tg_state" ] || do_status_result=0
1560 else
1561 echo "${pfx}working directory is DIRTY"
1562 [ -z "$do_status_verbose" ] || git status --short --untracked-files=no
1567 ## Pager stuff
1569 # isatty FD
1570 isatty()
1572 test -t $1
1575 # pass "diff" to get pager.diff
1576 # if pager.$1 is a boolean false returns cat
1577 # if set to true or unset fails
1578 # otherwise succeeds and returns the value
1579 get_pager()
1581 if _x="$(git config --bool "pager.$1" 2>/dev/null)"; then
1582 [ "$_x" != "true" ] || return 1
1583 echo "cat"
1584 return 0
1586 if _x="$(git config "pager.$1" 2>/dev/null)"; then
1587 echol "$_x"
1588 return 0
1590 return 1
1593 # setup_pager
1594 # Set TG_PAGER to a valid executable
1595 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
1596 # See also the following "page" function for ease of use
1597 # emptypager will be set to 1 (otherwise empty) if TG_PAGER was set to "cat" to not be empty
1598 # Preference is (same as Git):
1599 # 1. GIT_PAGER
1600 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
1601 # 3. core.pager (only if set)
1602 # 4. PAGER
1603 # 5. git var GIT_PAGER
1604 # 6. less
1605 setup_pager()
1607 isatty 1 || { emptypager=1; TG_PAGER=cat; return 0; }
1609 emptypager=
1610 if [ -z "$TG_PAGER_IN_USE" ]; then
1611 # TG_PAGER = GIT_PAGER | PAGER | less
1612 # NOTE: GIT_PAGER='' is significant
1613 if [ -n "${GIT_PAGER+set}" ]; then
1614 TG_PAGER="$GIT_PAGER"
1615 elif [ -n "$USE_PAGER_TYPE" ] && _dp="$(get_pager "$USE_PAGER_TYPE")"; then
1616 TG_PAGER="$_dp"
1617 elif _cp="$(git config core.pager 2>/dev/null)"; then
1618 TG_PAGER="$_cp"
1619 elif [ -n "${PAGER+set}" ]; then
1620 TG_PAGER="$PAGER"
1621 else
1622 _gp="$(git var GIT_PAGER 2>/dev/null)" || :
1623 [ "$_gp" != ":" ] || _gp=
1624 TG_PAGER="${_gp:-less}"
1626 if [ -z "$TG_PAGER" ]; then
1627 emptypager=1
1628 TG_PAGER=cat
1630 else
1631 emptypager=1
1632 TG_PAGER=cat
1635 # Set pager default environment variables
1636 # see pager.c:setup_pager
1637 if [ -z "${LESS+set}" ]; then
1638 LESS="-FRX"
1639 export LESS
1641 if [ -z "${LV+set}" ]; then
1642 LV="-c"
1643 export LV
1646 # this is needed so e.g. $(git diff) will still colorize it's output if
1647 # requested in ~/.gitconfig with color.diff=auto
1648 GIT_PAGER_IN_USE=1
1649 export GIT_PAGER_IN_USE
1651 # this is needed so we don't get nested pagers
1652 TG_PAGER_IN_USE=1
1653 export TG_PAGER_IN_USE
1656 # page eval_arg [arg ...]
1658 # Calls setup_pager then evals the first argument passing it all the rest
1659 # where the output is piped through eval "$TG_PAGER" unless emptypager is set
1660 # by setup_pager (in which case the output is left as-is).
1662 # To handle arbitrary paging duties, collect lines to be paged into a
1663 # function and then call page with the function name or perhaps func_name "$@".
1665 # If no arguments at all are passed in do nothing (return with success).
1666 page()
1668 [ $# -gt 0 ] || return 0
1669 setup_pager
1670 _evalarg="$1"; shift
1671 if [ -n "$emptypager" ]; then
1672 eval "$_evalarg" '"$@"'
1673 else
1674 { eval "$_evalarg" '"$@"';} | eval "$TG_PAGER"
1678 # get_temp NAME [-d]
1679 # creates a new temporary file (or directory with -d) in the global
1680 # temporary directory $tg_tmp_dir with pattern prefix NAME
1681 get_temp()
1683 mktemp $2 "$tg_tmp_dir/$1.XXXXXX"
1686 # automatically called by strftime
1687 # does nothing if already setup
1688 # may be called explicitly if the first call would otherwise be in a subshell
1689 # so that the setup is only done once before subshells start being spawned
1690 setup_strftime()
1692 [ -z "$strftime_is_setup" ] || return 0
1694 # date option to format raw epoch seconds values
1695 daterawopt=
1696 _testes='951807788'
1697 _testdt='2000-02-29 07:03:08 UTC'
1698 _testfm='%Y-%m-%d %H:%M:%S %Z'
1699 if [ "$(TZ=UTC date "-d@$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1700 daterawopt='-d@'
1701 elif [ "$(TZ=UTC date "-r$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1702 daterawopt='-r'
1704 strftime_is_setup=1
1707 # $1 => strftime format string to use
1708 # $2 => raw timestamp as seconds since epoch
1709 # $3 => optional time zone string (empty/absent for local time zone)
1710 strftime()
1712 setup_strftime
1713 if [ -n "$daterawopt" ]; then
1714 if [ -n "$3" ]; then
1715 TZ="$3" date "$daterawopt$2" "+$1"
1716 else
1717 date "$daterawopt$2" "+$1"
1719 else
1720 if [ -n "$3" ]; then
1721 TZ="$3" perl -MPOSIX=strftime -le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1722 else
1723 perl -MPOSIX=strftime -le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1728 got_cdup_result=
1729 git_cdup_result=
1730 v_get_show_cdup()
1732 if [ -z "$got_cdup_result" ]; then
1733 git_cdup_result="$(git rev-parse --show-cdup)"
1734 got_cdup_result=1
1736 [ -z "$1" ] || eval "$1="'"$git_cdup_result"'
1739 setup_git_dir_is_bare()
1741 if [ -z "$git_dir_is_bare_setup" ]; then
1742 git_dir_is_bare="$(git rev-parse --is-bare-repository)"
1743 [ z"$git_dir_is_bare" = z"true" ] || git_dir_is_bare=
1744 git_dir_is_bare_setup=1
1748 setup_git_dirs()
1750 [ -n "$git_dir" ] || git_dir="$(git rev-parse --git-dir)"
1751 if [ -n "$git_dir" ] && [ -d "$git_dir" ]; then
1752 git_dir="$(cd "$git_dir" && pwd)"
1754 if [ -z "$git_common_dir" ]; then
1755 if vcmp "$git_version" '>=' "2.5"; then
1756 # rev-parse --git-common-dir is broken and may give
1757 # an incorrect result unless the current directory is
1758 # already set to the top level directory
1759 v_get_show_cdup
1760 git_common_dir="$(cd "./$git_cdup_result" && cd "$(git rev-parse --git-common-dir)" && pwd)"
1761 else
1762 git_common_dir="$git_dir"
1765 [ -n "$git_dir" ] && [ -n "$git_common_dir" ] &&
1766 [ -d "$git_dir" ] && [ -d "$git_common_dir" ] || die "Not a git repository"
1767 git_hooks_dir="$git_common_dir/hooks"
1768 if vcmp "$git_version" '>=' "2.9" && gchp="$(git config --path --get core.hooksPath 2>/dev/null)" && [ -n "$gchp" ]; then
1769 case "$gchp" in
1770 /[!/]*)
1771 git_hooks_dir="$gchp"
1774 [ -n "$1" ] || warn "ignoring non-absolute core.hooksPath: $gchp"
1776 esac
1777 unset_ gchp
1781 basic_setup_remote()
1783 if [ -z "$base_remote" ]; then
1784 if [ "${TG_EXPLICIT_REMOTE+set}" = "set" ]; then
1785 base_remote="$TG_EXPLICIT_REMOTE"
1786 else
1787 base_remote="$(git config topgit.remote 2>/dev/null)" || :
1792 basic_setup()
1794 setup_git_dirs $1
1795 basic_setup_remote
1796 tgsequester="$(git config --bool topgit.sequester 2>/dev/null)" || :
1797 tgnosequester=
1798 [ "$tgsequester" != "false" ] || tgnosequester=1
1799 unset_ tgsequester
1801 # catch errors if topbases is used without being set
1802 unset_ tg_topbases_set
1803 topbases="programmer*:error"
1804 topbasesrx="programmer*:error}"
1805 oldbases="$topbases"
1808 ## Initial setup
1809 initial_setup()
1811 # suppress the merge log editor feature since git 1.7.10
1813 GIT_MERGE_AUTOEDIT=no
1814 export GIT_MERGE_AUTOEDIT
1816 basic_setup $1
1817 iowopt=
1818 ! vcmp "$git_version" '>=' "2.5" || iowopt="--ignore-other-worktrees"
1819 gcfbopt=
1820 ! vcmp "$git_version" '>=' "2.6" || gcfbopt="--buffer"
1821 auhopt=
1822 ! vcmp "$git_version" '>=' "2.9" || auhopt="--allow-unrelated-histories"
1823 v_get_show_cdup root_dir
1824 root_dir="${root_dir:-.}"
1825 logrefupdates="$(git config --bool core.logallrefupdates 2>/dev/null)" || :
1826 [ "$logrefupdates" = "true" ] || logrefupdates=
1828 # make sure root_dir doesn't end with a trailing slash.
1830 root_dir="${root_dir%/}"
1832 # create global temporary directories, inside GIT_DIR
1834 if [ -n "$TG_TMPDIR" ] && [ -d "$TG_TMPDIR" ] && [ -w "$TG_TMPDIR" ] &&
1835 { >"$TG_TMPDIR/.check"; } >/dev/null 2>&1; then
1836 tg_tmp_dir="$TG_TMPDIR"
1837 else
1838 tg_tmp_dir=
1839 TRAPEXIT_='${TG_DEBUG:+echo} rm -rf "$tg_tmp_dir" >&2'
1840 trap 'trapexit_ 129' HUP
1841 trap 'trapexit_ 130' INT
1842 trap 'trapexit_ 131' QUIT
1843 trap 'trapexit_ 134' ABRT
1844 trap 'trapexit_ 141' PIPE
1845 trap 'trapexit_ 143' TERM
1846 tg_tmp_dir="$(mktemp -d "$git_dir/tg-tmp.XXXXXX" 2>/dev/null)" || tg_tmp_dir=
1847 [ -n "$tg_tmp_dir" ] || tg_tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/tg-tmp.XXXXXX" 2>/dev/null)" || tg_tmp_dir=
1848 [ -n "$tg_tmp_dir" ] || [ -z "$TMPDIR" ] || tg_tmp_dir="$(mktemp -d "/tmp/tg-tmp.XXXXXX" 2>/dev/null)" || tg_tmp_dir=
1850 unset_ TG_TMPDIR
1851 tg_ref_cache="$tg_tmp_dir/tg~ref-cache"
1852 tg_ref_cache_br="$tg_ref_cache.br"
1853 tg_ref_cache_rbr="$tg_ref_cache.rbr"
1854 tg_ref_cache_ann="$tg_ref_cache.ann"
1855 tg_ref_cache_dep="$tg_ref_cache.dep"
1856 [ -n "$tg_tmp_dir" ] && [ -w "$tg_tmp_dir" ] && { >"$tg_ref_cache"; } >/dev/null 2>&1 ||
1857 die "could not create a writable temporary directory"
1859 # make sure global cache directory exists inside GIT_DIR or $tg_tmp_dir
1861 user_id_no="$(id -u)" || :
1862 : "${user_id_no:=_99_}"
1863 tg_cache_dir="$git_common_dir/tg-cache"
1864 [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir" >/dev/null 2>&1 || tg_cache_dir=
1865 [ -z "$tg_cache_dir" ] || tg_cache_dir="$tg_cache_dir/$user_id_no"
1866 [ -z "$tg_cache_dir" ] || [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir" >/dev/null 2>&1 || tg_cache_dir=
1867 [ -z "$tg_cache_dir" ] || { >"$tg_cache_dir/.tgcache"; } >/dev/null 2>&1 || tg_cache_dir=
1868 if [ -z "$tg_cache_dir" ]; then
1869 tg_cache_dir="$tg_tmp_dir/tg-cache"
1870 [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir" >/dev/null 2>&1 || tg_cache_dir=
1871 [ -z "$tg_cache_dir" ] || { >"$tg_cache_dir/.tgcache"; } >/dev/null 2>&1 || tg_cache_dir=
1873 [ -n "$tg_cache_dir" ] ||
1874 die "could not create a writable tg-cache directory (even a temporary one)"
1876 # GIT_ALTERNATE_OBJECT_DIRECTORIES can contain double-quoted entries
1877 # since Git v2.11.1; however, it's only necessary for : (or perhaps ;)
1878 # so we avoid it if possible and require v2.11.1 to do it at all
1879 # otherwise just don't make an alternates temporary store in that case;
1880 # it's okay to not have one; everything will still work; the nicety of
1881 # making the temporary tree objects vanish when tg exits just won't
1882 # happen in that case but nothing will break also be sure to reuse
1883 # the parent's if we've been recursively invoked and it's for the
1884 # same repository we were invoked on
1886 tg_use_alt_odb=1
1887 _odbdir="${GIT_OBJECT_DIRECTORY:-$git_common_dir/objects}"
1888 [ -n "$_odbdir" ] && [ -d "$_odbdir" ] || tg_use_alt_odb=
1889 _fulltmpdir=
1890 [ -z "$tg_use_alt_odb" ] || _fulltmpdir="$(cd "$tg_tmp_dir" && pwd -P)"
1891 case "$_fulltmpdir" in *[";:"]*) vcmp "$git_version" '>=' "2.11.1" || tg_use_alt_odb=; esac
1892 _fullodbdir=
1893 [ -z "$tg_use_alt_odb" ] || _fullodbdir="$(cd "$_odbdir" && pwd -P)"
1894 if [ -n "$tg_use_alt_odb" ] && [ -n "$TG_OBJECT_DIRECTORY" ] && [ -d "$TG_OBJECT_DIRECTORY/info" ] &&
1895 [ -f "$TG_OBJECT_DIRECTORY/info/alternates" ] && [ -r "$TG_OBJECT_DIRECTORY/info/alternates" ]; then
1896 if IFS= read -r _otherodbdir <"$TG_OBJECT_DIRECTORY/info/alternates" &&
1897 [ -n "$_otherodbdir" ] && [ "$_otherodbdir" = "$_fullodbdir" ]; then
1898 tg_use_alt_odb=2
1901 if [ "$tg_use_alt_odb" = "1" ]; then
1902 # create an alternate objects database to keep the ephemeral objects in
1903 mkdir -p "$tg_tmp_dir/objects/info"
1904 echol "$_fullodbdir" >"$tg_tmp_dir/objects/info/alternates"
1905 TG_OBJECT_DIRECTORY="$_fulltmpdir/objects"
1906 case "$TG_OBJECT_DIRECTORY" in
1907 *[";:"]*)
1908 # surround in "..." and backslash-escape internal '"' and '\\'
1909 _altodbdq="\"$(printf '%s\n' "$TG_OBJECT_DIRECTORY" |
1910 sed 's/\([""\\]\)/\\\1/g')\""
1913 _altodbdq="$TG_OBJECT_DIRECTORY"
1915 esac
1916 TG_PRESERVED_ALTERNATES="$GIT_ALTERNATE_OBJECT_DIRECTORIES"
1917 if [ -n "$GIT_ALTERNATE_OBJECT_DIRECTORIES" ]; then
1918 GIT_ALTERNATE_OBJECT_DIRECTORIES="$_altodbdq:$GIT_ALTERNATE_OBJECT_DIRECTORIES"
1919 else
1920 GIT_ALTERNATE_OBJECT_DIRECTORIES="$_altodbdq"
1922 export TG_PRESERVED_ALTERNATES TG_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_OBJECT_DIRECTORY
1926 noalt_setup()
1928 if [ "${TG_PRESERVED_ALTERNATES+set}" = "set" ]; then
1929 GIT_ALTERNATE_OBJECT_DIRECTORIES="$TG_PRESERVED_ALTERNATES"
1930 if [ -n "$GIT_ALTERNATE_OBJECT_DIRECTORIES" ]; then
1931 export GIT_ALTERNATE_OBJECT_DIRECTORIES
1932 else
1933 unset_ GIT_ALTERNATE_OBJECT_DIRECTORIES
1936 unset_ TG_TMPDIR TG_OBJECT_DIRECTORY TG_PRESERVED_ALTERNATES tg_use_alt_odb
1939 set_topbases()
1941 # refer to "top-bases" in a refname with $topbases
1943 [ -z "$tg_topbases_set" ] || return 0
1945 topbases_implicit_default=1
1946 # See if topgit.top-bases is set to heads or refs
1947 tgtb="$(git config "topgit.top-bases" 2>/dev/null)" || :
1948 if [ -n "$tgtb" ] && [ "$tgtb" != "heads" ] && [ "$tgtb" != "refs" ]; then
1949 if [ -n "$1" ]; then
1950 # never die on the hook script
1951 unset_ tgtb
1952 else
1953 die "invalid \"topgit.top-bases\" setting (must be \"heads\" or \"refs\")"
1956 if [ -n "$tgtb" ]; then
1957 case "$tgtb" in
1958 heads)
1959 topbases="heads/{top-bases}"
1960 topbasesrx="heads/[{]top-bases[}]"
1961 oldbases="top-bases";;
1962 refs)
1963 topbases="top-bases"
1964 topbasesrx="top-bases"
1965 oldbases="heads/{top-bases}";;
1966 esac
1967 # MUST NOT be exported
1968 unset_ tgtb tg_topbases_set topbases_implicit_default
1969 tg_topbases_set=1
1970 return 0
1972 unset_ tgtb
1974 # check heads and top-bases and see what state the current
1975 # repository is in. remotes are ignored.
1977 rc=0 activebases=
1978 activebases="$(
1979 git for-each-ref --format='%(refname)' "refs/heads" "refs/top-bases" 2>/dev/null |
1980 run_awk_ref_prefixes ${1:+-e} -n -- "refs/heads/{top-bases}" "refs/top-bases" "refs/heads")" ||
1981 rc=$?
1982 if [ "$rc" = "65" ]; then
1983 # Complain and die
1984 err "repository contains existing TopGit branches"
1985 err "but some use refs/top-bases/... for the base"
1986 err "and some use refs/heads/{top-bases}/... for the base"
1987 err "with the latter being the new, preferred location"
1988 err "set \"topgit.top-bases\" to either \"heads\" to use"
1989 err "the new heads/{top-bases} location or \"refs\" to use"
1990 err "the old top-bases location."
1991 err "(the tg migrate-bases command can also resolve this issue)"
1992 die "schizophrenic repository requires topgit.top-bases setting"
1994 [ -z "$activebases" ] || unset_ topbases_implicit_default
1995 if [ "$activebases" = "refs/heads/{top-bases}" ]; then
1996 topbases="heads/{top-bases}"
1997 topbasesrx="heads/[{]top-bases[}]"
1998 oldbases="top-bases"
1999 else
2000 # default is still top-bases for now
2001 topbases="top-bases"
2002 topbasesrx="top-bases"
2003 oldbases="heads/{top-bases}"
2005 # MUST NOT be exported
2006 unset_ rc activebases tg_topases_set
2007 tg_topbases_set=1
2008 return 0
2011 # $1 is remote name to check
2012 # $2 is optional variable name to set to result of check
2013 # $3 is optional command name to use in message (defaults to $cmd)
2014 # Fatal error if remote has schizophrenic top-bases
2015 # No error (and $2, if provided, will be set to empty) if remote has no top-bases at all
2016 check_remote_topbases()
2018 [ -n "$1" ] || die "programmer error: check_remote_topbases called with no remote argument"
2019 _crrc=0 _crremotebases=
2020 _crremotebases="$(
2021 git for-each-ref --format='%(refname)' "refs/remotes/$1" 2>/dev/null |
2022 run_awk_ref_prefixes -n -- "refs/remotes/$1/{top-bases}" "refs/remotes/$1/top-bases" "refs/remotes/$1")" ||
2023 _crrc=$?
2024 if [ "$_crrc" = "65" ]; then
2025 err "remote \"$1\" has top-bases in both locations:"
2026 err " refs/remotes/$1/{top-bases}/..."
2027 err " refs/remotes/$1/top-bases/..."
2028 err "set \"topgit.top-bases\" to \"heads\" for the first, preferred location"
2029 err "or set \"topgit.top-bases\" to \"refs\" for the second, old location"
2030 err "(the \"-c topgit.top-bases=<val>\" option can be used for this)"
2031 err "then re-run the tg ${3:-$cmd} command"
2032 err "(the tg migrate-bases command can also help with this problem)"
2033 die "schizophrenic remote \"$1\" requires topgit.top-bases setting"
2035 [ "$_crrc" != "66" ] || _crremotebases= # just to be sure
2036 [ -z "$2" ] || eval "$2="'"$_crremotebases"'
2037 unset _crrc _crremotebases
2038 return 0
2041 # init_reflog "ref"
2042 # if "$logrefupdates" is set and ref is not under refs/heads/ then force
2043 # an empty log file to exist so that ref changes will be logged
2044 # "$1" must be a fully-qualified refname (i.e. start with "refs/")
2045 # However, if "$1" is "refs/tgstash" then always make the reflog
2046 # The only ref not under refs/ that Git will write a reflog for is HEAD;
2047 # no matter what, it will NOT update a reflog for any other bare refs so
2048 # just quietly succeed when passed TG_STASH without doing anything.
2049 init_reflog()
2051 [ -n "$1" ] && [ "$1" != "TG_STASH" ] || return 0
2052 [ -n "$logrefupdates" ] || [ "$1" = "refs/tgstash" ] || return 0
2053 case "$1" in refs/heads/*|HEAD) return 0;; refs/*[!/]);; *) return 1; esac
2054 mkdir -p "$git_common_dir/logs/${1%/*}" 2>/dev/null || :
2055 { >>"$git_common_dir/logs/$1" || :; } 2>/dev/null
2058 # store the "realpath" for "$2" in "$1" except the leaf is not resolved if it's
2059 # a symbolic link. The directory part must exist, but the basename need not.
2060 v_get_abs_path()
2062 [ -n "$1" ] && [ -n "$2" ] || return 1
2063 set -- "$1" "$2" "${2%/}"
2064 case "$3" in
2065 */*) set -- "$1" "$2" "${3%/*}";;
2066 * ) set -- "$1" "$2" ".";;
2067 esac
2068 case "$2" in */)
2069 set -- "$1" "${2%/}" "$3" "/"
2070 esac
2071 [ -d "$3" ] || return 1
2072 eval "$1="'"$(cd "$3" && pwd -P)/${2##*/}$4"'
2075 ## Startup
2077 : "${TG_INST_CMDDIR:=@cmddir@}"
2078 : "${TG_INST_SHAREDIR:=@sharedir@}"
2079 : "${TG_INST_HOOKSDIR:=@hooksdir@}"
2081 [ -d "$TG_INST_CMDDIR" ] ||
2082 die "No command directory: '$TG_INST_CMDDIR'"
2084 ## Include awk scripts and their utility functions (separated for easier debugging)
2086 [ -f "$TG_INST_CMDDIR/tg--awksome" ] && [ -r "$TG_INST_CMDDIR/tg--awksome" ] ||
2087 die "Missing awk scripts: '$TG_INST_CMDDIR/tg--awksome'"
2088 . "$TG_INST_CMDDIR/tg--awksome"
2090 if [ -n "$tg__include" ]; then
2092 # We were sourced from another script for our utility functions;
2093 # this is set by hooks. Skip the rest of the file. A simple return doesn't
2094 # work as expected in every shell. See http://bugs.debian.org/516188
2096 # ensure setup happens
2098 initial_setup 1
2099 set_topbases 1
2100 noalt_setup
2102 else
2104 set -e
2106 tgbin="$0"
2107 tgdir="${tgbin%/}"
2108 case "$tgdir" in */*);;*) tgdir="./$tgdir"; esac
2109 tgdir="${tgdir%/*}/"
2110 tgname="${tgbin##*/}"
2111 [ "$0" != "$tgname" ] || tgdir=""
2113 # If tg contains a '/' but does not start with one then replace it with an absolute path
2115 case "$0" in /*) ;; */*)
2116 tgdir="$(cd "${0%/*}" && pwd -P)/"
2117 tgbin="$tgdir$tgname"
2118 esac
2120 # tgdisplay will include any explicit -C <dir> etc. options whereas tgname will not
2121 # tgdisplayac is the same as tgdisplay but without any -r or -u options (ac => abort/continue)
2123 tgdisplaydir="$tgdir"
2124 tgdisplay="$tgbin"
2125 tgdisplayac="$tgdisplay"
2127 v_get_abs_path _tgnameabs "$(cmd_path "$tgname")" &&
2128 _tgabs="$_tgnameabs" &&
2129 { [ "$tgbin" = "$tgname" ] || v_get_abs_path _tgabs "$tgbin"; } &&
2130 [ "$_tgabs" = "$_tgnameabs" ]
2131 then
2132 tgdisplaydir=""
2133 tgdisplay="$tgname"
2134 tgdisplayac="$tgdisplay"
2136 [ -z "$_tgabs" ] || tgbin="$_tgabs"
2137 unset_ _tgabs _tgnameabs
2139 tg() (
2140 TG_TMPDIR="$tg_tmp_dir" && export TG_TMPDIR &&
2141 exec "$tgbin" "$@"
2144 explicit_remote=
2145 explicit_dir=
2146 gitcdopt=
2147 noremote=
2149 cmd=
2150 while :; do case "$1" in
2152 help|--help|-h)
2153 cmd=help
2154 shift
2155 break;;
2157 status|--status)
2158 cmd=status
2159 shift
2160 break;;
2162 --hooks-path)
2163 cmd=hooks-path
2164 shift
2165 break;;
2167 --exec-path)
2168 cmd=exec-path
2169 shift
2170 break;;
2172 --awk-path)
2173 cmd=awk-path
2174 shift
2175 break;;
2177 --top-bases)
2178 cmd=top-bases
2179 shift
2180 break;;
2182 --no-pager)
2183 GIT_PAGER_IN_USE=1 TG_PAGER_IN_USE=1 &&
2184 export GIT_PAGER_IN_USE TG_PAGER_IN_USE
2185 shift;;
2188 shift
2189 if [ -z "$1" ]; then
2190 echo "Option -r requires an argument." >&2
2191 do_help
2192 exit 1
2194 unset_ noremote
2195 base_remote="$1"
2196 explicit_remote="$base_remote"
2197 tgdisplay="$tgdisplaydir$tgname$gitcdopt -r $explicit_remote"
2198 TG_EXPLICIT_REMOTE="$base_remote" && export TG_EXPLICIT_REMOTE
2199 shift;;
2202 unset_ base_remote explicit_remote
2203 noremote=1
2204 tgdisplay="$tgdisplaydir$tgname$gitcdopt -u"
2205 TG_EXPLICIT_REMOTE= && export TG_EXPLICIT_REMOTE
2206 shift;;
2209 shift
2210 if [ -z "$1" ]; then
2211 echo "Option -C requires an argument." >&2
2212 do_help
2213 exit 1
2215 cd "$1"
2216 unset_ GIT_DIR GIT_COMMON_DIR
2217 if [ -z "$explicit_dir" ]; then
2218 explicit_dir="$1"
2219 else
2220 explicit_dir="$PWD"
2222 gitcdopt=" -C \"$explicit_dir\""
2223 [ "$explicit_dir" != "." ] || explicit_dir="." gitcdopt=" -C ."
2224 tgdisplay="$tgdisplaydir$tgname$gitcdopt"
2225 tgdisplayac="$tgdisplay"
2226 [ -z "$explicit_remote" ] || tgdisplay="$tgdisplay -r $explicit_remote"
2227 [ -z "$noremote" ] || tgdisplay="$tgdisplay -u"
2228 shift;;
2231 shift
2232 if [ -z "$1" ]; then
2233 echo "Option -c requires an argument." >&2
2234 do_help
2235 exit 1
2237 param="'$(printf '%s\n' "$1" | sed "s/[']/'\\\\''/g")'"
2238 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }$param"
2239 export GIT_CONFIG_PARAMETERS
2240 shift;;
2243 shift
2244 break;;
2247 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
2248 do_help
2249 exit 1;;
2252 break;;
2254 esac; done
2256 [ -n "$cmd" -o $# -lt 1 ] || { cmd="$1"; shift; }
2258 ## Dispatch
2260 [ -n "$cmd" ] || { do_help; exit 1; }
2262 case "$cmd" in
2264 help)
2265 do_help "$@"
2266 exit 0;;
2268 status|st)
2269 unset_ base_remote
2270 basic_setup
2271 set_topbases
2272 do_status "$@"
2273 exit ${do_status_result:-0};;
2275 hooks-path)
2276 # Internal command
2277 echol "$TG_INST_HOOKSDIR";;
2279 exec-path)
2280 # Internal command
2281 echol "$TG_INST_CMDDIR";;
2283 awk-path)
2284 # Internal command
2285 echol "$TG_INST_CMDDIR/awk";;
2287 top-bases)
2288 # Maintenance command
2289 do_topbases_help=
2290 show_remote_topbases=
2291 case "$1" in
2292 --help|-h)
2293 do_topbases_help=0;;
2294 -r|--remote)
2295 if [ $# -eq 2 ] && [ -n "$2" ]; then
2296 # unadvertised, but make it work
2297 base_remote="$2"
2298 shift
2300 show_remote_topbases=1;;
2302 [ $# -eq 0 ] || do_topbases_help=1;;
2303 esac
2304 [ $# -le 1 ] || do_topbases_help=1
2305 if [ -n "$do_topbases_help" ]; then
2306 helpcmd='echo "Usage: ${tgname:-tg} [-r <remote>] --top-bases [-r]"'
2307 [ $do_topbases_help -eq 0 ] || helpcmd="$helpcmd >&2"
2308 eval "$helpcmd"
2309 exit $do_topbases_help
2311 git_dir=
2312 ! git_dir="$(git rev-parse --git-dir 2>&1)" || setup_git_dirs
2313 set_topbases
2314 if [ -n "$show_remote_topbases" ]; then
2315 basic_setup_remote
2316 [ -n "$base_remote" ] ||
2317 die "no remote location given. Either use -r <remote> option or set topgit.remote"
2318 rbases=
2319 [ -z "$topbases_implicit_default" ] ||
2320 check_remote_topbases "$base_remote" rbases "--top-bases"
2321 if [ -n "$rbases" ]; then
2322 echol "$rbases"
2323 else
2324 echol "refs/remotes/$base_remote/${topbases#heads/}"
2326 else
2327 echol "refs/$topbases"
2328 fi;;
2331 isutil=
2332 case "$cmd" in index-merge-one-file)
2333 isutil="-"
2334 esac
2335 [ -r "$TG_INST_CMDDIR"/tg-$isutil$cmd ] || {
2336 looplevel="$TG_ALIAS_DEPTH"
2337 [ "${looplevel#[1-9]}" != "$looplevel" ] &&
2338 [ "${looplevel%%[!0-9]*}" = "$looplevel" ] ||
2339 looplevel=0
2340 tgalias="$(git config "topgit.alias.$cmd" 2>/dev/null)" || :
2341 [ -n "$tgalias" ] || {
2342 echo "Unknown subcommand: $cmd" >&2
2343 do_help
2344 exit 1
2346 looplevel=$(( $looplevel + 1 ))
2347 [ $looplevel -le 10 ] || die "topgit.alias nesting level 10 exceeded"
2348 TG_ALIAS_DEPTH="$looplevel"
2349 export TG_ALIAS_DEPTH
2350 if [ "!${tgalias#?}" = "$tgalias" ]; then
2351 unset_ GIT_PREFIX
2352 if pfx="$(git rev-parse --show-prefix 2>/dev/null)"; then
2353 GIT_PREFIX="$pfx"
2354 export GIT_PREFIX
2356 cd "./$(git rev-parse --show-cdup 2>/dev/null)"
2357 exec @SHELL_PATH@ -c "${tgalias#?} \"\$@\"" @SHELL_PATH@ "$@"
2358 else
2359 eval 'exec "$tgbin"' "$tgalias" '"$@"'
2361 die "alias execution failed for: $tgalias"
2363 unset_ TG_ALIAS_DEPTH
2365 showing_help=
2366 if [ "$*" = "-h" ] || [ "$*" = "--help" ]; then
2367 showing_help=1
2370 [ -n "$showing_help" ] || initial_setup
2371 [ -z "$noremote" ] || unset_ base_remote
2373 nomergesetup="$showing_help"
2374 case "$cmd" in base|contains|info|log|rebase|revert|summary|tag)
2375 # avoid merge setup where not necessary
2377 nomergesetup=1
2378 esac
2380 if [ -z "$nomergesetup" ]; then
2381 # make sure merging the .top* files will always behave sanely
2383 setup_ours
2384 setup_hook "pre-commit"
2387 # everything but rebase needs topbases set
2388 carefully="$showing_help"
2389 [ "$cmd" != "migrate-bases" ] || carefully=1
2390 [ "$cmd" = "rebase" ] || set_topbases $carefully
2392 _use_ref_cache=
2393 tg_read_only=1
2394 _suppress_alt=
2395 case "$cmd$showing_help" in
2396 contains|info|summary|tag)
2397 _use_ref_cache=1;;
2398 "export")
2399 _use_ref_cache=1
2400 suppress_alt=1;;
2401 annihilate|create|delete|depend|import|update)
2402 tg_read_only=
2403 suppress_alt=1;;
2404 esac
2405 [ -z "$_suppress_alt" ] || noalt_setup
2406 [ -z "$_use_ref_cache" ] || v_create_ref_cache
2408 fullcmd="${tgname:-tg} $cmd $*"
2409 . "$TG_INST_CMDDIR"/tg-$isutil$cmd;;
2410 esac