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>
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@"
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
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.
34 eval "${TRAPEXIT_:-exit $EXITCODE_}"
37 trap 'trapexit_ $?' EXIT
39 # unset that ignores error code that shouldn't be produced according to POSIX
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
52 # This is the POSIX equivalent of which
55 { "unset" -f command unset unalias "$1"; } >/dev
/null
2>&1 ||
:
56 { "unalias" -a ||
unalias -m "*"; } >/dev
/null
2>&1 ||
:
61 # note deliberate use of '(' ... ')' rather than '{' ... '}'
64 { "unset" -f "$1" ||
:; } >/dev
/null
2>&1 &&
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")
97 echol
"${TG_RECURSIVE}${tgname:-tg}: $*"
102 info
"warning: $*" >&2
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
128 set -- "$_quotearg_v" \
129 "sed \"s/'/'\\\\\\''/g;1s/^/'/;\\\$s/\\\$/'/;s/'''/'/g;1s/^''\\(.\\)/\\1/\"" "$*"
139 printf "%s$4" "$3" |
eval "$2"
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
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.]*}"
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
174 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
175 unset_ vcmp_a_ vcmp_b_
176 case "$2" in ">"|
">="|
"!=") return 0; esac
179 vcmp_a_
="${1#$vcmp_a_}"
180 vcmp_b_
="${3#$vcmp_b_}"
181 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
183 unset_ vcmp_a_ vcmp_b_
184 case "$2" in "="|
"=="|
"<="|
">=") return 0; esac
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)
193 test -n "$1" && test -d "$1" ||
return 1
197 if [ z
"$1" = z
"." ]; then
200 while ! case "$1" in "."*) ! :; esac; do shift; done
202 if [ $# -eq 0 ]; then
203 ! \
ls -a1 "$iedd_" |
grep -q -E -v '^\.'"$ieddnok_"
205 # we only handle ".git" right now for efficiency
206 [ z
"$*" = z
".git" ] ||
{
207 fatal
"[BUG] is_empty_dir not implemented for arguments: $*"
210 ! \
ls -a1 "$iedd_" |
grep -q -E -v -i -e '^\.\.?$' -e '^\.git$'
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 '"
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"
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
240 while IFS
= read -r _line ||
[ -n "$_line" ]; do
241 printf '%s\n' "$_line"
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"
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"
256 git cat-file blob
"$_rev:.$2" 2>/dev
/null
260 # cat_deps BRANCHNAME
264 cat_depsmsg_internal
"$1" topdeps
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.
283 cat "$root_dir/${path#*:}"
286 # ':file' means cat from index
287 git cat-file blob
":${path#*:}" 2>/dev
/null
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
305 die
"Wrong argument to cat_file: '$2'"
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
329 git_write_tree
() { git_temp_alt_odb_cmd write-tree
"$@"; }
330 git_mktree
() { git_temp_alt_odb_cmd mktree
"$@"; }
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
344 eval "$1="'"refs/heads/$2"'
347 # get tree for the base (second arg)
348 # store result in variable named by first arg
351 eval "$1="'"refs/$topbases/$2"'
354 # get tree for the index
355 # store result in variable named by first arg
358 eval "$1="'"$(git_write_tree)"'
361 # get tree for the worktree
362 # store result in variable named by first arg
366 i_tree="$(git_write_tree)"
367 # the file for --index-output needs to sit next to the
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 &&
382 # get tree for arbitrary ref (second arg)
383 # store result in variable named by first arg
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
396 eval "$1="'"${2#refs/$topbases/}"'
399 eval "$1="'"${2#refs/heads/}"'
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
417 [ "$1" != "-t" ] ||
{ shift; use_alt_temp_odb
=1; }
418 eval "v_get_tree_${2#?}" _tree
'"$1"'
420 git ls-tree --full-tree \"\$_tree\" |
421 sed -ne '/ \.top.*\$/!p' |
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
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
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')"
443 -[01][0-9][0-5][0-9]|
+[01][0-9][0-5][0-9])
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
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'"
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
506 setup_git_dir_is_bare
507 [ -z "$git_dir_is_bare" ] ||
return 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!
515 # Prepare incantation
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@"
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 ))
527 mv -f "$git_hooks_dir/$1" "$git_hooks_dir/$1-chain$chain_num"
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@"
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\" \"\$@\" || :"
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)
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...]
573 _bname
="$1"; _base
="$2"
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
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
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.
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
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 ||
:
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"
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" &&
627 awk -v p
="$tg_tmp_dir/cached/" '
630 $2 ~ /^[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]+$/ {
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
642 [ -n "$tg_ref_cache" ] && ! [ -s "$tg_ref_cache" ] ||
return 0
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"
653 [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ] ||
return 0
662 core_abbrev_is_setup
=
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
678 if [ -n "$tg_ref_cache" ] && [ -s "$tg_ref_cache" ]; then
680 awk -v r
="$1" 'BEGIN {e=1}; $1 == r {print $2; e=0; exit}; END {exit e}' <"$tg_ref_cache" ||
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
702 die
"v_ref_exists_rev requires fully-qualified ref name (given: $2)"
704 [ -n "$tg_read_only" ] ||
{ eval "$1="'"$(git rev-parse --quiet --verify "$2^0" -- 2>/dev/null)"'; return; }
707 { read -r _result _result_rev
<"$tg_tmp_dir/cached/$2/.ref"; } 2>/dev
/null ||
:
708 [ -z "$_result" ] ||
{ eval "$1="'"$_result_rev"'; return $_result; }
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"'
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
()
728 die
"v_ref_exists_rev_short requires fully-qualified ref name (given: $2)"
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; }
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; }
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" --)"
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"'
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/
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
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"'
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"'
786 eval "$1="'"$
(git rev-parse
--verify "$2^{tree}" -- 2>/dev
/null
)"'
790 # Whether BRANCH has a remote equivalent (accepts ${topbases#heads/}/ too)
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
"
810 set -- "$1" "$_verifyname" "$3"
814 _verifyname="${2#refs/$topbases/}"
817 _verifyname="${2#refs/heads/}"
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"'
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"
842 [ -n "$_rev" ] || v_ref_exists_rev _rev "refs
/heads
/$_branch_name"
844 [ -n "$_rev_base" ] || v_ref_exists_rev _rev_base "refs
/$topbases/$_branch_name"
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
)" || :
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 || :
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
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()
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
908 if [ -e "${git_cdup_result}.topdeps
" ] || [ -e "${git_cdup_result}.topmsg
" ]; then
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
)";;
926 # Whether REF is a SHA1 (compared to a symbolic name).
929 case "$1" in $octet20) return 0;; esac
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
943 ratd_opts="${TG_DEBUG:+-p=\"\$tg_ref_cache.pre\" }"
945 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
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=
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.$$
"
961 refpats="\"refs
/heads
\" \"refs
/\
$topbases\""
962 [ -z "$base_remote" ] || refpats="$refpats \"refs
/remotes
/\
$base_remote\""
964 eval git for-each-ref '--format="%(refname
) %(objectname
)"' "$refpats" >"$tmprfs"
965 depscmd="run_awk_topgit_deps
$ratd_opts"
968 depscmd="$depscmd $1"
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" '
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 # If with_deps_opts is non-empty, always run_awk_topgit_deps with those extra opts
996 # any branch names in the space-separated recurse_deps_exclude variable
997 # are skipped (along with their dependencies)
998 unset_ no_remotes recurse_preorder with_top_level with_deps_opts
999 recurse_deps_internal()
1001 case " $recurse_deps_exclude " in *" $1 "*) return 0; esac
1002 ratr_opts="${recurse_preorder:+-f} ${with_top_level:+-s}"
1007 if [ -n "$tg_read_only" ] && [ -n "$tg_ref_cache" ]; then
1009 tmprfs="$tg_ref_cache"
1010 tmptgbr="$tg_ref_cache_br"
1011 tmpann="$tg_ref_cache_ann"
1012 tmpdep="$tg_ref_cache_dep"
1013 [ -s "$tg_ref_cache" ] || dogfer=1
1014 [ -n "$dogfer" ] || ! [ -s "$tmptgbr" ] || ! [ -f "$tmpann" ] || ! [ -s "$tmpdep" ] || dorad=
1016 ratr_opts="$ratr_opts -rmh -rma -rmb"
1017 tmprfs="$tg_tmp_dir/refs.$$
"
1018 tmpann="$tg_tmp_dir/ann.$$
"
1019 tmptgbr="$tg_tmp_dir/tgbr.$$
"
1022 refpats="\"refs
/heads
\" \"refs
/\
$topbases\""
1023 [ -z "$base_remote" ] || refpats="$refpats \"refs
/remotes
/\
$base_remote\""
1026 if [ -z "$no_remotes" ] && [ -n "$base_remote" ]; then
1027 if [ -n "$userc" ]; then
1028 tmptgrmtbr="$tg_ref_cache_rbr"
1029 [ -n "$dogfer" ] || ! [ -s "$tmptgrmtbr" ] || dorab=
1031 tmptgrmtbr="$tg_tmp_dir/tgrmtbr.$$
"
1032 ratr_opts="$ratr_opts -rmr"
1034 ratr_opts="$ratr_opts -r=\"\
$tmptgrmtbr\" -u=\":refs
/remotes
/\
$base_remote/\
${topbases#heads/}\""
1037 eval git for-each-ref '--format="%(refname
) %(objectname
)"' "$refpats" >"$tmprfs"
1038 if [ -n "$tmptgrmtbr" ] && [ -n "$dorab" ]; then
1039 run_awk_topgit_branches -n -h="refs
/remotes
/$base_remote" -r="$tmprfs" \
1040 "refs
/remotes
/$base_remote/${topbases#heads/}" >"$tmptgrmtbr"
1042 depscmd="run_awk_topgit_deps
-s${with_deps_opts:+ $with_deps_opts}${TG_DEBUG:+ -p=\"\$tg_ref_cache.pre\"}"
1043 depscmd="$depscmd"' -a="$tmpann" -b="$tmptgbr" -r="$tmprfs" "refs
/$topbases"'
1044 if [ -z "$with_deps_opts" ] && [ -n "$userc" ]; then
1045 if [ -n "$dorad" ]; then
1046 eval "$depscmd" >"$tmpdep"
1048 depscmd='<"$tmpdep" '
1050 depscmd="$depscmd |
"
1052 eval "$depscmd" run_awk_topgit_recurse '-a="$tmpann" -b="$tmptgbr"' \
1053 '-c=1 -h="$tmprfs"' "$ratr_opts" '-x="$recurse_deps_exclude"' '"$@
"'
1057 # helper for recurse_deps so that a return statement executed inside CMD
1058 # does not return from recurse_deps. This shouldn't be necessary, but it
1059 # seems that it actually is.
1065 # becomes read-only for caching purposes
1066 # assigns new value to tg_read_only
1067 # become_cacheable/undo_become_cacheable calls may be nested
1070 _old_tg_read_only="$tg_read_only"
1071 if [ -z "$tg_read_only" ]; then
1072 ! [ -e "$tg_tmp_dir/cached
" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created
" ] ||
1073 rm -rf "$tg_tmp_dir/cached
" "$tg_tmp_dir/tg~ref-dirs-created
"
1077 v_create_ref_cache _my_ref_cache
1078 _my_ref_cache="${_my_ref_cache:+1}"
1079 tg_read_only="undo
${_my_ref_cache:-0}-$_old_tg_read_only"
1082 # restores tg_read_only and ref_cache to state before become_cacheable call
1083 # become_cacheable/undo_bocome_cacheable calls may be nested
1084 undo_become_cacheable()
1086 case "$tg_read_only" in
1088 _suffix="${tg_read_only#undo?-}"
1089 [ "${tg_read_only%$_suffix}" = "undo0-
" ] || remove_ref_cache
1090 tg_read_only="$_suffix"
1094 # just call this, no undo, sets tg_read_only= and removes ref cache and cached results
1095 become_non_cacheable()
1099 ! [ -e "$tg_tmp_dir/cached
" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created
" ] ||
1100 rm -rf "$tg_tmp_dir/cached
" "$tg_tmp_dir/tg~ref-dirs-created
"
1103 # call this to make sure the current Git repository has an associated work tree
1104 # also make sure we are not in wayback mode
1107 [ -z "$wayback" ] ||
1108 die "the wayback machine cannot be used with the specified options
"
1109 setup_git_dir_is_bare
1110 [ -n "$git_dir_is_bare" ] || return 0
1111 die "This operation must be run
in a work tree
"
1114 # call this to make sure Git will not complain about a missing user/email
1115 # result is cached in TG_IDENT_CHECKED and a non-empty value suppresses the check
1116 ensure_ident_available()
1118 [ -z "$TG_IDENT_CHECKED" ] || return 0
1119 git var GIT_AUTHOR_IDENT >/dev/null &&
1120 git var GIT_COMMITTER_IDENT >/dev/null || exit
1122 export TG_IDENT_CHECKED
1126 # recurse_deps [-o=<options string>] CMD NAME [BRANCHPATH...]
1127 # Recursively eval CMD on all dependencies of NAME.
1128 # Dependencies are visited in topological order.
1129 # If <options string> is given, it's eval'd into the recurse_deps_internal
1130 # call just before the "--" that's passed just before NAME
1131 # CMD can refer to the following variables:
1133 # _ret starts as 0; CMD can change; will be final return result
1134 # _dep bare branch name or ":refs
/remotes
/...
" for a remote
1135 # _name has $_dep in its .topdeps ("" for top and $with_top_level)
1136 # _depchain 0+ space-sep branch names (_name first) form a path to top
1137 # _dep_missing boolean "1" if no such $_dep ref; "" if ref present
1138 # _dep_is_leaf boolean "1" if leaf; "" if not
1139 # _dep_is_tgish boolean "1" if tgish; "" if not (which implies no remote)
1140 # _dep_has_remote boolean "1" if $_dep has_remote; "" if not
1141 # _dep_annihilated boolean "1" if $_dep annihilated; "" if not
1142 # _dep_xvisits non-negative integer number of excess visits (often 0)
1144 # CMD may use a "return" statement without issue; its return value is ignored,
1145 # but if CMD sets _ret to a negative value, e.g. "-0" or "-1" the enumeration
1146 # will stop immediately and the value with the leading "-" stripped off will
1147 # be the final result code
1149 # CMD can refer to $_name for queried branch name,
1150 # $_dep for dependency name,
1151 # $_depchain for space-seperated branch backtrace,
1152 # $_dep_missing boolean to check whether $_dep is present
1153 # and the $_dep_is_tgish and $_dep_annihilated booleans.
1154 # If recurse_preorder is NOT set then the $_dep_is_leaf boolean is also valid.
1155 # It can modify $_ret to affect the return value
1156 # of the whole function.
1157 # If recurse_deps() hits missing dependencies, it will append
1158 # them to space-separated $missing_deps list and skip them
1159 # after calling CMD with _dep_missing set.
1160 # remote dependencies are processed if no_remotes is unset.
1161 # any branch names in the space-separated recurse_deps_exclude variable
1162 # are skipped (along with their dependencies)
1164 # If no_remotes is non-empty, exclude remotes
1165 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
1166 # If with_top_level is non-empty, include the top-level that's normally omitted
1167 # If with_deps_opts is non-empty, always run_awk_topgit_deps with those extra opts
1168 # any branch names in the space-separated recurse_deps_exclude variable
1169 # are skipped (along with their dependencies)
1173 case "$1" in -o=*) _opts="${1#-o=}"; shift; esac
1176 _depsfile="$
(get_temp tg-depsfile
)"
1177 eval recurse_deps_internal "$_opts" -- '"$@
"' >"$_depsfile" || :
1180 while read _ismissing _istgish _isleaf _dep_xvisits _dep _name _deppath; do
1181 _depchain="$_name${_deppath:+ $_deppath}"
1183 [ "$_istgish" = "0" ] || _dep_is_tgish=1
1185 [ "$_istgish" != "2" ] || _dep_has_remote=1
1187 if [ "$_ismissing" != "0" ]; then
1189 case " $missing_deps " in *" $_dep "*);;*)
1190 missing_deps="${missing_deps:+$missing_deps }$_dep"
1195 if [ "$_isleaf" = "1" ]; then
1197 elif [ "$_isleaf" = "2" ]; then
1200 do_eval "$_cmd" || :
1201 if [ "${_ret#-}" != "$_ret" ]; then
1211 # output (one per line) the unique leaves of NAME
1213 # 1) a non-tgish dependency
1214 # 2) the base of a tgish dependency with no non-annihilated dependencies
1215 # duplicates are suppressed (by commit rev) and remotes are always ignored
1216 # if a leaf has an exact tag match that will be output
1217 # note that recurse_deps_exclude IS honored for this operation
1218 # if with_deps_opts is set it will take effect here
1226 while read _ismissing _istgish _isleaf _xvsts _dep _name _deppath; do
1227 [ "$_isleaf" = "1" ] && [ "$_ismissing" = "0" ] || continue
1228 if [ "$_istgish" != "0" ]; then
1229 fulldep="refs
/$topbases/$_dep"
1231 fulldep="refs
/heads
/$_dep"
1233 case " $seen_leaf_refs " in *" $fulldep "*);;*)
1234 seen_leaf_refs="${seen_leaf_refs:+$seen_leaf_refs }$fulldep"
1235 if v_ref_exists_rev fullrev "$fulldep"; then
1236 case " $seen_leaf_revs " in *" $fullrev "*);;*)
1237 seen_leaf_revs="${seen_leaf_revs:+$seen_leaf_revs }$fullrev"
1238 # See if Git knows it by another name
1239 if tagname="$
(git describe
--exact-match "$fullrev" 2>/dev
/null
)" && [ -n "$tagname" ]; then
1240 echo "refs
/tags
/$tagname"
1248 $(recurse_deps_internal -l -o=1 -- "$1")
1253 # branch_needs_update
1254 # This is a helper function for determining whether given branch
1255 # is up-to-date wrt. its dependencies. It expects input as if it
1256 # is called as a recurse_deps() helper.
1257 # In case the branch does need update, it will echo it together
1258 # with the branch backtrace on the output (see needs_update()
1259 # description for details) and set $_ret to non-zero.
1260 branch_needs_update()
1262 if [ -n "$_dep_missing" ]; then
1263 echo "! $_dep $_depchain"
1267 if [ -n "$_dep_is_tgish" ]; then
1268 [ -z "$_dep_annihilated" ] || return 0
1270 if [ -n "$_dep_has_remote" ]; then
1271 branch_contains "refs
/heads
/$_dep" "refs
/remotes
/$base_remote/$_dep" || {
1272 echo ":refs
/remotes
/$base_remote/$_dep $_dep $_depchain"
1276 # We want to sync with our base first and should output this before
1277 # the remote branch, but the order does not actually matter to tg-update
1278 # as it just recurses regardless, but it does matter for tg-info (which
1279 # treats out-of-date bases as though they were already merged in) so
1280 # we output the remote before the base.
1281 branch_contains "refs
/heads
/$_dep" "refs
/$topbases/$_dep" || {
1282 echo ": $_dep $_depchain"
1288 if [ -n "$_name" ]; then
1289 case "$_dep" in :*) _fulldep="${_dep#:}";; *) _fulldep="refs
/heads
/$_dep";; esac
1290 if ! branch_contains "refs
/$topbases/$_name" "$_fulldep"; then
1291 # Some new commits in _dep
1292 echo "$_dep $_depchain"
1299 # This function is recursive; it outputs reverse path from NAME
1300 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
1301 # inner paths first. Innermost name can be :refs/remotes/<remote>/<name>
1302 # if the head is not in sync with the <remote> branch <name>, ':' if
1303 # the head is not in sync with the base (in this order of priority)
1304 # or '!' if dependency is missing. Note that the remote branch, base
1305 # order is reversed from the order they will actually be updated in
1306 # order to accomodate tg info which treats out-of-date items that are
1307 # only in the base as already being in the head for status purposes.
1308 # It will also return non-zero status if NAME needs update (seems backwards
1309 # but think of it as non-zero status if any non-missing output lines produced)
1310 # If needs_update() hits missing dependencies, it will append
1311 # them to space-separated $missing_deps list and skip them.
1312 # if with_deps_opts is set it will take effect here
1315 recurse_deps branch_needs_update "$1"
1318 # append second arg to first arg variable gluing with space if first already set
1321 eval "$1=\"\
${$1:+\$$1 }\
$2\""
1324 # true if whitespace separated first var name list contains second arg
1325 # use `vcontains 3 "value
" "some list
"` for a literal list
1328 eval case "\" \
${$1} \"" in '*" $2 "*) return 0; esac; return 1'
1331 # if the $1 var does not already contain $2 it's appended
1334 vcontains "$1" "$2" || vplus "$1" "$2"
1337 # reset needs_update_check results to empty
1338 needs_update_check_clear()
1340 unset_ needs_update_processed needs_update_behind needs_update_ahead needs_update_partial
1343 # needs_update_check NAME...
1345 # A faster version of needs_update that always succeeds
1346 # No output and unsuitable for actually performing updates themselves
1347 # If any of NAME... are NOT up-to-date AND they were not already processed
1348 # return status always will be zero however a simple check of
1349 # needs_update_behind after the call will answer the:
1350 # "are any out of
date?
": test -n "$needs_update_behind"
1351 # "is
<x
> out of
date?
": vcontains needs_update_behind "<x
>"
1353 # Note that results are cumulative and "no_remotes
" is honored as well as other
1354 # variables that modify recurse_deps_internal behavior. See the preceding
1355 # function to reset the results to empty when accumulation should start over.
1357 # Unlike needs_update, the branch names are themselves also checked to see if
1358 # they are out-of-date with respect to their bases or remote branches (not just
1359 # their remote bases). However, this can muddy some status results so this
1360 # can be disabled by setting needs_update_check_no_self to a non-empty value.
1362 # Unlike needs_update, here the remote base check is handled together with the
1363 # remote head check so if one is modified the other is too in the same way.
1365 # Dependencies are normally considered "behind
" if they need an update from
1366 # their base or remote but this can be suppressed by setting the
1367 # needs_update_check_no_same to a non-empty value. This will NOT prevent
1368 # parents of those dependencies from still being considered behind in such a
1369 # case even though the dependency itself will not be. Note that setting
1370 # needs_update_check_no_same also implies needs_update_check_no_self.
1372 # The following whitespace-separated lists are updated with the results:
1374 # if with_deps_opts is set it will take effect here
1376 # The "no_remotes
" setting is obeyed but remote names themselves will never
1377 # appear in any of the lists
1379 # needs_update_processed
1380 # The branch names in here have been processed and will be skipped
1382 # needs_update_behind
1383 # Any branch named in here needs an update from one or more of its
1384 # direct or indirect dependencies (i.e. it's "out-of-date
")
1386 # needs_update_ahead
1387 # Any branch named in here is NOT fully contained by at least one of
1388 # its dependents (i.e. it's a source of "out-of-date
(aka dirty
)"ness
1390 # needs_update_partial
1391 # Any branch names in here are either missing themselves or have one
1392 # or more detected missing dependencies (a completely missing remote
1393 # branch is never "detected
")
1394 needs_update_check()
1396 # each head must be processed independently or else there will be
1397 # confusion about who's missing what and which branches actually are
1399 tmptgrdi="$tg_tmp_dir/tgrdi.$$
"
1400 for nucname in "$@
"; do
1401 ! vcontains needs_update_processed "$nucname" || continue
1402 # no need to fuss with recurse_deps, just use
1403 # recurse_deps_internal directly
1404 recurse_deps_internal -s -o=-1 "$nucname" >"$tmptgrdi"
1405 while read -r _rdi_m _rdi_t _rdi_l _rdi_v _rdi_node _rdi_parent _rdi_chain; do
1406 case "$_rdi_node" in ""|:*) continue; esac # empty or checked with remote
1407 vsetadd needs_update_processed "$_rdi_node"
1408 if [ "$_rdi_m" != "0" ]; then # missing
1409 vsetadd needs_update_partial "$_rdi_node"
1410 [ -z "$_rdi_parent" ] || vsetadd needs_update_partial "$_rdi_parent"
1413 [ "$_rdi_t$_rdi_l" != "12" ] || continue # always skip annihilated
1415 if [ -n "$_rdi_parent" ]; then # not a "self
" line
1416 ! vcontains needs_update_partial "$_rdi_node" || vsetadd needs_update_partial "$_rdi_parent"
1417 ! vcontains needs_update_behind "$_rdi_node" || _rdi_dertee=2
1419 [ -z "$needs_update_check_no_self$needs_update_check_no_same" ] || continue # skip self
1421 if [ -z "$_rdi_dertee" ]; then
1422 if [ "$_rdi_t" != "0" ]; then # tgish
1423 if branch_contains "refs
/heads
/$_rdi_node" "refs
/$topbases/$_rdi_node"; then
1424 if [ "$_rdi_t" = "2" ]; then # will never be "2" when no_remotes is set
1425 branch_contains "refs
/heads
/$_rdi_node" "refs
/remotes
/$base_remote/$_rdi_node" &&
1426 branch_contains "refs
/$topbases/$_rdi_node" "refs
/remotes
/$base_remote/${topbases#heads/}/$_rdi_node" ||
1432 [ -z "$_rdi_dertee" ] || [ -n "$needs_update_check_no_same" ] || _rdi_dertee=1
1435 [ z"$_rdi_dertee" != z"1" ] || vsetadd needs_update_behind "$_rdi_node"
1436 [ -n "$_rdi_parent" ] || continue # self line
1437 if ! branch_contains "refs
/$topbases/$_rdi_parent" "refs
/heads
/$_rdi_node"; then
1439 vsetadd needs_update_ahead "$_rdi_node"
1441 [ -z "$_rdi_dertee" ] || vsetadd needs_update_behind "$_rdi_parent"
1446 # branch_empty NAME [-i | -w]
1449 if [ -z "$2" ]; then
1450 v_ref_exists_rev _rev "refs
/heads
/$1" || return 0
1453 { read -r _result _result_rev <"$tg_cache_dir/refs
/heads
/$1/.mt
"; } 2>/dev/null || :
1454 [ -z "$_result" ] || [ "$_result_rev" != "$_rev" ] || return $_result
1456 v_pretty_tree _pretty1 -t "$1" -b
1457 v_pretty_tree _pretty2 -t "$1" $2
1458 [ "$_pretty1" = "$_pretty2" ] || _result=$?
1459 [ -d "$tg_cache_dir/refs
/heads
/$1" ] || mkdir -p "$tg_cache_dir/refs
/heads
/$1" 2>/dev/null
1460 [ ! -d "$tg_cache_dir/refs
/heads
/$1" ] || echo $_result $_rev >"$tg_cache_dir/refs
/heads
/$1/.mt
"
1463 v_pretty_tree _pretty1 -t "$1" -b
1464 v_pretty_tree _pretty2 -t "$1" $2
1465 [ "$_pretty1" = "$_pretty2" ]
1469 v_get_tdmopt_internal()
1471 [ -n "$1" ] && [ -n "$3" ] || return 0
1472 [ "$2" = "-i" ] || [ "$2" = "-w" ] || return 0
1475 if v_verify_topgit_branch _tghead "HEAD
" -f; then
1476 if [ "$2" = "-w" ] && [ -f "$root_dir/$3" ] && [ -r "$root_dir/$3" ]; then
1478 if _opthash="$
(git hash-object
-w -t blob
--stdin <"$root_dir/$3")" && [ -n "$_opthash" ]; then
1479 _optval="$4\"$_tghead:$_opthash\""
1481 elif [ "$2" = "-i" ]; then
1482 if _opthash="$
(git rev-parse
--quiet --verify ":0:$3" --)" && [ -n "$_opthash" ]; then
1483 _optval="$4\"$_tghead:$_opthash\""
1487 eval "$1="'"$_optval"'
1490 # set var $1 to the correct -td= option for use in an eval for $2 -i or -w mode
1491 v_get_tdopt() { v_get_tdmopt_internal "$1" "$2" ".topdeps
" "-td="; }
1493 # set var $1 to the correct -tm= option for use in an eval for $2 -i or -w mode
1494 v_get_tmopt() { v_get_tdmopt_internal "$1" "$2" ".topmsg
" "-tm="; }
1496 # checkout_symref_full [-f] FULLREF [SEED]
1497 # Just like git checkout $iowopt -b FULLREF [SEED] except that FULLREF MUST start with
1498 # refs/ and HEAD is ALWAYS set to a symref to it and [SEED] (default is FULLREF)
1499 # MUST be a committish which if present will be used instead of current FULLREF
1500 # (and FULLREF will be updated to it as well in that case)
1501 # Any merge state is always cleared by this function
1502 # With -f it's like git checkout $iowopt -f -b FULLREF (uses read-tree --reset
1503 # instead of -m) but it will clear out any unmerged entries
1504 # As an extension, FULLREF may also be a full hash to create a detached HEAD instead
1505 checkout_symref_full()
1509 if [ "$1" = "-f" ]; then
1520 [ -z "$2" ] || [ "$1" = "$2" ] ||
1521 die "programmer error
: invalid checkout_symref_full
\"$1\" \"$2\""
1525 die "programmer error
: invalid checkout_symref_full
\"$1\""
1528 _seedrev="$
(git rev-parse
--quiet --verify "${2:-$1}^0" --)" ||
1529 die "invalid committish
: \"${2:-$1}\""
1530 # Clear out any MERGE_HEAD kruft
1531 rm -f "$git_dir/MERGE_HEAD
" || :
1532 # We have to do all the hard work ourselves :/
1533 # This is like git checkout -b "$1" "$2"
1534 # (or just git checkout "$1"),
1535 # but never creates a detached HEAD (unless $1 is a hash)
1536 git read-tree -u $_mode $_head "$_seedrev" &&
1538 [ -z "$2" ] && [ "$
(git cat-file
-t "$1")" = "commit
" ] ||
1539 git update-ref ${_ishash:+--no-deref} "$1" "$_seedrev"
1541 [ -n "$_ishash" ] || git symbolic-ref HEAD "$1"
1545 # switch_to_base NAME [SEED]
1548 checkout_symref_full "refs
/$topbases/$1" "$2"
1551 # run editor with arguments
1552 # the editor setting will be cached in $tg_editor (which is eval'd)
1553 # result non-zero if editor fails or GIT_EDITOR cannot be determined
1554 # just in case, noalt_setup will be in effect while the editor is running
1557 tg_editor="$GIT_EDITOR"
1558 [ -n "$tg_editor" ] || tg_editor="$
(git var GIT_EDITOR
)" || return $?
1561 eval "$tg_editor" '"$@
"'
1565 # Show the help messages.
1569 if [ "$1" = "-w" ]; then
1573 if [ "$1" = "st
" ]; then
1575 set -- "status
" "$@
"
1577 if [ -z "$1" ] ; then
1578 # This is currently invoked in all kinds of circumstances,
1579 # including when the user made a usage error. Should we end up
1580 # providing more than a short help message, then we should
1582 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
1584 ## Build available commands list for help output
1588 for cmd in "$TG_INST_CMDDIR"/tg-[!-]*; do
1589 ! [ -r "$cmd" ] && continue
1590 # strip directory part and "tg-
" prefix
1593 [ "$cmd" != "migrate-bases
" ] || continue
1594 [ "$cmd" != "summary
" ] || cmd="st
[atus
]|
$cmd"
1595 cmds="$cmds$sep$cmd"
1599 echo "TopGit version
$TG_VERSION - A different
patch queue manager
"
1600 echo "Usage
: $tgname [-C <dir
>] [-r <remote
> |
-u]" \
1601 "[-c <name
>=<val
>] [--[no-
]pager|
-p] [-w [:]<tgtag
>] ($cmds) ...
"
1602 echo " Or
: $tgname help [-w] [<command>]"
1603 echo "Use
\"$tgdisplaydir$tgname help tg
\" for overview of TopGit
"
1604 elif [ -r "$TG_INST_CMDDIR"/tg-$1 ] || [ -r "$TG_INST_SHAREDIR/tg-
$1.txt
" ] ; then
1605 if [ -n "$_www" ]; then
1607 if ! [ -r "$TG_INST_SHAREDIR/topgit.html
" ]; then
1608 echo "${0##*/}: missing html
help file:" \
1609 "$TG_INST_SHAREDIR/topgit.html
" 1>&2
1612 if ! [ -r "$TG_INST_SHAREDIR/tg-
$1.html
" ]; then
1613 echo "${0##*/}: missing html
help file:" \
1614 "$TG_INST_SHAREDIR/tg-
$1.html
" 1>&2
1617 if [ -n "$nohtml" ]; then
1618 echo "${0##*/}: use
" \
1619 "\"${0##*/} help $1\" instead
" 1>&2
1622 git web--browse -c help.browser "$TG_INST_SHAREDIR/tg-
$1.html
"
1627 if [ -r "$TG_INST_CMDDIR"/tg-$1 ] ; then
1628 "$TG_INST_CMDDIR"/tg-$1 -h 2>&1 || :
1630 elif [ "$1" = "help" ]; then
1631 echo "Usage
: ${tgname:-tg} help [-w] [<command>]"
1633 elif [ "$1" = "status
" ] || [ "$1" = "st
" ]; then
1634 echo "Usage
: ${tgname:-tg} @tgsthelpusage@
"
1637 if [ -r "$TG_INST_SHAREDIR/tg-
$1.txt
" ] ; then
1638 cat "$TG_INST_SHAREDIR/tg-
$1.txt
"
1643 echo "${0##*/}: no
help for $1" 1>&2
1656 setup_git_dir_is_bare
1657 [ -z "$git_dir_is_bare" ] || return 0
1659 if [ -e "$git_dir/MERGE_HEAD
" ]; then
1661 elif [ -e "$git_dir/rebase-apply
/applying
" ]; then
1663 git_remove="$git_dir/rebase-apply
"
1664 elif [ -e "$git_dir/rebase-apply
" ]; then
1666 git_remove="$git_dir/rebase-apply
"
1667 elif [ -e "$git_dir/rebase-merge
" ]; then
1669 git_remove="$git_dir/rebase-merge
"
1670 elif [ -e "$git_dir/CHERRY_PICK_HEAD
" ]; then
1671 git_state="cherry-pick
"
1672 elif [ -e "$git_dir/BISECT_LOG
" ]; then
1674 elif [ -e "$git_dir/REVERT_HEAD
" ]; then
1677 git_remove="${git_remove#./}"
1679 if [ -e "$git_dir/tg-update
" ]; then
1681 tg_remove="$git_dir/tg-update
"
1682 ! [ -s "$git_dir/tg-update
/merging_topfiles
" ] || tg_topmerge=1
1684 tg_remove="${tg_remove#./}"
1687 # Show status information
1695 while [ $# -gt 0 ] && case "$1" in
1700 # kludge in this common bundling option
1706 [ -z "$do_status_verbose" ] || abbrev
=
1714 die
"unknown status argument: $1"
1716 esac; do shift; done
1717 if [ -n "$do_status_help" ]; then
1718 echo "Usage: ${tgname:-tg} @tgsthelpusage@"
1722 symref
="$(git symbolic-ref --quiet HEAD)" ||
:
1723 headrv
="$(git rev-parse --quiet --verify ${abbrev:+--short} HEAD --)" ||
:
1724 if [ -n "$symref" ]; then
1726 if [ -n "$headrv" ]; then
1727 upref
="$(git rev-parse --revs-only --symbolic-full-name @{upstream} -- 2>/dev/null)" ||
:
1728 if [ -n "$upref" ]; then
1729 uprefpart
=" ... ${upref#$abbrev/remotes/}"
1730 mbase
="$(git merge-base HEAD "$upref")" ||
:
1731 ahead
="$(git rev-list --count HEAD ${mbase:+--not} $mbase)" || ahead
=0
1732 behind
="$(git rev-list --count "$upref" ${mbase:+--not} $mbase)" || behind
=0
1733 [ "$ahead$behind" = "00" ] || uprefpart
="$uprefpart ["
1734 [ "$ahead" = "0" ] || uprefpart
="${uprefpart}ahead $ahead"
1735 [ "$ahead" = "0" ] ||
[ "$behind" = "0" ] || uprefpart
="$uprefpart, "
1736 [ "$behind" = "0" ] || uprefpart
="${uprefpart}behind $behind"
1737 [ "$ahead$behind" = "00" ] || uprefpart
="$uprefpart]"
1740 echol
"${pfx}HEAD -> ${symref#$abbrev/heads/} [${headrv:-unborn}]$uprefpart"
1742 echol
"${pfx}HEAD -> ${headrv:-?}"
1744 if [ -n "$tg_state" ]; then
1746 if [ "$tg_state" = "update" ]; then
1747 IFS
= read -r uname
<"$git_dir/tg-update/name" ||
:
1749 extra
="; currently updating branch '$uname'"
1751 echol
"${pfx}tg $tg_state in progress$extra"
1752 if [ -s "$git_dir/tg-update/fullcmd" ] && [ -s "$git_dir/tg-update/names" ]; then
1753 printf "${pfx}You are currently updating as a result of:\n${pfx} "
1754 cat "$git_dir/tg-update/fullcmd"
1755 bcnt
="$(( $(wc -w < "$git_dir/tg-update
/names
") ))"
1756 if [ $bcnt -gt 1 ]; then
1758 ! [ -s "$git_dir/tg-update/processed" ] ||
1759 pcnt
="$(( $(wc -w < "$git_dir/tg-update
/processed
") ))"
1760 echo "${pfx}$pcnt of $bcnt branches updated so far"
1763 if [ "$tg_state" = "update" ]; then
1764 echol
"${pfx} (use \"$tgdisplayac update --continue\" to continue)"
1765 echol
"${pfx} (use \"$tgdisplayac update --skip\" to skip this branch and continue)"
1766 echol
"${pfx} (use \"$tgdisplayac update --stop\" to stop and retain changes so far)"
1767 echol
"${pfx} (use \"$tgdisplayac update --abort\" to restore pre-update state)"
1770 [ -z "$git_state" ] ||
echo "${pfx}git $git_state in progress"
1771 if [ "$git_state" = "merge" ]; then
1772 ucnt
="$(( $(git ls-files --unmerged --full-name --abbrev :/ | wc -l) ))"
1773 if [ $ucnt -gt 0 ]; then
1774 echo "${pfx}"'fix conflicts and then "git commit" the result'
1776 echo "${pfx}"'all conflicts fixed; run "git commit" to record result'
1779 if [ -z "$git_state" ]; then
1780 setup_git_dir_is_bare
1781 [ -z "$git_dir_is_bare" ] ||
return 0
1782 gsp
="$(git status --porcelain 2>/dev/null)" ||
return 0 # bare repository???
1785 gspcnt
="$(( $(printf '%s\n' "$gsp" | sed -n '/^??/!p' | wc -l) ))"
1787 if [ "$gspcnt" -eq 0 ]; then
1788 [ -z "$gsp" ] || untr
="; non-ignored, untracked files present"
1789 echo "${pfx}working directory is clean$untr"
1790 [ -n "$tg_state" ] || do_status_result
=0
1792 echo "${pfx}working directory is DIRTY"
1793 [ -z "$do_status_verbose" ] || git status
--short --untracked-files=no
1806 # pass "diff" to get pager.diff
1807 # if pager.$1 is a boolean false returns cat
1808 # if set to true or unset fails
1809 # otherwise succeeds and returns the value
1812 if _x
="$(git config --bool "pager.
$1" 2>/dev/null)"; then
1813 [ "$_x" != "true" ] ||
return 1
1817 if _x
="$(git config "pager.
$1" 2>/dev/null)"; then
1825 # Set TG_PAGER to a valid executable
1826 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
1827 # See also the following "page" function for ease of use
1828 # emptypager will be set to 1 (otherwise empty) if TG_PAGER was set to "cat" to not be empty
1829 # Preference is (same as Git):
1831 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
1832 # 3. core.pager (only if set)
1834 # 5. git var GIT_PAGER
1838 isatty
1 ||
{ emptypager
=1; TG_PAGER
=cat; return 0; }
1841 if [ -z "$TG_PAGER_IN_USE" ]; then
1842 # TG_PAGER = GIT_PAGER | PAGER | less
1843 # NOTE: GIT_PAGER='' is significant
1844 if [ -n "${GIT_PAGER+set}" ]; then
1845 TG_PAGER
="$GIT_PAGER"
1846 elif [ -n "$USE_PAGER_TYPE" ] && _dp
="$(get_pager "$USE_PAGER_TYPE")"; then
1848 elif _cp
="$(git config core.pager 2>/dev/null)"; then
1850 elif [ -n "${PAGER+set}" ]; then
1853 _gp
="$(git var GIT_PAGER 2>/dev/null)" ||
:
1854 [ "$_gp" != ":" ] || _gp
=
1855 TG_PAGER
="${_gp:-less}"
1857 if [ -z "$TG_PAGER" ]; then
1866 # Set pager default environment variables
1867 # see pager.c:setup_pager
1868 if [ -z "${LESS+set}" ]; then
1872 if [ -z "${LV+set}" ]; then
1877 # this is needed so e.g. $(git diff) will still colorize it's output if
1878 # requested in ~/.gitconfig with color.diff=auto
1880 export GIT_PAGER_IN_USE
1882 # this is needed so we don't get nested pagers
1884 export TG_PAGER_IN_USE
1887 # page eval_arg [arg ...]
1889 # Calls setup_pager then evals the first argument passing it all the rest
1890 # where the output is piped through eval "$TG_PAGER" unless emptypager is set
1891 # by setup_pager (in which case the output is left as-is).
1893 # To handle arbitrary paging duties, collect lines to be paged into a
1894 # function and then call page with the function name or perhaps func_name "$@".
1896 # If no arguments at all are passed in do nothing (return with success).
1899 [ $# -gt 0 ] ||
return 0
1901 _evalarg
="$1"; shift
1902 if [ -n "$emptypager" ]; then
1903 eval "$_evalarg" '"$@"'
1905 { eval "$_evalarg" '"$@"';} |
eval "$TG_PAGER"
1909 # get_temp NAME [-d]
1910 # creates a new temporary file (or directory with -d) in the global
1911 # temporary directory $tg_tmp_dir with pattern prefix NAME
1914 mktemp
$2 "$tg_tmp_dir/$1.XXXXXX"
1917 # automatically called by strftime
1918 # does nothing if already setup
1919 # may be called explicitly if the first call would otherwise be in a subshell
1920 # so that the setup is only done once before subshells start being spawned
1923 [ -z "$strftime_is_setup" ] ||
return 0
1925 # date option to format raw epoch seconds values
1928 _testdt
='2000-02-29 07:03:08 UTC'
1929 _testfm
='%Y-%m-%d %H:%M:%S %Z'
1930 if [ "$(TZ=UTC date "-d@
$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1932 elif [ "$(TZ=UTC date "-r$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1938 # $1 => strftime format string to use
1939 # $2 => raw timestamp as seconds since epoch
1940 # $3 => optional time zone string (empty/absent for local time zone)
1944 if [ -n "$daterawopt" ]; then
1945 if [ -n "$3" ]; then
1946 TZ
="$3" date "$daterawopt$2" "+$1"
1948 date "$daterawopt$2" "+$1"
1951 if [ -n "$3" ]; then
1952 TZ
="$3" perl
-MPOSIX=strftime
-le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1954 perl
-MPOSIX=strftime
-le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1963 if [ -z "$got_cdup_result" ]; then
1964 git_cdup_result
="$(git rev-parse --show-cdup)"
1967 [ -z "$1" ] ||
eval "$1="'"$git_cdup_result"'
1970 git_dir_is_bare_setup
=
1971 setup_git_dir_is_bare
()
1973 if [ -z "$git_dir_is_bare_setup" ]; then
1974 git_dir_is_bare
="$(git rev-parse --is-bare-repository)"
1975 [ z
"$git_dir_is_bare" = z
"true" ] || git_dir_is_bare
=
1976 git_dir_is_bare_setup
=1
1980 git_hooks_pat_list
="\
1981 [a]pplypatch-ms[g] [p]re-applypatc[h] [p]ost-applypatc[h] [p]re-commi[t] \
1982 [p]repare-commit-ms[g] [c]ommit-ms[g] [p]ost-commi[t] [p]re-rebas[e] \
1983 [p]ost-checkou[t] [p]ost-merg[e] [p]re-pus[h] [p]re-receiv[e] [u]pdat[e] \
1984 [p]ost-receiv[e] [p]ost-updat[e] [p]ush-to-checkou[t] [p]re-auto-g[c] \
1987 # git_hooks_dir must already be set to the value of core.hooksPath which
1988 # exists and is an absolute path. The first and only argument is the
1989 # "pwd -P" of the $git_hooks_dir directory. If the core.hooksPath setting
1990 # appears to be "friendly" attempt to alter it to be an absolute path to
1991 # "$git_common_dir/hooks" instead. A "friendly" core.hooksPath setting
1992 # points to a directory for which "$git_common_dir/hooks" already has
1993 # entries which are symbolic links to the same core.hooksPath items.
1994 # There's no POSIX readlink utility, but there is a 'cmp -s' utility so we
1995 # use that instead to check. Also the "friendly" core.hooksPath must be
1996 # something that's recognizable as belonging to a "friendly".
1997 maybe_adjust_friendly_hooks_path
()
1999 case "$1" in */_global
/hooks
);;*) return 0; esac
2000 [ -n "$1" ] && [ -d "$1" ] && [ -d "$git_common_dir/hooks" ] ||
return 0
2001 [ -w "$git_common_dir" ] ||
return 0
2002 ! [ -e "$git_common_dir/config" ] ||
{
2003 [ -f "$git_common_dir/config" ] && [ -w "$git_common_dir/config" ]
2006 for ghook
in $
(cd "$1" && eval "echo $git_hooks_pat_list"); do
2007 case "$ghook" in "["*) continue; esac
2008 [ -x "$1/$ghook" ] &&
2009 [ -f "$1/$ghook" ] ||
continue
2010 [ -x "$git_common_dir/hooks/$ghook" ] &&
2011 [ -f "$git_common_dir/hooks/$ghook" ] &&
2012 cmp -s "$1/$ghook" "$git_common_dir/hooks/$ghook" ||
{
2017 if [ -n "$oktoswitch" ]; then
2018 # a known "friendly" was detected and the hooks match;
2019 # go ahead and silently switch the path
2020 ! git config core.hooksPath
"$git_common_dir/hooks" >/dev
/null
2>&1 ||
2021 git_hooks_dir
="$git_common_dir/hooks"
2028 setup_git_hooks_dir
()
2030 [ -z "$git_hooks_dir" ] ||
return 0
2031 git_hooks_dir
="$git_common_dir/hooks"
2032 if vcmp
"$git_version" '>=' "2.9" && gchp
="$(git config --path --get core.hooksPath 2>/dev/null)" && [ -n "$gchp" ]; then
2035 if [ -d "$gchp" ]; then
2036 # if core.hooksPath is just another name for
2037 # $git_common_dir/hooks, keep referring to it
2038 # by $git_common_dir/hooks
2039 abscdh
="$(cd "$git_common_dir" && pwd -P)/hooks"
2040 abshpd
="$(cd "$gchp" && pwd -P)"
2041 if [ "$abshpd" != "$abscdh" ]; then
2042 git_hooks_dir
="$gchp"
2043 maybe_adjust_friendly_hooks_path
"$abshpd"
2045 unset_ abscdh abshpd
2047 [ -n "$1" ] || warn
"ignoring non-existent core.hooksPath: $gchp"
2051 [ -n "$1" ] || warn
"ignoring non-absolute core.hooksPath: $gchp"
2060 [ -n "$git_dir" ] || git_dir
="$(git rev-parse --git-dir)"
2061 if [ -n "$git_dir" ] && [ -d "$git_dir" ]; then
2062 git_dir
="$(cd "$git_dir" && pwd)"
2064 if [ -z "$git_common_dir" ]; then
2065 if vcmp
"$git_version" '>=' "2.5"; then
2066 # rev-parse --git-common-dir is broken and may give
2067 # an incorrect result unless the current directory is
2068 # already set to the top level directory
2070 git_common_dir
="$(cd ".
/$git_cdup_result" && cd "$
(git rev-parse
--git-common-dir)" && pwd)"
2072 git_common_dir
="$git_dir"
2075 [ -n "$git_dir" ] && [ -n "$git_common_dir" ] &&
2076 [ -d "$git_dir" ] && [ -d "$git_common_dir" ] || die
"Not a git repository"
2079 basic_setup_remote
()
2081 if [ -z "$base_remote" ]; then
2082 if [ "${TG_EXPLICIT_REMOTE+set}" = "set" ]; then
2083 base_remote
="$TG_EXPLICIT_REMOTE"
2085 base_remote
="$(git config topgit.remote 2>/dev/null)" ||
:
2094 tgsequester
="$(git config --bool topgit.sequester 2>/dev/null)" ||
:
2096 [ "$tgsequester" != "false" ] || tgnosequester
=1
2099 # catch errors if topbases is used without being set
2100 unset_ tg_topbases_set
2101 topbases
="programmer*:error"
2102 topbasesrx
="programmer*:error}"
2103 oldbases
="$topbases"
2108 test -z "$tg_tmp_dir" ||
! test -d "$tg_tmp_dir" ||
${TG_DEBUG:+echo} rm -rf "$tg_tmp_dir" >&2 ||
:
2113 [ -z "$tg_tmp_dir" ] ||
return 0
2114 if [ -n "$TG_TMPDIR" ] && [ -d "$TG_TMPDIR" ] && [ -w "$TG_TMPDIR" ] &&
2115 { >"$TG_TMPDIR/.check"; } >/dev
/null
2>&1; then
2116 tg_tmp_dir
="$TG_TMPDIR"
2119 TRAPEXIT_
='tmpdir_cleanup'
2120 trap 'trapexit_ 129' HUP
2121 trap 'trapexit_ 130' INT
2122 trap 'trapexit_ 131' QUIT
2123 trap 'trapexit_ 134' ABRT
2124 trap 'trapexit_ 141' PIPE
2125 trap 'trapexit_ 143' TERM
2126 tg_tmp_dir
="$(mktemp -d "$git_dir/tg-tmp.XXXXXX
" 2>/dev/null)" || tg_tmp_dir
=
2127 [ -n "$tg_tmp_dir" ] || tg_tmp_dir
="$(mktemp -d "${TMPDIR:-/tmp}/tg-tmp.XXXXXX
" 2>/dev/null)" || tg_tmp_dir
=
2128 [ -n "$tg_tmp_dir" ] ||
[ -z "$TMPDIR" ] || tg_tmp_dir
="$(mktemp -d "/tmp
/tg-tmp.XXXXXX
" 2>/dev/null)" || tg_tmp_dir
=
2129 [ -z "$tg_tmp_dir" ] || tg_tmp_dir
="$(cd "$tg_tmp_dir" && pwd -P)"
2131 [ -n "$tg_tmp_dir" ] && [ -w "$tg_tmp_dir" ] && { >"$tg_tmp_dir/.check"; } >/dev
/null
2>&1 ||
2132 die
"could not create a writable temporary directory"
2134 # whenever tg_tmp_dir is != "" these must always be set
2135 tg_ref_cache
="$tg_tmp_dir/tg~ref-cache"
2136 tg_ref_cache_br
="$tg_ref_cache.br"
2137 tg_ref_cache_rbr
="$tg_ref_cache.rbr"
2138 tg_ref_cache_ann
="$tg_ref_cache.ann"
2139 tg_ref_cache_dep
="$tg_ref_cache.dep"
2144 [ -z "$tg_cache_dir" ] ||
return 0
2145 user_id_no
="$(id -u)" ||
:
2146 : "${user_id_no:=_99_}"
2147 tg_cache_dir
="$git_common_dir/tg-cache"
2148 [ -d "$tg_cache_dir" ] || mkdir
"$tg_cache_dir" >/dev
/null
2>&1 || tg_cache_dir
=
2149 [ -z "$tg_cache_dir" ] || tg_cache_dir
="$tg_cache_dir/$user_id_no"
2150 [ -z "$tg_cache_dir" ] ||
[ -d "$tg_cache_dir" ] || mkdir
"$tg_cache_dir" >/dev
/null
2>&1 || tg_cache_dir
=
2151 [ -z "$tg_cache_dir" ] ||
{ >"$tg_cache_dir/.tgcache"; } >/dev
/null
2>&1 || tg_cache_dir
=
2152 if [ -z "$tg_cache_dir" ]; then
2153 tg_cache_dir
="$tg_tmp_dir/tg-cache"
2154 [ -d "$tg_cache_dir" ] || mkdir
"$tg_cache_dir" >/dev
/null
2>&1 || tg_cache_dir
=
2155 [ -z "$tg_cache_dir" ] ||
{ >"$tg_cache_dir/.tgcache"; } >/dev
/null
2>&1 || tg_cache_dir
=
2157 [ -n "$tg_cache_dir" ] ||
2158 die
"could not create a writable tg-cache directory (even a temporary one)"
2160 if [ -n "$2" ]; then
2161 # allow the wayback machine to share a separate cache
2162 [ -d "$tg_cache_dir/wayback" ] || mkdir
"$tg_cache_dir/wayback" >/dev
/null
2>&1 ||
:
2163 ! [ -d "$tg_cache_dir/wayback" ] ||
! { >"$tg_cache_dir/wayback/.tgcache"; } >/dev
/null
2>&1 ||
2164 tg_cache_dir
="$tg_cache_dir/wayback"
2168 # set up alternate deb dirs
2171 # GIT_ALTERNATE_OBJECT_DIRECTORIES can contain double-quoted entries
2172 # since Git v2.11.1; however, it's only necessary for : (or perhaps ;)
2173 # so we avoid it if possible and require v2.11.1 to do it at all
2174 # otherwise just don't make an alternates temporary store in that case;
2175 # it's okay to not have one; everything will still work; the nicety of
2176 # making the temporary tree objects vanish when tg exits just won't
2177 # happen in that case but nothing will break also be sure to reuse
2178 # the parent's if we've been recursively invoked and it's for the
2179 # same repository we were invoked on
2183 _odbdir
="${GIT_OBJECT_DIRECTORY:-$git_common_dir/objects}"
2184 [ -n "$_odbdir" ] && [ -d "$_odbdir" ] && _fullodbdir
="$(cd "$_odbdir" && pwd -P)" ||
2185 die
"could not find objects directory"
2186 if [ -n "$TG_OBJECT_DIRECTORY" ] && [ -d "$TG_OBJECT_DIRECTORY/info" ] &&
2187 [ -f "$TG_OBJECT_DIRECTORY/info/alternates" ] && [ -r "$TG_OBJECT_DIRECTORY/info/alternates" ]; then
2188 if IFS
= read -r _otherodbdir
<"$TG_OBJECT_DIRECTORY/info/alternates" &&
2189 [ -n "$_otherodbdir" ] && [ "$_otherodbdir" = "$_fullodbdir" ]; then
2193 _fulltmpdir
="$(cd "$tg_tmp_dir" && pwd -P)"
2194 if [ "$tg_use_alt_odb" = "1" ]; then
2195 # create an alternate objects database to keep the ephemeral objects in
2196 mkdir
-p "$tg_tmp_dir/objects/info"
2197 TG_OBJECT_DIRECTORY
="$_fulltmpdir/objects"
2198 [ "$_fullodbdir" = "$TG_OBJECT_DIRECTORY" ] ||
2199 echol
"$_fullodbdir" >"$tg_tmp_dir/objects/info/alternates"
2201 case "$_fulltmpdir" in *[";:"]*|
'"'*) vcmp
"$git_version" '>=' "2.11.1" || tg_use_alt_odb
=; esac
2202 if [ "$tg_use_alt_odb" = "1" ]; then
2203 case "$TG_OBJECT_DIRECTORY" in
2205 # surround in "..." and backslash-escape internal '"' and '\\'
2206 _altodbdq
="\"$(printf '%s\n' "$TG_OBJECT_DIRECTORY" |
2207 sed 's/\([""\\]\)/\\\1/g')\""
2210 _altodbdq
="$TG_OBJECT_DIRECTORY"
2213 TG_PRESERVED_ALTERNATES
="$GIT_ALTERNATE_OBJECT_DIRECTORIES"
2214 if [ -n "$GIT_ALTERNATE_OBJECT_DIRECTORIES" ]; then
2215 GIT_ALTERNATE_OBJECT_DIRECTORIES
="$_altodbdq:$GIT_ALTERNATE_OBJECT_DIRECTORIES"
2217 GIT_ALTERNATE_OBJECT_DIRECTORIES
="$_altodbdq"
2219 export TG_PRESERVED_ALTERNATES TG_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES
2220 if [ -n "$GIT_OBJECT_DIRECTORY" ]; then
2221 export GIT_OBJECT_DIRECTORY
2223 unset_ GIT_OBJECT_DIRECTORY
2230 if [ "${TG_PRESERVED_ALTERNATES+set}" = "set" ]; then
2231 GIT_ALTERNATE_OBJECT_DIRECTORIES
="$TG_PRESERVED_ALTERNATES"
2232 if [ -n "$GIT_ALTERNATE_OBJECT_DIRECTORIES" ]; then
2233 export GIT_ALTERNATE_OBJECT_DIRECTORIES
2235 unset_ GIT_ALTERNATE_OBJECT_DIRECTORIES
2238 unset_ TG_TMPDIR TG_OBJECT_DIRECTORY TG_PRESERVED_ALTERNATES tg_use_alt_odb
2244 # suppress the merge log editor feature since git 1.7.10
2246 GIT_MERGE_AUTOEDIT
=no
2247 export GIT_MERGE_AUTOEDIT
2251 ! vcmp
"$git_version" '>=' "2.5" || iowopt
="--ignore-other-worktrees"
2253 ! vcmp
"$git_version" '>=' "2.6" || gcfbopt
="--buffer"
2255 ! vcmp
"$git_version" '>=' "2.9" || auhopt
="--allow-unrelated-histories"
2256 v_get_show_cdup root_dir
2257 root_dir
="${root_dir:-.}"
2258 logrefupdates
="$(git config --bool core.logallrefupdates 2>/dev/null)" ||
:
2259 [ "$logrefupdates" = "true" ] || logrefupdates
=
2261 # make sure root_dir doesn't end with a trailing slash.
2263 root_dir
="${root_dir%/}"
2265 # create global temporary and cache directories, usually inside GIT_DIR
2271 # the wayback machine directory serves as its own "altodb"
2272 [ -n "$wayback" ] || altodb_setup
2275 activate_wayback_machine
()
2277 [ -n "${1#:}" ] ||
[ -n "$2" ] ||
{ wayback
=; return 0; }
2283 if [ -n "${1#:}" ]; then
2284 tgwbr
="$(get_temp wbinfo)"
2286 tg revert
--list --no-short "${1#:}" >"$tgwbr" && test -s "$tgwbr" ||
return 1
2287 # tg revert will likely leave a revert-tag-only cache which is not what we want
2290 cachedir_setup
"" 1 # use a separate wayback cache dir
2291 # but don't step on the normal one if the separate one could not be set up
2292 case "$tg_cache_dir" in */wayback
);;*) tg_cache_dir
=; esac
2293 altodb
="$TG_OBJECT_DIRECTORY"
2294 if [ -n "$3" ] && [ -n "$2" ]; then
2295 [ -d "$3" ] ||
{ mkdir
-p "$3" && [ -d "$3" ]; } ||
2296 die
"could not create wayback directory: $3"
2297 tg_wayback_dir
="$(cd "$3" && pwd -P)" || die
"could not get wayback directory full path"
2298 [ -d "$tg_wayback_dir/.git" ] ||
{ mkdir
-p "$tg_wayback_dir/.git" && [ -d "$tg_wayback_dir/.git" ]; } ||
2299 die
"could not initialize wayback directory: $3"
2300 is_empty_dir
"$tg_wayback_dir" ".git" && is_empty_dir
"$tg_wayback_dir/.git" "." ||
2301 die
"wayback directory is not empty: $3"
2302 mkdir
"$tg_wayback_dir/.git/objects"
2303 mkdir
"$tg_wayback_dir/.git/objects/info"
2304 cat "$altodb/info/alternates" >"$tg_wayback_dir/.git/objects/info/alternates"
2306 tg_wayback_dir
="$tg_tmp_dir/wayback"
2307 mkdir
"$tg_wayback_dir"
2308 mkdir
"$tg_wayback_dir/.git"
2309 ln -s "$altodb" "$tg_wayback_dir/.git/objects"
2311 mkdir
"$tg_wayback_dir/.git/refs"
2312 printf '0 Wayback Machine' >"$tg_wayback_dir/.git/gc.pid"
2313 qpesc
="$(printf '%s\n' "$git_common_dir" | sed -e 's/\([\\""]\)/\\\1/g' -e '$!s/$/\\n/' | tr -d '\n')"
2315 [ -z "$2" ] || laru
="true"
2318 path = \"$qpesc/config\"
2321 logAllRefUpdates = $laru
2322 repositoryFormatVersion = 0
2324 preciousObjects = true
2330 [remote \"wayback\"]
2336 wayback-updates = fetch -u --force --no-tags --dry-run wayback refs/*:refs/*
2337 " >"$tg_wayback_dir/.git/config"
2338 cat "$git_dir/HEAD" >"$tg_wayback_dir/.git/HEAD"
2339 case "$1" in ":"?
*);;*)
2340 git show-ref
>"$tg_wayback_dir/.git/packed-refs"
2341 git
--git-dir="$tg_wayback_dir/.git" pack-refs
--all
2344 TG_OBJECT_DIRECTORY
="$altodb" && export TG_OBJECT_DIRECTORY
2345 if [ -n "${1#:}" ]; then
2346 <"$tgwbr" sed 's/^\([^ ][^ ]*\) \([^ ][^ ]*\)$/update \2 \1/' |
2347 git
--git-dir="$tg_wayback_dir/.git" update-ref
-m "wayback to $1" ${2:+--create-reflog} --stdin
2349 if test -n "$2"; then
2350 # extra setup for potential shell
2351 qpesc2
="$(printf '%s\n' "$git_common_dir" | sed -e 's/\([\\""]\)/\\\\\1/g' -e '$!s/$/\\n/' | tr -d '\n')"
2352 printf '\twayback-repository = "!printf '\''%%s\\\\n'\'' \\"%s\\""\n' "$qpesc2" >>"$tg_wayback_dir/.git/config"
2353 qtesc
="$(printf '%s\n' "${1:-:}" | sed 's/\([""]\)/\\\1/g')"
2354 printf '\twayback-tag = "!printf '\''%%s\\\\n'\'' \\"%s\\""\n' "$qtesc" >>"$tg_wayback_dir/.git/config"
2355 if [ -d "$git_common_dir/rr-cache" ]; then
2356 ln -s "$git_common_dir/rr-cache" "$tg_wayback_dir/.git/rr-cache"
2357 printf "[rerere]\n\tenabled = true\n" >>"$tg_wayback_dir/.git/config"
2359 if [ z
"$2" != z
"2" ]; then
2362 if [ -n "${1#:}" ]; then
2363 [ -n "$tgwbr2" ] || tgwbr2
="$(get_temp wbtag)"
2364 git
--git-dir="$git_common_dir" cat-file tag
"${1#:}" >"$tgwbr2" ||
return 1
2365 wbprnt
="${lf}parent $(git --git-dir="$git_common_dir" rev-parse --verify --quiet "${1#:}"^0 -- 2>/dev/null)" || wbprnt
=
2366 wbauth
="$(<"$tgwbr2" awk '{if(!$0)exit;if($1=="tagger
")print "author
" substr($0,7)}')"
2368 wbcmtr
="committer Wayback Machine <-> $(date "+%s
%z
")"
2369 [ -n "$wbauth" ] || wbauth
="author${wbcmtr#committer}"
2370 wbtree
="$(git --git-dir="$tg_wayback_dir/.git
" mktree </dev/null)"
2372 printf '%s\n' "tree
$wbtree$wbprnt" "$wbauth" "$wbcmtr" ""
2373 if [ -n "$tgwbr2" ]; then
2374 <"$tgwbr2" sed -e '1,/^$/d' -e '/^-----BEGIN/,$d' | git stripspace
2376 echo "Wayback Machine
"
2378 } | git --git-dir="$tg_wayback_dir/.git
" hash-object -t commit -w --stdin)"
2379 test -n "$wbcmt" ||
return 1
2380 echo "$wbcmt" >"$tg_wayback_dir/.git/HEAD"
2383 cd "$tg_wayback_dir"
2384 unset git_dir git_common_dir
2389 # refer to "top-bases" in a refname with $topbases
2391 [ -z "$tg_topbases_set" ] ||
return 0
2393 topbases_implicit_default
=1
2394 # See if topgit.top-bases is set to heads or refs
2395 tgtb
="$(git config "topgit.top-bases
" 2>/dev/null)" ||
:
2396 if [ -n "$tgtb" ] && [ "$tgtb" != "heads" ] && [ "$tgtb" != "refs" ]; then
2397 if [ -n "$1" ]; then
2398 # never die on the hook script
2401 die
"invalid \"topgit.top-bases\" setting (must be \"heads\" or \"refs\")"
2404 if [ -n "$tgtb" ]; then
2407 topbases
="heads/{top-bases}"
2408 topbasesrx
="heads/[{]top-bases[}]"
2409 oldbases
="top-bases";;
2411 topbases
="top-bases"
2412 topbasesrx
="top-bases"
2413 oldbases
="heads/{top-bases}";;
2415 # MUST NOT be exported
2416 unset_ tgtb tg_topbases_set topbases_implicit_default
2422 # check heads and top-bases and see what state the current
2423 # repository is in. remotes are ignored.
2427 git for-each-ref --format='%(refname)' "refs
/heads
" "refs
/top-bases
" 2>/dev/null |
2428 run_awk_ref_prefixes ${1:+-e} -n -- "refs
/heads
/{top-bases
}" "refs
/top-bases
" "refs
/heads
")" ||
2430 if [ "$rc" = "65" ]; then
2432 err
"repository contains existing TopGit branches"
2433 err
"but some use refs/top-bases/... for the base"
2434 err
"and some use refs/heads/{top-bases}/... for the base"
2435 err
"with the latter being the new, preferred location"
2436 err
"set \"topgit.top-bases\" to either \"heads\" to use"
2437 err
"the new heads/{top-bases} location or \"refs\" to use"
2438 err
"the old top-bases location."
2439 err
"(the tg migrate-bases command can also resolve this issue)"
2440 die
"schizophrenic repository requires topgit.top-bases setting"
2442 [ -z "$activebases" ] || unset_ topbases_implicit_default
2443 if [ "$activebases" = "refs/heads/{top-bases}" ]; then
2444 topbases
="heads/{top-bases}"
2445 topbasesrx
="heads/[{]top-bases[}]"
2446 oldbases
="top-bases"
2448 # default is still top-bases for now
2449 topbases
="top-bases"
2450 topbasesrx
="top-bases"
2451 oldbases
="heads/{top-bases}"
2453 # MUST NOT be exported
2454 unset_ rc activebases tg_topases_set
2459 # $1 is remote name to check
2460 # $2 is optional variable name to set to result of check
2461 # $3 is optional command name to use in message (defaults to $cmd)
2462 # Fatal error if remote has schizophrenic top-bases
2463 # No error (and $2, if provided, will be set to empty) if remote has no top-bases at all
2464 check_remote_topbases
()
2466 [ -n "$1" ] || die
"programmer error: check_remote_topbases called with no remote argument"
2467 _crrc
=0 _crremotebases
=
2469 git for-each-ref --format='%(refname)' "refs
/remotes
/$1" 2>/dev/null |
2470 run_awk_ref_prefixes -n -- "refs
/remotes
/$1/{top-bases
}" "refs
/remotes
/$1/top-bases
" "refs
/remotes
/$1")" ||
2472 if [ "$_crrc" = "65" ]; then
2473 err
"remote \"$1\" has top-bases in both locations:"
2474 err
" refs/remotes/$1/{top-bases}/..."
2475 err
" refs/remotes/$1/top-bases/..."
2476 err
"set \"topgit.top-bases\" to \"heads\" for the first, preferred location"
2477 err
"or set \"topgit.top-bases\" to \"refs\" for the second, old location"
2478 err
"(the \"-c topgit.top-bases=<val>\" option can be used for this)"
2479 err
"then re-run the tg ${3:-$cmd} command"
2480 err
"(the tg migrate-bases command can also help with this problem)"
2481 die
"schizophrenic remote \"$1\" requires topgit.top-bases setting"
2483 [ "$_crrc" != "66" ] || _crremotebases
= # just to be sure
2484 [ -z "$2" ] ||
eval "$2="'"$_crremotebases"'
2485 unset _crrc _crremotebases
2490 # if "$logrefupdates" is set and ref is not under refs/heads/ then force
2491 # an empty log file to exist so that ref changes will be logged
2492 # "$1" must be a fully-qualified refname (i.e. start with "refs/")
2493 # However, if "$1" is "refs/tgstash" then always make the reflog
2494 # The only ref not under refs/ that Git will write a reflog for is HEAD;
2495 # no matter what, it will NOT update a reflog for any other bare refs so
2496 # just quietly succeed when passed TG_STASH without doing anything.
2499 [ -n "$1" ] && [ "$1" != "TG_STASH" ] ||
return 0
2500 [ -n "$logrefupdates" ] ||
[ "$1" = "refs/tgstash" ] ||
return 0
2501 case "$1" in refs
/heads
/*|HEAD
) return 0;; refs
/*[!/]);; *) return 1; esac
2502 mkdir
-p "$git_common_dir/logs/${1%/*}" 2>/dev
/null ||
:
2503 { >>"$git_common_dir/logs/$1" ||
:; } 2>/dev
/null
2506 # store the "realpath" for "$2" in "$1" except the leaf is not resolved if it's
2507 # a symbolic link. The directory part must exist, but the basename need not.
2510 [ -n "$1" ] && [ -n "$2" ] ||
return 1
2511 set -- "$1" "$2" "${2%/}"
2513 */*) set -- "$1" "$2" "${3%/*}";;
2514 * ) set -- "$1" "$2" ".";;
2517 set -- "$1" "${2%/}" "$3" "/"
2519 [ -d "$3" ] ||
return 1
2520 eval "$1="'"$(cd "$3" && pwd -P)/${2##*/}$4"'
2525 : "${TG_INST_CMDDIR:=@cmddir@}"
2526 : "${TG_INST_SHAREDIR:=@sharedir@}"
2527 : "${TG_INST_HOOKSDIR:=@hooksdir@}"
2529 [ -d "$TG_INST_CMDDIR" ] ||
2530 die
"No command directory: '$TG_INST_CMDDIR'"
2532 ## Include awk scripts and their utility functions (separated for easier debugging)
2534 [ -f "$TG_INST_CMDDIR/tg--awksome" ] && [ -r "$TG_INST_CMDDIR/tg--awksome" ] ||
2535 die
"Missing awk scripts: '$TG_INST_CMDDIR/tg--awksome'"
2536 .
"$TG_INST_CMDDIR/tg--awksome"
2538 if [ -n "$tg__include" ]; then
2540 # We were sourced from another script for our utility functions;
2541 # this is set by hooks. Skip the rest of the file. A simple return doesn't
2542 # work as expected in every shell. See http://bugs.debian.org/516188
2544 # ensure setup happens
2556 case "$tgdir" in */*);;*) tgdir
="./$tgdir"; esac
2557 tgdir
="${tgdir%/*}/"
2558 tgname
="${tgbin##*/}"
2559 [ "$0" != "$tgname" ] || tgdir
=""
2561 # If tg contains a '/' but does not start with one then replace it with an absolute path
2563 case "$0" in /*) ;; */*)
2564 tgdir
="$(cd "${0%/*}" && pwd -P)/"
2565 tgbin
="$tgdir$tgname"
2568 # tgdisplay will include any explicit -C <dir> etc. options whereas tgname will not
2569 # tgdisplayac is the same as tgdisplay but without any -r or -u options (ac => abort/continue)
2571 tgdisplaydir
="$tgdir"
2573 tgdisplayac
="$tgdisplay"
2575 v_get_abs_path _tgnameabs
"$(cmd_path "$tgname")" &&
2576 _tgabs
="$_tgnameabs" &&
2577 { [ "$tgbin" = "$tgname" ] || v_get_abs_path _tgabs
"$tgbin"; } &&
2578 [ "$_tgabs" = "$_tgnameabs" ]
2582 tgdisplayac
="$tgdisplay"
2584 [ -z "$_tgabs" ] || tgbin
="$_tgabs"
2585 unset_ _tgabs _tgnameabs
2588 TG_TMPDIR
="$tg_tmp_dir" && export TG_TMPDIR
&&
2600 while :; do case "$1" in
2636 --pager|
-p|
--paginate)
2642 if [ -z "$1" ]; then
2643 echo "Option -r requires an argument." >&2
2649 explicit_remote
="$base_remote"
2650 tgdisplay
="$tgdisplaydir$tgname$gitcdopt -r $explicit_remote"
2651 TG_EXPLICIT_REMOTE
="$base_remote" && export TG_EXPLICIT_REMOTE
2655 unset_ base_remote explicit_remote
2657 tgdisplay
="$tgdisplaydir$tgname$gitcdopt -u"
2658 TG_EXPLICIT_REMOTE
= && export TG_EXPLICIT_REMOTE
2663 if [ -z "$1" ]; then
2664 echo "Option -C requires an argument." >&2
2669 unset_ GIT_DIR GIT_COMMON_DIR
2670 if [ -z "$explicit_dir" ]; then
2675 gitcdopt
=" -C \"$explicit_dir\""
2676 [ "$explicit_dir" != "." ] || explicit_dir
="." gitcdopt
=" -C ."
2677 tgdisplay
="$tgdisplaydir$tgname$gitcdopt"
2678 tgdisplayac
="$tgdisplay"
2679 [ -z "$explicit_remote" ] || tgdisplay
="$tgdisplay -r $explicit_remote"
2680 [ -z "$noremote" ] || tgdisplay
="$tgdisplay -u"
2685 if [ -z "$1" ]; then
2686 echo "Option -c requires an argument." >&2
2690 param
="'$(printf '%s\n' "$1" | sed "s
/[']/'\\\\''/g
")'"
2691 GIT_CONFIG_PARAMETERS
="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }$param"
2692 export GIT_CONFIG_PARAMETERS
2696 if [ -n "$wayback" ]; then
2697 echo "Option -w may be used at most once." >&2
2702 if [ -z "$1" ]; then
2703 echo "Option -w requires an argument." >&2
2715 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
2723 if [ z
"$forcepager" = z
"0" ]; then
2724 GIT_PAGER_IN_USE
=1 TG_PAGER_IN_USE
=1 &&
2725 export GIT_PAGER_IN_USE TG_PAGER_IN_USE
2728 [ -n "$cmd" ] ||
[ $# -lt 1 ] ||
{ cmd
="$1"; shift; }
2732 [ -n "$cmd" ] ||
{ do_help
; exit 1; }
2745 exit ${do_status_result:-0};;
2749 echol
"$TG_INST_HOOKSDIR";;
2753 echol
"$TG_INST_CMDDIR";;
2757 echol
"$TG_INST_CMDDIR/awk";;
2760 # Maintenance command
2762 show_remote_topbases
=
2765 do_topbases_help
=0;;
2767 if [ $# -eq 2 ] && [ -n "$2" ]; then
2768 # unadvertised, but make it work
2772 show_remote_topbases
=1;;
2774 [ $# -eq 0 ] || do_topbases_help
=1;;
2776 [ $# -le 1 ] || do_topbases_help
=1
2777 if [ -n "$do_topbases_help" ]; then
2778 helpcmd
='echo "Usage: ${tgname:-tg} [-r <remote>] --top-bases [-r]"'
2779 [ $do_topbases_help -eq 0 ] || helpcmd
="$helpcmd >&2"
2781 exit $do_topbases_help
2784 if git_dir
="$(git rev-parse --git-dir 2>&1)"; then
2785 [ -z "$wayback" ] || activate_wayback_machine
"$wayback"
2789 if [ -n "$show_remote_topbases" ]; then
2791 [ -n "$base_remote" ] ||
2792 die
"no remote location given. Either use -r <remote> option or set topgit.remote"
2794 [ -z "$topbases_implicit_default" ] ||
2795 check_remote_topbases
"$base_remote" rbases
"--top-bases"
2796 if [ -n "$rbases" ]; then
2799 echol
"refs/remotes/$base_remote/${topbases#heads/}"
2802 echol
"refs/$topbases"
2807 case "$cmd" in index-merge-one-file
)
2810 [ -r "$TG_INST_CMDDIR/tg-$isutil$cmd" ] ||
{
2811 looplevel
="$TG_ALIAS_DEPTH"
2812 [ "${looplevel#[1-9]}" != "$looplevel" ] &&
2813 [ "${looplevel%%[!0-9]*}" = "$looplevel" ] ||
2815 tgalias
="$(git config "topgit.
alias.
$cmd" 2>/dev/null)" ||
:
2816 [ -n "$tgalias" ] ||
{
2817 echo "Unknown subcommand: $cmd" >&2
2821 looplevel
=$
(( $looplevel + 1 ))
2822 [ $looplevel -le 10 ] || die
"topgit.alias nesting level 10 exceeded"
2823 TG_ALIAS_DEPTH
="$looplevel"
2824 export TG_ALIAS_DEPTH
2825 if [ "!${tgalias#?}" = "$tgalias" ]; then
2826 [ -z "$wayback" ] ||
2827 die
"-w is not allowed before an '!' alias command"
2829 if pfx
="$(git rev-parse --show-prefix 2>/dev/null)"; then
2833 cd "./$(git rev-parse --show-cdup 2>/dev/null)"
2834 if [ $# -gt 0 ]; then
2835 exec @SHELL_PATH@
-c "${tgalias#?} \"\$@\"" @SHELL_PATH@
"$@"
2837 exec @SHELL_PATH@
-c "${tgalias#?}" @SHELL_PATH@
2840 eval 'exec "$tgbin"' "${wayback:+-w \"\$wayback\"}" "$tgalias" '"$@"'
2842 die
"alias execution failed for: $tgalias"
2844 unset_ TG_ALIAS_DEPTH
2847 if [ "$*" = "-h" ] ||
[ "$*" = "--help" ]; then
2851 nomergesetup
="$showing_help"
2852 case "$cmd" in base|contains|
export|files|info|log|
mail|next|
patch|prev|rebase|revert|shell|summary|tag
)
2853 # avoid merge setup where not necessary
2858 if [ -n "$wayback" ] && [ -z "$showing_help" ]; then
2859 [ -n "$nomergesetup" ] ||
2860 die
"the wayback machine cannot be used with the \"$cmd\" subcommand"
2861 if [ "$cmd" = "shell" ]; then
2862 # this is ugly; `tg shell` should handle this but it's too
2863 # late there so we have to do it here
2867 wayback_dir
="${1#--directory=}" && shift;;
2869 die
"--directory requires an argument";;
2871 [ $# -ge 2 ] || die
"--directory requires an argument"
2872 wayback_dir
="$2" && shift 2;;
2874 activate_wayback_machine
"$wayback" 1 "$wayback_dir"
2877 # export might drop out into a shell for conflict resolution
2878 [ "$cmd" != "export" ] || _fullwb
=2
2879 activate_wayback_machine
"$wayback" "$_fullwb"
2881 die
"failed to set the wayback machine to target \"$wayback\""
2884 [ -n "$showing_help" ] || initial_setup
2885 [ -z "$noremote" ] || unset_ base_remote
2887 if [ -z "$nomergesetup" ]; then
2888 # make sure merging the .top* files will always behave sanely
2891 setup_hook
"pre-commit"
2894 # everything but rebase needs topbases set
2895 carefully
="$showing_help"
2896 [ "$cmd" != "migrate-bases" ] || carefully
=1
2897 [ "$cmd" = "rebase" ] || set_topbases
$carefully
2902 case "$cmd$showing_help" in
2903 contains|info|summary|tag
)
2908 annihilate|create|delete|depend|import|update
)
2912 [ -z "$_suppress_alt" ] || noalt_setup
2913 [ -z "$_use_ref_cache" ] || v_create_ref_cache
2915 fullcmd
="${tgname:-tg} $cmd $*"
2916 fullcmd
="${fullcmd% }"
2917 if [ z
"$forcepager" = z
"1" ]; then
2918 page
'. "$TG_INST_CMDDIR"/tg-$isutil$cmd' "$@"
2920 .
"$TG_INST_CMDDIR"/tg-
$isutil$cmd