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
10 # Update if you add any code that requires a newer version of git
11 GIT_MINIMUM_VERSION
=1.7.7.2
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"
21 ## Auxiliary functions
25 echo "${TG_RECURSIVE}${tgname:-tg}: $*"
30 info
"warning: $*" >&2
52 echo "$3" |
tr "${separator}" '\n' |
(for l
in $
(echo "$2"|
tr "${separator}" ' '); do
54 [ $l -ge $r ] ||
return 1
55 [ $l -gt $r ] && return 0
60 git_ver
="$(git version | sed -e 's/^[^0-9][^0-9]*//')"
61 compare_versions .
"${git_ver%%[!0-9.]*}" "${GIT_MINIMUM_VERSION}" \
62 || die
"git version >= ${GIT_MINIMUM_VERSION} required"
65 case "$1" in version|
--version|
-V)
66 echo "TopGit version $TG_VERSION"
71 [ "$1" = "precheck" ] && exit 0
74 cat_depsmsg_internal
()
76 _rev
="$(ref_exists_rev "refs
/heads
/$1")" ||
return 0
77 if [ -s "$tg_cache_dir/$1/.$2" ]; then
78 if read _rev_match
&& [ "$_rev" = "$_rev_match" ]; then
80 while IFS
= read -r _line ||
[ -n "$_line" ]; do
81 printf '%s\n' "$_line"
84 fi <"$tg_cache_dir/$1/.$2"
86 [ -d "$tg_cache_dir/$1" ] || mkdir
-p "$tg_cache_dir/$1" 2>/dev
/null ||
:
87 if [ -d "$tg_cache_dir/$1" ]; then
88 printf '%s\n' "$_rev" >"$tg_cache_dir/$1/.$2"
90 git cat-file blob
"$_rev:.$2" 2>/dev
/null |
91 while IFS
= read -r _line ||
[ -n "$_line" ]; do
92 printf '%s\n' "$_line" >&3
93 printf '%s\n' "$_line"
94 done 3>>"$tg_cache_dir/$1/.$2"
96 git cat-file blob
"$_rev:.$2" 2>/dev
/null
100 # cat_deps BRANCHNAME
104 cat_depsmsg_internal
"$1" topdeps
111 cat_depsmsg_internal
"$1" topmsg
114 # cat_file TOPIC:PATH [FROM]
115 # cat the file PATH from branch TOPIC when FROM is empty.
116 # FROM can be -i or -w, than the file will be from the index or worktree,
117 # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
123 cat "$root_dir/${path#*:}"
126 # ':file' means cat from index
127 git cat-file blob
":${path#*:}"
131 refs
/heads
/*:.topdeps
)
132 _temp
="${path%:.topdeps}"
133 cat_deps
"${_temp#refs/heads/}"
135 refs
/heads
/*:.topmsg
)
136 _temp
="${path%:.topmsg}"
137 cat_msg
"${_temp#refs/heads/}"
140 git cat-file blob
"$path"
145 die
"Wrong argument to cat_file: '$2'"
150 # get tree for the committed topic
156 # get tree for the base
159 echo "refs/top-bases/$1"
162 # get tree for the index
168 # get tree for the worktree
171 i_tree
=$
(git write-tree
)
173 # the file for --index-output needs to sit next to the
176 : ${GIT_INDEX_FILE:="$git_dir/index"}
177 TMP_INDEX
="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX
")"
178 git read-tree
-m $i_tree --index-output="$TMP_INDEX" &&
179 GIT_INDEX_FILE
="$TMP_INDEX" &&
180 export GIT_INDEX_FILE
&&
181 git
diff --name-only -z HEAD |
182 git update-index
-z --add --remove --stdin &&
188 # strip_ref "$(git symbolic-ref HEAD)"
189 # Output will have a leading refs/heads/ or refs/top-bases/ stripped if present
194 echo "${1#refs/heads/}"
197 echo "${1#refs/top-bases/}"
204 # pretty_tree NAME [-b | -i | -w]
205 # Output tree ID of a cleaned-up tree without tg's artifacts.
206 # NAME will be ignored for -i and -w, but needs to be present
211 git ls-tree
--full-tree "$(get_tree_$source "$name")" |
212 LC_ALL
=C
sed -ne '/ \.top.*$/!p' |
219 tgname
="$(basename "$0")"
220 hook_call
="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
221 if [ -f "$git_dir/hooks/$1" ] && fgrep
-q "$hook_call" "$git_dir/hooks/$1"; then
222 # Another job well done!
225 # Prepare incantation
227 if [ -s "$git_dir/hooks/$1" -a -x "$git_dir/hooks/$1" ]; then
228 hook_call
="$hook_call"' || exit $?'
229 if ! LC_ALL
=C
sed -n 1p
<"$git_dir/hooks/$1" | LC_ALL
=C fgrep
-qx "#!@SHELL_PATH@"; then
231 while [ -e "$git_dir/hooks/$1-chain$chain_num" ]; do
232 chain_num
=$
(( $chain_num + 1 ))
234 cp -p "$git_dir/hooks/$1" "$git_dir/hooks/$1-chain$chain_num"
238 hook_call
="exec $hook_call"
240 # Don't call hook if tg is not installed
241 hook_call
="if which \"$tgname\" > /dev/null; then $hook_call; fi"
242 # Insert call into the hook
244 echo "#!@SHELL_PATH@"
246 if [ -n "$hook_chain" ]; then
247 echo "exec \"\$0-chain$chain_num\" \"\$@\""
249 [ ! -s "$git_dir/hooks/$1" ] ||
cat "$git_dir/hooks/$1"
251 } >"$git_dir/hooks/$1+"
252 chmod a
+x
"$git_dir/hooks/$1+"
253 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
256 # setup_ours (no arguments)
259 if [ ! -s "$git_dir/info/attributes" ] ||
! grep -q topmsg
"$git_dir/info/attributes"; then
260 [ -d "$git_dir/info" ] || mkdir
"$git_dir/info"
262 echo ".topmsg merge=ours"
263 echo ".topdeps merge=ours"
264 } >>"$git_dir/info/attributes"
266 if ! git config merge.ours.driver
>/dev
/null
; then
267 git config merge.ours.name
'"always keep ours" merge driver'
268 git config merge.ours.driver
'touch %A'
272 # measure_branch NAME [BASE]
275 _bname
="$1"; _base
="$2"
276 [ -n "$_base" ] || _base
="refs/top-bases/$_bname"
277 # The caller should've verified $name is valid
278 _commits
="$(git rev-list "$_bname" ^"$_base" -- | wc_l)"
279 _nmcommits
="$(git rev-list --no-merges "$_bname" ^"$_base" -- | wc_l)"
280 if [ $_commits -ne 1 ]; then
285 echo "$_commits/$_nmcommits $_suffix"
288 # branch_contains B1 B2
289 # Whether B1 is a superset of B2.
292 _revb1
="$(ref_exists_rev "$1")" ||
return 0
293 _revb2
="$(ref_exists_rev "$2")" ||
return 0
294 if [ -s "$tg_cache_dir/$1/.bc/$2/.d" ]; then
295 if read _result _rev_matchb1 _rev_matchb2
&& \
296 [ "$_revb1" = "$_rev_matchb1" -a "$_revb2" = "$_rev_matchb2" ]; then
298 fi <"$tg_cache_dir/$1/.bc/$2/.d"
300 [ -d "$tg_cache_dir/$1/.bc/$2" ] || mkdir
-p "$tg_cache_dir/$1/.bc/$2" 2>/dev
/null ||
:
302 [ -z "$(git rev-list --max-count=1 ^"$_revb1" "$_revb2" --)" ] || _result
=$?
303 if [ -d "$tg_cache_dir/$1/.bc/$2" ]; then
304 echo "$_result" "$_revb1" "$_revb2" >"$tg_cache_dir/$1/.bc/$2/.d"
311 [ ! -s "$tg_tmp_dir/tg~ref-dirs-created" -a -s "$tg_ref_cache" ] ||
return 0
312 sed -e 's/ .*$//;'"s~^~$tg_tmp_dir/cached/~" <"$tg_ref_cache" |
xargs mkdir
-p
313 echo 1 >"$tg_tmp_dir/tg~ref-dirs-created"
316 # If the first argument is non-empty, outputs "1" if this call created the cache
319 [ -n "$tg_ref_cache" -a ! -s "$tg_ref_cache" ] ||
return 0
321 [ -z "$base_remote" ] || _remotespec
="refs/remotes/$base_remote"
322 [ -z "$1" ] ||
printf '1'
323 git for-each-ref
--format='%(refname) %(objectname)' \
324 refs
/heads refs
/top-bases
$_remotespec >"$tg_ref_cache"
330 [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ] ||
return 0
334 # setting tg_ref_cache_only to non-empty will force non-$tg_ref_cache lookups to fail
337 if [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ]; then
338 LC_ALL
=C
awk -v r
="$1" 'BEGIN {e=1}; $1 == r {print $2; e=0; exit}; END {exit e}' <"$tg_ref_cache"
340 [ -z "$tg_ref_cache_only" ] ||
return 1
341 git rev-parse
--quiet --verify "$1^0" -- 2>/dev
/null
346 # Whether REF is a valid ref name
347 # REF must be fully qualified and start with refs/heads/, refs/top-bases/
348 # or, if $base_remote is set, refs/remotes/$base_remote/
349 # Caches result if $tg_read_only and outputs HASH on success
359 die
"ref_exists_rev requires fully-qualified ref name"
361 [ -n "$tg_read_only" ] ||
{ git rev-parse
--quiet --verify "$1^0" -- 2>/dev
/null
; return; }
364 { read -r _result _result_rev
<"$tg_tmp_dir/cached/$1/.ref"; } 2>/dev
/null ||
:
365 [ -z "$_result" ] ||
{ printf '%s' "$_result_rev"; return $_result; }
366 _result_rev
="$(rev_parse "$1")"
368 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir
-p "$tg_tmp_dir/cached/$1" 2>/dev
/null
369 [ ! -d "$tg_tmp_dir/cached/$1" ] || \
370 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.ref" 2>/dev
/null ||
:
371 printf '%s' "$_result_rev"
375 # Same as ref_exists_rev but output is abbreviated hash
376 ref_exists_rev_short
()
384 die
"ref_exists_rev_short requires fully-qualified ref name"
386 [ -n "$tg_read_only" ] ||
{ git rev-parse
--quiet --verify --short "$1^0" -- 2>/dev
/null
; return; }
389 { read -r _result _result_rev
<"$tg_tmp_dir/cached/$1/.rfs"; } 2>/dev
/null ||
:
390 [ -z "$_result" ] ||
{ printf '%s' "$_result_rev"; return $_result; }
391 _result_rev
="$(rev_parse "$1")"
393 if [ $_result -eq 0 ]; then
394 _result_rev
="$(git rev-parse --verify --short --quiet "$_result_rev" --)"
397 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir
-p "$tg_tmp_dir/cached/$1" 2>/dev
/null
398 [ ! -d "$tg_tmp_dir/cached/$1" ] || \
399 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.rfs" 2>/dev
/null ||
:
400 printf '%s' "$_result_rev"
405 # Whether REF is a valid ref name
406 # REF must be fully qualified and start with refs/heads/, refs/top-bases/
407 # or, if $base_remote is set, refs/remotes/$base_remote/
411 ref_exists_rev
"$1" >/dev
/null
415 # Runs git rev-parse REF^{tree}
416 # Caches result if $tg_read_only
419 [ -n "$tg_read_only" ] ||
{ git rev-parse
--verify "$1^{tree}" -- 2>/dev
/null
; return; }
420 if [ -f "$tg_tmp_dir/cached/$1/.rpt" ]; then
421 if IFS
= read -r _result
<"$tg_tmp_dir/cached/$1/.rpt"; then
422 printf '%s\n' "$_result"
427 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir
-p "$tg_tmp_dir/cached/$1" 2>/dev
/null ||
:
428 if [ -d "$tg_tmp_dir/cached/$1" ]; then
429 git rev-parse
--verify "$1^{tree}" -- >"$tg_tmp_dir/cached/$1/.rpt" 2>/dev
/null ||
:
430 if IFS
= read -r _result
<"$tg_tmp_dir/cached/$1/.rpt"; then
431 printf '%s\n' "$_result"
436 git rev-parse
--verify "$1^{tree}" -- 2>/dev
/null
440 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
443 [ -n "$base_remote" ] && ref_exists
"refs/remotes/$base_remote/$1"
446 # Return the verified TopGit branch name or die with an error.
447 # As a convenience, if HEAD is given and HEAD is a symbolic ref to
448 # refs/heads/... then ... will be verified instead.
449 # if "$2" = "-f" (for fail) then return an error rather than dying.
450 verify_topgit_branch
()
454 _verifyname
="${1#refs/heads/}"
457 _verifyname
="${1#refs/top-bases/}"
460 _verifyname
="$(git symbolic-ref HEAD 2>/dev/null || :)"
461 [ -n "$_verifyname" -o "$2" = "-f" ] || die
"HEAD is not a symbolic ref"
462 case "$_verifyname" in refs
/heads
/*) :;; *)
463 [ "$2" != "-f" ] ||
return 1
464 die
"HEAD is not a symbolic ref to the refs/heads namespace"
466 _verifyname
="${_verifyname#refs/heads/}"
472 if ! ref_exists
"refs/heads/$_verifyname"; then
473 [ "$2" != "-f" ] ||
return 1
474 die
"no such branch: $_verifyname"
476 if ! ref_exists
"refs/top-bases/$_verifyname"; then
477 [ "$2" != "-f" ] ||
return 1
478 die
"not a TopGit-controlled branch: $_verifyname"
480 printf '%s' "$_verifyname"
484 # $1 = branch name (i.e. "t/foo/bar")
485 # $2 = optional result of rev-parse "refs/heads/$1"
486 # $3 = optional result of rev-parse "refs/top-bases/$1"
490 _rev
="${2:-$(ref_exists_rev "refs/heads/$_branch_name")}"
491 _rev_base
="${3:-$(ref_exists_rev "refs/top-bases/$_branch_name")}"
496 { read -r _result _result_rev _result_rev_base
<"$tg_cache_dir/$_branch_name/.ann"; } 2>/dev
/null ||
:
497 [ -z "$_result" -o "$_result_rev" != "$_rev" -o "$_result_rev_base" != "$_rev_base" ] ||
return $_result
499 # use the merge base in case the base is ahead.
500 mb
="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)"
502 test -z "$mb" ||
test "$(rev_parse_tree "$mb")" = "$(rev_parse_tree "$_rev")"
504 [ -d "$tg_cache_dir/$_branch_name" ] || mkdir
-p "$tg_cache_dir/$_branch_name" 2>/dev
/null
505 [ ! -d "$tg_cache_dir/$_branch_name" ] || \
506 echo $_result $_rev $_rev_base >"$tg_cache_dir/$_branch_name/.ann" 2>/dev
/null ||
:
510 non_annihilated_branches
()
512 git for-each-ref
--format='%(objectname) %(refname)' refs
/top-bases |
513 while read rev ref
; do
514 name
="${ref#refs/top-bases/}"
515 if branch_annihilated
"$name" "" "$rev"; then
522 # Make sure our tree is clean
525 git update-index
--ignore-submodules --refresh ||
526 die
"the working directory has uncommitted changes (see above) - first commit or reset them"
527 [ -z "$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)" ] ||
528 die
"the index has uncommited changes"
532 # Whether REF is a SHA1 (compared to a symbolic name).
535 case "$1" in $octet20) return 0;; esac
539 # recurse_deps_internal NAME [BRANCHPATH...]
540 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
541 # followed by a 1 if the branch is "tgish" or a 0 if not
542 # then the branch name followed by its depedency chain (which might be empty)
543 # An output line might look like this:
544 # 0 1 t/foo/leaf t/foo/int t/stage
545 # If no_remotes is non-empty, exclude remotes
546 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
547 # any branch names in the space-separated recurse_deps_exclude variable
548 # are skipped (along with their dependencies)
549 recurse_deps_internal
()
551 case " $recurse_deps_exclude " in *" $1 "*) return 0; esac
553 if ! _ref_hash
="$(ref_exists_rev "refs
/heads
/$1")"; then
554 [ -z "$2" ] ||
echo "1 0 $*"
560 ! _ref_hash_base
="$(ref_exists_rev "refs
/top-bases
/$1")" || _is_tgish
=1
561 [ -z "$recurse_preorder" -o -z "$2" ] ||
echo "0 $_is_tgish $*"
563 # If no_remotes is unset also check our base against remote base.
564 # Checking our head against remote head has to be done in the helper.
565 if [ -n "$_is_tgish" -a -z "$no_remotes" ] && has_remote
"top-bases/$1"; then
566 echo "0 0 refs/remotes/$base_remote/top-bases/$1 $*"
569 # if the branch was annihilated, it is considered to have no dependencies
570 if [ -n "$_is_tgish" ] && ! branch_annihilated
"$1" "$_ref_hash" "$_ref_hash_base"; then
571 #TODO: handle nonexisting .topdeps?
573 while read _dname
; do
574 # Shoo shoo, leave our environment alone!
575 (recurse_deps_internal
"$_dname" "$@")
579 [ -n "$recurse_preorder" -o -z "$2" ] ||
echo "0 $_is_tgish $*"
583 # helper for recurse_deps so that a return statement executed inside CMD
584 # does not return from recurse_deps. This shouldn't be necessary, but it
585 # seems that it actually is.
591 # becomes read-only for caching purposes
592 # assigns new value to tg_read_only
593 # become_cacheable/undo_become_cacheable calls may be nested
596 _old_tg_read_only
="$tg_read_only"
597 if [ -z "$tg_read_only" ]; then
598 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
601 _my_ref_cache
="$(create_ref_cache 1)"
602 _my_ref_cache
="${_my_ref_cache:+1}"
603 tg_read_only
="undo${_my_ref_cache:-0}-$_old_tg_read_only"
606 # restores tg_read_only and ref_cache to state before become_cacheable call
607 # become_cacheable/undo_bocome_cacheable calls may be nested
608 undo_become_cacheable
()
610 case "$tg_read_only" in
612 _suffix
="${tg_read_only#undo?-}"
613 [ "${tg_read_only%$_suffix}" = "undo0-" ] || remove_ref_cache
614 tg_read_only
="$_suffix"
618 # just call this, no undo, sets tg_read_only= and removes ref cache and cached results
619 become_non_cacheable
()
623 rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
626 # recurse_deps CMD NAME [BRANCHPATH...]
627 # Recursively eval CMD on all dependencies of NAME.
628 # Dependencies are visited in topological order.
629 # CMD can refer to $_name for queried branch name,
630 # $_dep for dependency name,
631 # $_depchain for space-seperated branch backtrace,
632 # $_dep_missing boolean to check whether $_dep is present
633 # and the $_dep_is_tgish boolean.
634 # It can modify $_ret to affect the return value
635 # of the whole function.
636 # If recurse_deps() hits missing dependencies, it will append
637 # them to space-separated $missing_deps list and skip them
638 # after calling CMD with _dep_missing set.
639 # remote dependencies are processed if no_remotes is unset.
640 # any branch names in the space-separated recurse_deps_exclude variable
641 # are skipped (along with their dependencies)
647 _depsfile
="$(get_temp tg-depsfile)"
648 recurse_deps_internal
"$@" >>"$_depsfile"
649 undo_become_cacheable
652 while read _ismissing _istgish _dep _name _deppath
; do
653 _depchain
="$_name${_deppath:+ $_deppath}"
655 [ "$_istgish" = "0" ] || _dep_is_tgish
=1
657 if [ "$_ismissing" != "0" ]; then
659 case " $missing_deps " in *" $_dep "*) :;; *)
660 missing_deps
="${missing_deps:+$missing_deps }$_dep"
669 # branch_needs_update
670 # This is a helper function for determining whether given branch
671 # is up-to-date wrt. its dependencies. It expects input as if it
672 # is called as a recurse_deps() helper.
673 # In case the branch does need update, it will echo it together
674 # with the branch backtrace on the output (see needs_update()
675 # description for details) and set $_ret to non-zero.
676 branch_needs_update
()
678 if [ -n "$_dep_missing" ]; then
679 echo "! $_dep $_depchain"
684 if [ -n "$_dep_is_tgish" ]; then
685 branch_annihilated
"$_dep" && return 0
687 if has_remote
"$_dep"; then
688 branch_contains
"refs/heads/$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update
=%
690 # This can possibly override the remote check result;
691 # we want to sync with our base first
692 branch_contains
"refs/heads/$_dep" "refs/top-bases/$_dep" || _dep_base_update
=:
695 if [ -n "$_dep_base_update" ]; then
696 # _dep needs to be synced with its base/remote
697 echo "$_dep_base_update $_dep $_depchain"
699 elif [ -n "$_name" ]; then
700 case "$_dep" in refs
/*) _fulldep
="$_dep";; *) _fulldep
="refs/heads/$_dep";; esac
701 if ! branch_contains
"refs/top-bases/$_name" "$_fulldep"; then
702 # Some new commits in _dep
703 echo "$_dep $_depchain"
710 # This function is recursive; it outputs reverse path from NAME
711 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
712 # inner paths first. Innermost name can be ':' if the head is
713 # not in sync with the base, '%' if the head is not in sync
714 # with the remote (in this order of priority) or '!' if depednecy
716 # It will also return non-zero status if NAME needs update.
717 # If needs_update() hits missing dependencies, it will append
718 # them to space-separated $missing_deps list and skip them.
721 recurse_deps branch_needs_update
"$1"
724 # branch_empty NAME [-i | -w]
728 _rev
="$(ref_exists_rev "refs
/heads
/$1")" ||
return 0
731 { read -r _result _result_rev
<"$tg_cache_dir/$1/.mt"; } 2>/dev
/null ||
:
732 [ -z "$_result" -o "$_result_rev" != "$_rev" ] ||
return $_result
734 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ] || _result
=$?
735 [ -d "$tg_cache_dir/$1" ] || mkdir
-p "$tg_cache_dir/$1" 2>/dev
/null
736 [ ! -d "$tg_cache_dir/$1" ] ||
echo $_result $_rev >"$tg_cache_dir/$1/.mt"
739 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ]
743 # list_deps [-i | -w] [BRANCH]
744 # -i/-w apply only to HEAD
748 [ "$1" != "-i" -a "$1" != "-w" ] ||
{ head_from
="$1"; shift; }
749 head="$(git symbolic-ref -q HEAD)" ||
752 git for-each-ref
--format='%(objectname) %(refname)' refs
/top-bases
"${1:+/$1}" |
753 while read rev ref
; do
754 name
="${ref#refs/top-bases/}"
755 if branch_annihilated
"$name" "" "$rev"; then
760 [ "refs/heads/$name" = "$head" ] ||
762 cat_file
"refs/heads/$name:.topdeps" $from |
while read dep
; do
764 ref_exists
"refs/top-bases/$dep" ||
766 if ! "$dep_is_tgish" ||
! branch_annihilated
$dep; then
773 # switch_to_base NAME [SEED]
776 _base
="refs/top-bases/$1"; _seed
="$2"
777 # We have to do all the hard work ourselves :/
778 # This is like git checkout -b "$_base" "$_seed"
779 # (or just git checkout "$_base"),
780 # but does not create a detached HEAD.
781 git read-tree
-u -m HEAD
"${_seed:-$_base}"
782 [ -z "$_seed" ] || git update-ref
"$_base" "$_seed"
783 git symbolic-ref HEAD
"$_base"
786 # run editor with arguments
787 # the editor setting will be cached in $tg_editor (which is eval'd)
788 # result non-zero if editor fails or GIT_EDITOR cannot be determined
791 tg_editor
="$GIT_EDITOR"
792 [ -n "$tg_editor" ] || tg_editor
="$(git var GIT_EDITOR)" ||
return $?
793 eval "$tg_editor" '"$@"'
796 # Show the help messages.
800 if [ "$1" = "-w" ]; then
804 if [ -z "$1" ] ; then
805 # This is currently invoked in all kinds of circumstances,
806 # including when the user made a usage error. Should we end up
807 # providing more than a short help message, then we should
809 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
811 ## Build available commands list for help output
815 for cmd
in "@cmddir@"/tg-
*; do
816 ! [ -r "$cmd" ] && continue
817 # strip directory part and "tg-" prefix
818 cmd
="$(basename "$cmd")"
824 echo "TopGit version $TG_VERSION - A different patch queue manager"
825 echo "Usage: $tgname [-C <dir>] [-r <remote> | -u] [-c <name>=<val>] ($cmds) ..."
826 echo " Or: $tgname help [-w] [<command>]"
827 echo "Use \"$tgdisplaydir$tgname help tg\" for overview of TopGit"
828 elif [ -r "@cmddir@"/tg-
$1 -o -r "@sharedir@/tg-$1.txt" ] ; then
829 if [ -n "$_www" ]; then
831 if ! [ -r "@sharedir@/topgit.html" ]; then
832 echo "`basename $0`: missing html help file:" \
833 "@sharedir@/topgit.html" 1>&2
836 if ! [ -r "@sharedir@/tg-$1.html" ]; then
837 echo "`basename $0`: missing html help file:" \
838 "@sharedir@/tg-$1.html" 1>&2
841 if [ -n "$nohtml" ]; then
842 echo "`basename $0`: use" \
843 "\"`basename $0` help $1\" instead" 1>&2
846 git web--browse
-c help.browser
"@sharedir@/tg-$1.html"
851 if [ -r "@cmddir@"/tg-
$1 ] ; then
852 "@cmddir@"/tg-
$1 -h 2>&1 ||
:
855 if [ -r "@sharedir@/tg-$1.txt" ] ; then
856 cat "@sharedir@/tg-$1.txt"
861 echo "`basename $0`: no help for $1" 1>&2
875 # pass "diff" to get pager.diff
876 # if pager.$1 is a boolean false returns cat
877 # if set to true or unset fails
878 # otherwise succeeds and returns the value
881 if _x
="$(git config --bool "pager.
$1" 2>/dev/null)"; then
882 [ "$_x" != "true" ] ||
return 1
886 if _x
="$(git config "pager.
$1" 2>/dev/null)"; then
894 # Set TG_PAGER to a valid executable
895 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
896 # See also the following "page" function for ease of use
897 # emptypager will be set to 1 (otherwise empty) if TG_PAGER was set to "cat" to not be empty
898 # Preference is (same as Git):
900 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
901 # 3. core.pager (only if set)
903 # 5. git var GIT_PAGER
907 isatty
1 ||
{ emptypager
=1; TG_PAGER
=cat; return 0; }
910 if [ -z "$TG_PAGER_IN_USE" ]; then
911 # TG_PAGER = GIT_PAGER | PAGER | less
912 # NOTE: GIT_PAGER='' is significant
913 if [ -n "${GIT_PAGER+set}" ]; then
914 TG_PAGER
="$GIT_PAGER"
915 elif [ -n "$USE_PAGER_TYPE" ] && _dp
="$(get_pager "$USE_PAGER_TYPE")"; then
917 elif _cp
="$(git config core.pager 2>/dev/null)"; then
919 elif [ -n "${PAGER+set}" ]; then
922 _gp
="$(git var GIT_PAGER 2>/dev/null || :)"
923 [ "$_gp" != ":" ] || _gp
=
924 TG_PAGER
="${_gp:-less}"
926 if [ -z "$TG_PAGER" ]; then
935 # Set pager default environment variables
936 # see pager.c:setup_pager
937 if [ -z "${LESS+set}" ]; then
941 if [ -z "${LV+set}" ]; then
946 # this is needed so e.g. `git diff` will still colorize it's output if
947 # requested in ~/.gitconfig with color.diff=auto
949 export GIT_PAGER_IN_USE
951 # this is needed so we don't get nested pagers
953 export TG_PAGER_IN_USE
956 # page eval_arg [arg ...]
958 # Calls setup_pager then evals the first argument passing it all the rest
959 # where the output is piped through eval "$TG_PAGER" unless emptypager is set
960 # by setup_pager (in which case the output is left as-is).
962 # To handle arbitrary paging duties, collect lines to be paged into a
963 # function and then call page with the function name or perhaps func_name "$@".
965 # If no arguments at all are passed in do nothing (return with success).
968 [ $# -gt 0 ] ||
return 0
971 if [ -n "$emptypager" ]; then
972 eval "$_evalarg" '"$@"'
974 eval "$_evalarg" '"$@"' |
eval "$TG_PAGER"
979 # creates a new temporary file (or directory with -d) in the global
980 # temporary directory $tg_tmp_dir with pattern prefix NAME
983 mktemp
$2 "$tg_tmp_dir/$1.XXXXXX"
986 # automatically called by strftime
987 # does nothing if already setup
988 # may be called explicitly if the first call would otherwise be in a subshell
989 # so that the setup is only done once before subshells start being spawned
992 [ -z "$strftime_is_setup" ] ||
return 0
994 # date option to format raw epoch seconds values
997 _testdt
='2000-02-29 07:03:08 UTC'
998 _testfm
='%Y-%m-%d %H:%M:%S %Z'
999 if [ "$(TZ=UTC date "-d@
$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1001 elif [ "$(TZ=UTC date "-r$_testes" "+$_testfm" 2>/dev/null)" = "$_testdt" ]; then
1007 # $1 => strftime format string to use
1008 # $2 => raw timestamp as seconds since epoch
1009 # $3 => optional time zone string (empty/absent for local time zone)
1013 if [ -n "$daterawopt" ]; then
1014 if [ -n "$3" ]; then
1015 TZ
="$3" date "$daterawopt$2" "+$1"
1017 date "$daterawopt$2" "+$1"
1020 if [ -n "$3" ]; then
1021 TZ
="$3" perl
-MPOSIX=strftime
-le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1023 perl
-MPOSIX=strftime
-le 'print strftime($ARGV[0],localtime($ARGV[1]))' "$1" "$2"
1031 # suppress the merge log editor feature since git 1.7.10
1033 GIT_MERGE_AUTOEDIT
=no
1034 export GIT_MERGE_AUTOEDIT
1035 git_dir
="$(git rev-parse --git-dir)"
1036 root_dir
="$(git rev-parse --show-cdup)"; root_dir
="${root_dir:-.}"
1037 logrefupdates
="$(git config --bool core.logallrefupdates 2>/dev/null || :)"
1038 [ "$logrefupdates" = "true" ] || logrefupdates
=
1039 tgsequester
="$(git config --bool topgit.sequester 2>/dev/null || :)"
1041 [ "$tgsequester" != "false" ] || tgnosequester
=1
1044 # make sure root_dir doesn't end with a trailing slash.
1046 root_dir
="${root_dir%/}"
1047 [ -n "$base_remote" ] || base_remote
="$(git config topgit.remote 2>/dev/null)" ||
:
1049 # make sure global cache directory exists inside GIT_DIR
1051 tg_cache_dir
="$git_dir/tg-cache"
1052 [ -d "$tg_cache_dir" ] || mkdir
"$tg_cache_dir"
1054 # create global temporary directories, inside GIT_DIR
1057 trap 'rm -rf "$tg_tmp_dir"' EXIT
1060 trap 'exit 131' QUIT
1061 trap 'exit 134' ABRT
1062 trap 'exit 143' TERM
1063 tg_tmp_dir
="$(mktemp -d "$git_dir/tg-tmp.XXXXXX
")"
1064 tg_ref_cache
="$tg_tmp_dir/tg~ref-cache"
1067 # return the "realpath" for the item except the leaf is not resolved if it's
1068 # a symbolic link. The directory part must exist, but the basename need not.
1071 [ -n "$1" -a -d "$(dirname -- "$1")" ] ||
return 1
1072 printf '%s' "$(cd -- "$
(dirname -- "$1")" && pwd -P)/$(basename -- "$1")"
1077 [ -d "@cmddir@" ] ||
1078 die
"No command directory: '@cmddir@'"
1080 if [ -n "$tg__include" ]; then
1082 # We were sourced from another script for our utility functions;
1083 # this is set by hooks. Skip the rest of the file. A simple return doesn't
1084 # work as expected in every shell. See http://bugs.debian.org/516188
1086 # ensure setup happens
1095 tgdir
="$(dirname -- "$tg")/"
1096 tgname
="$(basename -- "$tg")"
1097 [ "$0" != "$tgname" ] || tgdir
=""
1099 # If tg contains a '/' but does not start with one then replace it with an absolute path
1101 case "$0" in /*) :;; */*)
1102 tgdir
="$(cd "$
(dirname -- "$0")" && pwd -P)/"
1106 # If the tg in the PATH is the same as "$tg" just display the basename
1107 # tgdisplay will include any explicit -C <dir> option whereas tg will not
1109 tgdisplaydir
="$tgdir"
1111 if [ "$(get_abs_path "$tg")" = "$(get_abs_path "$
(which "$tgname" ||
:)" || :)" ]; then
1122 while :; do case "$1" in
1136 if [ -z "$1" ]; then
1137 echo "Option -r requires an argument." >&2
1143 explicit_remote
="$base_remote"
1144 tg
="$tgdir$tgname -r $explicit_remote"
1145 tgdisplay
="$tgdisplaydir$tgname"
1146 [ -z "$explicit_dir" ] || tgdisplay
="$tgdisplay -C \"$explicit_dir\""
1147 tgdisplay
="$tgdisplay -r $explicit_remote"
1151 unset base_remote explicit_remote
1153 tg
="$tgdir$tgname -u"
1154 tgdisplay
="$tgdisplaydir$tgname"
1155 [ -z "$explicit_dir" ] || tgdisplay
="$tgdisplay -C \"$explicit_dir\""
1156 tgdisplay
="$tgdisplay -u"
1161 if [ -z "$1" ]; then
1162 echo "Option -C requires an argument." >&2
1169 gitcdopt
=" -C \"$explicit_dir\""
1171 tgdisplay
="$tgdisplaydir$tgname -C \"$explicit_dir\""
1172 [ -z "$explicit_remote" ] || tg
="$tg -r $explicit_remote"
1173 [ -z "$explicit_remote" ] || tgdisplay
="$tgdisplay -r $explicit_remote"
1174 [ -z "$noremote" ] || tg
="$tg -u"
1175 [ -z "$noremote" ] || tg
="$tgdisplay -u"
1180 if [ -z "$1" ]; then
1181 echo "Option -c requires an argument." >&2
1185 param
="'$(printf '%s\n' "$1" | sed "s
/[']/'\\\\''/g
")'"
1186 GIT_CONFIG_PARAMETERS
="${GIT_CONFIG_PARAMETERS:+$GIT_CONFIG_PARAMETERS }$param"
1187 export GIT_CONFIG_PARAMETERS
1195 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
1204 [ -n "$cmd" -o $# -lt 1 ] ||
{ cmd
="$1"; shift; }
1208 [ -n "$cmd" ] ||
{ do_help
; exit 1; }
1221 [ -r "@cmddir@"/tg-
$cmd ] ||
{
1222 echo "Unknown subcommand: $cmd" >&2
1228 [ -z "$noremote" ] ||
unset base_remote
1231 case "$cmd" in info|log|summary|rebase|revert|tag
)
1232 # avoid merge setup where not necessary
1237 if [ -z "$nomergesetup" ]; then
1238 # make sure merging the .top* files will always behave sanely
1241 setup_hook
"pre-commit"
1247 summary|info|
export|tag
)
1249 annihilate|create|delete|depend|import|update
)
1252 [ -z "$_use_ref_cache" ] || create_ref_cache
1254 .
"@cmddir@"/tg-
$cmd;;