tg.sh: next version is 0.17.1
[topgit/pro.git] / tg.sh
blob4908cd5a6ccaec75bb587b3d58822c4ad33d5c3b
1 #!/bin/sh
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
5 # GPLv2
7 TG_VERSION=0.17.1
9 # Update if you add any code that requires a newer version of git
10 GIT_MINIMUM_VERSION=1.7.7.2
12 ## SHA-1 pattern
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
21 info()
23 echo "${TG_RECURSIVE}${tgname:-tg}: $*"
26 die()
28 info "fatal: $*" >&2
29 exit 1
32 wc_l()
34 echo $(wc -l)
37 compare_versions()
39 separator="$1"
40 echo "$3" | tr "${separator}" '\n' | (for l in $(echo "$2"|tr "${separator}" ' '); do
41 read r || return 0
42 [ $l -ge $r ] || return 1
43 [ $l -gt $r ] && return 0
44 done)
47 precheck() {
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"
55 exit 0
56 esac
58 precheck
59 [ "$1" = "precheck" ] && exit 0
61 # cat_deps BRANCHNAME
62 # Caches result
63 cat_deps()
65 if [ -f "$tg_tmp_dir/cached/$1/.tpd" ]; then
66 _line=
67 while IFS= read -r _line || [ -n "$_line" ]; do
68 printf '%s\n' "$_line"
69 done <"$tg_tmp_dir/cached/$1/.tpd"
70 return
72 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null || :
73 if [ -d "$tg_tmp_dir/cached/$1" ]; then
74 git cat-file blob "$1:.topdeps" 2>/dev/null >"$tg_tmp_dir/cached/$1/.tpd"
75 _line=
76 while IFS= read -r _line || [ -n "$_line" ]; do
77 printf '%s\n' "$_line"
78 done <"$tg_tmp_dir/cached/$1/.tpd"
79 else
80 git cat-file blob "$1:.topdeps" 2>/dev/null
84 # cat_file TOPIC:PATH [FROM]
85 # cat the file PATH from branch TOPIC when FROM is empty.
86 # FROM can be -i or -w, than the file will be from the index or worktree,
87 # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
88 cat_file()
90 path="$1"
91 case "$2" in
92 -w)
93 cat "$root_dir/${path#*:}"
95 -i)
96 # ':file' means cat from index
97 git cat-file blob ":${path#*:}"
99 '')
100 case "$path" in
101 *:.topdeps)
102 cat_deps "${path%:.topdeps}"
105 git cat-file blob "$path"
107 esac
110 die "Wrong argument to cat_file: '$2'"
112 esac
115 # get tree for the committed topic
116 get_tree_()
118 echo "$1"
121 # get tree for the base
122 get_tree_b()
124 echo "refs/top-bases/$1"
127 # get tree for the index
128 get_tree_i()
130 git write-tree
133 # get tree for the worktree
134 get_tree_w()
136 i_tree=$(git write-tree)
138 # the file for --index-output needs to sit next to the
139 # current index file
140 cd "$root_dir"
141 : ${GIT_INDEX_FILE:="$git_dir/index"}
142 TMP_INDEX="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX")"
143 git read-tree -m $i_tree --index-output="$TMP_INDEX" &&
144 GIT_INDEX_FILE="$TMP_INDEX" &&
145 export GIT_INDEX_FILE &&
146 git diff --name-only -z HEAD |
147 git update-index -z --add --remove --stdin &&
148 git write-tree &&
149 rm -f "$TMP_INDEX"
153 # strip_ref "$(git symbolic-ref HEAD)"
154 # Output will have a leading refs/heads/ or refs/top-bases/ stripped if present
155 strip_ref()
157 case "$1" in
158 refs/heads/*)
159 echo "${1#refs/heads/}"
161 refs/top-bases/*)
162 echo "${1#refs/top-bases/}"
165 echo "$1"
166 esac
169 # pretty_tree NAME [-b | -i | -w]
170 # Output tree ID of a cleaned-up tree without tg's artifacts.
171 # NAME will be ignored for -i and -w, but needs to be present
172 pretty_tree()
174 name=$1
175 source=${2#?}
176 git ls-tree --full-tree "$(get_tree_$source "$name")" |
177 awk -F ' ' '$2 !~ /^.top/' |
178 git mktree
181 # setup_hook NAME
182 setup_hook()
184 tgname="$(basename "$0")"
185 hook_call="\"\$(\"$tgname\" --hooks-path)\"/$1 \"\$@\""
186 if [ -f "$git_dir/hooks/$1" ] && fgrep -q "$hook_call" "$git_dir/hooks/$1"; then
187 # Another job well done!
188 return
190 # Prepare incantation
191 if [ -x "$git_dir/hooks/$1" ]; then
192 hook_call="$hook_call"' || exit $?'
193 else
194 hook_call="exec $hook_call"
196 # Don't call hook if tg is not installed
197 hook_call="if which \"$tgname\" > /dev/null; then $hook_call; fi"
198 # Insert call into the hook
200 echo "#!/bin/sh"
201 echo "$hook_call"
202 [ ! -s "$git_dir/hooks/$1" ] || cat "$git_dir/hooks/$1"
203 } >"$git_dir/hooks/$1+"
204 chmod a+x "$git_dir/hooks/$1+"
205 mv "$git_dir/hooks/$1+" "$git_dir/hooks/$1"
208 # setup_ours (no arguments)
209 setup_ours()
211 if [ ! -s "$git_dir/info/attributes" ] || ! grep -q topmsg "$git_dir/info/attributes"; then
212 [ -d "$git_dir/info" ] || mkdir "$git_dir/info"
214 echo ".topmsg merge=ours"
215 echo ".topdeps merge=ours"
216 } >>"$git_dir/info/attributes"
218 if ! git config merge.ours.driver >/dev/null; then
219 git config merge.ours.name '"always keep ours" merge driver'
220 git config merge.ours.driver 'touch %A'
224 # measure_branch NAME [BASE]
225 measure_branch()
227 _bname="$1"; _base="$2"
228 [ -n "$_base" ] || _base="refs/top-bases/$_bname"
229 # The caller should've verified $name is valid
230 _commits="$(git rev-list "$_bname" ^"$_base" -- | wc_l)"
231 _nmcommits="$(git rev-list --no-merges "$_bname" ^"$_base" -- | wc_l)"
232 if [ $_commits -ne 1 ]; then
233 _suffix="commits"
234 else
235 _suffix="commit"
237 echo "$_commits/$_nmcommits $_suffix"
240 # branch_contains B1 B2
241 # Whether B1 is a superset of B2.
242 branch_contains()
244 [ -z "$(git rev-list --max-count=1 ^"$1" "$2" --)" ]
247 # ref_exists REF
248 # Whether REF is a valid ref name
249 # Caches result
250 ref_exists()
252 _result=
253 { read -r _result <"$tg_tmp_dir/cached/$1/.ref"; } 2>/dev/null || :
254 [ -z "$_result" ] || return $_result
255 git rev-parse --verify "$@" >/dev/null 2>&1
256 _result=$?
257 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null && \
258 echo $_result >"$tg_tmp_dir/cached/$1/.ref" 2>/dev/null || :
259 return $_result
262 # rev_parse_tree REF
263 # Runs git rev-parse REF^{tree}
264 # Caches result
265 rev_parse_tree()
267 if [ -f "$tg_tmp_dir/cached/$1/.rpt" ]; then
268 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
269 printf '%s\n' "$_result"
270 return 0
272 return 1
274 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null || :
275 if [ -d "$tg_tmp_dir/cached/$1" ]; then
276 git rev-parse "$1^{tree}" >"$tg_tmp_dir/cached/$1/.rpt" 2>/dev/null || :
277 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
278 printf '%s\n' "$_result"
279 return 0
281 return 1
283 git rev-parse "$1^{tree}" 2>/dev/null
286 # has_remote BRANCH
287 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
288 has_remote()
290 [ -n "$base_remote" ] && ref_exists "remotes/$base_remote/$1"
293 # Return the verified TopGit branch name or die with an error.
294 # As a convenience, if HEAD is given and HEAD is a symbolic ref to
295 # refs/heads/... then ... will be verified instead.
296 # if "$2" = "-f" then return an error rather than dying.
297 verify_topgit_branch()
299 case "$1" in
300 refs/heads/*)
301 _verifyname="${1#refs/heads/}"
303 refs/top-bases/*)
304 _verifyname="${1#refs/top-bases/}"
306 HEAD)
307 _verifyname="$(git symbolic-ref HEAD 2>/dev/null || :)"
308 [ -n "$_verifyname" ] || die "HEAD is not a symbolic ref"
309 case "$_verifyname" in refs/heads/*) :;; *)
310 [ "$2" != "-f" ] || return 1
311 die "HEAD is not a symbolic ref to the refs/heads namespace"
312 esac
313 _verifyname="${_verifyname#refs/heads/}"
316 _verifyname="$1"
318 esac
319 if ! git rev-parse --short --verify "refs/heads/$_verifyname" >/dev/null 2>&1; then
320 [ "$2" != "-f" ] || return 1
321 die "no such branch"
323 if ! git rev-parse --short --verify "refs/top-bases/$_verifyname" >/dev/null 2>&1; then
324 [ "$2" != "-f" ] || return 1
325 die "not a TopGit-controlled branch"
327 printf '%s' "$_verifyname"
330 # Caches result
331 branch_annihilated()
333 _branch_name="$1"
335 _result=
336 { read -r _result <"$tg_tmp_dir/cached/$_branch_name/.ann"; } 2>/dev/null || :
337 [ -z "$_result" ] || return $_result
339 # use the merge base in case the base is ahead.
340 mb="$(git merge-base "refs/top-bases/$_branch_name" "$_branch_name" 2> /dev/null)"
342 test -z "$mb" || test "$(rev_parse_tree "$mb")" = "$(rev_parse_tree "$_branch_name")"
343 _result=$?
344 [ -d "$tg_tmp_dir/cached/$_branch_name" ] || mkdir -p "$tg_tmp_dir/cached/$_branch_name" 2>/dev/null && \
345 echo $_result >"$tg_tmp_dir/cached/$_branch_name/.ann" 2>/dev/null || :
346 return $_result
349 non_annihilated_branches()
351 _pattern="$@"
352 git for-each-ref ${_pattern:-refs/top-bases} |
353 while read rev type ref; do
354 name="${ref#refs/top-bases/}"
355 if branch_annihilated "$name"; then
356 continue
358 echo "$name"
359 done
362 # Make sure our tree is clean
363 ensure_clean_tree()
365 git update-index --ignore-submodules --refresh ||
366 die "the working directory has uncommitted changes (see above) - first commit or reset them"
367 [ -z "$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)" ] ||
368 die "the index has uncommited changes"
371 # is_sha1 REF
372 # Whether REF is a SHA1 (compared to a symbolic name).
373 is_sha1()
375 case "$1" in $octet20) return 0;; esac
376 return 1
379 # recurse_deps_internal NAME [BRANCHPATH...]
380 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
381 # followed by a 1 if the branch is "tgish" or a 0 if not
382 # then the branch name followed by its depedency chain (which might be empty)
383 # An output line might look like this:
384 # 0 1 t/foo/leaf t/foo/int t/stage
385 # If no_remotes is non-empty, exclude remotes
386 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
387 recurse_deps_internal()
389 if ! ref_exists "$1"; then
390 [ -z "$2" ] || echo "1 0 $*"
391 return
394 # If no_remotes is unset also check our base against remote base.
395 # Checking our head against remote head has to be done in the helper.
396 if test -z "$no_remotes" && has_remote "top-bases/$1"; then
397 echo "0 0 refs/remotes/$base_remote/top-bases/$1 $*"
400 _is_tgish=0
401 if ref_exists "refs/top-bases/$1"; then
402 _is_tgish=1
403 [ -z "$recurse_preorder" -o -z "$2" ] || echo "0 $_is_tgish $*"
405 # if the branch was annihilated, it is considered to have no dependencies
406 if ! branch_annihilated "$1"; then
407 #TODO: handle nonexisting .topdeps?
408 cat_deps "$1" |
409 while read _dname; do
410 # Shoo shoo, leave our environment alone!
411 (recurse_deps_internal "$_dname" "$@")
412 done
416 [ -n "$recurse_preorder" -o -z "$2" ] || echo "0 $_is_tgish $*"
419 # do_eval CMD
420 # helper for recurse_deps so that a return statement executed inside CMD
421 # does not return from recurse_deps. This shouldn't be necessary, but it
422 # seems that it actually is.
423 do_eval()
425 eval "$@"
428 # recurse_deps CMD NAME [BRANCHPATH...]
429 # Recursively eval CMD on all dependencies of NAME.
430 # Dependencies are visited in topological order.
431 # CMD can refer to $_name for queried branch name,
432 # $_dep for dependency name,
433 # $_depchain for space-seperated branch backtrace,
434 # $_dep_missing boolean to check whether $_dep is present
435 # and the $_dep_is_tgish boolean.
436 # It can modify $_ret to affect the return value
437 # of the whole function.
438 # If recurse_deps() hits missing dependencies, it will append
439 # them to space-separated $missing_deps list and skip them
440 # after calling CMD with _dep_missing set.
441 # remote dependencies are processed if no_remotes is unset.
442 recurse_deps()
444 _cmd="$1"; shift
446 _depsfile="$(get_temp tg-depsfile)"
447 recurse_deps_internal "$@" >>"$_depsfile"
449 _ret=0
450 while read _ismissing _istgish _dep _name _deppath; do
451 _depchain="$_name${_deppath:+ $_deppath}"
452 _dep_is_tgish=
453 [ "$_istgish" = "0" ] || _dep_is_tgish=1
454 _dep_missing=
455 if [ "$_ismissing" != "0" ]; then
456 _dep_missing=1
457 case " $missing_deps " in *" $_dep "*) :;; *)
458 missing_deps="${missing_deps:+$missing_deps }$_dep"
459 esac
461 do_eval "$_cmd"
462 done <"$_depsfile"
463 rm -f "$_depsfile"
464 return $_ret
467 # branch_needs_update
468 # This is a helper function for determining whether given branch
469 # is up-to-date wrt. its dependencies. It expects input as if it
470 # is called as a recurse_deps() helper.
471 # In case the branch does need update, it will echo it together
472 # with the branch backtrace on the output (see needs_update()
473 # description for details) and set $_ret to non-zero.
474 branch_needs_update()
476 if [ -n "$_dep_missing" ]; then
477 echo "! $_dep $_depchain"
478 return 0
481 _dep_base_update=
482 if [ -n "$_dep_is_tgish" ]; then
483 branch_annihilated "$_dep" && return 0
485 if has_remote "$_dep"; then
486 branch_contains "$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
488 # This can possibly override the remote check result;
489 # we want to sync with our base first
490 branch_contains "$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
493 if [ -n "$_dep_base_update" ]; then
494 # _dep needs to be synced with its base/remote
495 echo "$_dep_base_update $_dep $_depchain"
496 _ret=1
497 elif [ -n "$_name" ] && ! branch_contains "refs/top-bases/$_name" "$_dep"; then
498 # Some new commits in _dep
499 echo "$_dep $_depchain"
500 _ret=1
504 # needs_update NAME
505 # This function is recursive; it outputs reverse path from NAME
506 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
507 # inner paths first. Innermost name can be ':' if the head is
508 # not in sync with the base, '%' if the head is not in sync
509 # with the remote (in this order of priority) or '!' if depednecy
510 # is missing.
511 # It will also return non-zero status if NAME needs update.
512 # If needs_update() hits missing dependencies, it will append
513 # them to space-separated $missing_deps list and skip them.
514 needs_update()
516 recurse_deps branch_needs_update "$@"
519 # branch_empty NAME [-i | -w]
520 branch_empty()
522 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ]
525 # list_deps [-i | -w] [BRANCH]
526 # -i/-w apply only to HEAD
527 list_deps()
529 head_from=
530 [ "$1" != "-i" -a "$1" != "-w" ] || { head_from="$1"; shift; }
531 head="$(git symbolic-ref -q HEAD)" ||
532 head="..detached.."
534 git for-each-ref refs/top-bases"${1:+/$1}" |
535 while read rev type ref; do
536 name="${ref#refs/top-bases/}"
537 if branch_annihilated "$name"; then
538 continue
541 from=$head_from
542 [ "refs/heads/$name" = "$head" ] ||
543 from=
544 cat_file "$name:.topdeps" $from | while read dep; do
545 dep_is_tgish=true
546 ref_exists "refs/top-bases/$dep" ||
547 dep_is_tgish=false
548 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
549 echo "$name $dep"
551 done
552 done
555 # switch_to_base NAME [SEED]
556 switch_to_base()
558 _base="refs/top-bases/$1"; _seed="$2"
559 # We have to do all the hard work ourselves :/
560 # This is like git checkout -b "$_base" "$_seed"
561 # (or just git checkout "$_base"),
562 # but does not create a detached HEAD.
563 git read-tree -u -m HEAD "${_seed:-$_base}"
564 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
565 git symbolic-ref HEAD "$_base"
568 # Show the help messages.
569 do_help()
571 _www=
572 if [ "$1" = "-w" ]; then
573 _www=1
574 shift
576 if [ -z "$1" ] ; then
577 # This is currently invoked in all kinds of circumstances,
578 # including when the user made a usage error. Should we end up
579 # providing more than a short help message, then we should
580 # differentiate.
581 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
583 ## Build available commands list for help output
585 cmds=
586 sep=
587 for cmd in "@cmddir@"/tg-*; do
588 ! [ -r "$cmd" ] && continue
589 # strip directory part and "tg-" prefix
590 cmd="$(basename "$cmd")"
591 cmd="${cmd#tg-}"
592 cmds="$cmds$sep$cmd"
593 sep="|"
594 done
596 echo "TopGit version $TG_VERSION - A different patch queue manager"
597 echo "Usage: $tgname ( help [-w] [<command>] | [-C <dir>] [-r <remote>] ($cmds) ...)"
598 echo "Use \"$tgdisplaydir$tgname help tg\" for overview of TopGit"
599 elif [ -r "@cmddir@"/tg-$1 -o -r "@sharedir@/tg-$1.txt" ] ; then
600 if [ -n "$_www" ]; then
601 nohtml=
602 if ! [ -r "@sharedir@/topgit.html" ]; then
603 echo "`basename $0`: missing html help file:" \
604 "@sharedir@/topgit.html" 1>&2
605 nohtml=1
607 if ! [ -r "@sharedir@/tg-$1.html" ]; then
608 echo "`basename $0`: missing html help file:" \
609 "@sharedir@/tg-$1.html" 1>&2
610 nohtml=1
612 if [ -n "$nohtml" ]; then
613 echo "`basename $0`: use" \
614 "\"`basename $0` help $1\" instead" 1>&2
615 exit 1
617 git web--browse -c help.browser "@sharedir@/tg-$1.html"
618 exit
620 setup_pager
622 if [ -r "@cmddir@"/tg-$1 ] ; then
623 "@cmddir@"/tg-$1 -h 2>&1 || :
624 echo
626 if [ -r "@sharedir@/tg-$1.txt" ] ; then
627 cat "@sharedir@/tg-$1.txt"
629 } | eval "$TG_PAGER"
630 else
631 echo "`basename $0`: no help for $1" 1>&2
632 do_help
633 exit 1
637 ## Pager stuff
639 # isatty FD
640 isatty()
642 test -t $1
645 # pass "diff" to get pager.diff
646 # if pager.$1 is a boolean false returns cat
647 # if set to true or unset fails
648 # otherwise succeeds and returns the value
649 get_pager()
651 if _x="$(git config --bool "pager.$1" 2>/dev/null)"; then
652 [ "$_x" != "true" ] || return 1
653 echo "cat"
654 return 0
656 if _x="$(git config "pager.$1" 2>/dev/null)"; then
657 echo "$_x"
658 return 0
660 return 1
663 # setup_pager
664 # Set TG_PAGER to a valid executable
665 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
666 # Preference is (same as Git):
667 # 1. GIT_PAGER
668 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
669 # 3. core.pager (only if set)
670 # 4. PAGER
671 # 5. less
672 setup_pager()
674 isatty 1 || { TG_PAGER=cat; return 0; }
676 if [ -z "$TG_PAGER_IN_USE" ]; then
677 # TG_PAGER = GIT_PAGER | PAGER | less
678 # NOTE: GIT_PAGER='' is significant
679 if [ -n "${GIT_PAGER+set}" ]; then
680 TG_PAGER="$GIT_PAGER"
681 elif [ -n "$USE_PAGER_TYPE" ] && _dp="$(get_pager "$USE_PAGER_TYPE")"; then
682 TG_PAGER="$_dp"
683 elif _cp="$(git config core.pager 2>/dev/null)"; then
684 TG_PAGER="$_cp"
685 elif [ -n "${PAGER+set}" ]; then
686 TG_PAGER="$PAGER"
687 else
688 TG_PAGER="less"
690 : ${TG_PAGER:=cat}
691 else
692 TG_PAGER=cat
695 # Set pager default environment variables
696 # see pager.c:setup_pager
697 if [ -z "${LESS+set}" ]; then
698 LESS="-FRSX"
699 export LESS
701 if [ -z "${LV+set}" ]; then
702 LV="-c"
703 export LV
706 # this is needed so e.g. `git diff` will still colorize it's output if
707 # requested in ~/.gitconfig with color.diff=auto
708 GIT_PAGER_IN_USE=1
709 export GIT_PAGER_IN_USE
711 # this is needed so we don't get nested pagers
712 TG_PAGER_IN_USE=1
713 export TG_PAGER_IN_USE
716 # get_temp NAME [-d]
717 # creates a new temporary file (or directory with -d) in the global
718 # temporary directory $tg_tmp_dir with pattern prefix NAME
719 get_temp()
721 mktemp $2 "$tg_tmp_dir/$1.XXXXXX"
724 ## Initial setup
725 initial_setup()
727 # suppress the merge log editor feature since git 1.7.10
729 GIT_MERGE_AUTOEDIT=no
730 export GIT_MERGE_AUTOEDIT
731 git_dir="$(git rev-parse --git-dir)"
732 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
733 logrefupdates="$(git config --bool core.logallrefupdates 2>/dev/null || :)"
734 [ "$logrefupdates" = "true" ] || logrefupdates=
736 # Make sure root_dir doesn't end with a trailing slash.
738 root_dir="${root_dir%/}"
739 [ -n "$base_remote" ] || base_remote="$(git config topgit.remote 2>/dev/null)" || :
741 # create global temporary directories, inside GIT_DIR
743 tg_tmp_dir=
744 trap 'rm -rf "$tg_tmp_dir"' EXIT
745 trap 'exit 129' HUP
746 trap 'exit 130' INT
747 trap 'exit 131' QUIT
748 trap 'exit 134' ABRT
749 trap 'exit 143' TERM
750 tg_tmp_dir="$(mktemp -d "$git_dir/tg-tmp.XXXXXX")"
753 # return the "realpath" for the item except the leaf is not resolved if it's
754 # a symbolic link. The directory part must exist, but the basename need not.
755 get_abs_path()
757 [ -n "$1" -a -d "$(dirname -- "$1")" ] || return 1
758 printf '%s' "$(cd -- "$(dirname -- "$1")" && pwd -P)/$(basename -- "$1")"
761 ## Startup
763 [ -d "@cmddir@" ] ||
764 die "No command directory: '@cmddir@'"
766 if [ -n "$tg__include" ]; then
768 # We were sourced from another script for our utility functions;
769 # this is set by hooks. Skip the rest of the file. A simple return doesn't
770 # work as expected in every shell. See http://bugs.debian.org/516188
772 # ensure setup happens
774 initial_setup
776 else
778 set -e
780 tg="$0"
781 tgdir="$(dirname -- "$tg")/"
782 tgname="$(basename -- "$tg")"
783 [ "$0" != "$tgname" ] || tgdir=""
785 # If tg contains a '/' but does not start with one then replace it with an absolute path
787 case "$0" in /*) :;; */*)
788 tgdir="$(cd "$(dirname -- "$0")" && pwd -P)/"
789 tg="$tgdir$tgname"
790 esac
792 # If the tg in the PATH is the same as "$tg" just display the basename
793 # tgdisplay will include any explicit -C <dir> option whereas tg will not
795 tgdisplaydir="$tgdir"
796 tgdisplay="$tg"
797 if [ "$(get_abs_path "$tg")" = "$(get_abs_path "$(which "$tgname" || :)" || :)" ]; then
798 tgdisplaydir=""
799 tgdisplay="$tgname"
802 explicit_remote=
803 explicit_dir=
804 gitcdopt=
805 noremote=
807 cmd=
808 while :; do case "$1" in
810 help|--help|-h)
811 cmd=help
812 shift
813 break;;
815 --hooks-path)
816 cmd=hooks-path
817 shift
818 break;;
821 shift
822 if [ -z "$1" ]; then
823 echo "Option -r requires an argument." >&2
824 do_help
825 exit 1
827 unset noremote
828 base_remote="$1"
829 explicit_remote="$base_remote"
830 tg="$tgdir$tgname -r $explicit_remote"
831 tgdisplay="$tgdisplaydir$tgname"
832 [ -z "$explicit_dir" ] || tgdisplay="$tgdisplay -C \"$explicit_dir\""
833 tgdisplay="$tgdisplay -r $explicit_remote"
834 shift;;
837 unset base_remote explicit_remote
838 noremote=1
839 tg="$tgdir$tgname -u"
840 tgdisplay="$tgdisplaydir$tgname"
841 [ -z "$explicit_dir" ] || tgdisplay="$tgdisplay -C \"$explicit_dir\""
842 tgdisplay="$tgdisplay -u"
843 shift;;
846 shift
847 if [ -z "$1" ]; then
848 echo "Option -C requires an argument." >&2
849 do_help
850 exit 1
852 cd "$1"
853 unset GIT_DIR
854 explicit_dir="$1"
855 gitcdopt=" -C \"$explicit_dir\""
856 tg="$tgdir$tgname"
857 tgdisplay="$tgdisplaydir$tgname -C \"$explicit_dir\""
858 [ -z "$explicit_remote" ] || tg="$tg -r $explicit_remote"
859 [ -z "$explicit_remote" ] || tgdisplay="$tgdisplay -r $explicit_remote"
860 [ -z "$noremote" ] || tg="$tg -u"
861 [ -z "$noremote" ] || tg="$tgdisplay -u"
862 shift;;
865 shift
866 break;;
869 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
870 do_help
871 exit 1;;
874 break;;
876 esac; done
878 [ -n "$cmd" ] || { cmd="$1"; shift || :; }
880 ## Dispatch
882 [ -n "$cmd" ] || { do_help; exit 1; }
884 case "$cmd" in
886 help)
887 do_help "$@"
888 exit 0;;
890 hooks-path)
891 # Internal command
892 echo "@hooksdir@";;
895 [ -r "@cmddir@"/tg-$cmd ] || {
896 echo "Unknown subcommand: $cmd" >&2
897 do_help
898 exit 1
901 initial_setup
902 [ -z "$noremote" ] || unset base_remote
904 # make sure merging the .top* files will always behave sanely
906 setup_ours
907 setup_hook "pre-commit"
909 . "@cmddir@"/tg-$cmd;;
910 esac
914 # vim:noet