README: mention effect of tg tag --drop ...@{0}
[topgit/pro.git] / tg.sh
blobca7e68b42b773cb3cdf8a6cbe25bba6f6041c837
1 #!/bin/sh
2 # TopGit - A different patch queue manager
3 # Copyright (C) 2008 Petr Baudis <pasky@suse.cz>
4 # Copyright (C) 2014-2018 Kyle J. McKay <mackyle@gmail.com>
5 # All rights reserved.
6 # GPLv2
8 TG_VERSION="0.19.11-PRE"
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 "$@"; } >/dev/null 2>&1 || :
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 cmp() { exec_lc_all_c cmp cmp "$@"; }
77 cut() { exec_lc_all_c cut cut "$@"; }
78 find() { exec_lc_all_c find find "$@"; }
79 grep() { exec_lc_all_c grep grep "$@"; }
80 join() { exec_lc_all_c join join "$@"; }
81 paste() { exec_lc_all_c paste paste "$@"; }
82 sed() { exec_lc_all_c sed sed "$@"; }
83 sort() { exec_lc_all_c sort sort "$@"; }
84 tr() { exec_lc_all_c tr tr "$@"; }
85 wc() { exec_lc_all_c wc wc "$@"; }
86 xargs() { exec_lc_all_c xargs xargs "$@"; }
88 # Output arguments without any possible interpretation
89 # (Avoid misinterpretation of '\' characters or leading "-n", "-E" or "-e")
90 echol()
92 printf '%s\n' "$*"
95 info()
97 echol "${TG_RECURSIVE}${tgname:-tg}: $*"
100 warn()
102 info "warning: $*" >&2
105 err()
107 info "error: $*" >&2
110 fatal()
112 info "fatal: $*" >&2
115 die()
117 fatal "$@"
118 exit 1
121 # shift off first arg then return "$*" properly quoted in single-quotes
122 # if $1 was '' output goes to stdout otherwise it's assigned to $1
123 # the final \n, if any, is omitted from the result but any others are included
124 v_quotearg()
126 _quotearg_v="$1"
127 shift
128 set -- "$_quotearg_v" \
129 "sed \"s/'/'\\\\\\''/g;1s/^/'/;\\\$s/\\\$/'/;s/'''/'/g;1s/^''\\(.\\)/\\1/\"" "$*"
130 unset_ _quotearg_v
131 if [ -z "$3" ]; then
132 if [ -z "$1" ]; then
133 echo "''"
134 else
135 eval "$1=\"''\""
137 else
138 if [ -z "$1" ]; then
139 printf "%s$4" "$3" | eval "$2"
140 else
141 eval "$1="'"$(printf "%s$4" "$3" | eval "$2")"'
146 # same as v_quotearg except there's no extra $1 so output always goes to stdout
147 quotearg()
149 v_quotearg '' "$@"
152 vcmp()
154 # Compare $1 to $3 each of which must match ^[^0-9]*\d*(\.\d*)*.*$
155 # where only the "\d*" parts in the regex participate in the comparison
156 # Since EVERY string matches that regex this function is easy to use
157 # An empty string ('') for $1 or $3 or any "\d*" part is treated as 0
158 # $2 is a compare op '<', '<=', '=', '==', '!=', '>=', '>'
159 # Return code is 0 for true, 1 for false (or unknown compare op)
160 # There is NO difference in behavior between '=' and '=='
161 # Note that "vcmp 1.8 == 1.8.0.0.0.0" correctly returns 0
162 set -- "$1" "$2" "$3" "${1%%[0-9]*}" "${3%%[0-9]*}"
163 set -- "${1#"$4"}" "$2" "${3#"$5"}"
164 set -- "${1%%[!0-9.]*}" "$2" "${3%%[!0-9.]*}"
165 while
166 vcmp_a_="${1%%.*}"
167 vcmp_b_="${3%%.*}"
168 [ "z$vcmp_a_" != "z" ] || [ "z$vcmp_b_" != "z" ]
170 if [ "${vcmp_a_:-0}" -lt "${vcmp_b_:-0}" ]; then
171 unset_ vcmp_a_ vcmp_b_
172 case "$2" in "<"|"<="|"!=") return 0; esac
173 return 1
174 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
175 unset_ vcmp_a_ vcmp_b_
176 case "$2" in ">"|">="|"!=") return 0; esac
177 return 1;
179 vcmp_a_="${1#$vcmp_a_}"
180 vcmp_b_="${3#$vcmp_b_}"
181 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
182 done
183 unset_ vcmp_a_ vcmp_b_
184 case "$2" in "="|"=="|"<="|">=") return 0; esac
185 return 1
188 # true if "$1" is an existing dir and is empty except for
189 # any additional files given as extra arguments. If "$2"
190 # is the single character "." then all ".*" files will be
191 # ignored for the test (plus any further args, if any)
192 is_empty_dir() {
193 test -n "$1" && test -d "$1" || return 1
194 iedd_="$1"
195 shift
196 ieddnok_='\.?$'
197 if [ z"$1" = z"." ]; then
198 ieddnok_=
199 shift
200 while ! case "$1" in "."*) ! :; esac; do shift; done
202 if [ $# -eq 0 ]; then
203 ! \ls -a1 "$iedd_" | grep -q -E -v '^\.'"$ieddnok_"
204 else
205 # we only handle ".git" right now for efficiency
206 [ z"$*" = z".git" ] || {
207 fatal "[BUG] is_empty_dir not implemented for arguments: $*"
208 exit 70
210 ! \ls -a1 "$iedd_" | grep -q -E -v -i -e '^\.\.?$' -e '^\.git$'
214 precheck() {
215 if ! git_version="$(git version)"; then
216 die "'git version' failed"
218 case "$git_version" in [Gg]"it version "*);;*)
219 die "'git version' output does not start with 'git version '"
220 esac
222 vcmp "$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
223 die "git version >= $GIT_MINIMUM_VERSION required but found $git_version instead"
226 case "$1" in version|--version|-V)
227 echo "TopGit version $TG_VERSION"
228 exit 0
229 esac
231 [ $# -eq 1 ] && [ "$1" = "--make-empty-blob" ] || precheck
232 [ $# -ne 1 ] || [ "$1" != "precheck" ] || exit 0
234 cat_depsmsg_internal()
236 v_ref_exists_rev _rev "refs/heads/$1" || return 0
237 if [ -s "$tg_cache_dir/refs/heads/$1/.$2" ]; then
238 if read _rev_match && [ "$_rev" = "$_rev_match" ]; then
239 _line=
240 while IFS= read -r _line || [ -n "$_line" ]; do
241 printf '%s\n' "$_line"
242 done
243 return 0
244 fi <"$tg_cache_dir/refs/heads/$1/.$2"
246 [ -d "$tg_cache_dir/refs/heads/$1" ] || mkdir -p "$tg_cache_dir/refs/heads/$1" 2>/dev/null || :
247 if [ -d "$tg_cache_dir/refs/heads/$1" ]; then
248 printf '%s\n' "$_rev" >"$tg_cache_dir/refs/heads/$1/.$2"
249 _line=
250 git cat-file blob "$_rev:.$2" 2>/dev/null |
251 while IFS= read -r _line || [ -n "$_line" ]; do
252 printf '%s\n' "$_line" >&3
253 printf '%s\n' "$_line"
254 done 3>>"$tg_cache_dir/refs/heads/$1/.$2"
255 else
256 git cat-file blob "$_rev:.$2" 2>/dev/null
260 # cat_deps BRANCHNAME
261 # Caches result
262 cat_deps()
264 cat_depsmsg_internal "$1" topdeps
267 # cat_msg BRANCHNAME
268 # Caches result
269 cat_msg()
271 cat_depsmsg_internal "$1" topmsg
274 # cat_file TOPIC:PATH [FROM]
275 # cat the file PATH from branch TOPIC when FROM is empty.
276 # FROM can be -i or -w, than the file will be from the index or worktree,
277 # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
278 cat_file()
280 path="$1"
281 case "$2" in
283 cat "$root_dir/${path#*:}"
286 # ':file' means cat from index
287 git cat-file blob ":${path#*:}" 2>/dev/null
290 case "$path" in
291 refs/heads/*:.topdeps)
292 _temp="${path%:.topdeps}"
293 cat_deps "${_temp#refs/heads/}"
295 refs/heads/*:.topmsg)
296 _temp="${path%:.topmsg}"
297 cat_msg "${_temp#refs/heads/}"
300 git cat-file blob "$path" 2>/dev/null
302 esac
305 die "Wrong argument to cat_file: '$2'"
307 esac
310 # if use_alt_temp_odb and tg_use_alt_odb are true try to write the object(s)
311 # into the temporary alt odb area instead of the usual location
312 git_temp_alt_odb_cmd()
314 if [ -n "$use_alt_temp_odb" ] && [ -n "$tg_use_alt_odb" ] &&
315 [ -n "$TG_OBJECT_DIRECTORY" ] &&
316 [ -f "$TG_OBJECT_DIRECTORY/info/alternates" ]; then
318 GIT_ALTERNATE_OBJECT_DIRECTORIES="$TG_PRESERVED_ALTERNATES"
319 GIT_OBJECT_DIRECTORY="$TG_OBJECT_DIRECTORY"
320 unset_ TG_OBJECT_DIRECTORY TG_PRESERVED_ALTERNATES
321 export GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_OBJECT_DIRECTORY
322 git "$@"
324 else
325 git "$@"
329 git_write_tree() { git_temp_alt_odb_cmd write-tree "$@"; }
330 git_mktree() { git_temp_alt_odb_cmd mktree "$@"; }
332 make_mtblob() {
333 use_alt_temp_odb=1
334 tg_use_alt_odb=1
335 git_temp_alt_odb_cmd hash-object -t blob -w --stdin </dev/null >/dev/null 2>&1
337 # short-circuit this for speed
338 [ $# -ne 1 ] || [ "$1" != "--make-empty-blob" ] || { make_mtblob || :; exit 0; }
340 # get tree for the committed topic (second arg)
341 # store result in variable named by first arg
342 v_get_tree_()
344 eval "$1="'"refs/heads/$2"'
347 # get tree for the base (second arg)
348 # store result in variable named by first arg
349 v_get_tree_b()
351 eval "$1="'"refs/$topbases/$2"'
354 # get tree for the index
355 # store result in variable named by first arg
356 v_get_tree_i()
358 eval "$1="'"$(git_write_tree)"'
361 # get tree for the worktree
362 # store result in variable named by first arg
363 v_get_tree_w()
365 eval "$1="'"$(
366 i_tree="$(git_write_tree)"
367 # the file for --index-output needs to sit next to the
368 # current index file
369 cd "$root_dir"
370 : ${GIT_INDEX_FILE:="$git_dir/index"}
371 TMP_INDEX="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX")"
372 git read-tree -m "$i_tree" --index-output="$TMP_INDEX" &&
373 GIT_INDEX_FILE="$TMP_INDEX" &&
374 export GIT_INDEX_FILE &&
375 git diff --name-only -z HEAD |
376 git update-index -z --add --remove --stdin &&
377 git_write_tree &&
378 rm -f "$TMP_INDEX"
382 # get tree for arbitrary ref (second arg)
383 # store result in variable named by first arg
384 v_get_tree_r()
386 eval "$1="'"$2"'
389 # v_strip_ref answer "$(git symbolic-ref HEAD)"
390 # Output will have a leading refs/heads/ or refs/$topbases/ stripped if present
391 # store result in variable named by first arg
392 v_strip_ref()
394 case "$2" in
395 refs/"$topbases"/*)
396 eval "$1="'"${2#refs/$topbases/}"'
398 refs/heads/*)
399 eval "$1="'"${2#refs/heads/}"'
402 eval "$1="'"$2"'
403 esac
406 # v_pretty_tree answer [-t] NAME [-b | -i | -w | -r]
407 # Output tree ID of a cleaned-up tree without tg's artifacts.
408 # NAME will be ignored for -i and -w, but needs to be present
409 # With -r NAME must be a full ref name to a treeish (it's used as-is)
410 # If -t is used the tree is written into the alternate temporary objects area
411 # store result in variable named by first arg
412 v_pretty_tree()
414 _vname="$1"
415 shift
416 use_alt_temp_odb=
417 [ "$1" != "-t" ] || { shift; use_alt_temp_odb=1; }
418 eval "v_get_tree_${2#?}" _tree '"$1"'
419 eval "$_vname=\"\$(
420 git ls-tree --full-tree \"\$_tree\" |
421 sed -ne '/ \.top.*\$/!p' |
422 git_mktree
423 )\""
426 # return an empty-tree root commit -- date is either passed in or current
427 # If passed in "$*" must be epochsecs followed by optional hhmm offset (+0000 default)
428 # An invalid secs causes the current date to be used, an invalid zone offset
429 # causes +0000 to be used
430 make_empty_commit()
432 # the empty tree is guaranteed to always be there even in a repo with
433 # zero objects, but for completeness we force it to exist as a real object
434 SECS=
435 read -r SECS ZONE JUNK <<-EOT || :
438 case "$SECS" in *[!0-9]*) SECS=; esac
439 if [ -z "$SECS" ]; then
440 MTDATE="$(date '+%s %z')"
441 else
442 case "$ZONE" in
443 -[01][0-9][0-5][0-9]|+[01][0-9][0-5][0-9])
445 [01][0-9][0-5][0-9])
446 ZONE="+$ZONE"
449 ZONE="+0000"
450 esac
451 MTDATE="$SECS $ZONE"
453 EMPTYID="- <-> $MTDATE"
454 EMPTYTREE="$(git hash-object -t tree -w --stdin < /dev/null)"
455 printf '%s\n' "tree $EMPTYTREE" "author $EMPTYID" "committer $EMPTYID" '' |
456 git hash-object -t commit -w --stdin
459 # standard input is a diff
460 # standard output is the "+" lines with leading "+ " removed
461 # beware that old lines followed by the dreaded '\ No newline at end of file'
462 # will appear to be new lines if lines are added after them
463 # the git diff --ignore-space-at-eol option can be used to prevent this
464 diff_added_lines()
466 awk '
467 BEGIN { in_hunk = 0; }
468 /^@@ / { in_hunk = 1; }
469 /^\+/ { if (in_hunk == 1) printf("%s\n", substr($0, 2)); }
470 !/^\\ No newline at end of file/ &&
471 /^[^@ +-]/ { in_hunk = 0; }
475 # $1 is name of new branch to create locally if all of these are true:
476 # a) exists as a remote TopGit branch for "$base_remote"
477 # b) the branch "name" does not have any invalid characters in it
478 # c) neither of the two branch refs (branch or base) exist locally
479 # returns success only if a new local branch was created (and dumps message)
480 auto_create_local_remote()
482 case "$1" in ""|*[" $tab$lf~^:\\*?["]*|.*|*/.*|*.|*./|/*|*/|*//*) return 1; esac
483 [ -n "$base_remote" ] &&
484 git update-ref --stdin <<-EOT >/dev/null 2>&1 &&
485 verify refs/remotes/$base_remote/${topbases#heads/}/$1 refs/remotes/$base_remote/${topbases#heads/}/$1
486 verify refs/remotes/$base_remote/$1 refs/remotes/$base_remote/$1
487 create refs/$topbases/$1 refs/remotes/$base_remote/${topbases#heads/}/$1^0
488 create refs/heads/$1 refs/remotes/$base_remote/$1^0
490 { init_reflog "refs/$topbases/$1" || :; } &&
491 info "topic branch '$1' automatically set up from remote '$base_remote'"
494 is_writable_hook()
496 if [ -n "$1" ] && [ -e "$1" ] && [ ! -L "$1" ] && [ -f "$1" ] && [ -r "$1" ] && [ -w "$1" ] && [ -x "$1" ]; then
497 hook_links="$(ls -ld "$1" 2>/dev/null | awk '{print $2}')" || :
498 [ "$hook_links" != "1" ] || return 0
500 return 1
503 # setup_hook NAME
504 setup_hook()
506 setup_git_dir_is_bare
507 [ -z "$git_dir_is_bare" ] || return 0
508 setup_git_hooks_dir
509 tgname="${0##*/}"
510 hook_call="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
511 if [ -f "$git_hooks_dir/$1" ] && grep -Fq "$hook_call" "$git_hooks_dir/$1"; then
512 # Another job well done!
513 return
515 # Prepare incantation
516 hook_chain=
517 if [ -e "$git_hooks_dir/$1" ] || [ -L "$git_hooks_dir/$1" ]; then
518 hook_call="$hook_call"' || exit $?'
520 ! is_writable_hook "$git_hooks_dir/$1" ||
521 ! sed -n 1p <"$git_hooks_dir/$1" | grep -Fqx "#!@SHELL_PATH@"
522 then
523 chain_num=
524 while [ -e "$git_hooks_dir/$1-chain$chain_num" ] || [ -L "$git_hooks_dir/$1-chain$chain_num" ]; do
525 chain_num=$(( $chain_num + 1 ))
526 done
527 mv -f "$git_hooks_dir/$1" "$git_hooks_dir/$1-chain$chain_num"
528 hook_chain=1
530 else
531 hook_call="exec $hook_call"
532 [ -d "$git_hooks_dir" ] || mkdir -p "$git_hooks_dir" || :
534 # Don't call hook if tg is not installed
535 hook_call="if command -v \"$tgname\" >/dev/null 2>&1; then $hook_call; fi"
536 # Insert call into the hook
538 echol "#!@SHELL_PATH@"
539 echol "$hook_call"
540 if [ -n "$hook_chain" ]; then
541 echol "test -f \"\$0-chain$chain_num\" &&"
542 echol "test -x \"\$0-chain$chain_num\" &&"
543 echol "exec \"\$0-chain$chain_num\" \"\$@\" || :"
544 else
545 [ ! -s "$git_hooks_dir/$1" ] || cat "$git_hooks_dir/$1"
547 } >"$git_hooks_dir/$1+"
548 chmod a+x "$git_hooks_dir/$1+"
549 mv "$git_hooks_dir/$1+" "$git_hooks_dir/$1"
552 # setup_ours (no arguments)
553 setup_ours()
555 setup_git_dir_is_bare
556 [ -z "$git_dir_is_bare" ] || return 0
557 if [ ! -s "$git_common_dir/info/attributes" ] || ! grep -q topmsg "$git_common_dir/info/attributes"; then
558 [ -d "$git_common_dir/info" ] || mkdir "$git_common_dir/info"
560 echo ".topmsg merge=ours"
561 echo ".topdeps merge=ours"
562 } >>"$git_common_dir/info/attributes"
564 if ! git config merge.ours.driver >/dev/null; then
565 git config merge.ours.name '"always keep ours" merge driver'
566 git config merge.ours.driver 'touch %A'
570 # measure_branch NAME [BASE] [EXTRAHEAD...]
571 measure_branch()
573 _bname="$1"; _base="$2"
574 shift; shift
575 if [ -z "$_base" ]; then
576 v_strip_ref _base "$_bname"
577 _base="refs/$topbases/$_base"
579 # The caller should've verified $name is valid
580 _commits="$(git rev-list --count "$_bname" "$@" ^"$_base" --)"
581 _nmcommits="$(git rev-list --count --no-merges "$_bname" "$@" ^"$_base" --)"
582 if [ $_commits -ne 1 ]; then
583 _suffix="commits"
584 else
585 _suffix="commit"
587 echo "$_commits/$_nmcommits $_suffix"
590 # true if $1 is contained by (or the same as) $2
591 # this is never slower than merge-base --is-ancestor and is often slightly faster
592 contained_by()
594 [ "$(git rev-list --count --max-count=1 "$1" --not "$2" --)" = "0" ]
597 # branch_contains B1 B2
598 # Whether B1 is a superset of B2.
599 branch_contains()
601 v_ref_exists_rev _revb1 "$1" || return 0
602 v_ref_exists_rev _revb2 "$2" || return 0
603 if [ -s "$tg_cache_dir/$1/.bc/$2/.d" ]; then
604 if read _result _rev_matchb1 _rev_matchb2 &&
605 [ "$_revb1" = "$_rev_matchb1" ] && [ "$_revb2" = "$_rev_matchb2" ]; then
606 return $_result
607 fi <"$tg_cache_dir/$1/.bc/$2/.d"
609 [ -d "$tg_cache_dir/$1/.bc/$2" ] || mkdir -p "$tg_cache_dir/$1/.bc/$2" 2>/dev/null || :
610 _result=0
611 contained_by "$_revb2" "$_revb1" || _result=1
612 if [ -d "$tg_cache_dir/$1/.bc/$2" ]; then
613 echo "$_result" "$_revb1" "$_revb2" >"$tg_cache_dir/$1/.bc/$2/.d"
615 return $_result
618 create_ref_dirs()
620 [ ! -s "$tg_tmp_dir/tg~ref-dirs-created" ] && [ -s "$tg_ref_cache" ] || return 0
621 mkdir -p "$tg_tmp_dir/cached/refs"
622 awk '{x=$1; sub(/^refs\//,"",x); if (x != "") {gsub(/[^A-Za-z0-9\/_.+-]/,"\\\\&",x); print x;}}' <"$tg_ref_cache" |
624 cd "$tg_tmp_dir/cached/refs" &&
625 xargs mkdir -p
627 awk -v p="$tg_tmp_dir/cached/" '
628 NF == 2 &&
629 $1 ~ /^refs\/./ &&
630 $2 ~ /^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+$/ {
631 fn = p $1 "/.ref"
632 print "0 " $2 >fn
633 close(fn)
635 ' <"$tg_ref_cache"
636 echo 1 >"$tg_tmp_dir/tg~ref-dirs-created"
639 # If the first argument is non-empty, stores "1" there if this call created the cache
640 v_create_ref_cache()
642 [ -n "$tg_ref_cache" ] && ! [ -s "$tg_ref_cache" ] || return 0
643 _remotespec=
644 [ -z "$base_remote" ] || _remotespec="refs/remotes/$base_remote"
645 [ -z "$1" ] || eval "$1=1"
646 git for-each-ref --format='%(refname) %(objectname)' \
647 refs/heads "refs/$topbases" $_remotespec >"$tg_ref_cache"
648 create_ref_dirs
651 remove_ref_cache()
653 [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ] || return 0
654 >"$tg_ref_cache"
655 >"$tg_ref_cache_br"
656 >"$tg_ref_cache_rbr"
657 >"$tg_ref_cache_ann"
658 >"$tg_ref_cache_dep"
661 core_abbrev_val=
662 core_abbrev_is_setup=
663 v_get_core_abbrev()
665 if [ -z "$core_abbrev_is_setup" ]; then
666 core_abbrev_val="$(git config --int --get core.abbrev 2>/dev/null)" || :
667 : "${core_abbrev_val:=7}"
668 [ "$core_abbrev_val" -ge 4 ] || core_abbrev_val=4
669 core_abbrev_is_setup=1
671 eval "$1="'"$core_abbrev_val"'
674 # setting tg_ref_cache_only to non-empty will force non-$tg_ref_cache lookups to fail
675 rev_parse()
677 rev_parse_code_=1
678 if [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ]; then
679 rev_parse_code_=0
680 awk -v r="$1" 'BEGIN {e=1}; $1 == r {print $2; e=0; exit}; END {exit e}' <"$tg_ref_cache" ||
681 rev_parse_code_=$?
683 [ $rev_parse_code_ -ne 0 ] && [ -z "$tg_ref_cache_only" ] || return $rev_parse_code_
684 git rev-parse --quiet --verify "$1^0" -- 2>/dev/null
687 # v_ref_exists_rev answer REF
688 # Whether REF (second arg) is a valid ref name
689 # REF must be fully qualified and start with refs/heads/, refs/$topbases/
690 # or, if $base_remote is set, refs/remotes/$base_remote/
691 # Caches result if $tg_read_only and outputs HASH on success
692 # store result in variable named by first arg
693 v_ref_exists_rev()
695 case "$2" in
696 refs/*)
698 $octet20)
699 eval "$1="'"$2"'
700 return;;
702 die "v_ref_exists_rev requires fully-qualified ref name (given: $2)"
703 esac
704 [ -n "$tg_read_only" ] || { eval "$1="'"$(git rev-parse --quiet --verify "$2^0" -- 2>/dev/null)"'; return; }
705 _result=
706 _result_rev=
707 { read -r _result _result_rev <"$tg_tmp_dir/cached/$2/.ref"; } 2>/dev/null || :
708 [ -z "$_result" ] || { eval "$1="'"$_result_rev"'; return $_result; }
709 _result=0
710 _result_rev="$(rev_parse "$2")" || _result=$?
711 [ -d "$tg_tmp_dir/cached/$2" ] || mkdir -p "$tg_tmp_dir/cached/$2" 2>/dev/null
712 [ ! -d "$tg_tmp_dir/cached/$2" ] ||
713 echo $_result $_result_rev >"$tg_tmp_dir/cached/$2/.ref" 2>/dev/null || :
714 eval "$1="'"$_result_rev"'
715 return $_result
718 # Same as v_ref_exists_rev but output is abbreviated hash
719 # Optional third argument defaults to --short but may be any --short=.../--no-short option
720 v_ref_exists_rev_short()
722 case "$2" in
723 refs/*)
725 $octet20)
728 die "v_ref_exists_rev_short requires fully-qualified ref name (given: $2)"
729 esac
730 if [ "${3:---short}" = "--short" ]; then
731 v_get_core_abbrev _shortval
732 set -- "$1" "$2" "--short=$_shortval"
734 [ -n "$tg_read_only" ] || { eval "$1="'"$(git rev-parse --quiet --verify ${3:---short} "$2^0" -- 2>/dev/null)"'; return; }
735 _result=
736 _result_rev=
737 _result_arg=
738 { read -r _result _result_rev _result_arg <"$tg_tmp_dir/cached/$2/.rfs"; } 2>/dev/null || :
739 [ -z "$_result" ] || [ "${_result_arg:-missing}" != "${3:---short}" ] || { eval "$1="'"$_result_rev"'; return $_result; }
740 _result=0
741 _result_rev="$(rev_parse "$2")" || _result=$?
742 if [ $_result -eq 0 ]; then
743 _result_rev="$(git rev-parse --verify ${3:---short} --quiet "$_result_rev^0" --)"
744 _result=$?
746 [ -d "$tg_tmp_dir/cached/$2" ] || mkdir -p "$tg_tmp_dir/cached/$2" 2>/dev/null
747 [ ! -d "$tg_tmp_dir/cached/$2" ] ||
748 echo $_result $_result_rev "${3:---short}" >"$tg_tmp_dir/cached/$2/.rfs" 2>/dev/null || :
749 eval "$1="'"$_result_rev"'
750 return $_result
753 # ref_exists REF
754 # Whether REF is a valid ref name
755 # REF must be fully qualified and start with refs/heads/, refs/$topbases/
756 # or, if $base_remote is set, refs/remotes/$base_remote/
757 # Caches result
758 ref_exists()
760 v_ref_exists_rev _dummy "$1"
763 # v_rev_parse_tree answer REF
764 # Runs git rev-parse REF^{tree}
765 # Caches result if $tg_read_only
766 # store result in variable named by first arg
767 v_rev_parse_tree()
769 [ -n "$tg_read_only" ] || { eval "$1="'"$(git rev-parse --verify "$2^{tree}" -- 2>/dev/null)"'; return; }
770 if [ -f "$tg_tmp_dir/cached/$2/.rpt" ]; then
771 if IFS= read -r _result <"$tg_tmp_dir/cached/$2/.rpt" && [ -n "$_result" ]; then
772 eval "$1="'"$_result"'
773 return 0
775 return 1
777 [ -d "$tg_tmp_dir/cached/$2" ] || mkdir -p "$tg_tmp_dir/cached/$2" 2>/dev/null || :
778 if [ -d "$tg_tmp_dir/cached/$2" ]; then
779 git rev-parse --verify "$2^{tree}" -- >"$tg_tmp_dir/cached/$2/.rpt" 2>/dev/null || :
780 if IFS= read -r _result <"$tg_tmp_dir/cached/$2/.rpt" && [ -n "$_result" ]; then
781 eval "$1="'"$_result"'
782 return 0
784 return 1
786 eval "$1="'"$(git rev-parse --verify "$2^{tree}" -- 2>/dev/null)"'
789 # has_remote BRANCH
790 # Whether BRANCH has a remote equivalent (accepts ${topbases#heads/}/ too)
791 has_remote()
793 [ -n "$base_remote" ] && ref_exists "refs/remotes/$base_remote/$1"
796 # Return the verified TopGit branch name for "$2" in "$1" or die with an error.
797 # If -z "$1" still set return code but do not return result
798 # As a convenience, if HEAD or @ is given and HEAD is a symbolic ref to
799 # refs/heads/... then ... will be verified instead.
800 # if "$3" = "-f" (for fail) then return an error rather than dying.
801 v_verify_topgit_branch()
803 if [ "$2" = "HEAD" ] || [ "$2" = "@" ]; then
804 _verifyname="$(git symbolic-ref HEAD 2>/dev/null)" || :
805 [ -n "$_verifyname" ] || [ "$3" = "-f" ] || die "HEAD is not a symbolic ref"
806 case "$_verifyname" in refs/"$topbases"/*|refs/heads/*);;*)
807 [ "$3" != "-f" ] || return 1
808 die "HEAD is not a symbolic ref to the refs/heads namespace"
809 esac
810 set -- "$1" "$_verifyname" "$3"
812 case "$2" in
813 refs/"$topbases"/*)
814 _verifyname="${2#refs/$topbases/}"
816 refs/heads/*)
817 _verifyname="${2#refs/heads/}"
820 _verifyname="$2"
822 esac
823 if ! ref_exists "refs/heads/$_verifyname"; then
824 [ "$3" != "-f" ] || return 1
825 die "no such branch: $_verifyname"
827 if ! ref_exists "refs/$topbases/$_verifyname"; then
828 [ "$3" != "-f" ] || return 1
829 die "not a TopGit-controlled branch: $_verifyname"
831 [ -z "$1" ] || eval "$1="'"$_verifyname"'
834 # Caches result
835 # $1 = branch name (i.e. "t/foo/bar")
836 # $2 = optional result of rev-parse "refs/heads/$1"
837 # $3 = optional result of rev-parse "refs/$topbases/$1"
838 branch_annihilated()
840 _branch_name="$1"
841 _rev="$2"
842 [ -n "$_rev" ] || v_ref_exists_rev _rev "refs/heads/$_branch_name"
843 _rev_base="$3"
844 [ -n "$_rev_base" ] || v_ref_exists_rev _rev_base "refs/$topbases/$_branch_name"
846 _result=
847 _result_rev=
848 _result_rev_base=
849 { read -r _result _result_rev _result_rev_base <"$tg_cache_dir/refs/heads/$_branch_name/.ann"; } 2>/dev/null || :
850 [ -z "$_result" ] || [ "$_result_rev" != "$_rev" ] || [ "$_result_rev_base" != "$_rev_base" ] || return $_result
852 # use the merge base in case the base is ahead.
853 _mb="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)" || :
855 _result=0
856 if [ -n "$_mb" ]; then
857 v_rev_parse_tree _mbtree "$_mb"
858 v_rev_parse_tree _revtree "$_rev"
859 test "$_mbtree" = "$_revtree" || _result=1
861 [ -d "$tg_cache_dir/refs/heads/$_branch_name" ] || mkdir -p "$tg_cache_dir/refs/heads/$_branch_name" 2>/dev/null
862 [ ! -d "$tg_cache_dir/refs/heads/$_branch_name" ] ||
863 echo $_result $_rev $_rev_base >"$tg_cache_dir/refs/heads/$_branch_name/.ann" 2>/dev/null || :
864 return $_result
867 non_annihilated_branches()
869 refscacheopt="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
870 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ]; then
871 refscacheopt="$refscacheopt"'-r="$tg_ref_cache" "refs/$topbases"'
873 eval run_awk_topgit_branches -n "$refscacheopt" '"refs/$topbases" "$@"'
876 # Make sure our tree is clean
877 # if optional "$1" given also verify that a checkout to "$1" would succeed
878 ensure_clean_tree()
880 check_status
881 [ -z "$tg_state$git_state" ] || { do_status; exit 1; }
882 git update-index --ignore-submodules --refresh ||
883 die "the working directory has uncommitted changes (see above) - first commit or reset them"
884 [ -z "$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)" ] ||
885 die "the index has uncommited changes"
886 [ -z "$1" ] || git read-tree -n -u -m "$1" ||
887 die "git checkout \"$1\" would fail"
890 # Make sure .topdeps and .topmsg are "clean"
891 # They are considered "clean" if each is identical in worktree, index and HEAD
892 # With "-u" as the argument skip the HEAD check (-u => unborn)
893 # untracked .topdeps and/or .topmsg files are always considered "dirty" as well
894 # with -u them just existing constitutes "dirty"
895 ensure_clean_topfiles()
897 _dirtw=0
898 _dirti=0
899 _dirtu=0
900 _check="$(git diff-files --ignore-submodules --name-only -- :/.topdeps :/.topmsg)" &&
901 [ -z "$_check" ] || _dirtw=1
902 if [ "$1" != "-u" ]; then
903 _check="$(git diff-index --cached --ignore-submodules --name-only HEAD -- :/.topdeps :/.topmsg)" &&
904 [ -z "$_check" ] || _dirti=1
906 if [ "$_dirti$_dirtw" = "00" ]; then
907 v_get_show_cdup
908 if [ -e "${git_cdup_result}.topdeps" ] || [ -e "${git_cdup_result}.topmsg" ]; then
909 [ "$1" != "-u" ] &&
910 _check="$(git status --porcelain --ignored --untracked-files --ignore-submodules -- :/.topdeps :/.topmsg)" &&
911 [ -z "$_check" ] || _dirtu=1
914 if [ "$_dirtu$_dirti$_dirtw" != "000" ]; then
915 git status --ignored --untracked-files --ignore-submodules -- :/.topdeps :/.topmsg || :
916 case "$_dirtu$_dirti$_dirtw" in
917 001) die "the working directory has uncommitted changes (see above) - first commit or reset them";;
918 010) die "the index has uncommited changes (see above)";;
919 011) die "the working directory and index have uncommitted changes (see above) - first commit or reset them";;
920 100) die "the working directory has untracked files that would be overwritten (see above)";;
921 esac
925 # is_sha1 REF
926 # Whether REF is a SHA1 (compared to a symbolic name).
927 is_sha1()
929 case "$1" in $octet20) return 0;; esac
930 return 1
933 # navigate_deps <run_awk_topgit_navigate options and arguments>
934 # all options and arguments are passed through to run_awk_topgit_navigate
935 # except for a leading -td= option, if any, which is picked off for deps
936 # after arranging to feed it a suitable deps list
937 navigate_deps()
939 dogfer=
940 dorad=1
941 userc=
942 tmpdep=
943 ratd_opts="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
944 ratn_opts=
945 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
946 userc=1
947 tmprfs="$tg_ref_cache"
948 tmptgbr="$tg_ref_cache_br"
949 tmpann="$tg_ref_cache_ann"
950 tmpdep="$tg_ref_cache_dep"
951 [ -s "$tg_ref_cache" ] || dogfer=1
952 [ -n "$dogfer" ] || ! [ -s "$tmptgbr" ] || ! [ -f "$tmpann" ] || ! [ -s "$tmpdep" ] || dorad=
953 else
954 ratd_opts="${ratd_opts}-rmr"
955 ratn_opts="-rma -rmb"
956 tmprfs="$tg_tmp_dir/refs.$$"
957 tmpann="$tg_tmp_dir/ann.$$"
958 tmptgbr="$tg_tmp_dir/tgbr.$$"
959 dogfer=1
961 refpats="\"refs/heads\" \"refs/\$topbases\""
962 [ -z "$base_remote" ] || refpats="$refpats \"refs/remotes/\$base_remote\""
963 [ -z "$dogfer" ] ||
964 eval git for-each-ref '--format="%(refname) %(objectname)"' "$refpats" >"$tmprfs"
965 depscmd="run_awk_topgit_deps $ratd_opts"
966 case "$1" in -td=*)
967 userc=
968 depscmd="$depscmd $1"
969 shift
970 esac
971 depscmd="$depscmd"' -a="$tmpann" -b="$tmptgbr" -r="$tmprfs" -s "refs/$topbases"'
972 if [ -n "$userc" ]; then
973 if [ -n "$dorad" ]; then
974 eval "$depscmd" >"$tmpdep"
976 depscmd='<"$tmpdep" '
977 else
978 depscmd="$depscmd |"
980 eval "$depscmd" run_awk_topgit_navigate '-a="$tmpann" -b="$tmptgbr"' "$ratn_opts" '"$@"'
983 # recurse_deps_internal NAME [BRANCHPATH...]
984 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
985 # followed by a 1 if the branch is "tgish" (2 if it also has a remote); 0 if not
986 # followed by a 0 for a non-leaf, 1 for a leaf or 2 for annihilated tgish
987 # (but missing and remotes are always "0")
988 # followed by a 0 for no excess visits or a positive number of excess visits
989 # then the branch name followed by its depedency chain (which might be empty)
990 # An output line might look like this:
991 # 0 1 1 0 t/foo/leaf t/foo/int t/stage
992 # If no_remotes is non-empty, exclude remotes
993 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
994 # If with_top_level is non-empty, include the top-level that's normally omitted
995 # any branch names in the space-separated recurse_deps_exclude variable
996 # are skipped (along with their dependencies)
997 recurse_deps_internal()
999 case " $recurse_deps_exclude " in *" $1 "*) return 0; esac
1000 ratr_opts="${recurse_preorder:+-f} ${with_top_level:+-s}"
1001 dogfer=
1002 dorad=1
1003 userc=
1004 tmpdep=
1005 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
1006 userc=1
1007 tmprfs="$tg_ref_cache"
1008 tmptgbr="$tg_ref_cache_br"
1009 tmpann="$tg_ref_cache_ann"
1010 tmpdep="$tg_ref_cache_dep"
1011 [ -s "$tg_ref_cache" ] || dogfer=1
1012 [ -n "$dogfer" ] || ! [ -s "$tmptgbr" ] || ! [ -f "$tmpann" ] || ! [ -s "$tmpdep" ] || dorad=
1013 else
1014 ratr_opts="$ratr_opts -rmh -rma -rmb"
1015 tmprfs="$tg_tmp_dir/refs.$$"
1016 tmpann="$tg_tmp_dir/ann.$$"
1017 tmptgbr="$tg_tmp_dir/tgbr.$$"
1018 dogfer=1
1020 refpats="\"refs/heads\" \"refs/\$topbases\""
1021 [ -z "$base_remote" ] || refpats="$refpats \"refs/remotes/\$base_remote\""
1022 tmptgrmtbr=
1023 dorab=1
1024 if [ -z "$no_remotes" ] && [ -n "$base_remote" ]; then
1025 if [ -n "$userc" ]; then
1026 tmptgrmtbr="$tg_ref_cache_rbr"
1027 [ -n "$dogfer" ] || ! [ -s "$tmptgrmtbr" ] || dorab=
1028 else
1029 tmptgrmtbr="$tg_tmp_dir/tgrmtbr.$$"
1030 ratr_opts="$ratr_opts -rmr"
1032 ratr_opts="$ratr_opts -r=\"\$tmptgrmtbr\" -u=\":refs/remotes/\$base_remote/\${topbases#heads/}\""
1034 [ -z "$dogfer" ] ||
1035 eval git for-each-ref '--format="%(refname) %(objectname)"' "$refpats" >"$tmprfs"
1036 if [ -n "$tmptgrmtbr" ] && [ -n "$dorab" ]; then
1037 run_awk_topgit_branches -n -h="refs/remotes/$base_remote" -r="$tmprfs" \
1038 "refs/remotes/$base_remote/${topbases#heads/}" >"$tmptgrmtbr"
1040 depscmd="run_awk_topgit_deps -s${TG_DEBUG:+ -p=\"\$tg_ref_cache.pre\"}"
1041 depscmd="$depscmd"' -a="$tmpann" -b="$tmptgbr" -r="$tmprfs" "refs/$topbases"'
1042 if [ -n "$userc" ]; then
1043 if [ -n "$dorad" ]; then
1044 eval "$depscmd" >"$tmpdep"
1046 depscmd='<"$tmpdep" '
1047 else
1048 depscmd="$depscmd |"
1050 eval "$depscmd" run_awk_topgit_recurse '-a="$tmpann" -b="$tmptgbr"' \
1051 '-c=1 -h="$tmprfs"' "$ratr_opts" '-x="$recurse_deps_exclude"' '"$@"'
1054 # do_eval CMD
1055 # helper for recurse_deps so that a return statement executed inside CMD
1056 # does not return from recurse_deps. This shouldn't be necessary, but it
1057 # seems that it actually is.
1058 do_eval()
1060 eval "$@"
1063 # becomes read-only for caching purposes
1064 # assigns new value to tg_read_only
1065 # become_cacheable/undo_become_cacheable calls may be nested
1066 become_cacheable()
1068 _old_tg_read_only="$tg_read_only"
1069 if [ -z "$tg_read_only" ]; then
1070 ! [ -e "$tg_tmp_dir/cached" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created" ] ||
1071 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
1072 tg_read_only=1
1074 _my_ref_cache=
1075 v_create_ref_cache _my_ref_cache
1076 _my_ref_cache="${_my_ref_cache:+1}"
1077 tg_read_only="undo${_my_ref_cache:-0}-$_old_tg_read_only"
1080 # restores tg_read_only and ref_cache to state before become_cacheable call
1081 # become_cacheable/undo_bocome_cacheable calls may be nested
1082 undo_become_cacheable()
1084 case "$tg_read_only" in
1085 "undo"[01]"-"*)
1086 _suffix="${tg_read_only#undo?-}"
1087 [ "${tg_read_only%$_suffix}" = "undo0-" ] || remove_ref_cache
1088 tg_read_only="$_suffix"
1089 esac
1092 # just call this, no undo, sets tg_read_only= and removes ref cache and cached results
1093 become_non_cacheable()
1095 remove_ref_cache
1096 tg_read_only=
1097 ! [ -e "$tg_tmp_dir/cached" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created" ] ||
1098 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
1101 # call this to make sure the current Git repository has an associated work tree
1102 # also make sure we are not in wayback mode
1103 ensure_work_tree()
1105 [ -z "$wayback" ] ||
1106 die "the wayback machine cannot be used with the specified options"
1107 setup_git_dir_is_bare
1108 [ -n "$git_dir_is_bare" ] || return 0
1109 die "This operation must be run in a work tree"
1112 # call this to make sure Git will not complain about a missing user/email
1113 # result is cached in TG_IDENT_CHECKED and a non-empty value suppresses the check
1114 ensure_ident_available()
1116 [ -z "$TG_IDENT_CHECKED" ] || return 0
1117 git var GIT_AUTHOR_IDENT >/dev/null &&
1118 git var GIT_COMMITTER_IDENT >/dev/null || exit
1119 TG_IDENT_CHECKED=1
1120 export TG_IDENT_CHECKED
1121 return 0
1124 # recurse_deps [-o=<options string>] CMD NAME [BRANCHPATH...]
1125 # Recursively eval CMD on all dependencies of NAME.
1126 # Dependencies are visited in topological order.
1127 # If <options string> is given, it's eval'd into the recurse_deps_internal
1128 # call just before the "--" that's passed just before NAME
1129 # CMD can refer to the following variables:
1131 # _ret starts as 0; CMD can change; will be final return result
1132 # _dep bare branch name or ":refs/remotes/..." for a remote
1133 # _name has $_dep in its .topdeps ("" for top and $with_top_level)
1134 # _depchain 0+ space-sep branch names (_name first) form a path to top
1135 # _dep_missing boolean "1" if no such $_dep ref; "" if ref present
1136 # _dep_is_leaf boolean "1" if leaf; "" if not
1137 # _dep_is_tgish boolean "1" if tgish; "" if not (which implies no remote)
1138 # _dep_has_remote boolean "1" if $_dep has_remote; "" if not
1139 # _dep_annihilated boolean "1" if $_dep annihilated; "" if not
1140 # _dep_xvisits non-negative integer number of excess visits (often 0)
1142 # CMD may use a "return" statement without issue; its return value is ignored,
1143 # but if CMD sets _ret to a negative value, e.g. "-0" or "-1" the enumeration
1144 # will stop immediately and the value with the leading "-" stripped off will
1145 # be the final result code
1147 # CMD can refer to $_name for queried branch name,
1148 # $_dep for dependency name,
1149 # $_depchain for space-seperated branch backtrace,
1150 # $_dep_missing boolean to check whether $_dep is present
1151 # and the $_dep_is_tgish and $_dep_annihilated booleans.
1152 # If recurse_preorder is NOT set then the $_dep_is_leaf boolean is also valid.
1153 # It can modify $_ret to affect the return value
1154 # of the whole function.
1155 # If recurse_deps() hits missing dependencies, it will append
1156 # them to space-separated $missing_deps list and skip them
1157 # after calling CMD with _dep_missing set.
1158 # remote dependencies are processed if no_remotes is unset.
1159 # any branch names in the space-separated recurse_deps_exclude variable
1160 # are skipped (along with their dependencies)
1162 # If no_remotes is non-empty, exclude remotes
1163 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
1164 # If with_top_level is non-empty, include the top-level that's normally omitted
1165 # any branch names in the space-separated recurse_deps_exclude variable
1166 # are skipped (along with their dependencies)
1167 recurse_deps()
1169 _opts=
1170 case "$1" in -o=*) _opts="${1#-o=}"; shift; esac
1171 _cmd="$1"; shift
1173 _depsfile="$(get_temp tg-depsfile)"
1174 eval recurse_deps_internal "$_opts" -- '"$@"' >"$_depsfile" || :
1176 _ret=0
1177 while read _ismissing _istgish _isleaf _dep_xvisits _dep _name _deppath; do
1178 _depchain="$_name${_deppath:+ $_deppath}"
1179 _dep_is_tgish=
1180 [ "$_istgish" = "0" ] || _dep_is_tgish=1
1181 _dep_has_remote=
1182 [ "$_istgish" != "2" ] || _dep_has_remote=1
1183 _dep_missing=
1184 if [ "$_ismissing" != "0" ]; then
1185 _dep_missing=1
1186 case " $missing_deps " in *" $_dep "*);;*)
1187 missing_deps="${missing_deps:+$missing_deps }$_dep"
1188 esac
1190 _dep_annihilated=
1191 _dep_is_leaf=
1192 if [ "$_isleaf" = "1" ]; then
1193 _dep_is_leaf=1
1194 elif [ "$_isleaf" = "2" ]; then
1195 _dep_annihilated=1
1197 do_eval "$_cmd" || :
1198 if [ "${_ret#-}" != "$_ret" ]; then
1199 _ret="${_ret#-}"
1200 break
1202 done <"$_depsfile"
1203 rm -f "$_depsfile"
1204 return ${_ret:-0}
1207 # find_leaves NAME
1208 # output (one per line) the unique leaves of NAME
1209 # a leaf is either
1210 # 1) a non-tgish dependency
1211 # 2) the base of a tgish dependency with no non-annihilated dependencies
1212 # duplicates are suppressed (by commit rev) and remotes are always ignored
1213 # if a leaf has an exact tag match that will be output
1214 # note that recurse_deps_exclude IS honored for this operation
1215 find_leaves()
1217 no_remotes=1
1218 with_top_level=1
1219 recurse_preorder=
1220 seen_leaf_refs=
1221 seen_leaf_revs=
1222 while read _ismissing _istgish _isleaf _xvsts _dep _name _deppath; do
1223 [ "$_isleaf" = "1" ] && [ "$_ismissing" = "0" ] || continue
1224 if [ "$_istgish" != "0" ]; then
1225 fulldep="refs/$topbases/$_dep"
1226 else
1227 fulldep="refs/heads/$_dep"
1229 case " $seen_leaf_refs " in *" $fulldep "*);;*)
1230 seen_leaf_refs="${seen_leaf_refs:+$seen_leaf_refs }$fulldep"
1231 if v_ref_exists_rev fullrev "$fulldep"; then
1232 case " $seen_leaf_revs " in *" $fullrev "*);;*)
1233 seen_leaf_revs="${seen_leaf_revs:+$seen_leaf_revs }$fullrev"
1234 # See if Git knows it by another name
1235 if tagname="$(git describe --exact-match "$fullrev" 2>/dev/null)" && [ -n "$tagname" ]; then
1236 echo "refs/tags/$tagname"
1237 else
1238 echo "$fulldep"
1240 esac
1242 esac
1243 done <<-EOT
1244 $(recurse_deps_internal -l -o=1 -- "$1")
1246 with_top_level=
1249 # branch_needs_update
1250 # This is a helper function for determining whether given branch
1251 # is up-to-date wrt. its dependencies. It expects input as if it
1252 # is called as a recurse_deps() helper.
1253 # In case the branch does need update, it will echo it together
1254 # with the branch backtrace on the output (see needs_update()
1255 # description for details) and set $_ret to non-zero.
1256 branch_needs_update()
1258 if [ -n "$_dep_missing" ]; then
1259 echo "! $_dep $_depchain"
1260 return 0
1263 if [ -n "$_dep_is_tgish" ]; then
1264 [ -z "$_dep_annihilated" ] || return 0
1266 if [ -n "$_dep_has_remote" ]; then
1267 branch_contains "refs/heads/$_dep" "refs/remotes/$base_remote/$_dep" || {
1268 echo ":refs/remotes/$base_remote/$_dep $_dep $_depchain"
1269 _ret=1
1272 # We want to sync with our base first and should output this before
1273 # the remote branch, but the order does not actually matter to tg-update
1274 # as it just recurses regardless, but it does matter for tg-info (which
1275 # treats out-of-date bases as though they were already merged in) so
1276 # we output the remote before the base.
1277 branch_contains "refs/heads/$_dep" "refs/$topbases/$_dep" || {
1278 echo ": $_dep $_depchain"
1279 _ret=1
1280 return
1284 if [ -n "$_name" ]; then
1285 case "$_dep" in :*) _fulldep="${_dep#:}";; *) _fulldep="refs/heads/$_dep";; esac
1286 if ! branch_contains "refs/$topbases/$_name" "$_fulldep"; then
1287 # Some new commits in _dep
1288 echo "$_dep $_depchain"
1289 _ret=1
1294 # needs_update NAME
1295 # This function is recursive; it outputs reverse path from NAME
1296 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
1297 # inner paths first. Innermost name can be :refs/remotes/<remote>/<name>
1298 # if the head is not in sync with the <remote> branch <name>, ':' if
1299 # the head is not in sync with the base (in this order of priority)
1300 # or '!' if dependency is missing. Note that the remote branch, base
1301 # order is reversed from the order they will actually be updated in
1302 # order to accomodate tg info which treats out-of-date items that are
1303 # only in the base as already being in the head for status purposes.
1304 # It will also return non-zero status if NAME needs update (seems backwards
1305 # but think of it as non-zero status if any non-missing output lines produced)
1306 # If needs_update() hits missing dependencies, it will append
1307 # them to space-separated $missing_deps list and skip them.
1308 needs_update()
1310 recurse_deps branch_needs_update "$1"
1313 # append second arg to first arg variable gluing with space if first already set
1314 vplus()
1316 eval "$1=\"\${$1:+\$$1 }\$2\""
1319 # true if whitespace separated first var name list contains second arg
1320 # use `vcontains 3 "value" "some list"` for a literal list
1321 vcontains()
1323 eval case "\" \${$1} \"" in '*" $2 "*) return 0; esac; return 1'
1326 # if the $1 var does not already contain $2 it's appended
1327 vsetadd()
1329 vcontains "$1" "$2" || vplus "$1" "$2"
1332 # reset needs_update_check results to empty
1333 needs_update_check_clear()
1335 unset_ needs_update_processed needs_update_behind needs_update_ahead needs_update_partial
1338 # needs_update_check NAME...
1340 # A faster version of needs_update that always succeeds
1341 # No output and unsuitable for actually performing updates themselves
1342 # If any of NAME... are NOT up-to-date AND they were not already processed
1343 # return status always will be zero however a simple check of
1344 # needs_update_behind after the call will answer the:
1345 # "are any out of date?": test -n "$needs_update_behind"
1346 # "is <x> out of date?": vcontains needs_update_behind "<x>"
1348 # Note that results are cumulative and "no_remotes" is honored as well as other
1349 # variables that modify recurse_deps_internal behavior. See the preceding
1350 # function to reset the results to empty when accumulation should start over.
1352 # Unlike needs_update, the branch names are themselves also checked to see if
1353 # they are out-of-date with respect to their bases or remote branches (not just
1354 # their remote bases). However, this can muddy some status results so this
1355 # can be disabled by setting needs_update_check_no_self to a non-empty value.
1357 # Unlike needs_update, here the remote base check is handled together with the
1358 # remote head check so if one is modified the other is too in the same way.
1360 # Dependencies are normally considered "behind" if they need an update from
1361 # their base or remote but this can be suppressed by setting the
1362 # needs_update_check_no_same to a non-empty value. This will NOT prevent
1363 # parents of those dependencies from still being considered behind in such a
1364 # case even though the dependency itself will not be. Note that setting
1365 # needs_update_check_no_same also implies needs_update_check_no_self.
1367 # The following whitespace-separated lists are updated with the results:
1369 # The "no_remotes" setting is obeyed but remote names themselves will never
1370 # appear in any of the lists
1372 # needs_update_processed
1373 # The branch names in here have been processed and will be skipped
1375 # needs_update_behind
1376 # Any branch named in here needs an update from one or more of its
1377 # direct or indirect dependencies (i.e. it's "out-of-date")
1379 # needs_update_ahead
1380 # Any branch named in here is NOT fully contained by at least one of
1381 # its dependents (i.e. it's a source of "out-of-date (aka dirty)"ness
1383 # needs_update_partial
1384 # Any branch names in here are either missing themselves or have one
1385 # or more detected missing dependencies (a completely missing remote
1386 # branch is never "detected")
1387 needs_update_check()
1389 # each head must be processed independently or else there will be
1390 # confusion about who's missing what and which branches actually are
1391 # out of date
1392 tmptgrdi="$tg_tmp_dir/tgrdi.$$"
1393 for nucname in "$@"; do
1394 ! vcontains needs_update_processed "$nucname" || continue
1395 # no need to fuss with recurse_deps, just use
1396 # recurse_deps_internal directly
1397 recurse_deps_internal -s -o=-1 "$nucname" >"$tmptgrdi"
1398 while read -r _rdi_m _rdi_t _rdi_l _rdi_v _rdi_node _rdi_parent _rdi_chain; do
1399 case "$_rdi_node" in ""|:*) continue; esac # empty or checked with remote
1400 vsetadd needs_update_processed "$_rdi_node"
1401 if [ "$_rdi_m" != "0" ]; then # missing
1402 vsetadd needs_update_partial "$_rdi_node"
1403 [ -z "$_rdi_parent" ] || vsetadd needs_update_partial "$_rdi_parent"
1404 continue
1406 [ "$_rdi_t$_rdi_l" != "12" ] || continue # always skip annihilated
1407 _rdi_dertee= # :)
1408 if [ -n "$_rdi_parent" ]; then # not a "self" line
1409 ! vcontains needs_update_partial "$_rdi_node" || vsetadd needs_update_partial "$_rdi_parent"
1410 ! vcontains needs_update_behind "$_rdi_node" || _rdi_dertee=2
1411 else
1412 [ -z "$needs_update_check_no_self$needs_update_check_no_same" ] || continue # skip self
1414 if [ -z "$_rdi_dertee" ]; then
1415 if [ "$_rdi_t" != "0" ]; then # tgish
1416 if branch_contains "refs/heads/$_rdi_node" "refs/$topbases/$_rdi_node"; then
1417 if [ "$_rdi_t" = "2" ]; then # will never be "2" when no_remotes is set
1418 branch_contains "refs/heads/$_rdi_node" "refs/remotes/$base_remote/$_rdi_node" &&
1419 branch_contains "refs/$topbases/$_rdi_node" "refs/remotes/$base_remote/${topbases#heads/}/$_rdi_node" ||
1420 _rdi_dertee=3
1422 else
1423 _rdi_dertee=3
1425 [ -z "$_rdi_dertee" ] || [ -n "$needs_update_check_no_same" ] || _rdi_dertee=1
1428 [ z"$_rdi_dertee" != z"1" ] || vsetadd needs_update_behind "$_rdi_node"
1429 [ -n "$_rdi_parent" ] || continue # self line
1430 if ! branch_contains "refs/$topbases/$_rdi_parent" "refs/heads/$_rdi_node"; then
1431 _rdi_dertee=1
1432 vsetadd needs_update_ahead "$_rdi_node"
1434 [ -z "$_rdi_dertee" ] || vsetadd needs_update_behind "$_rdi_parent"
1435 done <"$tmptgrdi"
1436 done
1439 # branch_empty NAME [-i | -w]
1440 branch_empty()
1442 if [ -z "$2" ]; then
1443 v_ref_exists_rev _rev "refs/heads/$1" || return 0
1444 _result=
1445 _result_rev=
1446 { read -r _result _result_rev <"$tg_cache_dir/refs/heads/$1/.mt"; } 2>/dev/null || :
1447 [ -z "$_result" ] || [ "$_result_rev" != "$_rev" ] || return $_result
1448 _result=0
1449 v_pretty_tree _pretty1 -t "$1" -b
1450 v_pretty_tree _pretty2 -t "$1" $2
1451 [ "$_pretty1" = "$_pretty2" ] || _result=$?
1452 [ -d "$tg_cache_dir/refs/heads/$1" ] || mkdir -p "$tg_cache_dir/refs/heads/$1" 2>/dev/null
1453 [ ! -d "$tg_cache_dir/refs/heads/$1" ] || echo $_result $_rev >"$tg_cache_dir/refs/heads/$1/.mt"
1454 return $_result
1455 else
1456 v_pretty_tree _pretty1 -t "$1" -b
1457 v_pretty_tree _pretty2 -t "$1" $2
1458 [ "$_pretty1" = "$_pretty2" ]
1462 v_get_tdmopt_internal()
1464 [ -n "$1" ] && [ -n "$3" ] || return 0
1465 [ "$2" = "-i" ] || [ "$2" = "-w" ] || return 0
1466 ensure_work_tree
1467 _optval=
1468 if v_verify_topgit_branch _tghead "HEAD" -f; then
1469 if [ "$2" = "-w" ] && [ -f "$root_dir/$3" ] && [ -r "$root_dir/$3" ]; then
1470 _opthash=
1471 if _opthash="$(git hash-object -w -t blob --stdin <"$root_dir/$3")" && [ -n "$_opthash" ]; then
1472 _optval="$4\"$_tghead:$_opthash\""
1474 elif [ "$2" = "-i" ]; then
1475 if _opthash="$(git rev-parse --quiet --verify ":0:$3" --)" && [ -n "$_opthash" ]; then
1476 _optval="$4\"$_tghead:$_opthash\""
1480 eval "$1="'"$_optval"'
1483 # set var $1 to the correct -td= option for use in an eval for $2 -i or -w mode
1484 v_get_tdopt() { v_get_tdmopt_internal "$1" "$2" ".topdeps" "-td="; }
1486 # set var $1 to the correct -tm= option for use in an eval for $2 -i or -w mode
1487 v_get_tmopt() { v_get_tdmopt_internal "$1" "$2" ".topmsg" "-tm="; }
1489 # checkout_symref_full [-f] FULLREF [SEED]
1490 # Just like git checkout $iowopt -b FULLREF [SEED] except that FULLREF MUST start with
1491 # refs/ and HEAD is ALWAYS set to a symref to it and [SEED] (default is FULLREF)
1492 # MUST be a committish which if present will be used instead of current FULLREF
1493 # (and FULLREF will be updated to it as well in that case)
1494 # Any merge state is always cleared by this function
1495 # With -f it's like git checkout $iowopt -f -b FULLREF (uses read-tree --reset
1496 # instead of -m) but it will clear out any unmerged entries
1497 # As an extension, FULLREF may also be a full hash to create a detached HEAD instead
1498 checkout_symref_full()
1500 _mode=-m
1501 _head="HEAD"
1502 if [ "$1" = "-f" ]; then
1503 _mode="--reset"
1504 _head=
1505 shift
1507 _ishash=
1508 case "$1" in
1509 refs/?*)
1511 $octet20)
1512 _ishash=1
1513 [ -z "$2" ] || [ "$1" = "$2" ] ||
1514 die "programmer error: invalid checkout_symref_full \"$1\" \"$2\""
1515 set -- HEAD "$1"
1518 die "programmer error: invalid checkout_symref_full \"$1\""
1520 esac
1521 _seedrev="$(git rev-parse --quiet --verify "${2:-$1}^0" --)" ||
1522 die "invalid committish: \"${2:-$1}\""
1523 # Clear out any MERGE_HEAD kruft
1524 rm -f "$git_dir/MERGE_HEAD" || :
1525 # We have to do all the hard work ourselves :/
1526 # This is like git checkout -b "$1" "$2"
1527 # (or just git checkout "$1"),
1528 # but never creates a detached HEAD (unless $1 is a hash)
1529 git read-tree -u $_mode $_head "$_seedrev" &&
1531 [ -z "$2" ] && [ "$(git cat-file -t "$1")" = "commit" ] ||
1532 git update-ref ${_ishash:+--no-deref} "$1" "$_seedrev"
1533 } && {
1534 [ -n "$_ishash" ] || git symbolic-ref HEAD "$1"
1538 # switch_to_base NAME [SEED]
1539 switch_to_base()
1541 checkout_symref_full "refs/$topbases/$1" "$2"
1544 # run editor with arguments
1545 # the editor setting will be cached in $tg_editor (which is eval'd)
1546 # result non-zero if editor fails or GIT_EDITOR cannot be determined
1547 # just in case, noalt_setup will be in effect while the editor is running
1548 run_editor()
1550 tg_editor="$GIT_EDITOR"
1551 [ -n "$tg_editor" ] || tg_editor="$(git var GIT_EDITOR)" || return $?
1553 noalt_setup
1554 eval "$tg_editor" '"$@"'
1558 # Show the help messages.
1559 do_help()
1561 _www=
1562 if [ "$1" = "-w" ]; then
1563 _www=1
1564 shift
1566 if [ "$1" = "st" ]; then
1567 shift
1568 set -- "status" "$@"
1570 if [ -z "$1" ] ; then
1571 # This is currently invoked in all kinds of circumstances,
1572 # including when the user made a usage error. Should we end up
1573 # providing more than a short help message, then we should
1574 # differentiate.
1575 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
1577 ## Build available commands list for help output
1579 cmds=
1580 sep=
1581 for cmd in "$TG_INST_CMDDIR"/tg-[!-]*; do
1582 ! [ -r "$cmd" ] && continue
1583 # strip directory part and "tg-" prefix
1584 cmd="${cmd##*/}"
1585 cmd="${cmd#tg-}"
1586 [ "$cmd" != "migrate-bases" ] || continue
1587 [ "$cmd" != "summary" ] || cmd="st[atus]|$cmd"
1588 cmds="$cmds$sep$cmd"
1589 sep="|"
1590 done
1592 echo "TopGit version $TG_VERSION - A different patch queue manager"
1593 echo "Usage: $tgname [-C <dir>] [-r <remote> | -u]" \
1594 "[-c <name>=<val>] [--[no-]pager|-p] [-w [:]<tgtag>] ($cmds) ..."
1595 echo " Or: $tgname help [-w] [<command>]"
1596 echo "Use \"$tgdisplaydir$tgname help tg\" for overview of TopGit"
1597 elif [ -r "$TG_INST_CMDDIR"/tg-$1 ] || [ -r "$TG_INST_SHAREDIR/tg-$1.txt" ] ; then
1598 if [ -n "$_www" ]; then
1599 nohtml=
1600 if ! [ -r "$TG_INST_SHAREDIR/topgit.html" ]; then
1601 echo "${0##*/}: missing html help file:" \
1602 "$TG_INST_SHAREDIR/topgit.html" 1>&2
1603 nohtml=1
1605 if ! [ -r "$TG_INST_SHAREDIR/tg-$1.html" ]; then
1606 echo "${0##*/}: missing html help file:" \
1607 "$TG_INST_SHAREDIR/tg-$1.html" 1>&2
1608 nohtml=1
1610 if [ -n "$nohtml" ]; then
1611 echo "${0##*/}: use" \
1612 "\"${0##*/} help $1\" instead" 1>&2
1613 exit 1
1615 git web--browse -c help.browser "$TG_INST_SHAREDIR/tg-$1.html"
1616 exit
1618 output()
1620 if [ -r "$TG_INST_CMDDIR"/tg-$1 ] ; then
1621 "$TG_INST_CMDDIR"/tg-$1 -h 2>&1 || :
1622 echo
1623 elif [ "$1" = "help" ]; then
1624 echo "Usage: ${tgname:-tg} help [-w] [<command>]"
1625 echo
1626 elif [ "$1" = "status" ] || [ "$1" = "st" ]; then
1627 echo "Usage: ${tgname:-tg} @tgsthelpusage@"
1628 echo
1630 if [ -r "$TG_INST_SHAREDIR/tg-$1.txt" ] ; then
1631 cat "$TG_INST_SHAREDIR/tg-$1.txt"
1634 page output "$1"
1635 else
1636 echo "${0##*/}: no help for $1" 1>&2
1637 do_help
1638 exit 1
1642 check_status()
1644 git_state=
1645 git_remove=
1646 tg_state=
1647 tg_remove=
1648 tg_topmerge=
1649 setup_git_dir_is_bare
1650 [ -z "$git_dir_is_bare" ] || return 0
1652 if [ -e "$git_dir/MERGE_HEAD" ]; then
1653 git_state="merge"
1654 elif [ -e "$git_dir/rebase-apply/applying" ]; then
1655 git_state="am"
1656 git_remove="$git_dir/rebase-apply"
1657 elif [ -e "$git_dir/rebase-apply" ]; then
1658 git_state="rebase"
1659 git_remove="$git_dir/rebase-apply"
1660 elif [ -e "$git_dir/rebase-merge" ]; then
1661 git_state="rebase"
1662 git_remove="$git_dir/rebase-merge"
1663 elif [ -e "$git_dir/CHERRY_PICK_HEAD" ]; then
1664 git_state="cherry-pick"
1665 elif [ -e "$git_dir/BISECT_LOG" ]; then
1666 git_state="bisect"
1667 elif [ -e "$git_dir/REVERT_HEAD" ]; then
1668 git_state="revert"
1670 git_remove="${git_remove#./}"
1672 if [ -e "$git_dir/tg-update" ]; then
1673 tg_state="update"
1674 tg_remove="$git_dir/tg-update"
1675 ! [ -s "$git_dir/tg-update/merging_topfiles" ] || tg_topmerge=1
1677 tg_remove="${tg_remove#./}"
1680 # Show status information
1681 do_status()
1683 do_status_result=0
1684 do_status_verbose=
1685 do_status_help=
1686 abbrev=refs
1687 pfx=
1688 while [ $# -gt 0 ] && case "$1" in
1689 --help|-h)
1690 do_status_help=1
1691 break;;
1692 -vv)
1693 # kludge in this common bundling option
1694 abbrev=
1695 do_status_verbose=1
1696 pfx="## "
1698 --verbose|-v)
1699 [ -z "$do_status_verbose" ] || abbrev=
1700 do_status_verbose=1
1701 pfx="## "
1703 --exit-code)
1704 do_status_result=2
1707 die "unknown status argument: $1"
1709 esac; do shift; done
1710 if [ -n "$do_status_help" ]; then
1711 echo "Usage: ${tgname:-tg} @tgsthelpusage@"
1712 return
1714 check_status
1715 symref="$(git symbolic-ref --quiet HEAD)" || :
1716 headrv="$(git rev-parse --quiet --verify ${abbrev:+--short} HEAD --)" || :
1717 if [ -n "$symref" ]; then
1718 uprefpart=
1719 if [ -n "$headrv" ]; then
1720 upref="$(git rev-parse --symbolic-full-name @{upstream} 2>/dev/null)" || :
1721 if [ -n "$upref" ]; then
1722 uprefpart=" ... ${upref#$abbrev/remotes/}"
1723 mbase="$(git merge-base HEAD "$upref")" || :
1724 ahead="$(git rev-list --count HEAD ${mbase:+--not} $mbase)" || ahead=0
1725 behind="$(git rev-list --count "$upref" ${mbase:+--not} $mbase)" || behind=0
1726 [ "$ahead$behind" = "00" ] || uprefpart="$uprefpart ["
1727 [ "$ahead" = "0" ] || uprefpart="${uprefpart}ahead $ahead"
1728 [ "$ahead" = "0" ] || [ "$behind" = "0" ] || uprefpart="$uprefpart, "
1729 [ "$behind" = "0" ] || uprefpart="${uprefpart}behind $behind"
1730 [ "$ahead$behind" = "00" ] || uprefpart="$uprefpart]"
1733 echol "${pfx}HEAD -> ${symref#$abbrev/heads/} [${headrv:-unborn}]$uprefpart"
1734 else
1735 echol "${pfx}HEAD -> ${headrv:-?}"
1737 if [ -n "$tg_state" ]; then
1738 extra=
1739 if [ "$tg_state" = "update" ]; then
1740 IFS= read -r uname <"$git_dir/tg-update/name" || :
1741 [ -z "$uname" ] ||
1742 extra="; currently updating branch '$uname'"
1744 echol "${pfx}tg $tg_state in progress$extra"
1745 if [ -s "$git_dir/tg-update/fullcmd" ] && [ -s "$git_dir/tg-update/names" ]; then
1746 printf "${pfx}You are currently updating as a result of:\n${pfx} "
1747 cat "$git_dir/tg-update/fullcmd"
1748 bcnt="$(( $(wc -w < "$git_dir/tg-update/names") ))"
1749 if [ $bcnt -gt 1 ]; then
1750 pcnt=0
1751 ! [ -s "$git_dir/tg-update/processed" ] ||
1752 pcnt="$(( $(wc -w < "$git_dir/tg-update/processed") ))"
1753 echo "${pfx}$pcnt of $bcnt branches updated so far"
1756 if [ "$tg_state" = "update" ]; then
1757 echol "${pfx} (use \"$tgdisplayac update --continue\" to continue)"
1758 echol "${pfx} (use \"$tgdisplayac update --skip\" to skip this branch and continue)"
1759 echol "${pfx} (use \"$tgdisplayac update --stop\" to stop and retain changes so far)"
1760 echol "${pfx} (use \"$tgdisplayac update --abort\" to restore pre-update state)"
1763 [ -z "$git_state" ] || echo "${pfx}git $git_state in progress"
1764 if [ "$git_state" = "merge" ]; then
1765 ucnt="$(( $(git ls-files --unmerged --full-name --abbrev :/ | wc -l) ))"
1766 if [ $ucnt -gt 0 ]; then
1767 echo "${pfx}"'fix conflicts and then "git commit" the result'
1768 else
1769 echo "${pfx}"'all conflicts fixed; run "git commit" to record result'
1772 if [ -z "$git_state" ]; then
1773 setup_git_dir_is_bare
1774 [ -z "$git_dir_is_bare" ] || return 0
1775 gsp="$(git status --porcelain 2>/dev/null)" || return 0 # bare repository???
1776 gspcnt=0
1777 [ -z "$gsp" ] ||
1778 gspcnt="$(( $(printf '%s\n' "$gsp" | sed -n '/^??/!p' | wc -l) ))"
1779 untr=
1780 if [ "$gspcnt" -eq 0 ]; then
1781 [ -z "$gsp" ] || untr="; non-ignored, untracked files present"
1782 echo "${pfx}working directory is clean$untr"
1783 [ -n "$tg_state" ] || do_status_result=0
1784 else
1785 echo "${pfx}working directory is DIRTY"
1786 [ -z "$do_status_verbose" ] || git status --short --untracked-files=no
1791 ## Pager stuff
1793 # isatty FD
1794 isatty()
1796 test -t $1
1799 # pass "diff" to get pager.diff
1800 # if pager.$1 is a boolean false returns cat
1801 # if set to true or unset fails
1802 # otherwise succeeds and returns the value
1803 get_pager()
1805 if _x="$(git config --bool "pager.$1" 2>/dev/null)"; then
1806 [ "$_x" != "true" ] || return 1
1807 echo "cat"
1808 return 0
1810 if _x="$(git config "pager.$1" 2>/dev/null)"; then
1811 echol "$_x"
1812 return 0
1814 return 1
1817 # setup_pager
1818 # Set TG_PAGER to a valid executable
1819 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
1820 # See also the following "page" function for ease of use
1821 # emptypager will be set to 1 (otherwise empty) if TG_PAGER was set to "cat" to not be empty
1822 # Preference is (same as Git):
1823 # 1. GIT_PAGER
1824 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
1825 # 3. core.pager (only if set)
1826 # 4. PAGER
1827 # 5. git var GIT_PAGER
1828 # 6. less
1829 setup_pager()
1831 isatty 1 || { emptypager=1; TG_PAGER=cat; return 0; }
1833 emptypager=
1834 if [ -z "$TG_PAGER_IN_USE" ]; then
1835 # TG_PAGER = GIT_PAGER | PAGER | less
1836 # NOTE: GIT_PAGER='' is significant
1837 if [ -n "${GIT_PAGER+set}" ]; then
1838 TG_PAGER="$GIT_PAGER"
1839 elif [ -n "$USE_PAGER_TYPE" ] && _dp="$(get_pager "$USE_PAGER_TYPE")"; then
1840 TG_PAGER="$_dp"
1841 elif _cp="$(git config core.pager 2>/dev/null)"; then
1842 TG_PAGER="$_cp"
1843 elif [ -n "${PAGER+set}" ]; then
1844 TG_PAGER="$PAGER"
1845 else
1846 _gp="$(git var GIT_PAGER 2>/dev/null)" || :
1847 [ "$_gp" != ":" ] || _gp=
1848 TG_PAGER="${_gp:-less}"
1850 if [ -z "$TG_PAGER" ]; then
1851 emptypager=1
1852 TG_PAGER=cat
1854 else
1855 emptypager=1
1856 TG_PAGER=cat
1859 # Set pager default environment variables
1860 # see pager.c:setup_pager
1861 if [ -z "${LESS+set}" ]; then
1862 LESS="-FRX"
1863 export LESS
1865 if [ -z "${LV+set}" ]; then
1866 LV="-c"
1867 export LV
1870 # this is needed so e.g. $(git diff) will still colorize it's output if
1871 # requested in ~/.gitconfig with color.diff=auto
1872 GIT_PAGER_IN_USE=1
1873 export GIT_PAGER_IN_USE
1875 # this is needed so we don't get nested pagers
1876 TG_PAGER_IN_USE=1
1877 export TG_PAGER_IN_USE
1880 # page eval_arg [arg ...]
1882 # Calls setup_pager then evals the first argument passing it all the rest
1883 # where the output is piped through eval "$TG_PAGER" unless emptypager is set
1884 # by setup_pager (in which case the output is left as-is).
1886 # To handle arbitrary paging duties, collect lines to be paged into a
1887 # function and then call page with the function name or perhaps func_name "$@".
1889 # If no arguments at all are passed in do nothing (return with success).
1890 page()
1892 [ $# -gt 0 ] || return 0
1893 setup_pager
1894 _evalarg="$1"; shift
1895 if [ -n "$emptypager" ]; then
1896 eval "$_evalarg" '"$@"'
1897 else
1898 { eval "$_evalarg" '"$@"';} | eval "$TG_PAGER"
1902 # get_temp NAME [-d]
1903 # creates a new temporary file (or directory with -d) in the global
1904 # temporary directory $tg_tmp_dir with pattern prefix NAME
1905 get_temp()
1907 mktemp $2 "$tg_tmp_dir/$1.XXXXXX"
1910 # automatically called by strftime
1911 # does nothing if already setup
1912 # may be called explicitly if the first call would otherwise be in a subshell
1913 # so that the setup is only done once before subshells start being spawned
1914 setup_strftime()
1916 [ -z "$strftime_is_setup" ] || return 0
1918 # date option to format raw epoch seconds values
1919 daterawopt=
1920 _testes='951807788'
1921 _testdt='2000-02-29 07:03:08 UTC'
1922 _testfm='%Y-%m-%d %H:%M:%S %Z'
1923 if [ "$(TZ=UTC date "-d@$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1924 daterawopt='-d@'
1925 elif [ "$(TZ=UTC date "-r$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1926 daterawopt='-r'
1928 strftime_is_setup=1
1931 # $1 => strftime format string to use
1932 # $2 => raw timestamp as seconds since epoch
1933 # $3 => optional time zone string (empty/absent for local time zone)
1934 strftime()
1936 setup_strftime
1937 if [ -n "$daterawopt" ]; then
1938 if [ -n "$3" ]; then
1939 TZ="$3" date "$daterawopt$2" "+$1"
1940 else
1941 date "$daterawopt$2" "+$1"
1943 else
1944 if [ -n "$3" ]; then
1945 TZ="$3" perl -MPOSIX=strftime -le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1946 else
1947 perl -MPOSIX=strftime -le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1952 got_cdup_result=
1953 git_cdup_result=
1954 v_get_show_cdup()
1956 if [ -z "$got_cdup_result" ]; then
1957 git_cdup_result="$(git rev-parse --show-cdup)"
1958 got_cdup_result=1
1960 [ -z "$1" ] || eval "$1="'"$git_cdup_result"'
1963 git_dir_is_bare_setup=
1964 setup_git_dir_is_bare()
1966 if [ -z "$git_dir_is_bare_setup" ]; then
1967 git_dir_is_bare="$(git rev-parse --is-bare-repository)"
1968 [ z"$git_dir_is_bare" = z"true" ] || git_dir_is_bare=
1969 git_dir_is_bare_setup=1
1973 git_hooks_pat_list="\
1974 [a]pplypatch-ms[g] [p]re-applypatc[h] [p]ost-applypatc[h] [p]re-commi[t] \
1975 [p]repare-commit-ms[g] [c]ommit-ms[g] [p]ost-commi[t] [p]re-rebas[e] \
1976 [p]ost-checkou[t] [p]ost-merg[e] [p]re-pus[h] [p]re-receiv[e] [u]pdat[e] \
1977 [p]ost-receiv[e] [p]ost-updat[e] [p]ush-to-checkou[t] [p]re-auto-g[c] \
1978 [p]ost-rewrit[e]"
1980 # git_hooks_dir must already be set to the value of core.hooksPath which
1981 # exists and is an absolute path. The first and only argument is the
1982 # "pwd -P" of the $git_hooks_dir directory. If the core.hooksPath setting
1983 # appears to be "friendly" attempt to alter it to be an absolute path to
1984 # "$git_common_dir/hooks" instead. A "friendly" core.hooksPath setting
1985 # points to a directory for which "$git_common_dir/hooks" already has
1986 # entries which are symbolic links to the same core.hooksPath items.
1987 # There's no POSIX readlink utility, but there is a 'cmp -s' utility so we
1988 # use that instead to check. Also the "friendly" core.hooksPath must be
1989 # something that's recognizable as belonging to a "friendly".
1990 maybe_adjust_friendly_hooks_path()
1992 case "$1" in */_global/hooks);;*) return 0; esac
1993 [ -n "$1" ] && [ -d "$1" ] && [ -d "$git_common_dir/hooks" ] || return 0
1994 [ -w "$git_common_dir" ] || return 0
1995 ! [ -e "$git_common_dir/config" ] || {
1996 [ -f "$git_common_dir/config" ] && [ -w "$git_common_dir/config" ]
1997 } || return 0
1998 oktoswitch=1
1999 for ghook in $(cd "$1" && eval "echo $git_hooks_pat_list"); do
2000 case "$ghook" in "["*) continue; esac
2001 [ -x "$1/$ghook" ] &&
2002 [ -f "$1/$ghook" ] || continue
2003 [ -x "$git_common_dir/hooks/$ghook" ] &&
2004 [ -f "$git_common_dir/hooks/$ghook" ] &&
2005 cmp -s "$1/$ghook" "$git_common_dir/hooks/$ghook" || {
2006 oktoswitch=
2007 break
2009 done
2010 if [ -n "$oktoswitch" ]; then
2011 # a known "friendly" was detected and the hooks match;
2012 # go ahead and silently switch the path
2013 ! git config core.hooksPath "$git_common_dir/hooks" >/dev/null 2>&1 ||
2014 git_hooks_dir="$git_common_dir/hooks"
2016 unset_ oktoswitch
2017 return 0
2020 git_hooks_dir=
2021 setup_git_hooks_dir()
2023 [ -z "$git_hooks_dir" ] || return 0
2024 git_hooks_dir="$git_common_dir/hooks"
2025 if vcmp "$git_version" '>=' "2.9" && gchp="$(git config --path --get core.hooksPath 2>/dev/null)" && [ -n "$gchp" ]; then
2026 case "$gchp" in
2027 /[!/]*)
2028 if [ -d "$gchp" ]; then
2029 # if core.hooksPath is just another name for
2030 # $git_common_dir/hooks, keep referring to it
2031 # by $git_common_dir/hooks
2032 abscdh="$(cd "$git_common_dir" && pwd -P)/hooks"
2033 abshpd="$(cd "$gchp" && pwd -P)"
2034 if [ "$abshpd" != "$abscdh" ]; then
2035 git_hooks_dir="$gchp"
2036 maybe_adjust_friendly_hooks_path "$abshpd"
2038 unset_ abscdh abshpd
2039 else
2040 [ -n "$1" ] || warn "ignoring non-existent core.hooksPath: $gchp"
2044 [ -n "$1" ] || warn "ignoring non-absolute core.hooksPath: $gchp"
2046 esac
2047 unset_ gchp
2051 setup_git_dirs()
2053 [ -n "$git_dir" ] || git_dir="$(git rev-parse --git-dir)"
2054 if [ -n "$git_dir" ] && [ -d "$git_dir" ]; then
2055 git_dir="$(cd "$git_dir" && pwd)"
2057 if [ -z "$git_common_dir" ]; then
2058 if vcmp "$git_version" '>=' "2.5"; then
2059 # rev-parse --git-common-dir is broken and may give
2060 # an incorrect result unless the current directory is
2061 # already set to the top level directory
2062 v_get_show_cdup
2063 git_common_dir="$(cd "./$git_cdup_result" && cd "$(git rev-parse --git-common-dir)" && pwd)"
2064 else
2065 git_common_dir="$git_dir"
2068 [ -n "$git_dir" ] && [ -n "$git_common_dir" ] &&
2069 [ -d "$git_dir" ] && [ -d "$git_common_dir" ] || die "Not a git repository"
2072 basic_setup_remote()
2074 if [ -z "$base_remote" ]; then
2075 if [ "${TG_EXPLICIT_REMOTE+set}" = "set" ]; then
2076 base_remote="$TG_EXPLICIT_REMOTE"
2077 else
2078 base_remote="$(git config topgit.remote 2>/dev/null)" || :
2083 basic_setup()
2085 setup_git_dirs $1
2086 basic_setup_remote
2087 tgsequester="$(git config --bool topgit.sequester 2>/dev/null)" || :
2088 tgnosequester=
2089 [ "$tgsequester" != "false" ] || tgnosequester=1
2090 unset_ tgsequester
2092 # catch errors if topbases is used without being set
2093 unset_ tg_topbases_set
2094 topbases="programmer*:error"
2095 topbasesrx="programmer*:error}"
2096 oldbases="$topbases"
2099 tmpdir_cleanup()
2101 test -z "$tg_tmp_dir" || ! test -d "$tg_tmp_dir" || ${TG_DEBUG:+echo} rm -rf "$tg_tmp_dir" >&2 || :
2104 tmpdir_setup()
2106 [ -z "$tg_tmp_dir" ] || return 0
2107 if [ -n "$TG_TMPDIR" ] && [ -d "$TG_TMPDIR" ] && [ -w "$TG_TMPDIR" ] &&
2108 { >"$TG_TMPDIR/.check"; } >/dev/null 2>&1; then
2109 tg_tmp_dir="$TG_TMPDIR"
2110 else
2111 tg_tmp_dir=
2112 TRAPEXIT_='tmpdir_cleanup'
2113 trap 'trapexit_ 129' HUP
2114 trap 'trapexit_ 130' INT
2115 trap 'trapexit_ 131' QUIT
2116 trap 'trapexit_ 134' ABRT
2117 trap 'trapexit_ 141' PIPE
2118 trap 'trapexit_ 143' TERM
2119 tg_tmp_dir="$(mktemp -d "$git_dir/tg-tmp.XXXXXX" 2>/dev/null)" || tg_tmp_dir=
2120 [ -n "$tg_tmp_dir" ] || tg_tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/tg-tmp.XXXXXX" 2>/dev/null)" || tg_tmp_dir=
2121 [ -n "$tg_tmp_dir" ] || [ -z "$TMPDIR" ] || tg_tmp_dir="$(mktemp -d "/tmp/tg-tmp.XXXXXX" 2>/dev/null)" || tg_tmp_dir=
2122 [ -z "$tg_tmp_dir" ] || tg_tmp_dir="$(cd "$tg_tmp_dir" && pwd -P)"
2124 [ -n "$tg_tmp_dir" ] && [ -w "$tg_tmp_dir" ] && { >"$tg_tmp_dir/.check"; } >/dev/null 2>&1 ||
2125 die "could not create a writable temporary directory"
2127 # whenever tg_tmp_dir is != "" these must always be set
2128 tg_ref_cache="$tg_tmp_dir/tg~ref-cache"
2129 tg_ref_cache_br="$tg_ref_cache.br"
2130 tg_ref_cache_rbr="$tg_ref_cache.rbr"
2131 tg_ref_cache_ann="$tg_ref_cache.ann"
2132 tg_ref_cache_dep="$tg_ref_cache.dep"
2135 cachedir_setup()
2137 [ -z "$tg_cache_dir" ] || return 0
2138 user_id_no="$(id -u)" || :
2139 : "${user_id_no:=_99_}"
2140 tg_cache_dir="$git_common_dir/tg-cache"
2141 [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir" >/dev/null 2>&1 || tg_cache_dir=
2142 [ -z "$tg_cache_dir" ] || tg_cache_dir="$tg_cache_dir/$user_id_no"
2143 [ -z "$tg_cache_dir" ] || [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir" >/dev/null 2>&1 || tg_cache_dir=
2144 [ -z "$tg_cache_dir" ] || { >"$tg_cache_dir/.tgcache"; } >/dev/null 2>&1 || tg_cache_dir=
2145 if [ -z "$tg_cache_dir" ]; then
2146 tg_cache_dir="$tg_tmp_dir/tg-cache"
2147 [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir" >/dev/null 2>&1 || tg_cache_dir=
2148 [ -z "$tg_cache_dir" ] || { >"$tg_cache_dir/.tgcache"; } >/dev/null 2>&1 || tg_cache_dir=
2150 [ -n "$tg_cache_dir" ] ||
2151 die "could not create a writable tg-cache directory (even a temporary one)"
2153 if [ -n "$2" ]; then
2154 # allow the wayback machine to share a separate cache
2155 [ -d "$tg_cache_dir/wayback" ] || mkdir "$tg_cache_dir/wayback" >/dev/null 2>&1 || :
2156 ! [ -d "$tg_cache_dir/wayback" ] || ! { >"$tg_cache_dir/wayback/.tgcache"; } >/dev/null 2>&1 ||
2157 tg_cache_dir="$tg_cache_dir/wayback"
2161 # set up alternate deb dirs
2162 altodb_setup()
2164 # GIT_ALTERNATE_OBJECT_DIRECTORIES can contain double-quoted entries
2165 # since Git v2.11.1; however, it's only necessary for : (or perhaps ;)
2166 # so we avoid it if possible and require v2.11.1 to do it at all
2167 # otherwise just don't make an alternates temporary store in that case;
2168 # it's okay to not have one; everything will still work; the nicety of
2169 # making the temporary tree objects vanish when tg exits just won't
2170 # happen in that case but nothing will break also be sure to reuse
2171 # the parent's if we've been recursively invoked and it's for the
2172 # same repository we were invoked on
2174 tg_use_alt_odb=1
2175 _fullodbdir=
2176 _odbdir="${GIT_OBJECT_DIRECTORY:-$git_common_dir/objects}"
2177 [ -n "$_odbdir" ] && [ -d "$_odbdir" ] && _fullodbdir="$(cd "$_odbdir" && pwd -P)" ||
2178 die "could not find objects directory"
2179 if [ -n "$TG_OBJECT_DIRECTORY" ] && [ -d "$TG_OBJECT_DIRECTORY/info" ] &&
2180 [ -f "$TG_OBJECT_DIRECTORY/info/alternates" ] && [ -r "$TG_OBJECT_DIRECTORY/info/alternates" ]; then
2181 if IFS= read -r _otherodbdir <"$TG_OBJECT_DIRECTORY/info/alternates" &&
2182 [ -n "$_otherodbdir" ] && [ "$_otherodbdir" = "$_fullodbdir" ]; then
2183 tg_use_alt_odb=2
2186 _fulltmpdir="$(cd "$tg_tmp_dir" && pwd -P)"
2187 if [ "$tg_use_alt_odb" = "1" ]; then
2188 # create an alternate objects database to keep the ephemeral objects in
2189 mkdir -p "$tg_tmp_dir/objects/info"
2190 TG_OBJECT_DIRECTORY="$_fulltmpdir/objects"
2191 [ "$_fullodbdir" = "$TG_OBJECT_DIRECTORY" ] ||
2192 echol "$_fullodbdir" >"$tg_tmp_dir/objects/info/alternates"
2194 case "$_fulltmpdir" in *[";:"]*|'"'*) vcmp "$git_version" '>=' "2.11.1" || tg_use_alt_odb=; esac
2195 if [ "$tg_use_alt_odb" = "1" ]; then
2196 case "$TG_OBJECT_DIRECTORY" in
2197 *[";:"]*|'"'*)
2198 # surround in "..." and backslash-escape internal '"' and '\\'
2199 _altodbdq="\"$(printf '%s\n' "$TG_OBJECT_DIRECTORY" |
2200 sed 's/\([""\\]\)/\\\1/g')\""
2203 _altodbdq="$TG_OBJECT_DIRECTORY"
2205 esac
2206 TG_PRESERVED_ALTERNATES="$GIT_ALTERNATE_OBJECT_DIRECTORIES"
2207 if [ -n "$GIT_ALTERNATE_OBJECT_DIRECTORIES" ]; then
2208 GIT_ALTERNATE_OBJECT_DIRECTORIES="$_altodbdq:$GIT_ALTERNATE_OBJECT_DIRECTORIES"
2209 else
2210 GIT_ALTERNATE_OBJECT_DIRECTORIES="$_altodbdq"
2212 export TG_PRESERVED_ALTERNATES TG_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES
2213 if [ -n "$GIT_OBJECT_DIRECTORY" ]; then
2214 export GIT_OBJECT_DIRECTORY
2215 else
2216 unset_ GIT_OBJECT_DIRECTORY
2221 noalt_setup()
2223 if [ "${TG_PRESERVED_ALTERNATES+set}" = "set" ]; then
2224 GIT_ALTERNATE_OBJECT_DIRECTORIES="$TG_PRESERVED_ALTERNATES"
2225 if [ -n "$GIT_ALTERNATE_OBJECT_DIRECTORIES" ]; then
2226 export GIT_ALTERNATE_OBJECT_DIRECTORIES
2227 else
2228 unset_ GIT_ALTERNATE_OBJECT_DIRECTORIES
2231 unset_ TG_TMPDIR TG_OBJECT_DIRECTORY TG_PRESERVED_ALTERNATES tg_use_alt_odb
2234 ## Initial setup
2235 initial_setup()
2237 # suppress the merge log editor feature since git 1.7.10
2239 GIT_MERGE_AUTOEDIT=no
2240 export GIT_MERGE_AUTOEDIT
2242 basic_setup $1
2243 iowopt=
2244 ! vcmp "$git_version" '>=' "2.5" || iowopt="--ignore-other-worktrees"
2245 gcfbopt=
2246 ! vcmp "$git_version" '>=' "2.6" || gcfbopt="--buffer"
2247 auhopt=
2248 ! vcmp "$git_version" '>=' "2.9" || auhopt="--allow-unrelated-histories"
2249 v_get_show_cdup root_dir
2250 root_dir="${root_dir:-.}"
2251 logrefupdates="$(git config --bool core.logallrefupdates 2>/dev/null)" || :
2252 [ "$logrefupdates" = "true" ] || logrefupdates=
2254 # make sure root_dir doesn't end with a trailing slash.
2256 root_dir="${root_dir%/}"
2258 # create global temporary and cache directories, usually inside GIT_DIR
2260 tmpdir_setup
2261 unset_ TG_TMPDIR
2262 cachedir_setup
2264 # the wayback machine directory serves as its own "altodb"
2265 [ -n "$wayback" ] || altodb_setup
2268 activate_wayback_machine()
2270 [ -n "${1#:}" ] || [ -n "$2" ] || { wayback=; return 0; }
2271 setup_git_dirs
2272 tmpdir_setup
2273 altodb_setup
2274 tgwbr=
2275 tgwbr2=
2276 if [ -n "${1#:}" ]; then
2277 tgwbr="$(get_temp wbinfo)"
2278 tgwbr2="${tgwbr}2"
2279 tg revert --list --no-short "${1#:}" >"$tgwbr" && test -s "$tgwbr" || return 1
2280 # tg revert will likely leave a revert-tag-only cache which is not what we want
2281 remove_ref_cache
2283 cachedir_setup "" 1 # use a separate wayback cache dir
2284 # but don't step on the normal one if the separate one could not be set up
2285 case "$tg_cache_dir" in */wayback);;*) tg_cache_dir=; esac
2286 altodb="$TG_OBJECT_DIRECTORY"
2287 if [ -n "$3" ] && [ -n "$2" ]; then
2288 [ -d "$3" ] || { mkdir -p "$3" && [ -d "$3" ]; } ||
2289 die "could not create wayback directory: $3"
2290 tg_wayback_dir="$(cd "$3" && pwd -P)" || die "could not get wayback directory full path"
2291 [ -d "$tg_wayback_dir/.git" ] || { mkdir -p "$tg_wayback_dir/.git" && [ -d "$tg_wayback_dir/.git" ]; } ||
2292 die "could not initialize wayback directory: $3"
2293 is_empty_dir "$tg_wayback_dir" ".git" && is_empty_dir "$tg_wayback_dir/.git" "." ||
2294 die "wayback directory is not empty: $3"
2295 mkdir "$tg_wayback_dir/.git/objects"
2296 mkdir "$tg_wayback_dir/.git/objects/info"
2297 cat "$altodb/info/alternates" >"$tg_wayback_dir/.git/objects/info/alternates"
2298 else
2299 tg_wayback_dir="$tg_tmp_dir/wayback"
2300 mkdir "$tg_wayback_dir"
2301 mkdir "$tg_wayback_dir/.git"
2302 ln -s "$altodb" "$tg_wayback_dir/.git/objects"
2304 mkdir "$tg_wayback_dir/.git/refs"
2305 printf '0 Wayback Machine' >"$tg_wayback_dir/.git/gc.pid"
2306 qpesc="$(printf '%s\n' "$git_common_dir" | sed -e 's/\([\\""]\)/\\\1/g' -e '$!s/$/\\n/' | tr -d '\n')"
2307 laru="false"
2308 [ -z "$2" ] || laru="true"
2309 printf '%s' "\
2310 [include]
2311 path = \"$qpesc/config\"
2312 [core]
2313 bare = false
2314 logAllRefUpdates = $laru
2315 repositoryFormatVersion = 0
2316 [extensions]
2317 preciousObjects = true
2318 [gc]
2319 auto = 0
2320 autoDetach = false
2321 autoPackLimit = 0
2322 packRefs = false
2323 [remote \"wayback\"]
2324 url = \"$qpesc\"
2325 [push]
2326 default = nothing
2327 followTags = true
2328 [alias]
2329 wayback-updates = fetch -u --force --no-tags --dry-run wayback refs/*:refs/*
2330 " >"$tg_wayback_dir/.git/config"
2331 cat "$git_dir/HEAD" >"$tg_wayback_dir/.git/HEAD"
2332 case "$1" in ":"?*);;*)
2333 git show-ref >"$tg_wayback_dir/.git/packed-refs"
2334 git --git-dir="$tg_wayback_dir/.git" pack-refs --all
2335 esac
2336 noalt_setup
2337 TG_OBJECT_DIRECTORY="$altodb" && export TG_OBJECT_DIRECTORY
2338 if [ -n "${1#:}" ]; then
2339 <"$tgwbr" sed 's/^\([^ ][^ ]*\) \([^ ][^ ]*\)$/update \2 \1/' |
2340 git --git-dir="$tg_wayback_dir/.git" update-ref -m "wayback to $1" ${2:+--create-reflog} --stdin
2342 if test -n "$2"; then
2343 # extra setup for potential shell
2344 qpesc2="$(printf '%s\n' "$git_common_dir" | sed -e 's/\([\\""]\)/\\\\\1/g' -e '$!s/$/\\n/' | tr -d '\n')"
2345 printf '\twayback-repository = "!printf '\''%%s\\\\n'\'' \\"%s\\""\n' "$qpesc2" >>"$tg_wayback_dir/.git/config"
2346 qtesc="$(printf '%s\n' "${1:-:}" | sed 's/\([""]\)/\\\1/g')"
2347 printf '\twayback-tag = "!printf '\''%%s\\\\n'\'' \\"%s\\""\n' "$qtesc" >>"$tg_wayback_dir/.git/config"
2348 if [ -d "$git_common_dir/rr-cache" ]; then
2349 ln -s "$git_common_dir/rr-cache" "$tg_wayback_dir/.git/rr-cache"
2350 printf "[rerere]\n\tenabled = true\n" >>"$tg_wayback_dir/.git/config"
2352 if [ z"$2" != z"2" ]; then
2353 wbauth=
2354 wbprnt=
2355 if [ -n "${1#:}" ]; then
2356 [ -n "$tgwbr2" ] || tgwbr2="$(get_temp wbtag)"
2357 git --git-dir="$git_common_dir" cat-file tag "${1#:}" >"$tgwbr2" || return 1
2358 wbprnt="${lf}parent $(git --git-dir="$git_common_dir" rev-parse --verify --quiet "${1#:}"^0 -- 2>/dev/null)" || wbprnt=
2359 wbauth="$(<"$tgwbr2" awk '{if(!$0)exit;if($1=="tagger")print "author" substr($0,7)}')"
2361 wbcmtr="committer Wayback Machine <-> $(date "+%s %z")"
2362 [ -n "$wbauth" ] || wbauth="author${wbcmtr#committer}"
2363 wbtree="$(git --git-dir="$tg_wayback_dir/.git" mktree </dev/null)"
2364 wbcmt="$({
2365 printf '%s\n' "tree $wbtree$wbprnt" "$wbauth" "$wbcmtr" ""
2366 if [ -n "$tgwbr2" ]; then
2367 <"$tgwbr2" sed -e '1,/^$/d' -e '/^-----BEGIN/,$d' | git stripspace
2368 else
2369 echo "Wayback Machine"
2371 } | git --git-dir="$tg_wayback_dir/.git" hash-object -t commit -w --stdin)"
2372 test -n "$wbcmt" || return 1
2373 echo "$wbcmt" >"$tg_wayback_dir/.git/HEAD"
2376 cd "$tg_wayback_dir"
2377 unset git_dir git_common_dir
2380 set_topbases()
2382 # refer to "top-bases" in a refname with $topbases
2384 [ -z "$tg_topbases_set" ] || return 0
2386 topbases_implicit_default=1
2387 # See if topgit.top-bases is set to heads or refs
2388 tgtb="$(git config "topgit.top-bases" 2>/dev/null)" || :
2389 if [ -n "$tgtb" ] && [ "$tgtb" != "heads" ] && [ "$tgtb" != "refs" ]; then
2390 if [ -n "$1" ]; then
2391 # never die on the hook script
2392 unset_ tgtb
2393 else
2394 die "invalid \"topgit.top-bases\" setting (must be \"heads\" or \"refs\")"
2397 if [ -n "$tgtb" ]; then
2398 case "$tgtb" in
2399 heads)
2400 topbases="heads/{top-bases}"
2401 topbasesrx="heads/[{]top-bases[}]"
2402 oldbases="top-bases";;
2403 refs)
2404 topbases="top-bases"
2405 topbasesrx="top-bases"
2406 oldbases="heads/{top-bases}";;
2407 esac
2408 # MUST NOT be exported
2409 unset_ tgtb tg_topbases_set topbases_implicit_default
2410 tg_topbases_set=1
2411 return 0
2413 unset_ tgtb
2415 # check heads and top-bases and see what state the current
2416 # repository is in. remotes are ignored.
2418 rc=0 activebases=
2419 activebases="$(
2420 git for-each-ref --format='%(refname)' "refs/heads" "refs/top-bases" 2>/dev/null |
2421 run_awk_ref_prefixes ${1:+-e} -n -- "refs/heads/{top-bases}" "refs/top-bases" "refs/heads")" ||
2422 rc=$?
2423 if [ "$rc" = "65" ]; then
2424 # Complain and die
2425 err "repository contains existing TopGit branches"
2426 err "but some use refs/top-bases/... for the base"
2427 err "and some use refs/heads/{top-bases}/... for the base"
2428 err "with the latter being the new, preferred location"
2429 err "set \"topgit.top-bases\" to either \"heads\" to use"
2430 err "the new heads/{top-bases} location or \"refs\" to use"
2431 err "the old top-bases location."
2432 err "(the tg migrate-bases command can also resolve this issue)"
2433 die "schizophrenic repository requires topgit.top-bases setting"
2435 [ -z "$activebases" ] || unset_ topbases_implicit_default
2436 if [ "$activebases" = "refs/heads/{top-bases}" ]; then
2437 topbases="heads/{top-bases}"
2438 topbasesrx="heads/[{]top-bases[}]"
2439 oldbases="top-bases"
2440 else
2441 # default is still top-bases for now
2442 topbases="top-bases"
2443 topbasesrx="top-bases"
2444 oldbases="heads/{top-bases}"
2446 # MUST NOT be exported
2447 unset_ rc activebases tg_topases_set
2448 tg_topbases_set=1
2449 return 0
2452 # $1 is remote name to check
2453 # $2 is optional variable name to set to result of check
2454 # $3 is optional command name to use in message (defaults to $cmd)
2455 # Fatal error if remote has schizophrenic top-bases
2456 # No error (and $2, if provided, will be set to empty) if remote has no top-bases at all
2457 check_remote_topbases()
2459 [ -n "$1" ] || die "programmer error: check_remote_topbases called with no remote argument"
2460 _crrc=0 _crremotebases=
2461 _crremotebases="$(
2462 git for-each-ref --format='%(refname)' "refs/remotes/$1" 2>/dev/null |
2463 run_awk_ref_prefixes -n -- "refs/remotes/$1/{top-bases}" "refs/remotes/$1/top-bases" "refs/remotes/$1")" ||
2464 _crrc=$?
2465 if [ "$_crrc" = "65" ]; then
2466 err "remote \"$1\" has top-bases in both locations:"
2467 err " refs/remotes/$1/{top-bases}/..."
2468 err " refs/remotes/$1/top-bases/..."
2469 err "set \"topgit.top-bases\" to \"heads\" for the first, preferred location"
2470 err "or set \"topgit.top-bases\" to \"refs\" for the second, old location"
2471 err "(the \"-c topgit.top-bases=<val>\" option can be used for this)"
2472 err "then re-run the tg ${3:-$cmd} command"
2473 err "(the tg migrate-bases command can also help with this problem)"
2474 die "schizophrenic remote \"$1\" requires topgit.top-bases setting"
2476 [ "$_crrc" != "66" ] || _crremotebases= # just to be sure
2477 [ -z "$2" ] || eval "$2="'"$_crremotebases"'
2478 unset _crrc _crremotebases
2479 return 0
2482 # init_reflog "ref"
2483 # if "$logrefupdates" is set and ref is not under refs/heads/ then force
2484 # an empty log file to exist so that ref changes will be logged
2485 # "$1" must be a fully-qualified refname (i.e. start with "refs/")
2486 # However, if "$1" is "refs/tgstash" then always make the reflog
2487 # The only ref not under refs/ that Git will write a reflog for is HEAD;
2488 # no matter what, it will NOT update a reflog for any other bare refs so
2489 # just quietly succeed when passed TG_STASH without doing anything.
2490 init_reflog()
2492 [ -n "$1" ] && [ "$1" != "TG_STASH" ] || return 0
2493 [ -n "$logrefupdates" ] || [ "$1" = "refs/tgstash" ] || return 0
2494 case "$1" in refs/heads/*|HEAD) return 0;; refs/*[!/]);; *) return 1; esac
2495 mkdir -p "$git_common_dir/logs/${1%/*}" 2>/dev/null || :
2496 { >>"$git_common_dir/logs/$1" || :; } 2>/dev/null
2499 # store the "realpath" for "$2" in "$1" except the leaf is not resolved if it's
2500 # a symbolic link. The directory part must exist, but the basename need not.
2501 v_get_abs_path()
2503 [ -n "$1" ] && [ -n "$2" ] || return 1
2504 set -- "$1" "$2" "${2%/}"
2505 case "$3" in
2506 */*) set -- "$1" "$2" "${3%/*}";;
2507 * ) set -- "$1" "$2" ".";;
2508 esac
2509 case "$2" in */)
2510 set -- "$1" "${2%/}" "$3" "/"
2511 esac
2512 [ -d "$3" ] || return 1
2513 eval "$1="'"$(cd "$3" && pwd -P)/${2##*/}$4"'
2516 ## Startup
2518 : "${TG_INST_CMDDIR:=@cmddir@}"
2519 : "${TG_INST_SHAREDIR:=@sharedir@}"
2520 : "${TG_INST_HOOKSDIR:=@hooksdir@}"
2522 [ -d "$TG_INST_CMDDIR" ] ||
2523 die "No command directory: '$TG_INST_CMDDIR'"
2525 ## Include awk scripts and their utility functions (separated for easier debugging)
2527 [ -f "$TG_INST_CMDDIR/tg--awksome" ] && [ -r "$TG_INST_CMDDIR/tg--awksome" ] ||
2528 die "Missing awk scripts: '$TG_INST_CMDDIR/tg--awksome'"
2529 . "$TG_INST_CMDDIR/tg--awksome"
2531 if [ -n "$tg__include" ]; then
2533 # We were sourced from another script for our utility functions;
2534 # this is set by hooks. Skip the rest of the file. A simple return doesn't
2535 # work as expected in every shell. See http://bugs.debian.org/516188
2537 # ensure setup happens
2539 initial_setup 1
2540 set_topbases 1
2541 noalt_setup
2543 else
2545 set -e
2547 tgbin="$0"
2548 tgdir="${tgbin%/}"
2549 case "$tgdir" in */*);;*) tgdir="./$tgdir"; esac
2550 tgdir="${tgdir%/*}/"
2551 tgname="${tgbin##*/}"
2552 [ "$0" != "$tgname" ] || tgdir=""
2554 # If tg contains a '/' but does not start with one then replace it with an absolute path
2556 case "$0" in /*) ;; */*)
2557 tgdir="$(cd "${0%/*}" && pwd -P)/"
2558 tgbin="$tgdir$tgname"
2559 esac
2561 # tgdisplay will include any explicit -C <dir> etc. options whereas tgname will not
2562 # tgdisplayac is the same as tgdisplay but without any -r or -u options (ac => abort/continue)
2564 tgdisplaydir="$tgdir"
2565 tgdisplay="$tgbin"
2566 tgdisplayac="$tgdisplay"
2568 v_get_abs_path _tgnameabs "$(cmd_path "$tgname")" &&
2569 _tgabs="$_tgnameabs" &&
2570 { [ "$tgbin" = "$tgname" ] || v_get_abs_path _tgabs "$tgbin"; } &&
2571 [ "$_tgabs" = "$_tgnameabs" ]
2572 then
2573 tgdisplaydir=""
2574 tgdisplay="$tgname"
2575 tgdisplayac="$tgdisplay"
2577 [ -z "$_tgabs" ] || tgbin="$_tgabs"
2578 unset_ _tgabs _tgnameabs
2580 tg() (
2581 TG_TMPDIR="$tg_tmp_dir" && export TG_TMPDIR &&
2582 exec "$tgbin" "$@"
2585 explicit_remote=
2586 explicit_dir=
2587 gitcdopt=
2588 noremote=
2589 forcepager=
2590 wayback=
2592 cmd=
2593 while :; do case "$1" in
2595 help|--help|-h)
2596 cmd=help
2597 shift
2598 break;;
2600 status|--status)
2601 cmd=status
2602 shift
2603 break;;
2605 --hooks-path)
2606 cmd=hooks-path
2607 shift
2608 break;;
2610 --exec-path)
2611 cmd=exec-path
2612 shift
2613 break;;
2615 --awk-path)
2616 cmd=awk-path
2617 shift
2618 break;;
2620 --top-bases)
2621 cmd=top-bases
2622 shift
2623 break;;
2625 --no-pager)
2626 forcepager=0
2627 shift;;
2629 --pager|-p)
2630 forcepager=1
2631 shift;;
2634 shift
2635 if [ -z "$1" ]; then
2636 echo "Option -r requires an argument." >&2
2637 do_help
2638 exit 1
2640 unset_ noremote
2641 base_remote="$1"
2642 explicit_remote="$base_remote"
2643 tgdisplay="$tgdisplaydir$tgname$gitcdopt -r $explicit_remote"
2644 TG_EXPLICIT_REMOTE="$base_remote" && export TG_EXPLICIT_REMOTE
2645 shift;;
2648 unset_ base_remote explicit_remote
2649 noremote=1
2650 tgdisplay="$tgdisplaydir$tgname$gitcdopt -u"
2651 TG_EXPLICIT_REMOTE= && export TG_EXPLICIT_REMOTE
2652 shift;;
2655 shift
2656 if [ -z "$1" ]; then
2657 echo "Option -C requires an argument." >&2
2658 do_help
2659 exit 1
2661 cd "$1"
2662 unset_ GIT_DIR GIT_COMMON_DIR
2663 if [ -z "$explicit_dir" ]; then
2664 explicit_dir="$1"
2665 else
2666 explicit_dir="$PWD"
2668 gitcdopt=" -C \"$explicit_dir\""
2669 [ "$explicit_dir" != "." ] || explicit_dir="." gitcdopt=" -C ."
2670 tgdisplay="$tgdisplaydir$tgname$gitcdopt"
2671 tgdisplayac="$tgdisplay"
2672 [ -z "$explicit_remote" ] || tgdisplay="$tgdisplay -r $explicit_remote"
2673 [ -z "$noremote" ] || tgdisplay="$tgdisplay -u"
2674 shift;;
2677 shift
2678 if [ -z "$1" ]; then
2679 echo "Option -c requires an argument." >&2
2680 do_help
2681 exit 1
2683 param="'$(printf '%s\n' "$1" | sed "s/[']/'\\\\''/g")'"
2684 GIT_CONFIG_PARAMETERS="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }$param"
2685 export GIT_CONFIG_PARAMETERS
2686 shift;;
2689 if [ -n "$wayback" ]; then
2690 echo "Option -w may be used at most once." >&2
2691 do_help
2692 exit 1
2694 shift
2695 if [ -z "$1" ]; then
2696 echo "Option -w requires an argument." >&2
2697 do_help
2698 exit 1
2700 wayback="$1"
2701 shift;;
2704 shift
2705 break;;
2708 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
2709 do_help
2710 exit 1;;
2713 break;;
2715 esac; done
2716 if [ z"$forcepager" = z"0" ]; then
2717 GIT_PAGER_IN_USE=1 TG_PAGER_IN_USE=1 &&
2718 export GIT_PAGER_IN_USE TG_PAGER_IN_USE
2721 [ -n "$cmd" ] || [ $# -lt 1 ] || { cmd="$1"; shift; }
2723 ## Dispatch
2725 [ -n "$cmd" ] || { do_help; exit 1; }
2727 case "$cmd" in
2729 help)
2730 do_help "$@"
2731 exit 0;;
2733 status|st)
2734 unset_ base_remote
2735 basic_setup
2736 set_topbases
2737 do_status "$@"
2738 exit ${do_status_result:-0};;
2740 hooks-path)
2741 # Internal command
2742 echol "$TG_INST_HOOKSDIR";;
2744 exec-path)
2745 # Internal command
2746 echol "$TG_INST_CMDDIR";;
2748 awk-path)
2749 # Internal command
2750 echol "$TG_INST_CMDDIR/awk";;
2752 top-bases)
2753 # Maintenance command
2754 do_topbases_help=
2755 show_remote_topbases=
2756 case "$1" in
2757 --help|-h)
2758 do_topbases_help=0;;
2759 -r|--remote)
2760 if [ $# -eq 2 ] && [ -n "$2" ]; then
2761 # unadvertised, but make it work
2762 base_remote="$2"
2763 shift
2765 show_remote_topbases=1;;
2767 [ $# -eq 0 ] || do_topbases_help=1;;
2768 esac
2769 [ $# -le 1 ] || do_topbases_help=1
2770 if [ -n "$do_topbases_help" ]; then
2771 helpcmd='echo "Usage: ${tgname:-tg} [-r <remote>] --top-bases [-r]"'
2772 [ $do_topbases_help -eq 0 ] || helpcmd="$helpcmd >&2"
2773 eval "$helpcmd"
2774 exit $do_topbases_help
2776 git_dir=
2777 if git_dir="$(git rev-parse --git-dir 2>&1)"; then
2778 [ -z "$wayback" ] || activate_wayback_machine "$wayback"
2779 setup_git_dirs
2781 set_topbases
2782 if [ -n "$show_remote_topbases" ]; then
2783 basic_setup_remote
2784 [ -n "$base_remote" ] ||
2785 die "no remote location given. Either use -r <remote> option or set topgit.remote"
2786 rbases=
2787 [ -z "$topbases_implicit_default" ] ||
2788 check_remote_topbases "$base_remote" rbases "--top-bases"
2789 if [ -n "$rbases" ]; then
2790 echol "$rbases"
2791 else
2792 echol "refs/remotes/$base_remote/${topbases#heads/}"
2794 else
2795 echol "refs/$topbases"
2796 fi;;
2799 isutil=
2800 case "$cmd" in index-merge-one-file)
2801 isutil="-"
2802 esac
2803 [ -r "$TG_INST_CMDDIR"/tg-$isutil$cmd ] || {
2804 looplevel="$TG_ALIAS_DEPTH"
2805 [ "${looplevel#[1-9]}" != "$looplevel" ] &&
2806 [ "${looplevel%%[!0-9]*}" = "$looplevel" ] ||
2807 looplevel=0
2808 tgalias="$(git config "topgit.alias.$cmd" 2>/dev/null)" || :
2809 [ -n "$tgalias" ] || {
2810 echo "Unknown subcommand: $cmd" >&2
2811 do_help
2812 exit 1
2814 looplevel=$(( $looplevel + 1 ))
2815 [ $looplevel -le 10 ] || die "topgit.alias nesting level 10 exceeded"
2816 TG_ALIAS_DEPTH="$looplevel"
2817 export TG_ALIAS_DEPTH
2818 if [ "!${tgalias#?}" = "$tgalias" ]; then
2819 [ -z "$wayback" ] ||
2820 die "-w is not allowed before an '!' alias command"
2821 unset_ GIT_PREFIX
2822 if pfx="$(git rev-parse --show-prefix 2>/dev/null)"; then
2823 GIT_PREFIX="$pfx"
2824 export GIT_PREFIX
2826 cd "./$(git rev-parse --show-cdup 2>/dev/null)"
2827 exec @SHELL_PATH@ -c "${tgalias#?} \"\$@\"" @SHELL_PATH@ "$@"
2828 else
2829 eval 'exec "$tgbin"' "${wayback:+-w \"\$wayback\"}" "$tgalias" '"$@"'
2831 die "alias execution failed for: $tgalias"
2833 unset_ TG_ALIAS_DEPTH
2835 showing_help=
2836 if [ "$*" = "-h" ] || [ "$*" = "--help" ]; then
2837 showing_help=1
2840 nomergesetup="$showing_help"
2841 case "$cmd" in base|contains|export|files|info|log|mail|next|patch|prev|rebase|revert|shell|summary|tag)
2842 # avoid merge setup where not necessary
2844 nomergesetup=1
2845 esac
2847 if [ -n "$wayback" ] && [ -z "$showing_help" ]; then
2848 [ -n "$nomergesetup" ] ||
2849 die "the wayback machine cannot be used with the \"$cmd\" subcommand"
2850 if [ "$cmd" = "shell" ]; then
2851 # this is ugly; `tg shell` should handle this but it's too
2852 # late there so we have to do it here
2853 wayback_dir=
2854 case "$1" in
2855 "--directory="?*)
2856 wayback_dir="${1#--directory=}" && shift;;
2857 "--directory=")
2858 die "--directory requires an argument";;
2859 "--directory")
2860 [ $# -ge 2 ] || die "--directory requires an argument"
2861 wayback_dir="$2" && shift 2;;
2862 esac
2863 activate_wayback_machine "$wayback" 1 "$wayback_dir"
2864 else
2865 _fullwb=
2866 # export might drop out into a shell for conflict resolution
2867 [ "$cmd" != "export" ] || _fullwb=2
2868 activate_wayback_machine "$wayback" "$_fullwb"
2869 fi ||
2870 die "failed to set the wayback machine to target \"$wayback\""
2873 [ -n "$showing_help" ] || initial_setup
2874 [ -z "$noremote" ] || unset_ base_remote
2876 if [ -z "$nomergesetup" ]; then
2877 # make sure merging the .top* files will always behave sanely
2879 setup_ours
2880 setup_hook "pre-commit"
2883 # everything but rebase needs topbases set
2884 carefully="$showing_help"
2885 [ "$cmd" != "migrate-bases" ] || carefully=1
2886 [ "$cmd" = "rebase" ] || set_topbases $carefully
2888 _use_ref_cache=
2889 tg_read_only=1
2890 _suppress_alt=
2891 case "$cmd$showing_help" in
2892 contains|info|summary|tag)
2893 _use_ref_cache=1;;
2894 "export")
2895 _use_ref_cache=1
2896 _suppress_alt=1;;
2897 annihilate|create|delete|depend|import|update)
2898 tg_read_only=
2899 _suppress_alt=1;;
2900 esac
2901 [ -z "$_suppress_alt" ] || noalt_setup
2902 [ -z "$_use_ref_cache" ] || v_create_ref_cache
2904 fullcmd="${tgname:-tg} $cmd $*"
2905 fullcmd="${fullcmd% }"
2906 if [ z"$forcepager" = z"1" ]; then
2907 page '. "$TG_INST_CMDDIR"/tg-$isutil$cmd' "$@"
2908 else
2909 . "$TG_INST_CMDDIR"/tg-$isutil$cmd
2910 fi;;
2911 esac