2 # TopGit - A different patch queue manager
3 # (C) Petr Baudis <pasky@suse.cz> 2008
4 # (C) Kyle J. McKay <mackyle@gmail.com> 2014,2015
9 # Update if you add any code that requires a newer version of git
10 GIT_MINIMUM_VERSION
=1.7.7.2
14 octet
='[0-9a-f][0-9a-f]'
15 octet4
="$octet$octet$octet$octet"
16 octet19
="$octet4$octet4$octet4$octet4$octet$octet$octet"
17 octet20
="$octet4$octet4$octet4$octet4$octet4"
19 ## Auxiliary functions
23 echo "${TG_RECURSIVE}${tgname:-tg}: $*"
40 echo "$3" |
tr "${separator}" '\n' |
(for l
in $
(echo "$2"|
tr "${separator}" ' '); do
42 [ $l -ge $r ] ||
return 1
43 [ $l -gt $r ] && return 0
48 git_ver
="$(git version | sed -e 's/^[^0-9][^0-9]*//')"
49 compare_versions .
"${git_ver%%[!0-9.]*}" "${GIT_MINIMUM_VERSION}" \
50 || die
"git version >= ${GIT_MINIMUM_VERSION} required"
53 case "$1" in version|
--version|
-V)
54 echo "TopGit version $TG_VERSION"
59 [ "$1" = "precheck" ] && exit 0
62 cat_depsmsg_internal
()
64 _rev
="$(ref_exists_rev "refs
/heads
/$1")" ||
return 0
65 if [ -s "$tg_cache_dir/$1/.$2" ]; then
66 if read _rev_match
&& [ "$_rev" = "$_rev_match" ]; then
68 while IFS
= read -r _line ||
[ -n "$_line" ]; do
69 printf '%s\n' "$_line"
72 fi <"$tg_cache_dir/$1/.$2"
74 [ -d "$tg_cache_dir/$1" ] || mkdir
-p "$tg_cache_dir/$1" 2>/dev
/null ||
:
75 if [ -d "$tg_cache_dir/$1" ]; then
76 printf '%s\n' "$_rev" >"$tg_cache_dir/$1/.$2"
78 git cat-file blob
"$_rev:.$2" 2>/dev
/null |
79 while IFS
= read -r _line ||
[ -n "$_line" ]; do
80 printf '%s\n' "$_line" >&3
81 printf '%s\n' "$_line"
82 done 3>>"$tg_cache_dir/$1/.$2"
84 git cat-file blob
"$_rev:.$2" 2>/dev
/null
92 cat_depsmsg_internal
"$1" topdeps
99 cat_depsmsg_internal
"$1" topmsg
102 # cat_file TOPIC:PATH [FROM]
103 # cat the file PATH from branch TOPIC when FROM is empty.
104 # FROM can be -i or -w, than the file will be from the index or worktree,
105 # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
111 cat "$root_dir/${path#*:}"
114 # ':file' means cat from index
115 git cat-file blob
":${path#*:}"
119 refs
/heads
/*:.topdeps
)
120 _temp
="${path%:.topdeps}"
121 cat_deps
"${_temp#refs/heads/}"
123 refs
/heads
/*:.topmsg
)
124 _temp
="${path%:.topmsg}"
125 cat_msg
"${_temp#refs/heads/}"
128 git cat-file blob
"$path"
133 die
"Wrong argument to cat_file: '$2'"
138 # get tree for the committed topic
144 # get tree for the base
147 echo "refs/top-bases/$1"
150 # get tree for the index
156 # get tree for the worktree
159 i_tree
=$
(git write-tree
)
161 # the file for --index-output needs to sit next to the
164 : ${GIT_INDEX_FILE:="$git_dir/index"}
165 TMP_INDEX
="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX
")"
166 git read-tree
-m $i_tree --index-output="$TMP_INDEX" &&
167 GIT_INDEX_FILE
="$TMP_INDEX" &&
168 export GIT_INDEX_FILE
&&
169 git
diff --name-only -z HEAD |
170 git update-index
-z --add --remove --stdin &&
176 # strip_ref "$(git symbolic-ref HEAD)"
177 # Output will have a leading refs/heads/ or refs/top-bases/ stripped if present
182 echo "${1#refs/heads/}"
185 echo "${1#refs/top-bases/}"
192 # pretty_tree NAME [-b | -i | -w]
193 # Output tree ID of a cleaned-up tree without tg's artifacts.
194 # NAME will be ignored for -i and -w, but needs to be present
199 git ls-tree
--full-tree "$(get_tree_$source "$name")" |
200 LC_ALL
=C
sed -ne '/ \.top.*$/!p' |
207 tgname
="$(basename "$0")"
208 hook_call
="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
209 if [ -f "$git_dir/hooks/$1" ] && fgrep
-q "$hook_call" "$git_dir/hooks/$1"; then
210 # Another job well done!
213 # Prepare incantation
214 if [ -x "$git_dir/hooks/$1" ]; then
215 hook_call
="$hook_call"' || exit $?'
217 hook_call
="exec $hook_call"
219 # Don't call hook if tg is not installed
220 hook_call
="if which \"$tgname\" > /dev/null; then $hook_call; fi"
221 # Insert call into the hook
225 [ ! -s "$git_dir/hooks/$1" ] ||
cat "$git_dir/hooks/$1"
226 } >"$git_dir/hooks/$1+"
227 chmod a
+x
"$git_dir/hooks/$1+"
228 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
231 # setup_ours (no arguments)
234 if [ ! -s "$git_dir/info/attributes" ] ||
! grep -q topmsg
"$git_dir/info/attributes"; then
235 [ -d "$git_dir/info" ] || mkdir
"$git_dir/info"
237 echo ".topmsg merge=ours"
238 echo ".topdeps merge=ours"
239 } >>"$git_dir/info/attributes"
241 if ! git config merge.ours.driver
>/dev
/null
; then
242 git config merge.ours.name
'"always keep ours" merge driver'
243 git config merge.ours.driver
'touch %A'
247 # measure_branch NAME [BASE]
250 _bname
="$1"; _base
="$2"
251 [ -n "$_base" ] || _base
="refs/top-bases/$_bname"
252 # The caller should've verified $name is valid
253 _commits
="$(git rev-list "$_bname" ^"$_base" -- | wc_l)"
254 _nmcommits
="$(git rev-list --no-merges "$_bname" ^"$_base" -- | wc_l)"
255 if [ $_commits -ne 1 ]; then
260 echo "$_commits/$_nmcommits $_suffix"
263 # branch_contains B1 B2
264 # Whether B1 is a superset of B2.
267 _revb1
="$(ref_exists_rev "$1")" ||
return 0
268 _revb2
="$(ref_exists_rev "$2")" ||
return 0
269 if [ -s "$tg_cache_dir/$1/.bc/$2/.d" ]; then
270 if read _result _rev_matchb1 _rev_matchb2
&& \
271 [ "$_revb1" = "$_rev_matchb1" -a "$_revb2" = "$_rev_matchb2" ]; then
273 fi <"$tg_cache_dir/$1/.bc/$2/.d"
275 [ -d "$tg_cache_dir/$1/.bc/$2" ] || mkdir
-p "$tg_cache_dir/$1/.bc/$2" 2>/dev
/null ||
:
277 [ -z "$(git rev-list --max-count=1 ^"$_revb1" "$_revb2" --)" ] || _result
=$?
278 if [ -d "$tg_cache_dir/$1/.bc/$2" ]; then
279 echo "$_result" "$_revb1" "$_revb2" >"$tg_cache_dir/$1/.bc/$2/.d"
286 [ ! -s "$tg_tmp_dir/tg~ref-dirs-created" -a -s "$tg_ref_cache" ] ||
return 0
287 sed -e 's/ .*$//;'"s~^~$tg_tmp_dir/cached/~" <"$tg_ref_cache" |
xargs mkdir
-p
288 echo 1 >"$tg_tmp_dir/tg~ref-dirs-created"
293 [ -n "$tg_ref_cache" -a ! -s "$tg_ref_cache" ] ||
return 0
295 [ -z "$base_remote" ] || _remotespec
="refs/remotes/$base_remote"
297 git for-each-ref
--format='%(refname) %(objectname)' \
298 refs
/heads refs
/top-bases
$_remotespec >"$tg_ref_cache"
304 [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ] ||
return 0
310 if [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ]; then
311 if read _refnm _revhs
&& [ -n "$_revhs" ]; then
312 printf '%s\n' "$_revhs"
315 $(sed -ne "\\~^$1 ~p" <"$tg_ref_cache")
319 git rev-parse --quiet --verify "$1" 2>/dev/null
323 # Whether REF is a valid ref name
324 # REF must be fully qualified and start with refs/heads/, refs/top-bases/
325 # or, if $base_remote is set, refs/remotes/$base_remote/
326 # Caches result and outputs HASH on success
336 die "ref_exists_rev requires fully-qualified ref name"
340 { read -r _result _result_rev <"$tg_tmp_dir/cached/$1/.ref"; } 2>/dev/null || :
341 [ -z "$_result" ] || { printf '%s' "$_result_rev"; return $_result; }
342 _result_rev="$(rev_parse "$1")"
344 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null
345 [ ! -d "$tg_tmp_dir/cached/$1" ] || \
346 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.ref" 2>/dev/null || :
347 printf '%s' "$_result_rev"
352 # Whether REF is a valid ref name
353 # REF must be fully qualified and start with refs/heads/, refs/top-bases/
354 # or, if $base_remote is set, refs/remotes/$base_remote/
358 ref_exists_rev "$1" >/dev/null
362 # Runs git rev-parse REF^{tree}
366 if [ -f "$tg_tmp_dir/cached/$1/.rpt" ]; then
367 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
368 printf '%s\n' "$_result"
373 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null || :
374 if [ -d "$tg_tmp_dir/cached/$1" ]; then
375 git rev-parse "$1^{tree}" >"$tg_tmp_dir/cached/$1/.rpt" 2>/dev/null || :
376 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
377 printf '%s\n' "$_result"
382 git rev-parse "$1^{tree}" 2>/dev/null
386 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
389 [ -n "$base_remote" ] && ref_exists "refs/remotes/$base_remote/$1"
392 # Return the verified TopGit branch name or die with an error.
393 # As a convenience, if HEAD is given and HEAD is a symbolic ref to
394 # refs/heads/... then ... will be verified instead.
395 # if "$2" = "-f" then return an error rather than dying.
396 verify_topgit_branch()
400 _verifyname="${1#refs/heads/}"
403 _verifyname="${1#refs/top-bases/}"
406 _verifyname="$(git symbolic-ref HEAD 2>/dev/null || :)"
407 [ -n "$_verifyname" ] || die "HEAD is not a symbolic ref"
408 case "$_verifyname" in refs/heads/*) :;; *)
409 [ "$2" != "-f" ] || return 1
410 die "HEAD is not a symbolic ref to the refs/heads namespace"
412 _verifyname="${_verifyname#refs/heads/}"
418 if ! ref_exists "refs/heads/$_verifyname"; then
419 [ "$2" != "-f" ] || return 1
422 if ! ref_exists "refs/top-bases/$_verifyname"; then
423 [ "$2" != "-f" ] || return 1
424 die "not a TopGit-controlled branch"
426 printf '%s' "$_verifyname"
430 # $1 = branch name (i.e. "t/foo/bar")
431 # $2 = optional result of rev-parse "refs/heads/$1"
432 # $3 = optional result of rev-parse "refs/top-bases/$1"
436 _rev="${2:-$(ref_exists_rev "refs/heads/$_branch_name")}"
437 _rev_base="${3:-$(ref_exists_rev "refs/top-bases/$_branch_name")}"
442 { read -r _result _result_rev _result_rev_base <"$tg_cache_dir/$_branch_name/.ann"; } 2>/dev/null || :
443 [ -z "$_result" -o "$_result_rev" != "$_rev" -o "$_result_rev_base" != "$_rev_base" ] || return $_result
445 # use the merge base in case the base is ahead.
446 mb="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)"
448 test -z "$mb" || test "$(rev_parse_tree "$mb")" = "$(rev_parse_tree "$_rev")"
450 [ -d "$tg_cache_dir/$_branch_name" ] || mkdir -p "$tg_cache_dir/$_branch_name" 2>/dev/null
451 [ ! -d "$tg_cache_dir/$_branch_name" ] || \
452 echo $_result $_rev $_rev_base >"$tg_cache_dir/$_branch_name/.ann" 2>/dev/null || :
456 non_annihilated_branches()
458 git for-each-ref --format='%(objectname) %(refname)' refs/top-bases |
459 while read rev ref; do
460 name="${ref#refs/top-bases/}"
461 if branch_annihilated "$name" "" "$rev"; then
468 # Make sure our tree is clean
471 git update-index --ignore-submodules --refresh ||
472 die "the working directory has uncommitted changes (see above) - first commit or
reset them
"
473 [ -z "$
(git diff-index
--cached --name-status -r --ignore-submodules HEAD
--)" ] ||
474 die "the index has uncommited changes
"
478 # Whether REF is a SHA1 (compared to a symbolic name).
481 case "$1" in $octet20) return 0;; esac
485 # recurse_deps_internal NAME [BRANCHPATH...]
486 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
487 # followed by a 1 if the branch is "tgish
" or a 0 if not
488 # then the branch name followed by its depedency chain (which might be empty)
489 # An output line might look like this:
490 # 0 1 t/foo/leaf t/foo/int t/stage
491 # If no_remotes is non-empty, exclude remotes
492 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
493 recurse_deps_internal()
496 if ! _ref_hash="$
(ref_exists_rev
"refs/heads/$1")"; then
497 [ -z "$2" ] || echo "1 0 $
*"
501 # If no_remotes is unset also check our base against remote base.
502 # Checking our head against remote head has to be done in the helper.
503 if test -z "$no_remotes" && has_remote "top-bases
/$1"; then
504 echo "0 0 refs
/remotes
/$base_remote/top-bases
/$1 $
*"
509 if _ref_hash_base="$
(ref_exists_rev
"refs/top-bases/$1")"; then
511 [ -z "$recurse_preorder" -o -z "$2" ] || echo "0 $_is_tgish $
*"
513 # if the branch was annihilated, it is considered to have no dependencies
514 if [ -n "$_is_tgish" ] && ! branch_annihilated "$1" "$_ref_hash" "$_ref_hash_base"; then
515 #TODO: handle nonexisting .topdeps?
517 while read _dname; do
518 # Shoo shoo, leave our environment alone!
519 (recurse_deps_internal "$_dname" "$@
")
524 [ -n "$recurse_preorder" -o -z "$2" ] || echo "0 $_is_tgish $
*"
528 # helper for recurse_deps so that a return statement executed inside CMD
529 # does not return from recurse_deps. This shouldn't be necessary, but it
530 # seems that it actually is.
536 # recurse_deps CMD NAME [BRANCHPATH...]
537 # Recursively eval CMD on all dependencies of NAME.
538 # Dependencies are visited in topological order.
539 # CMD can refer to $_name for queried branch name,
540 # $_dep for dependency name,
541 # $_depchain for space-seperated branch backtrace,
542 # $_dep_missing boolean to check whether $_dep is present
543 # and the $_dep_is_tgish boolean.
544 # It can modify $_ret to affect the return value
545 # of the whole function.
546 # If recurse_deps() hits missing dependencies, it will append
547 # them to space-separated $missing_deps list and skip them
548 # after calling CMD with _dep_missing set.
549 # remote dependencies are processed if no_remotes is unset.
554 _my_ref_cache="$
(create_ref_cache
)"
555 _depsfile="$
(get_temp tg-depsfile
)"
556 recurse_deps_internal "$@
" >>"$_depsfile"
557 [ -z "$_my_ref_cache" ] || remove_ref_cache
560 while read _ismissing _istgish _dep _name _deppath; do
561 _depchain="$_name${_deppath:+ $_deppath}"
563 [ "$_istgish" = "0" ] || _dep_is_tgish=1
565 if [ "$_ismissing" != "0" ]; then
567 case " $missing_deps " in *" $_dep "*) :;; *)
568 missing_deps="${missing_deps:+$missing_deps }$_dep"
577 # branch_needs_update
578 # This is a helper function for determining whether given branch
579 # is up-to-date wrt. its dependencies. It expects input as if it
580 # is called as a recurse_deps() helper.
581 # In case the branch does need update, it will echo it together
582 # with the branch backtrace on the output (see needs_update()
583 # description for details) and set $_ret to non-zero.
584 branch_needs_update()
586 if [ -n "$_dep_missing" ]; then
587 echo "! $_dep $_depchain"
592 if [ -n "$_dep_is_tgish" ]; then
593 branch_annihilated "$_dep" && return 0
595 if has_remote "$_dep"; then
596 branch_contains "refs
/heads
/$_dep" "refs
/remotes
/$base_remote/$_dep" || _dep_base_update=%
598 # This can possibly override the remote check result;
599 # we want to sync with our base first
600 branch_contains "refs
/heads
/$_dep" "refs
/top-bases
/$_dep" || _dep_base_update=:
603 if [ -n "$_dep_base_update" ]; then
604 # _dep needs to be synced with its base/remote
605 echo "$_dep_base_update $_dep $_depchain"
607 elif [ -n "$_name" ]; then
608 case "$_dep" in refs/*) _fulldep="$_dep";; *) _fulldep="refs
/heads
/$_dep";; esac
609 if ! branch_contains "refs
/top-bases
/$_name" "$_fulldep"; then
610 # Some new commits in _dep
611 echo "$_dep $_depchain"
618 # This function is recursive; it outputs reverse path from NAME
619 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
620 # inner paths first. Innermost name can be ':' if the head is
621 # not in sync with the base, '%' if the head is not in sync
622 # with the remote (in this order of priority) or '!' if depednecy
624 # It will also return non-zero status if NAME needs update.
625 # If needs_update() hits missing dependencies, it will append
626 # them to space-separated $missing_deps list and skip them.
629 recurse_deps branch_needs_update "$1"
632 # branch_empty NAME [-i | -w]
635 [ "$
(pretty_tree
"$1" -b)" = "$
(pretty_tree
"$1" $2)" ]
638 # list_deps [-i | -w] [BRANCH]
639 # -i/-w apply only to HEAD
643 [ "$1" != "-i" -a "$1" != "-w" ] || { head_from="$1"; shift; }
644 head="$
(git symbolic-ref
-q HEAD
)" ||
647 git for-each-ref --format='%(objectname) %(refname)' refs/top-bases"${1:+/$1}" |
648 while read rev ref; do
649 name="${ref#refs/top-bases/}"
650 if branch_annihilated "$name" "" "$rev"; then
655 [ "refs
/heads
/$name" = "$head" ] ||
657 cat_file "refs
/heads
/$name:.topdeps
" $from | while read dep; do
659 ref_exists "refs
/top-bases
/$dep" ||
661 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
668 # switch_to_base NAME [SEED]
671 _base="refs
/top-bases
/$1"; _seed="$2"
672 # We have to do all the hard work ourselves :/
673 # This is like git checkout -b "$_base" "$_seed"
674 # (or just git checkout "$_base"),
675 # but does not create a detached HEAD.
676 git read-tree -u -m HEAD "${_seed:-$_base}"
677 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
678 git symbolic-ref HEAD "$_base"
681 # Show the help messages.
685 if [ "$1" = "-w" ]; then
689 if [ -z "$1" ] ; then
690 # This is currently invoked in all kinds of circumstances,
691 # including when the user made a usage error. Should we end up
692 # providing more than a short help message, then we should
694 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
696 ## Build available commands list for help output
700 for cmd in "@cmddir@
"/tg-*; do
701 ! [ -r "$cmd" ] && continue
702 # strip directory part and "tg-
" prefix
703 cmd="$
(basename "$cmd")"
709 echo "TopGit version
$TG_VERSION - A different
patch queue manager
"
710 echo "Usage
: $tgname ( help [-w] [<command>] |
[-C <dir
>] [-r <remote
>] ($cmds) ...
)"
711 echo "Use
\"$tgdisplaydir$tgname help tg
\" for overview of TopGit
"
712 elif [ -r "@cmddir@
"/tg-$1 -o -r "@sharedir@
/tg-
$1.txt
" ] ; then
713 if [ -n "$_www" ]; then
715 if ! [ -r "@sharedir@
/topgit.html
" ]; then
716 echo "`basename $0`: missing html
help file:" \
717 "@sharedir@
/topgit.html
" 1>&2
720 if ! [ -r "@sharedir@
/tg-
$1.html
" ]; then
721 echo "`basename $0`: missing html
help file:" \
722 "@sharedir@
/tg-
$1.html
" 1>&2
725 if [ -n "$nohtml" ]; then
726 echo "`basename $0`: use
" \
727 "\"`basename $0` help $1\" instead
" 1>&2
730 git web--browse -c help.browser "@sharedir@
/tg-
$1.html
"
735 if [ -r "@cmddir@
"/tg-$1 ] ; then
736 "@cmddir@
"/tg-$1 -h 2>&1 || :
739 if [ -r "@sharedir@
/tg-
$1.txt
" ] ; then
740 cat "@sharedir@
/tg-
$1.txt
"
744 echo "`basename $0`: no
help for $1" 1>&2
758 # pass "diff" to get pager.diff
759 # if pager.$1 is a boolean false returns cat
760 # if set to true or unset fails
761 # otherwise succeeds and returns the value
764 if _x="$
(git config
--bool "pager.$1" 2>/dev
/null
)"; then
765 [ "$_x" != "true
" ] || return 1
769 if _x="$
(git config
"pager.$1" 2>/dev
/null
)"; then
777 # Set TG_PAGER to a valid executable
778 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
779 # Preference is (same as Git):
781 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
782 # 3. core.pager (only if set)
787 isatty 1 || { TG_PAGER=cat; return 0; }
789 if [ -z "$TG_PAGER_IN_USE" ]; then
790 # TG_PAGER = GIT_PAGER | PAGER | less
791 # NOTE: GIT_PAGER='' is significant
792 if [ -n "${GIT_PAGER+set}" ]; then
793 TG_PAGER="$GIT_PAGER"
794 elif [ -n "$USE_PAGER_TYPE" ] && _dp="$
(get_pager
"$USE_PAGER_TYPE")"; then
796 elif _cp="$
(git config core.pager
2>/dev
/null
)"; then
798 elif [ -n "${PAGER+set}" ]; then
808 # Set pager default environment variables
809 # see pager.c:setup_pager
810 if [ -z "${LESS+set}" ]; then
814 if [ -z "${LV+set}" ]; then
819 # this is needed so e.g. `git diff` will still colorize it's output if
820 # requested in ~/.gitconfig with color.diff=auto
822 export GIT_PAGER_IN_USE
824 # this is needed so we don't get nested pagers
826 export TG_PAGER_IN_USE
830 # creates a new temporary file (or directory with -d) in the global
831 # temporary directory $tg_tmp_dir with pattern prefix NAME
834 mktemp $2 "$tg_tmp_dir/$1.XXXXXX
"
840 # suppress the merge log editor feature since git 1.7.10
842 GIT_MERGE_AUTOEDIT=no
843 export GIT_MERGE_AUTOEDIT
844 git_dir="$
(git rev-parse
--git-dir)"
845 root_dir="$
(git rev-parse
--show-cdup)"; root_dir="${root_dir:-.}"
846 logrefupdates="$
(git config
--bool core.logallrefupdates
2>/dev
/null ||
:)"
847 [ "$logrefupdates" = "true
" ] || logrefupdates=
849 # make sure root_dir doesn't end with a trailing slash.
851 root_dir="${root_dir%/}"
852 [ -n "$base_remote" ] || base_remote="$
(git config topgit.remote
2>/dev
/null
)" || :
854 # make sure global cache directory exists inside GIT_DIR
856 tg_cache_dir="$git_dir/tg-cache
"
857 [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir"
859 # create global temporary directories, inside GIT_DIR
862 trap 'rm -rf "$tg_tmp_dir"' EXIT
868 tg_tmp_dir="$
(mktemp
-d "$git_dir/tg-tmp.XXXXXX")"
869 tg_ref_cache="$tg_tmp_dir/tg~ref-cache
"
872 # return the "realpath
" for the item except the leaf is not resolved if it's
873 # a symbolic link. The directory part must exist, but the basename need not.
876 [ -n "$1" -a -d "$
(dirname -- "$1")" ] || return 1
877 printf '%s' "$
(cd -- "$(dirname -- "$1")" && pwd -P)/$
(basename -- "$1")"
883 die "No
command directory
: '@cmddir@'"
885 if [ -n "$tg__include" ]; then
887 # We were sourced from another script for our utility functions;
888 # this is set by hooks. Skip the rest of the file. A simple return doesn't
889 # work as expected in every shell. See http://bugs.debian.org/516188
891 # ensure setup happens
900 tgdir="$
(dirname -- "$tg")/"
901 tgname="$
(basename -- "$tg")"
902 [ "$0" != "$tgname" ] || tgdir=""
904 # If tg contains a '/' but does not start with one then replace it with an absolute path
906 case "$0" in /*) :;; */*)
907 tgdir="$
(cd "$(dirname -- "$0")" && pwd -P)/"
911 # If the tg in the PATH is the same as "$tg" just display the basename
912 # tgdisplay will include any explicit -C <dir> option whereas tg will not
914 tgdisplaydir="$tgdir"
916 if [ "$
(get_abs_path
"$tg")" = "$
(get_abs_path
"$(which "$tgname" || :)" ||
:)" ]; then
927 while :; do case "$1" in
942 echo "Option
-r requires an argument.
" >&2
948 explicit_remote="$base_remote"
949 tg="$tgdir$tgname -r $explicit_remote"
950 tgdisplay="$tgdisplaydir$tgname"
951 [ -z "$explicit_dir" ] || tgdisplay="$tgdisplay -C \"$explicit_dir\""
952 tgdisplay="$tgdisplay -r $explicit_remote"
956 unset base_remote explicit_remote
958 tg="$tgdir$tgname -u"
959 tgdisplay="$tgdisplaydir$tgname"
960 [ -z "$explicit_dir" ] || tgdisplay="$tgdisplay -C \"$explicit_dir\""
961 tgdisplay="$tgdisplay -u"
967 echo "Option
-C requires an argument.
" >&2
974 gitcdopt=" -C \"$explicit_dir\""
976 tgdisplay="$tgdisplaydir$tgname -C \"$explicit_dir\""
977 [ -z "$explicit_remote" ] || tg="$tg -r $explicit_remote"
978 [ -z "$explicit_remote" ] || tgdisplay="$tgdisplay -r $explicit_remote"
979 [ -z "$noremote" ] || tg="$tg -u"
980 [ -z "$noremote" ] || tg="$tgdisplay -u"
988 echo "Invalid option
$1 (subcommand options must appear AFTER the subcommand
).
" >&2
997 [ -n "$cmd" ] || { cmd="$1"; shift || :; }
1001 [ -n "$cmd" ] || { do_help; exit 1; }
1014 [ -r "@cmddir@
"/tg-$cmd ] || {
1015 echo "Unknown subcommand
: $cmd" >&2
1021 [ -z "$noremote" ] || unset base_remote
1023 # make sure merging the .top* files will always behave sanely
1026 setup_hook "pre-commit
"
1031 summary|info|export)
1033 annihilate|create|delete|depend|import|update)
1036 [ -z "$_use_ref_cache" ] || create_ref_cache >/dev/null
1038 . "@cmddir@
"/tg-$cmd;;