2 # TopGit - A different patch queue manager
3 # Copyright (C) Petr Baudis <pasky@suse.cz> 2008
4 # Copyright (C) Kyle J. McKay <mackyle@gmail.com> 2014,2015,2016
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"
24 ## Auxiliary functions
26 # Preserves current $? value while triggering a non-zero set -e exit if active
27 # This works even for shells that sometimes fail to correctly trigger a -e exit
33 # This is the POSIX equivalent of which
36 { "unset" -f command unset unalias "$1"; } >/dev
/null
2>&1 ||
:
37 { "unalias" -a; } >/dev
/null
2>&1 ||
:
41 # Output arguments without any possible interpretation
42 # (Avoid misinterpretation of '\' characters or leading "-n", "-E" or "-e")
50 echol
"${TG_RECURSIVE}${tgname:-tg}: $*"
55 info
"warning: $*" >&2
69 # shift off first arg then return "$*" properly quoted in single-quotes
70 # if $1 was '' output goes to stdout otherwise it's assigned to $1
71 # the final \n, if any, is omitted from the result but any others are included
76 set -- "$_quotearg_v" \
77 "sed \"s/'/'\\\\\\''/g;1s/^/'/;\\\$s/\\\$/'/;s/'''/'/g;1s/^''\\(.\\)/\\1/\"" "$*"
87 printf "%s$4" "$3" |
eval "$2"
89 eval "$1="'"$(printf "%s$4" "$3" | eval "$2")"'
94 # same as v_quotearg except there's no extra $1 so output always goes to stdout
102 # Compare $1 to $3 each of which must match ^[^0-9]*\d*(\.\d*)*.*$
103 # where only the "\d*" parts in the regex participate in the comparison
104 # Since EVERY string matches that regex this function is easy to use
105 # An empty string ('') for $1 or $3 or any "\d*" part is treated as 0
106 # $2 is a compare op '<', '<=', '=', '==', '!=', '>=', '>'
107 # Return code is 0 for true, 1 for false (or unknown compare op)
108 # There is NO difference in behavior between '=' and '=='
109 # Note that "vcmp 1.8 == 1.8.0.0.0.0" correctly returns 0
110 set -- "$1" "$2" "$3" "${1%%[0-9]*}" "${3%%[0-9]*}"
111 set -- "${1#"$4"}" "$2" "${3#"$5"}"
112 set -- "${1%%[!0-9.]*}" "$2" "${3%%[!0-9.]*}"
116 [ "z$vcmp_a_" != "z" -o "z$vcmp_b_" != "z" ]
118 if [ "${vcmp_a_:-0}" -lt "${vcmp_b_:-0}" ]; then
119 unset vcmp_a_ vcmp_b_
120 case "$2" in "<"|
"<="|
"!=") return 0; esac
122 elif [ "${vcmp_a_:-0}" -gt "${vcmp_b_:-0}" ]; then
123 unset vcmp_a_ vcmp_b_
124 case "$2" in ">"|
">="|
"!=") return 0; esac
127 vcmp_a_
="${1#$vcmp_a_}"
128 vcmp_b_
="${3#$vcmp_b_}"
129 set -- "${vcmp_a_#.}" "$2" "${vcmp_b_#.}"
131 unset vcmp_a_ vcmp_b_
132 case "$2" in "="|
"=="|
"<="|
">=") return 0; esac
137 if ! git_version
="$(git version)"; then
138 die
"'git version' failed"
140 case "$git_version" in [Gg
]"it version "*);;*)
141 die
"'git version' output does not start with 'git version '"
144 vcmp
"$git_version" '>=' "$GIT_MINIMUM_VERSION" ||
145 die
"git version >= $GIT_MINIMUM_VERSION required but found $git_version instead"
148 case "$1" in version|
--version|
-V)
149 echo "TopGit version $TG_VERSION"
154 [ "$1" = "precheck" ] && exit 0
157 cat_depsmsg_internal
()
159 _rev
="$(ref_exists_rev "refs
/heads
/$1")" ||
return 0
160 if [ -s "$tg_cache_dir/$1/.$2" ]; then
161 if read _rev_match
&& [ "$_rev" = "$_rev_match" ]; then
163 while IFS
= read -r _line ||
[ -n "$_line" ]; do
164 printf '%s\n' "$_line"
167 fi <"$tg_cache_dir/$1/.$2"
169 [ -d "$tg_cache_dir/$1" ] || mkdir
-p "$tg_cache_dir/$1" 2>/dev
/null ||
:
170 if [ -d "$tg_cache_dir/$1" ]; then
171 printf '%s\n' "$_rev" >"$tg_cache_dir/$1/.$2"
173 git cat-file blob
"$_rev:.$2" 2>/dev
/null |
174 while IFS
= read -r _line ||
[ -n "$_line" ]; do
175 printf '%s\n' "$_line" >&3
176 printf '%s\n' "$_line"
177 done 3>>"$tg_cache_dir/$1/.$2"
179 git cat-file blob
"$_rev:.$2" 2>/dev
/null
183 # cat_deps BRANCHNAME
187 cat_depsmsg_internal
"$1" topdeps
194 cat_depsmsg_internal
"$1" topmsg
197 # cat_file TOPIC:PATH [FROM]
198 # cat the file PATH from branch TOPIC when FROM is empty.
199 # FROM can be -i or -w, than the file will be from the index or worktree,
200 # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
206 cat "$root_dir/${path#*:}"
209 # ':file' means cat from index
210 git cat-file blob
":${path#*:}"
214 refs
/heads
/*:.topdeps
)
215 _temp
="${path%:.topdeps}"
216 cat_deps
"${_temp#refs/heads/}"
218 refs
/heads
/*:.topmsg
)
219 _temp
="${path%:.topmsg}"
220 cat_msg
"${_temp#refs/heads/}"
223 git cat-file blob
"$path"
228 die
"Wrong argument to cat_file: '$2'"
233 # get tree for the committed topic
239 # get tree for the base
242 echo "refs/$topbases/$1"
245 # get tree for the index
251 # get tree for the worktree
254 i_tree
=$
(git write-tree
)
256 # the file for --index-output needs to sit next to the
259 : ${GIT_INDEX_FILE:="$git_dir/index"}
260 TMP_INDEX
="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX
")"
261 git read-tree
-m $i_tree --index-output="$TMP_INDEX" &&
262 GIT_INDEX_FILE
="$TMP_INDEX" &&
263 export GIT_INDEX_FILE
&&
264 git
diff --name-only -z HEAD |
265 git update-index
-z --add --remove --stdin &&
271 # strip_ref "$(git symbolic-ref HEAD)"
272 # Output will have a leading refs/heads/ or refs/$topbases/ stripped if present
277 echol
"${1#refs/$topbases/}"
280 echol
"${1#refs/heads/}"
287 # pretty_tree NAME [-b | -i | -w]
288 # Output tree ID of a cleaned-up tree without tg's artifacts.
289 # NAME will be ignored for -i and -w, but needs to be present
294 git ls-tree
--full-tree "$(get_tree_$source "$name")" |
295 LC_ALL
=C
sed -ne '/ \.top.*$/!p' |
299 # return an empty-tree root commit -- date is either passed in or current
300 # If passed in "$*" must be epochsecs followed by optional hhmm offset (+0000 default)
301 # An invalid secs causes the current date to be used, an invalid zone offset
302 # causes +0000 to be used
305 # the empty tree is guaranteed to always be there even in a repo with
306 # zero objects, but for completeness we force it to exist as a real object
308 read -r SECS ZONE JUNK
<<-EOT || :
311 case "$SECS" in *[!0-9]*) SECS
=; esac
312 if [ -z "$SECS" ]; then
313 MTDATE
="$(date '+%s %z')"
316 -[01][0-9][0-5][0-9]|
+[01][0-9][0-5][0-9])
326 EMPTYID
="- <-> $MTDATE"
327 EMPTYTREE
="$(git hash-object -t tree -w --stdin < /dev/null)"
328 printf '%s\n' "tree $EMPTYTREE" "author $EMPTYID" "committer $EMPTYID" '' |
329 git hash-object
-t commit
-w --stdin
336 hook_call
="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
337 if [ -f "$git_hooks_dir/$1" ] && LC_ALL
=C
grep -Fq "$hook_call" "$git_hooks_dir/$1"; then
338 # Another job well done!
341 # Prepare incantation
343 if [ -s "$git_hooks_dir/$1" -a -x "$git_hooks_dir/$1" ]; then
344 hook_call
="$hook_call"' || exit $?'
345 if [ -L "$git_hooks_dir/$1" ] ||
! LC_ALL
=C
sed -n 1p
<"$git_hooks_dir/$1" | LC_ALL
=C
grep -Fqx "#!@SHELL_PATH@"; then
347 while [ -e "$git_hooks_dir/$1-chain$chain_num" ]; do
348 chain_num
=$
(( $chain_num + 1 ))
350 mv -f "$git_hooks_dir/$1" "$git_hooks_dir/$1-chain$chain_num"
354 hook_call
="exec $hook_call"
355 [ -d "$git_hooks_dir" ] || mkdir
-p "$git_hooks_dir" ||
:
357 # Don't call hook if tg is not installed
358 hook_call
="if command -v \"$tgname\" >/dev/null 2>&1; then $hook_call; fi"
359 # Insert call into the hook
361 echol
"#!@SHELL_PATH@"
363 if [ -n "$hook_chain" ]; then
364 echol
"exec \"\$0-chain$chain_num\" \"\$@\""
366 [ ! -s "$git_hooks_dir/$1" ] ||
cat "$git_hooks_dir/$1"
368 } >"$git_hooks_dir/$1+"
369 chmod a
+x
"$git_hooks_dir/$1+"
370 mv "$git_hooks_dir/$1+" "$git_hooks_dir/$1"
373 # setup_ours (no arguments)
376 if [ ! -s "$git_common_dir/info/attributes" ] ||
! grep -q topmsg
"$git_common_dir/info/attributes"; then
377 [ -d "$git_common_dir/info" ] || mkdir
"$git_common_dir/info"
379 echo ".topmsg merge=ours"
380 echo ".topdeps merge=ours"
381 } >>"$git_common_dir/info/attributes"
383 if ! git config merge.ours.driver
>/dev
/null
; then
384 git config merge.ours.name
'"always keep ours" merge driver'
385 git config merge.ours.driver
'touch %A'
389 # measure_branch NAME [BASE] [EXTRAHEAD...]
392 _bname
="$1"; _base
="$2"
394 [ -n "$_base" ] || _base
="refs/$topbases/$(strip_ref "$_bname")"
395 # The caller should've verified $name is valid
396 _commits
="$(git rev-list --count "$_bname" "$@
" ^"$_base" --)"
397 _nmcommits
="$(git rev-list --count --no-merges "$_bname" "$@
" ^"$_base" --)"
398 if [ $_commits -ne 1 ]; then
403 echo "$_commits/$_nmcommits $_suffix"
406 # branch_contains B1 B2
407 # Whether B1 is a superset of B2.
410 _revb1
="$(ref_exists_rev "$1")" ||
return 0
411 _revb2
="$(ref_exists_rev "$2")" ||
return 0
412 if [ -s "$tg_cache_dir/$1/.bc/$2/.d" ]; then
413 if read _result _rev_matchb1 _rev_matchb2
&&
414 [ "$_revb1" = "$_rev_matchb1" -a "$_revb2" = "$_rev_matchb2" ]; then
416 fi <"$tg_cache_dir/$1/.bc/$2/.d"
418 [ -d "$tg_cache_dir/$1/.bc/$2" ] || mkdir
-p "$tg_cache_dir/$1/.bc/$2" 2>/dev
/null ||
:
420 [ "$(git rev-list --count --max-count=1 "$_revb2" --not "$_revb1" --)" = "0" ] || _result
=1
421 if [ -d "$tg_cache_dir/$1/.bc/$2" ]; then
422 echo "$_result" "$_revb1" "$_revb2" >"$tg_cache_dir/$1/.bc/$2/.d"
429 [ ! -s "$tg_tmp_dir/tg~ref-dirs-created" -a -s "$tg_ref_cache" ] ||
return 0
430 LC_ALL
=C
awk -v p
="$tg_tmp_dir/cached/" '{print p $1}' <"$tg_ref_cache" | LC_ALL
=C
tr '\n' '\0' |
xargs -0 mkdir
-p
431 echo 1 >"$tg_tmp_dir/tg~ref-dirs-created"
434 # If the first argument is non-empty, stores "1" there if this call created the cache
437 [ -n "$tg_ref_cache" -a ! -s "$tg_ref_cache" ] ||
return 0
439 [ -z "$base_remote" ] || _remotespec
="refs/remotes/$base_remote"
440 [ -z "$1" ] ||
eval "$1=1"
441 git for-each-ref
--format='%(refname) %(objectname)' \
442 refs
/heads
"refs/$topbases" $_remotespec >"$tg_ref_cache"
448 [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ] ||
return 0
452 # setting tg_ref_cache_only to non-empty will force non-$tg_ref_cache lookups to fail
455 if [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ]; then
456 LC_ALL
=C
awk -v r
="$1" 'BEGIN {e=1}; $1 == r {print $2; e=0; exit}; END {exit e}' <"$tg_ref_cache"
458 [ -z "$tg_ref_cache_only" ] ||
return 1
459 git rev-parse
--quiet --verify "$1^0" -- 2>/dev
/null
464 # Whether REF is a valid ref name
465 # REF must be fully qualified and start with refs/heads/, refs/$topbases/
466 # or, if $base_remote is set, refs/remotes/$base_remote/
467 # Caches result if $tg_read_only and outputs HASH on success
477 die
"ref_exists_rev requires fully-qualified ref name"
479 [ -n "$tg_read_only" ] ||
{ git rev-parse
--quiet --verify "$1^0" -- 2>/dev
/null
; return; }
482 { read -r _result _result_rev
<"$tg_tmp_dir/cached/$1/.ref"; } 2>/dev
/null ||
:
483 [ -z "$_result" ] ||
{ printf '%s' "$_result_rev"; return $_result; }
484 _result_rev
="$(rev_parse "$1")"
486 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir
-p "$tg_tmp_dir/cached/$1" 2>/dev
/null
487 [ ! -d "$tg_tmp_dir/cached/$1" ] ||
488 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.ref" 2>/dev
/null ||
:
489 printf '%s' "$_result_rev"
493 # Same as ref_exists_rev but output is abbreviated hash
494 # Optional second argument defaults to --short but may be any --short=.../--no-short option
495 ref_exists_rev_short
()
503 die
"ref_exists_rev_short requires fully-qualified ref name"
505 [ -n "$tg_read_only" ] ||
{ git rev-parse
--quiet --verify ${2:---short} "$1^0" -- 2>/dev
/null
; return; }
508 { read -r _result _result_rev
<"$tg_tmp_dir/cached/$1/.rfs"; } 2>/dev
/null ||
:
509 [ -z "$_result" ] ||
{ printf '%s' "$_result_rev"; return $_result; }
510 _result_rev
="$(rev_parse "$1")"
512 if [ $_result -eq 0 ]; then
513 _result_rev
="$(git rev-parse --verify ${2:---short} --quiet "$_result_rev" --)"
516 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir
-p "$tg_tmp_dir/cached/$1" 2>/dev
/null
517 [ ! -d "$tg_tmp_dir/cached/$1" ] ||
518 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.rfs" 2>/dev
/null ||
:
519 printf '%s' "$_result_rev"
524 # Whether REF is a valid ref name
525 # REF must be fully qualified and start with refs/heads/, refs/$topbases/
526 # or, if $base_remote is set, refs/remotes/$base_remote/
530 ref_exists_rev
"$1" >/dev
/null
534 # Runs git rev-parse REF^{tree}
535 # Caches result if $tg_read_only
538 [ -n "$tg_read_only" ] ||
{ git rev-parse
--verify "$1^{tree}" -- 2>/dev
/null
; return; }
539 if [ -f "$tg_tmp_dir/cached/$1/.rpt" ]; then
540 if IFS
= read -r _result
<"$tg_tmp_dir/cached/$1/.rpt"; then
541 printf '%s\n' "$_result"
546 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir
-p "$tg_tmp_dir/cached/$1" 2>/dev
/null ||
:
547 if [ -d "$tg_tmp_dir/cached/$1" ]; then
548 git rev-parse
--verify "$1^{tree}" -- >"$tg_tmp_dir/cached/$1/.rpt" 2>/dev
/null ||
:
549 if IFS
= read -r _result
<"$tg_tmp_dir/cached/$1/.rpt"; then
550 printf '%s\n' "$_result"
555 git rev-parse
--verify "$1^{tree}" -- 2>/dev
/null
559 # Whether BRANCH has a remote equivalent (accepts ${topbases#heads/}/ too)
562 [ -n "$base_remote" ] && ref_exists
"refs/remotes/$base_remote/$1"
565 # Return the verified TopGit branch name or die with an error.
566 # As a convenience, if HEAD or @ is given and HEAD is a symbolic ref to
567 # refs/heads/... then ... will be verified instead.
568 # if "$2" = "-f" (for fail) then return an error rather than dying.
569 verify_topgit_branch
()
573 _verifyname
="${1#refs/$topbases/}"
576 _verifyname
="${1#refs/heads/}"
579 _verifyname
="$(git symbolic-ref HEAD 2>/dev/null)" ||
:
580 [ -n "$_verifyname" -o "$2" = "-f" ] || die
"HEAD is not a symbolic ref"
581 case "$_verifyname" in refs
/heads
/*);;*)
582 [ "$2" != "-f" ] ||
return 1
583 die
"HEAD is not a symbolic ref to the refs/heads namespace"
585 _verifyname
="${_verifyname#refs/heads/}"
591 if ! ref_exists
"refs/heads/$_verifyname"; then
592 [ "$2" != "-f" ] ||
return 1
593 die
"no such branch: $_verifyname"
595 if ! ref_exists
"refs/$topbases/$_verifyname"; then
596 [ "$2" != "-f" ] ||
return 1
597 die
"not a TopGit-controlled branch: $_verifyname"
599 printf '%s' "$_verifyname"
603 # $1 = branch name (i.e. "t/foo/bar")
604 # $2 = optional result of rev-parse "refs/heads/$1"
605 # $3 = optional result of rev-parse "refs/$topbases/$1"
609 _rev
="${2:-$(ref_exists_rev "refs/heads/$_branch_name")}"
610 _rev_base
="${3:-$(ref_exists_rev "refs/$topbases/$_branch_name")}"
615 { read -r _result _result_rev _result_rev_base
<"$tg_cache_dir/$_branch_name/.ann"; } 2>/dev
/null ||
:
616 [ -z "$_result" -o "$_result_rev" != "$_rev" -o "$_result_rev_base" != "$_rev_base" ] ||
return $_result
618 # use the merge base in case the base is ahead.
619 mb
="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)"
621 test -z "$mb" ||
test "$(rev_parse_tree "$mb")" = "$(rev_parse_tree "$_rev")"
623 [ -d "$tg_cache_dir/$_branch_name" ] || mkdir
-p "$tg_cache_dir/$_branch_name" 2>/dev
/null
624 [ ! -d "$tg_cache_dir/$_branch_name" ] ||
625 echo $_result $_rev $_rev_base >"$tg_cache_dir/$_branch_name/.ann" 2>/dev
/null ||
:
629 non_annihilated_branches
()
631 [ $# -gt 0 ] ||
set -- "refs/$topbases"
632 git for-each-ref
--format='%(objectname) %(refname)' "$@" |
633 while read rev ref
; do
634 name
="${ref#refs/$topbases/}"
635 if branch_annihilated
"$name" "" "$rev"; then
642 # Make sure our tree is clean
643 # if optional "$1" given also verify that a checkout to "$1" would succeed
647 [ -z "$tg_state$git_state" ] ||
{ do_status
; exit 1; }
648 git update-index
--ignore-submodules --refresh ||
649 die
"the working directory has uncommitted changes (see above) - first commit or reset them"
650 [ -z "$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)" ] ||
651 die
"the index has uncommited changes"
652 [ -z "$1" ] || git read-tree
-n -u -m "$1" ||
653 die
"git checkout \"$1\" would fail"
657 # Whether REF is a SHA1 (compared to a symbolic name).
660 case "$1" in $octet20) return 0;; esac
664 # recurse_deps_internal NAME [BRANCHPATH...]
665 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
666 # followed by a 1 if the branch is "tgish" or a 0 if not
667 # followed by a 0 for a non-leaf, 1 for a leaf or 2 for annihilated tgish
668 # but missing and remotes are always "0"
669 # then the branch name followed by its depedency chain (which might be empty)
670 # An output line might look like this:
671 # 0 1 1 t/foo/leaf t/foo/int t/stage
672 # If no_remotes is non-empty, exclude remotes
673 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
674 # but the leaf info will always be 0 or 2 in that case
675 # If with_top_level is non-empty, include the top-level that's normally omitted
676 # any branch names in the space-separated recurse_deps_exclude variable
677 # are skipped (along with their dependencies)
678 recurse_deps_internal
()
680 case " $recurse_deps_exclude " in *" $1 "*) return 0; esac
682 if ! _ref_hash
="$(ref_exists_rev "refs
/heads
/$1")"; then
683 [ -z "$2" ] ||
echo "1 0 0 $*"
690 ! _ref_hash_base
="$(ref_exists_rev "refs
/$topbases/$1")" || _is_tgish
=1
691 [ "$_is_tgish" = "0" ] ||
! branch_annihilated
"$1" "$_ref_hash" "$_ref_hash_base" || _is_leaf
=2
692 [ -z "$recurse_preorder" -o -z "${2:-$with_top_level}" ] ||
echo "0 $_is_tgish $_is_leaf $*"
694 # If no_remotes is unset also check our base against remote base.
695 # Checking our head against remote head has to be done in the helper.
696 if [ "$_is_tgish" = "1" ] && [ -z "$no_remotes" ] && has_remote
"${topbases#heads/}/$1"; then
697 echo "0 0 0 refs/remotes/$base_remote/${topbases#heads/}/$1 $*"
700 # if the branch was annihilated, it is considered to have no dependencies
701 [ "$_is_leaf" = "2" ] || _is_leaf
=1
702 if [ "$_is_tgish" = "1" ] && [ "$_is_leaf" = "1" ]; then
703 #TODO: handle nonexisting .topdeps?
704 while read _dname
&& [ -n "$_dname" ]; do
705 # Avoid depedency loops
706 case " $* " in *" $_dname "*)
707 warn
"dependency loop detected in branch $_dname"
711 # Shoo shoo, leave our environment alone!
713 (recurse_deps_internal
"$_dname" "$@") || _dep_is_leaf
=$?
714 [ "$_dep_is_leaf" = "2" ] || _is_leaf
=0
720 [ -n "$recurse_preorder" -o -z "${2:-$with_top_level}" ] ||
echo "0 $_is_tgish $_is_leaf $*"
721 return ${_is_leaf:-0}
725 # helper for recurse_deps so that a return statement executed inside CMD
726 # does not return from recurse_deps. This shouldn't be necessary, but it
727 # seems that it actually is.
733 # becomes read-only for caching purposes
734 # assigns new value to tg_read_only
735 # become_cacheable/undo_become_cacheable calls may be nested
738 _old_tg_read_only
="$tg_read_only"
739 if [ -z "$tg_read_only" ]; then
740 ! [ -e "$tg_tmp_dir/cached" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created" ] ||
741 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
745 v_create_ref_cache _my_ref_cache
746 _my_ref_cache
="${_my_ref_cache:+1}"
747 tg_read_only
="undo${_my_ref_cache:-0}-$_old_tg_read_only"
750 # restores tg_read_only and ref_cache to state before become_cacheable call
751 # become_cacheable/undo_bocome_cacheable calls may be nested
752 undo_become_cacheable
()
754 case "$tg_read_only" in
756 _suffix
="${tg_read_only#undo?-}"
757 [ "${tg_read_only%$_suffix}" = "undo0-" ] || remove_ref_cache
758 tg_read_only
="$_suffix"
762 # just call this, no undo, sets tg_read_only= and removes ref cache and cached results
763 become_non_cacheable
()
767 ! [ -e "$tg_tmp_dir/cached" ] && ! [ -e "$tg_tmp_dir/tg~ref-dirs-created" ] ||
768 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
771 # call this to make sure Git will not complain about a missing user/email
772 # result is cached in TG_IDENT_CHECKED and a non-empty value suppresses the check
773 ensure_ident_available
()
775 [ -z "$TG_IDENT_CHECKED" ] ||
return 0
776 git var GIT_AUTHOR_IDENT
>/dev
/null
&&
777 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
779 export TG_IDENT_CHECKED
783 # recurse_deps CMD NAME [BRANCHPATH...]
784 # Recursively eval CMD on all dependencies of NAME.
785 # Dependencies are visited in topological order.
786 # CMD can refer to $_name for queried branch name,
787 # $_dep for dependency name,
788 # $_depchain for space-seperated branch backtrace,
789 # $_dep_missing boolean to check whether $_dep is present
790 # and the $_dep_is_tgish and $_dep_annihilated booleans.
791 # If recurse_preorder is NOT set then the $_dep_is_leaf boolean is also valid.
792 # It can modify $_ret to affect the return value
793 # of the whole function.
794 # If recurse_deps() hits missing dependencies, it will append
795 # them to space-separated $missing_deps list and skip them
796 # after calling CMD with _dep_missing set.
797 # remote dependencies are processed if no_remotes is unset.
798 # any branch names in the space-separated recurse_deps_exclude variable
799 # are skipped (along with their dependencies)
805 _depsfile
="$(get_temp tg-depsfile)"
806 recurse_deps_internal
"$@" >>"$_depsfile" ||
:
807 undo_become_cacheable
810 while read _ismissing _istgish _isleaf _dep _name _deppath
; do
811 _depchain
="$_name${_deppath:+ $_deppath}"
813 [ "$_istgish" = "0" ] || _dep_is_tgish
=1
815 if [ "$_ismissing" != "0" ]; then
817 case " $missing_deps " in *" $_dep "*);;*)
818 missing_deps
="${missing_deps:+$missing_deps }$_dep"
823 if [ "$_isleaf" = "1" ]; then
825 elif [ "$_isleaf" = "2" ]; then
834 find_leaves_internal
()
836 if [ -n "$_dep_is_leaf" ] && [ -z "$_dep_annihilated" ] && [ -z "$_dep_missing" ]; then
837 if [ -n "$_dep_is_tgish" ]; then
838 fulldep
="refs/$topbases/$_dep"
840 fulldep
="refs/heads/$_dep"
842 case " $seen_leaf_refs " in *" $fulldep "*);;*)
843 seen_leaf_refs
="${seen_leaf_refs:+$seen_leaf_refs }$fulldep"
844 if fullrev
="$(ref_exists_rev "$fulldep")"; then
845 case " $seen_leaf_revs " in *" $fullrev "*);;*)
846 seen_leaf_revs
="${seen_leaf_revs:+$seen_leaf_revs }$fullrev"
847 # See if Git knows it by another name
848 if tagname
="$(git describe --exact-match "$fullrev" 2>/dev/null)" && [ -n "$tagname" ]; then
849 echo "refs/tags/$tagname"
860 # output (one per line) the unique leaves of NAME
862 # 1) a non-tgish dependency
863 # 2) the base of a tgish dependency with no non-annihilated dependencies
864 # duplicates are suppressed (by commit rev) and remotes are always ignored
865 # if a leaf has an exact tag match that will be output
866 # note that recurse_deps_exclude IS honored for this operation
874 recurse_deps find_leaves_internal
"$1"
878 # branch_needs_update
879 # This is a helper function for determining whether given branch
880 # is up-to-date wrt. its dependencies. It expects input as if it
881 # is called as a recurse_deps() helper.
882 # In case the branch does need update, it will echo it together
883 # with the branch backtrace on the output (see needs_update()
884 # description for details) and set $_ret to non-zero.
885 branch_needs_update
()
887 if [ -n "$_dep_missing" ]; then
888 echo "! $_dep $_depchain"
892 if [ -n "$_dep_is_tgish" ]; then
893 branch_annihilated
"$_dep" && return 0
895 if has_remote
"$_dep"; then
896 branch_contains
"refs/heads/$_dep" "refs/remotes/$base_remote/$_dep" ||
897 echo "refs/remotes/$base_remote/$_dep $_dep $_depchain"
899 # We want to sync with our base first and should output this before
900 # the remote branch, but the order does not actually matter to tg-update
901 # as it just recurses regardless, but it does matter for tg-info (which
902 # treats out-of-date bases as though they were already merged in) so
903 # we output the remote before the base.
904 branch_contains
"refs/heads/$_dep" "refs/$topbases/$_dep" ||
{
905 echo ": $_dep $_depchain"
911 if [ -n "$_name" ]; then
912 case "$_dep" in refs
/*) _fulldep
="$_dep";; *) _fulldep
="refs/heads/$_dep";; esac
913 if ! branch_contains
"refs/$topbases/$_name" "$_fulldep"; then
914 # Some new commits in _dep
915 echo "$_dep $_depchain"
922 # This function is recursive; it outputs reverse path from NAME
923 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
924 # inner paths first. Innermost name can be refs/remotes/<remote>/<name>
925 # if the head is not in sync with the <remote> branch <name>, ':' if
926 # the head is not in sync with the base (in this order of priority)
927 # or '!' if dependency is missing. Note that the remote branch, base
928 # order is reversed from the order they will actually be updated in
929 # order to accomodate tg info which treats out-of-date items that are
930 # only in the base as already being in the head for status purposes.
931 # It will also return non-zero status if NAME needs update.
932 # If needs_update() hits missing dependencies, it will append
933 # them to space-separated $missing_deps list and skip them.
936 recurse_deps branch_needs_update
"$1"
939 # branch_empty NAME [-i | -w]
943 _rev
="$(ref_exists_rev "refs
/heads
/$1")" ||
return 0
946 { read -r _result _result_rev
<"$tg_cache_dir/$1/.mt"; } 2>/dev
/null ||
:
947 [ -z "$_result" -o "$_result_rev" != "$_rev" ] ||
return $_result
949 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ] || _result
=$?
950 [ -d "$tg_cache_dir/$1" ] || mkdir
-p "$tg_cache_dir/$1" 2>/dev
/null
951 [ ! -d "$tg_cache_dir/$1" ] ||
echo $_result $_rev >"$tg_cache_dir/$1/.mt"
954 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ]
958 # list_deps [-i | -w] [BRANCH]
959 # -i/-w apply only to HEAD
963 [ "$1" != "-i" -a "$1" != "-w" ] ||
{ head_from
="$1"; shift; }
964 head="$(git symbolic-ref -q HEAD)" ||
967 git for-each-ref
--format='%(objectname) %(refname)' "refs/$topbases${1:+/$1}" |
968 while read rev ref
; do
969 name
="${ref#refs/$topbases/}"
970 if branch_annihilated
"$name" "" "$rev"; then
975 [ "refs/heads/$name" = "$head" ] ||
977 cat_file
"refs/heads/$name:.topdeps" $from |
while read dep
; do
979 ref_exists
"refs/$topbases/$dep" ||
981 if ! "$dep_is_tgish" ||
! branch_annihilated
$dep; then
988 # checkout_symref_full [-f] FULLREF [SEED]
989 # Just like git checkout -b FULLREF [SEED] except that FULLREF MUST start with
990 # refs/ and HEAD is ALWAYS set to a symref to it and [SEED] (default is FULLREF)
991 # MUST be a committish which if present will be used instead of current FULLREF
992 # (and FULLREF will be updated to it as well in that case)
993 # With -f it's like git checkout -f (uses read-tree --reset instead of -m)
994 # As an extension, FULLREF may also be a full hash to create a detached HEAD
995 checkout_symref_full
()
998 if [ "$1" = "-f" ]; then
1008 [ -z "$2" ] ||
[ "$1" = "$2" ] ||
1009 die
"programmer error: invalid checkout_symref_full \"$1\" \"$2\""
1013 die
"programmer error: invalid checkout_symref_full \"$1\""
1016 _seedrev
="$(git rev-parse --quiet --verify "${2:-$1}^
0" --)" ||
1017 die
"invalid committish: \"${2:-$1}\""
1018 # We have to do all the hard work ourselves :/
1019 # This is like git checkout -b "$1" "$2"
1020 # (or just git checkout "$1"),
1021 # but never creates a detached HEAD (unless $1 is a hash)
1022 git read-tree
-u $_mode HEAD
"$_seedrev" &&
1024 [ -z "$2" ] && [ "$(git cat-file -t "$1")" = "commit" ] ||
1025 git update-ref
"$1" "$_seedrev"
1027 [ -n "$_ishash" ] || git symbolic-ref HEAD
"$1"
1031 # switch_to_base NAME [SEED]
1034 checkout_symref_full
"refs/$topbases/$1" "$2"
1037 # run editor with arguments
1038 # the editor setting will be cached in $tg_editor (which is eval'd)
1039 # result non-zero if editor fails or GIT_EDITOR cannot be determined
1042 tg_editor
="$GIT_EDITOR"
1043 [ -n "$tg_editor" ] || tg_editor
="$(git var GIT_EDITOR)" ||
return $?
1044 eval "$tg_editor" '"$@"'
1047 # Show the help messages.
1051 if [ "$1" = "-w" ]; then
1055 if [ -z "$1" ] ; then
1056 # This is currently invoked in all kinds of circumstances,
1057 # including when the user made a usage error. Should we end up
1058 # providing more than a short help message, then we should
1060 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
1062 ## Build available commands list for help output
1066 for cmd
in "$TG_INST_CMDDIR"/tg-
[!-]*; do
1067 ! [ -r "$cmd" ] && continue
1068 # strip directory part and "tg-" prefix
1071 [ "$cmd" != "migrate-bases" ] ||
continue
1072 [ "$cmd" != "summary" ] || cmd
="status|$cmd"
1073 cmds
="$cmds$sep$cmd"
1077 echo "TopGit version $TG_VERSION - A different patch queue manager"
1078 echo "Usage: $tgname [-C <dir>] [-r <remote> | -u] [-c <name>=<val>] ($cmds) ..."
1079 echo " Or: $tgname help [-w] [<command>]"
1080 echo "Use \"$tgdisplaydir$tgname help tg\" for overview of TopGit"
1081 elif [ -r "$TG_INST_CMDDIR"/tg-
$1 -o -r "$TG_INST_SHAREDIR/tg-$1.txt" ] ; then
1082 if [ -n "$_www" ]; then
1084 if ! [ -r "$TG_INST_SHAREDIR/topgit.html" ]; then
1085 echo "${0##*/}: missing html help file:" \
1086 "$TG_INST_SHAREDIR/topgit.html" 1>&2
1089 if ! [ -r "$TG_INST_SHAREDIR/tg-$1.html" ]; then
1090 echo "${0##*/}: missing html help file:" \
1091 "$TG_INST_SHAREDIR/tg-$1.html" 1>&2
1094 if [ -n "$nohtml" ]; then
1095 echo "${0##*/}: use" \
1096 "\"${0##*/} help $1\" instead" 1>&2
1099 git web--browse
-c help.browser
"$TG_INST_SHAREDIR/tg-$1.html"
1104 if [ -r "$TG_INST_CMDDIR"/tg-
$1 ] ; then
1105 "$TG_INST_CMDDIR"/tg-
$1 -h 2>&1 ||
:
1107 elif [ "$1" = "help" ]; then
1108 echo "Usage: ${tgname:-tg} help [-w] [<command>]"
1110 elif [ "$1" = "status" ]; then
1111 echo "Usage: ${tgname:-tg} status [-v] [--exit-code]"
1114 if [ -r "$TG_INST_SHAREDIR/tg-$1.txt" ] ; then
1115 cat "$TG_INST_SHAREDIR/tg-$1.txt"
1120 echo "${0##*/}: no help for $1" 1>&2
1130 if [ -e "$git_dir/MERGE_HEAD" ]; then
1132 elif [ -e "$git_dir/rebase-apply/applying" ]; then
1134 git_remove
="$git_dir/rebase-apply"
1135 elif [ -e "$git_dir/rebase-apply" ]; then
1137 git_remove
="$git_dir/rebase-apply"
1138 elif [ -e "$git_dir/rebase-merge" ]; then
1140 git_remove
="$git_dir/rebase-merge"
1141 elif [ -e "$git_dir/CHERRY_PICK_HEAD" ]; then
1142 git_state
="cherry-pick"
1143 elif [ -e "$git_dir/BISECT_LOG" ]; then
1145 elif [ -e "$git_dir/REVERT_HEAD" ]; then
1148 git_remove
="${git_remove#./}"
1152 if [ -e "$git_dir/tg-update" ]; then
1154 tg_remove
="$git_dir/tg-update"
1156 tg_remove
="${tg_remove#./}"
1159 # Show status information
1164 while [ $# -gt 0 ] && case "$1" in
1172 die
"unknown status argument: $1"
1174 esac; do shift; done
1176 symref
="$(git symbolic-ref --quiet HEAD)" ||
:
1177 headrv
="$(git rev-parse --quiet --verify --short HEAD --)" ||
:
1178 if [ -n "$symref" ]; then
1179 echol
"HEAD -> $symref (${headrv:-unborn})"
1181 echol
"HEAD -> ${headrv:-?}"
1183 if [ -n "$tg_state" ]; then
1185 if [ "$tg_state" = "update" ]; then
1186 IFS
= read -r uname
<"$git_dir/tg-update/name" ||
:
1188 extra
="; currently updating branch '$uname'"
1190 echol
"tg $tg_state in progress$extra"
1191 if [ -s "$git_dir/tg-update/fullcmd" ] && [ -s "$git_dir/tg-update/names" ]; then
1192 printf 'You are currently updating as a result of:\n '
1193 cat "$git_dir/tg-update/fullcmd"
1194 bcnt
="$(( $(wc -w < "$git_dir/tg-update
/names
") ))"
1195 if [ $bcnt -gt 1 ]; then
1197 ! [ -s "$git_dir/tg-update/processed" ] ||
1198 pcnt
="$(( $(wc -w < "$git_dir/tg-update
/processed
") ))"
1199 echo "$pcnt of $bcnt branches updated so far"
1202 if [ "$tg_state" = "update" ]; then
1203 echol
' (use "tg update --continue" to continue)'
1204 echol
' (use "tg update --skip" to skip this branch and continue)'
1205 echol
' (use "tg update --stop" to stop and retain updates so far)'
1206 echol
' (use "tg update --abort" to restore pre-update state)'
1209 [ -z "$git_state" ] ||
echo "git $git_state in progress"
1210 if [ "$git_state" = "merge" ]; then
1211 ucnt
="$(( $(git ls-files --unmerged --full-name --abbrev :/ | wc -l) ))"
1212 if [ $ucnt -gt 0 ]; then
1213 echo 'fix conflicts and then "git commit" the result'
1215 echo 'all conflicts fixed; run "git commit" to record result'
1218 if [ -z "$git_state" ]; then
1219 ccnt
="$(( $(git status --porcelain -uno | wc -l) ))"
1221 if [ "$ccnt" -eq 0 ]; then
1222 if git status
--porcelain |
grep -q '^[?]'; then
1223 untr
="; non-ignored, untracked files present"
1225 echo "working directory is clean$untr"
1226 [ -n "$tg_state" ] || do_status_result
=0
1228 echo "working directory is DIRTY"
1241 # pass "diff" to get pager.diff
1242 # if pager.$1 is a boolean false returns cat
1243 # if set to true or unset fails
1244 # otherwise succeeds and returns the value
1247 if _x
="$(git config --bool "pager.
$1" 2>/dev/null)"; then
1248 [ "$_x" != "true" ] ||
return 1
1252 if _x
="$(git config "pager.
$1" 2>/dev/null)"; then
1260 # Set TG_PAGER to a valid executable
1261 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
1262 # See also the following "page" function for ease of use
1263 # emptypager will be set to 1 (otherwise empty) if TG_PAGER was set to "cat" to not be empty
1264 # Preference is (same as Git):
1266 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
1267 # 3. core.pager (only if set)
1269 # 5. git var GIT_PAGER
1273 isatty
1 ||
{ emptypager
=1; TG_PAGER
=cat; return 0; }
1276 if [ -z "$TG_PAGER_IN_USE" ]; then
1277 # TG_PAGER = GIT_PAGER | PAGER | less
1278 # NOTE: GIT_PAGER='' is significant
1279 if [ -n "${GIT_PAGER+set}" ]; then
1280 TG_PAGER
="$GIT_PAGER"
1281 elif [ -n "$USE_PAGER_TYPE" ] && _dp
="$(get_pager "$USE_PAGER_TYPE")"; then
1283 elif _cp
="$(git config core.pager 2>/dev/null)"; then
1285 elif [ -n "${PAGER+set}" ]; then
1288 _gp
="$(git var GIT_PAGER 2>/dev/null)" ||
:
1289 [ "$_gp" != ":" ] || _gp
=
1290 TG_PAGER
="${_gp:-less}"
1292 if [ -z "$TG_PAGER" ]; then
1301 # Set pager default environment variables
1302 # see pager.c:setup_pager
1303 if [ -z "${LESS+set}" ]; then
1307 if [ -z "${LV+set}" ]; then
1312 # this is needed so e.g. $(git diff) will still colorize it's output if
1313 # requested in ~/.gitconfig with color.diff=auto
1315 export GIT_PAGER_IN_USE
1317 # this is needed so we don't get nested pagers
1319 export TG_PAGER_IN_USE
1322 # page eval_arg [arg ...]
1324 # Calls setup_pager then evals the first argument passing it all the rest
1325 # where the output is piped through eval "$TG_PAGER" unless emptypager is set
1326 # by setup_pager (in which case the output is left as-is).
1328 # To handle arbitrary paging duties, collect lines to be paged into a
1329 # function and then call page with the function name or perhaps func_name "$@".
1331 # If no arguments at all are passed in do nothing (return with success).
1334 [ $# -gt 0 ] ||
return 0
1336 _evalarg
="$1"; shift
1337 if [ -n "$emptypager" ]; then
1338 eval "$_evalarg" '"$@"'
1340 eval "$_evalarg" '"$@"' |
eval "$TG_PAGER"
1344 # get_temp NAME [-d]
1345 # creates a new temporary file (or directory with -d) in the global
1346 # temporary directory $tg_tmp_dir with pattern prefix NAME
1349 mktemp
$2 "$tg_tmp_dir/$1.XXXXXX"
1352 # automatically called by strftime
1353 # does nothing if already setup
1354 # may be called explicitly if the first call would otherwise be in a subshell
1355 # so that the setup is only done once before subshells start being spawned
1358 [ -z "$strftime_is_setup" ] ||
return 0
1360 # date option to format raw epoch seconds values
1363 _testdt
='2000-02-29 07:03:08 UTC'
1364 _testfm
='%Y-%m-%d %H:%M:%S %Z'
1365 if [ "$(TZ=UTC date "-d@
$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1367 elif [ "$(TZ=UTC date "-r$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1373 # $1 => strftime format string to use
1374 # $2 => raw timestamp as seconds since epoch
1375 # $3 => optional time zone string (empty/absent for local time zone)
1379 if [ -n "$daterawopt" ]; then
1380 if [ -n "$3" ]; then
1381 TZ
="$3" date "$daterawopt$2" "+$1"
1383 date "$daterawopt$2" "+$1"
1386 if [ -n "$3" ]; then
1387 TZ
="$3" perl
-MPOSIX=strftime
-le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1389 perl
-MPOSIX=strftime
-le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1396 [ -n "$git_dir" ] || git_dir
="$(git rev-parse --git-dir)"
1397 if [ -n "$git_dir" ] && [ -d "$git_dir" ]; then
1398 git_dir
="$(cd "$git_dir" && pwd)"
1400 if [ -z "$git_common_dir" ]; then
1401 if vcmp
"$git_version" '>=' "2.5"; then
1402 # rev-parse --git-common-dir is broken and may give
1403 # an incorrect result unless the current directory is
1404 # already set to the top level directory
1405 git_common_dir
="$(cd ".
/$
(git rev-parse
--show-cdup)" && cd "$
(git rev-parse
--git-common-dir)" && pwd)"
1407 git_common_dir
="$git_dir"
1410 [ -n "$git_dir" ] && [ -n "$git_common_dir" ] &&
1411 [ -d "$git_dir" ] && [ -d "$git_common_dir" ] || die
"Not a git repository"
1412 git_hooks_dir
="$git_common_dir/hooks"
1413 if vcmp
"$git_version" '>=' "2.9" && gchp
="$(git config --path --get core.hooksPath 2>/dev/null)" && [ -n "$gchp" ]; then
1416 git_hooks_dir
="$gchp"
1419 [ -n "$1" ] || warn
"ignoring non-absolute core.hooksPath: $gchp"
1429 [ -n "$base_remote" ] || base_remote
="$(git config topgit.remote 2>/dev/null)" ||
:
1430 tgsequester
="$(git config --bool topgit.sequester 2>/dev/null)" ||
:
1432 [ "$tgsequester" != "false" ] || tgnosequester
=1
1435 # catch errors if topbases is used without being set
1436 unset tg_topbases_set
1437 topbases
="programmer*:error"
1438 topbasesrx
="programmer*:error}"
1439 oldbases
="$topbases"
1445 # suppress the merge log editor feature since git 1.7.10
1447 GIT_MERGE_AUTOEDIT
=no
1448 export GIT_MERGE_AUTOEDIT
1452 ! vcmp
"$git_version" '>=' "2.9" || auhopt
="--allow-unrelated-histories"
1453 root_dir
="$(git rev-parse --show-cdup)"; root_dir
="${root_dir:-.}"
1454 logrefupdates
="$(git config --bool core.logallrefupdates 2>/dev/null)" ||
:
1455 [ "$logrefupdates" = "true" ] || logrefupdates
=
1457 # make sure root_dir doesn't end with a trailing slash.
1459 root_dir
="${root_dir%/}"
1461 # make sure global cache directory exists inside GIT_DIR
1463 tg_cache_dir
="$git_common_dir/tg-cache"
1464 [ -d "$tg_cache_dir" ] || mkdir
"$tg_cache_dir"
1466 # create global temporary directories, inside GIT_DIR
1469 trap 'rm -rf "$tg_tmp_dir"' EXIT
1472 trap 'exit 131' QUIT
1473 trap 'exit 134' ABRT
1474 trap 'exit 143' TERM
1475 tg_tmp_dir
="$(mktemp -d "$git_dir/tg-tmp.XXXXXX
")"
1476 tg_ref_cache
="$tg_tmp_dir/tg~ref-cache"
1481 # refer to "top-bases" in a refname with $topbases
1483 [ -z "$tg_topbases_set" ] ||
return 0
1485 topbases_implicit_default
=1
1486 # See if topgit.top-bases is set to heads or refs
1487 tgtb
="$(git config "topgit.top-bases
" 2>/dev/null)" ||
:
1488 if [ -n "$tgtb" ] && [ "$tgtb" != "heads" ] && [ "$tgtb" != "refs" ]; then
1489 if [ -n "$1" ]; then
1490 # never die on the hook script
1493 die
"invalid \"topgit.top-bases\" setting (must be \"heads\" or \"refs\")"
1496 if [ -n "$tgtb" ]; then
1499 topbases
="heads/{top-bases}"
1500 topbasesrx
="heads/[{]top-bases[}]"
1501 oldbases
="top-bases";;
1503 topbases
="top-bases"
1504 topbasesrx
="top-bases"
1505 oldbases
="heads/{top-bases}";;
1507 # MUST NOT be exported
1508 unset tgtb tg_topbases_set topbases_implicit_default
1514 # check heads and top-bases and see what state the current
1515 # repository is in. remotes are ignored.
1520 newtb
="heads/{top-bases}"
1521 while read -r rn
&& [ -n "$rn" ]; do case "$rn" in
1522 "refs/heads/{top-bases}"/*)
1523 case "$hblist" in *" ${rn#refs/$newtb/} "*)
1524 if [ "$topbases" != "heads/{top-bases}" ] && [ -n "$topbases" ]; then
1528 topbases
="heads/{top-bases}"
1529 topbasesrx
="heads/[{]top-bases[}]"
1530 oldbases
="top-bases"
1534 case "$hblist" in *" ${rn#refs/top-bases/} "*)
1535 if [ "$topbases" != "top-bases" ] && [ -n "$topbases" ]; then
1539 topbases
="top-bases"
1540 topbasesrx
="top-bases"
1541 oldbases
="heads/{top-bases}"
1545 hblist
="$hblist${rn#refs/heads/} ";;
1547 $(git for-each-ref --format='%(refname)' "refs/heads" "refs/top-bases" 2>/dev/null)
1549 if [ -n "$both" ]; then
1550 if [ -n "$1" ]; then
1551 # hook script always prefers newer without complaint
1552 topbases
="heads/{top-bases}"
1553 topbasesrx
="heads/[{]top-bases[}]"
1554 oldbases
="top-bases"
1557 err
"repository contains existing TopGit branches"
1558 err
"but some use refs/top-bases/... for the base"
1559 err
"and some use refs/heads/{top-bases}/... for the base"
1560 err
"with the latter being the new, preferred location"
1561 err
"set \"topgit.top-bases\" to either \"heads\" to use"
1562 err
"the new heads/{top-bases} location or \"refs\" to use"
1563 err
"the old top-bases location."
1564 err
"(the tg migrate-bases command can also resolve this issue)"
1565 die
"schizophrenic repository requires topgit.top-bases setting"
1567 elif [ -n "$topbases" ]; then
1568 unset topbases_implicit_default
1571 [ -n "$topbases" ] ||
{
1572 # default is still top-bases for now
1573 topbases
="top-bases"
1574 topbasesrx
="top-bases"
1575 oldbases
="heads/{top-bases}"
1577 # MUST NOT be exported
1578 unset hblist both newtb rn tg_topases_set
1584 # if "$logrefupdates" is set and ref is not under refs/heads/ then force
1585 # an empty log file to exist so that ref changes will be logged
1586 # "$1" must be a fully-qualified refname (i.e. start with "refs/")
1587 # However, if "$1" is "refs/tgstash" then always make the reflog
1588 # The only ref not under refs/ that Git will write a reflog for is HEAD;
1589 # no matter what, it will NOT update a reflog for any other bare refs so
1590 # just quietly succeed when passed TG_STASH without doing anything.
1593 [ -n "$1" ] && [ "$1" != "TG_STASH" ] ||
return 0
1594 [ -n "$logrefupdates" ] ||
[ "$1" = "refs/tgstash" ] ||
return 0
1595 case "$1" in refs
/heads
/*|HEAD
) return 0;; refs
/*[!/]);; *) return 1; esac
1596 mkdir
-p "$git_common_dir/logs/${1%/*}" 2>/dev
/null ||
:
1597 { >>"$git_common_dir/logs/$1" ||
:; } 2>/dev
/null
1600 # store the "realpath" for "$2" in "$1" except the leaf is not resolved if it's
1601 # a symbolic link. The directory part must exist, but the basename need not.
1604 [ -n "$1" ] && [ -n "$2" ] ||
return 1
1605 set -- "$1" "$2" "${2%/}"
1607 */*) set -- "$1" "$2" "${3%/*}";;
1608 * ) set -- "$1" "$2" ".";;
1610 [ -d "$3" ] ||
return 1
1611 eval "$1="'"$(cd "$3" && pwd -P)/${2##*/}"'
1616 : "${TG_INST_CMDDIR:=@cmddir@}"
1617 : "${TG_INST_SHAREDIR:=@sharedir@}"
1618 : "${TG_INST_HOOKSDIR:=@hooksdir@}"
1620 [ -d "$TG_INST_CMDDIR" ] ||
1621 die
"No command directory: '$TG_INST_CMDDIR'"
1623 if [ -n "$tg__include" ]; then
1625 # We were sourced from another script for our utility functions;
1626 # this is set by hooks. Skip the rest of the file. A simple return doesn't
1627 # work as expected in every shell. See http://bugs.debian.org/516188
1629 # ensure setup happens
1640 case "$tgdir" in */*);;*) tgdir
="./$tgdir"; esac
1641 tgdir
="${tgdir%/*}/"
1643 [ "$0" != "$tgname" ] || tgdir
=""
1645 # If tg contains a '/' but does not start with one then replace it with an absolute path
1647 case "$0" in /*) ;; */*)
1648 tgdir
="$(cd "${0%/*}" && pwd -P)/"
1652 # If the tg in the PATH is the same as "$tg" just display the basename
1653 # tgdisplay will include any explicit -C <dir> option whereas tg will not
1655 tgdisplaydir
="$tgdir"
1658 v_get_abs_path _tgabs
"$tg" &&
1659 v_get_abs_path _tgnameabs
"$(cmd_path "$tgname")" &&
1660 [ "$_tgabs" = "$_tgnameabs" ]
1665 unset _tgabs _tgnameabs
1673 while :; do case "$1" in
1697 if [ -z "$1" ]; then
1698 echo "Option -r requires an argument." >&2
1704 explicit_remote
="$base_remote"
1705 tg
="$tgdir$tgname -r $explicit_remote"
1706 tgdisplay
="$tgdisplaydir$tgname"
1707 [ -z "$explicit_dir" ] || tgdisplay
="$tgdisplay -C \"$explicit_dir\""
1708 tgdisplay
="$tgdisplay -r $explicit_remote"
1712 unset base_remote explicit_remote
1714 tg
="$tgdir$tgname -u"
1715 tgdisplay
="$tgdisplaydir$tgname"
1716 [ -z "$explicit_dir" ] || tgdisplay
="$tgdisplay -C \"$explicit_dir\""
1717 tgdisplay
="$tgdisplay -u"
1722 if [ -z "$1" ]; then
1723 echo "Option -C requires an argument." >&2
1728 unset GIT_DIR GIT_COMMON_DIR
1730 gitcdopt
=" -C \"$explicit_dir\""
1732 tgdisplay
="$tgdisplaydir$tgname -C \"$explicit_dir\""
1733 [ -z "$explicit_remote" ] || tg
="$tg -r $explicit_remote"
1734 [ -z "$explicit_remote" ] || tgdisplay
="$tgdisplay -r $explicit_remote"
1735 [ -z "$noremote" ] || tg
="$tg -u"
1736 [ -z "$noremote" ] || tg
="$tgdisplay -u"
1741 if [ -z "$1" ]; then
1742 echo "Option -c requires an argument." >&2
1746 param
="'$(printf '%s\n' "$1" | sed "s
/[']/'\\\\''/g
")'"
1747 GIT_CONFIG_PARAMETERS
="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }$param"
1748 export GIT_CONFIG_PARAMETERS
1756 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
1765 [ -n "$cmd" -o $# -lt 1 ] ||
{ cmd
="$1"; shift; }
1769 [ -n "$cmd" ] ||
{ do_help
; exit 1; }
1782 exit ${do_status_result:-0};;
1786 echol
"$TG_INST_HOOKSDIR";;
1789 # Maintenance command
1792 echol
"refs/$topbases";;
1796 case "$cmd" in index-merge-one-file
)
1799 [ -r "$TG_INST_CMDDIR"/tg-
$isutil$cmd ] ||
{
1800 echo "Unknown subcommand: $cmd" >&2
1806 if [ "$*" = "-h" ] ||
[ "$*" = "--help" ]; then
1810 [ -n "$showing_help" ] || initial_setup
1811 [ -z "$noremote" ] ||
unset base_remote
1813 nomergesetup
="$showing_help"
1814 case "$cmd" in base|info|log|summary|rebase|revert|tag
)
1815 # avoid merge setup where not necessary
1820 if [ -z "$nomergesetup" ]; then
1821 # make sure merging the .top* files will always behave sanely
1824 setup_hook
"pre-commit"
1827 # everything but rebase needs topbases set
1828 carefully
="$showing_help"
1829 [ "$cmd" != "migrate-bases" ] || carefully
=1
1830 [ "$cmd" = "rebase" ] || set_topbases
$carefully
1834 case "$cmd$showing_help" in
1835 summary|info|
export|tag
)
1837 annihilate|create|delete|depend|import|update
)
1840 [ -z "$_use_ref_cache" ] || v_create_ref_cache
1842 fullcmd
="${tgname:-tg} $cmd $*"
1843 .
"$TG_INST_CMDDIR"/tg-
$isutil$cmd;;