tg.sh: avoid using stale information for non-read-only operations
[topgit/pro.git] / tg.sh
blobb142f41ce9a759d0d67f1717926fd2e81cef456b
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.18
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
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
67 _line=
68 while IFS= read -r _line || [ -n "$_line" ]; do
69 printf '%s\n' "$_line"
70 done
71 return 0
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"
77 _line=
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"
83 else
84 git cat-file blob "$_rev:.$2" 2>/dev/null
88 # cat_deps BRANCHNAME
89 # Caches result
90 cat_deps()
92 cat_depsmsg_internal "$1" topdeps
95 # cat_msg BRANCHNAME
96 # Caches result
97 cat_msg()
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.
106 cat_file()
108 path="$1"
109 case "$2" in
111 cat "$root_dir/${path#*:}"
114 # ':file' means cat from index
115 git cat-file blob ":${path#*:}"
118 case "$path" in
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"
130 esac
133 die "Wrong argument to cat_file: '$2'"
135 esac
138 # get tree for the committed topic
139 get_tree_()
141 echo "refs/heads/$1"
144 # get tree for the base
145 get_tree_b()
147 echo "refs/top-bases/$1"
150 # get tree for the index
151 get_tree_i()
153 git write-tree
156 # get tree for the worktree
157 get_tree_w()
159 i_tree=$(git write-tree)
161 # the file for --index-output needs to sit next to the
162 # current index file
163 cd "$root_dir"
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 &&
171 git write-tree &&
172 rm -f "$TMP_INDEX"
176 # strip_ref "$(git symbolic-ref HEAD)"
177 # Output will have a leading refs/heads/ or refs/top-bases/ stripped if present
178 strip_ref()
180 case "$1" in
181 refs/heads/*)
182 echo "${1#refs/heads/}"
184 refs/top-bases/*)
185 echo "${1#refs/top-bases/}"
188 echo "$1"
189 esac
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
195 pretty_tree()
197 name=$1
198 source=${2#?}
199 git ls-tree --full-tree "$(get_tree_$source "$name")" |
200 LC_ALL=C sed -ne '/ \.top.*$/!p' |
201 git mktree
204 # setup_hook NAME
205 setup_hook()
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!
211 return
213 # Prepare incantation
214 if [ -x "$git_dir/hooks/$1" ]; then
215 hook_call="$hook_call"' || exit $?'
216 else
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
223 echo "#!/bin/sh"
224 echo "$hook_call"
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)
232 setup_ours()
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]
248 measure_branch()
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
256 _suffix="commits"
257 else
258 _suffix="commit"
260 echo "$_commits/$_nmcommits $_suffix"
263 # branch_contains B1 B2
264 # Whether B1 is a superset of B2.
265 branch_contains()
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
272 return $_result
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 || :
276 _result=0
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"
281 return $_result
284 create_ref_dirs()
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"
291 create_ref_cache()
293 [ -n "$tg_ref_cache" -a ! -s "$tg_ref_cache" ] || return 0
294 _remotespec=
295 [ -z "$base_remote" ] || _remotespec="refs/remotes/$base_remote"
296 printf '1'
297 git for-each-ref --format='%(refname) %(objectname)' \
298 refs/heads refs/top-bases $_remotespec >"$tg_ref_cache"
299 create_ref_dirs
302 remove_ref_cache()
304 [ -n "$tg_ref_cache" -a -s "$tg_ref_cache" ] || return 0
305 >"$tg_ref_cache"
308 rev_parse()
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"
313 return 0
314 fi <<-~EOT~
315 $(sed -ne "\\~^$1 ~p" <"$tg_ref_cache")
316 ~EOT~
317 return 1
319 git rev-parse --quiet --verify "$1" 2>/dev/null
322 # ref_exists_rev REF
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 if $tg_read_only and outputs HASH on success
327 ref_exists_rev()
329 case "$1" in
330 refs/*)
332 $octet20)
333 printf '%s' "$1"
334 return;;
336 die "ref_exists_rev requires fully-qualified ref name"
337 esac
338 [ -n "$tg_read_only" ] || { git rev-parse --quiet --verify "$1" 2>/dev/null; return; }
339 _result=
340 _result_rev=
341 { read -r _result _result_rev <"$tg_tmp_dir/cached/$1/.ref"; } 2>/dev/null || :
342 [ -z "$_result" ] || { printf '%s' "$_result_rev"; return $_result; }
343 _result_rev="$(rev_parse "$1")"
344 _result=$?
345 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null
346 [ ! -d "$tg_tmp_dir/cached/$1" ] || \
347 echo $_result $_result_rev >"$tg_tmp_dir/cached/$1/.ref" 2>/dev/null || :
348 printf '%s' "$_result_rev"
349 return $_result
352 # ref_exists REF
353 # Whether REF is a valid ref name
354 # REF must be fully qualified and start with refs/heads/, refs/top-bases/
355 # or, if $base_remote is set, refs/remotes/$base_remote/
356 # Caches result
357 ref_exists()
359 ref_exists_rev "$1" >/dev/null
362 # rev_parse_tree REF
363 # Runs git rev-parse REF^{tree}
364 # Caches result if $tg_read_only
365 rev_parse_tree()
367 [ -n "$tg_read_only" ] || { git rev-parse "$1^{tree}" 2>/dev/null; return; }
368 if [ -f "$tg_tmp_dir/cached/$1/.rpt" ]; then
369 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
370 printf '%s\n' "$_result"
371 return 0
373 return 1
375 [ -d "$tg_tmp_dir/cached/$1" ] || mkdir -p "$tg_tmp_dir/cached/$1" 2>/dev/null || :
376 if [ -d "$tg_tmp_dir/cached/$1" ]; then
377 git rev-parse "$1^{tree}" >"$tg_tmp_dir/cached/$1/.rpt" 2>/dev/null || :
378 if IFS= read -r _result <"$tg_tmp_dir/cached/$1/.rpt"; then
379 printf '%s\n' "$_result"
380 return 0
382 return 1
384 git rev-parse "$1^{tree}" 2>/dev/null
387 # has_remote BRANCH
388 # Whether BRANCH has a remote equivalent (accepts top-bases/ too)
389 has_remote()
391 [ -n "$base_remote" ] && ref_exists "refs/remotes/$base_remote/$1"
394 # Return the verified TopGit branch name or die with an error.
395 # As a convenience, if HEAD is given and HEAD is a symbolic ref to
396 # refs/heads/... then ... will be verified instead.
397 # if "$2" = "-f" then return an error rather than dying.
398 verify_topgit_branch()
400 case "$1" in
401 refs/heads/*)
402 _verifyname="${1#refs/heads/}"
404 refs/top-bases/*)
405 _verifyname="${1#refs/top-bases/}"
407 HEAD)
408 _verifyname="$(git symbolic-ref HEAD 2>/dev/null || :)"
409 [ -n "$_verifyname" ] || die "HEAD is not a symbolic ref"
410 case "$_verifyname" in refs/heads/*) :;; *)
411 [ "$2" != "-f" ] || return 1
412 die "HEAD is not a symbolic ref to the refs/heads namespace"
413 esac
414 _verifyname="${_verifyname#refs/heads/}"
417 _verifyname="$1"
419 esac
420 if ! ref_exists "refs/heads/$_verifyname"; then
421 [ "$2" != "-f" ] || return 1
422 die "no such branch"
424 if ! ref_exists "refs/top-bases/$_verifyname"; then
425 [ "$2" != "-f" ] || return 1
426 die "not a TopGit-controlled branch"
428 printf '%s' "$_verifyname"
431 # Caches result
432 # $1 = branch name (i.e. "t/foo/bar")
433 # $2 = optional result of rev-parse "refs/heads/$1"
434 # $3 = optional result of rev-parse "refs/top-bases/$1"
435 branch_annihilated()
437 _branch_name="$1"
438 _rev="${2:-$(ref_exists_rev "refs/heads/$_branch_name")}"
439 _rev_base="${3:-$(ref_exists_rev "refs/top-bases/$_branch_name")}"
441 _result=
442 _result_rev=
443 _result_rev_base=
444 { read -r _result _result_rev _result_rev_base <"$tg_cache_dir/$_branch_name/.ann"; } 2>/dev/null || :
445 [ -z "$_result" -o "$_result_rev" != "$_rev" -o "$_result_rev_base" != "$_rev_base" ] || return $_result
447 # use the merge base in case the base is ahead.
448 mb="$(git merge-base "$_rev_base" "$_rev" 2>/dev/null)"
450 test -z "$mb" || test "$(rev_parse_tree "$mb")" = "$(rev_parse_tree "$_rev")"
451 _result=$?
452 [ -d "$tg_cache_dir/$_branch_name" ] || mkdir -p "$tg_cache_dir/$_branch_name" 2>/dev/null
453 [ ! -d "$tg_cache_dir/$_branch_name" ] || \
454 echo $_result $_rev $_rev_base >"$tg_cache_dir/$_branch_name/.ann" 2>/dev/null || :
455 return $_result
458 non_annihilated_branches()
460 git for-each-ref --format='%(objectname) %(refname)' refs/top-bases |
461 while read rev ref; do
462 name="${ref#refs/top-bases/}"
463 if branch_annihilated "$name" "" "$rev"; then
464 continue
466 echo "$name"
467 done
470 # Make sure our tree is clean
471 ensure_clean_tree()
473 git update-index --ignore-submodules --refresh ||
474 die "the working directory has uncommitted changes (see above) - first commit or reset them"
475 [ -z "$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)" ] ||
476 die "the index has uncommited changes"
479 # is_sha1 REF
480 # Whether REF is a SHA1 (compared to a symbolic name).
481 is_sha1()
483 case "$1" in $octet20) return 0;; esac
484 return 1
487 # recurse_deps_internal NAME [BRANCHPATH...]
488 # get recursive list of dependencies with leading 0 if branch exists 1 if missing
489 # followed by a 1 if the branch is "tgish" or a 0 if not
490 # then the branch name followed by its depedency chain (which might be empty)
491 # An output line might look like this:
492 # 0 1 t/foo/leaf t/foo/int t/stage
493 # If no_remotes is non-empty, exclude remotes
494 # If recurse_preorder is non-empty, do a preorder rather than postorder traversal
495 recurse_deps_internal()
497 _ref_hash=
498 if ! _ref_hash="$(ref_exists_rev "refs/heads/$1")"; then
499 [ -z "$2" ] || echo "1 0 $*"
500 return
503 # If no_remotes is unset also check our base against remote base.
504 # Checking our head against remote head has to be done in the helper.
505 if test -z "$no_remotes" && has_remote "top-bases/$1"; then
506 echo "0 0 refs/remotes/$base_remote/top-bases/$1 $*"
509 _is_tgish=0
510 _ref_hash_base=
511 if _ref_hash_base="$(ref_exists_rev "refs/top-bases/$1")"; then
512 _is_tgish=1
513 [ -z "$recurse_preorder" -o -z "$2" ] || echo "0 $_is_tgish $*"
515 # if the branch was annihilated, it is considered to have no dependencies
516 if [ -n "$_is_tgish" ] && ! branch_annihilated "$1" "$_ref_hash" "$_ref_hash_base"; then
517 #TODO: handle nonexisting .topdeps?
518 cat_deps "$1" |
519 while read _dname; do
520 # Shoo shoo, leave our environment alone!
521 (recurse_deps_internal "$_dname" "$@")
522 done
526 [ -n "$recurse_preorder" -o -z "$2" ] || echo "0 $_is_tgish $*"
529 # do_eval CMD
530 # helper for recurse_deps so that a return statement executed inside CMD
531 # does not return from recurse_deps. This shouldn't be necessary, but it
532 # seems that it actually is.
533 do_eval()
535 eval "$@"
538 # recurse_deps CMD NAME [BRANCHPATH...]
539 # Recursively eval CMD on all dependencies of NAME.
540 # Dependencies are visited in topological order.
541 # CMD can refer to $_name for queried branch name,
542 # $_dep for dependency name,
543 # $_depchain for space-seperated branch backtrace,
544 # $_dep_missing boolean to check whether $_dep is present
545 # and the $_dep_is_tgish boolean.
546 # It can modify $_ret to affect the return value
547 # of the whole function.
548 # If recurse_deps() hits missing dependencies, it will append
549 # them to space-separated $missing_deps list and skip them
550 # after calling CMD with _dep_missing set.
551 # remote dependencies are processed if no_remotes is unset.
552 recurse_deps()
554 _cmd="$1"; shift
556 _was_read_only="$tg_read_only"
557 [ -n "$_was_read_only" ] || rm -rf "$tg_tmp_dir/cached" "$tg_tmp_dir/tg~ref-dirs-created"
558 tg_read_only=1
559 _my_ref_cache="$(create_ref_cache)"
560 _depsfile="$(get_temp tg-depsfile)"
561 recurse_deps_internal "$@" >>"$_depsfile"
562 [ -z "$_my_ref_cache" ] || remove_ref_cache
563 tg_read_only="$_was_read_only"
565 _ret=0
566 while read _ismissing _istgish _dep _name _deppath; do
567 _depchain="$_name${_deppath:+ $_deppath}"
568 _dep_is_tgish=
569 [ "$_istgish" = "0" ] || _dep_is_tgish=1
570 _dep_missing=
571 if [ "$_ismissing" != "0" ]; then
572 _dep_missing=1
573 case " $missing_deps " in *" $_dep "*) :;; *)
574 missing_deps="${missing_deps:+$missing_deps }$_dep"
575 esac
577 do_eval "$_cmd"
578 done <"$_depsfile"
579 rm -f "$_depsfile"
580 return $_ret
583 # branch_needs_update
584 # This is a helper function for determining whether given branch
585 # is up-to-date wrt. its dependencies. It expects input as if it
586 # is called as a recurse_deps() helper.
587 # In case the branch does need update, it will echo it together
588 # with the branch backtrace on the output (see needs_update()
589 # description for details) and set $_ret to non-zero.
590 branch_needs_update()
592 if [ -n "$_dep_missing" ]; then
593 echo "! $_dep $_depchain"
594 return 0
597 _dep_base_update=
598 if [ -n "$_dep_is_tgish" ]; then
599 branch_annihilated "$_dep" && return 0
601 if has_remote "$_dep"; then
602 branch_contains "refs/heads/$_dep" "refs/remotes/$base_remote/$_dep" || _dep_base_update=%
604 # This can possibly override the remote check result;
605 # we want to sync with our base first
606 branch_contains "refs/heads/$_dep" "refs/top-bases/$_dep" || _dep_base_update=:
609 if [ -n "$_dep_base_update" ]; then
610 # _dep needs to be synced with its base/remote
611 echo "$_dep_base_update $_dep $_depchain"
612 _ret=1
613 elif [ -n "$_name" ]; then
614 case "$_dep" in refs/*) _fulldep="$_dep";; *) _fulldep="refs/heads/$_dep";; esac
615 if ! branch_contains "refs/top-bases/$_name" "$_fulldep"; then
616 # Some new commits in _dep
617 echo "$_dep $_depchain"
618 _ret=1
623 # needs_update NAME
624 # This function is recursive; it outputs reverse path from NAME
625 # to the branch (e.g. B_DIRTY B1 B2 NAME), one path per line,
626 # inner paths first. Innermost name can be ':' if the head is
627 # not in sync with the base, '%' if the head is not in sync
628 # with the remote (in this order of priority) or '!' if depednecy
629 # is missing.
630 # It will also return non-zero status if NAME needs update.
631 # If needs_update() hits missing dependencies, it will append
632 # them to space-separated $missing_deps list and skip them.
633 needs_update()
635 recurse_deps branch_needs_update "$1"
638 # branch_empty NAME [-i | -w]
639 branch_empty()
641 if [ -z "$2" ]; then
642 _rev="$(ref_exists_rev "refs/heads/$1")" || return 0
643 _result=
644 _result_rev=
645 { read -r _result _result_rev <"$tg_cache_dir/$1/.mt"; } 2>/dev/null || :
646 [ -z "$_result" -o "$_result_rev" != "$_rev" ] || return $_result
647 _result=0
648 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ] || _result=$?
649 [ -d "$tg_cache_dir/$1" ] || mkdir -p "$tg_cache_dir/$1" 2>/dev/null
650 [ ! -d "$tg_cache_dir/$1" ] || echo $_result $_rev >"$tg_cache_dir/$1/.mt"
651 return $_result
652 else
653 [ "$(pretty_tree "$1" -b)" = "$(pretty_tree "$1" $2)" ]
657 # list_deps [-i | -w] [BRANCH]
658 # -i/-w apply only to HEAD
659 list_deps()
661 head_from=
662 [ "$1" != "-i" -a "$1" != "-w" ] || { head_from="$1"; shift; }
663 head="$(git symbolic-ref -q HEAD)" ||
664 head="..detached.."
666 git for-each-ref --format='%(objectname) %(refname)' refs/top-bases"${1:+/$1}" |
667 while read rev ref; do
668 name="${ref#refs/top-bases/}"
669 if branch_annihilated "$name" "" "$rev"; then
670 continue
673 from=$head_from
674 [ "refs/heads/$name" = "$head" ] ||
675 from=
676 cat_file "refs/heads/$name:.topdeps" $from | while read dep; do
677 dep_is_tgish=true
678 ref_exists "refs/top-bases/$dep" ||
679 dep_is_tgish=false
680 if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
681 echo "$name $dep"
683 done
684 done
687 # switch_to_base NAME [SEED]
688 switch_to_base()
690 _base="refs/top-bases/$1"; _seed="$2"
691 # We have to do all the hard work ourselves :/
692 # This is like git checkout -b "$_base" "$_seed"
693 # (or just git checkout "$_base"),
694 # but does not create a detached HEAD.
695 git read-tree -u -m HEAD "${_seed:-$_base}"
696 [ -z "$_seed" ] || git update-ref "$_base" "$_seed"
697 git symbolic-ref HEAD "$_base"
700 # Show the help messages.
701 do_help()
703 _www=
704 if [ "$1" = "-w" ]; then
705 _www=1
706 shift
708 if [ -z "$1" ] ; then
709 # This is currently invoked in all kinds of circumstances,
710 # including when the user made a usage error. Should we end up
711 # providing more than a short help message, then we should
712 # differentiate.
713 # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
715 ## Build available commands list for help output
717 cmds=
718 sep=
719 for cmd in "@cmddir@"/tg-*; do
720 ! [ -r "$cmd" ] && continue
721 # strip directory part and "tg-" prefix
722 cmd="$(basename "$cmd")"
723 cmd="${cmd#tg-}"
724 cmds="$cmds$sep$cmd"
725 sep="|"
726 done
728 echo "TopGit version $TG_VERSION - A different patch queue manager"
729 echo "Usage: $tgname ( help [-w] [<command>] | [-C <dir>] [-r <remote>] ($cmds) ...)"
730 echo "Use \"$tgdisplaydir$tgname help tg\" for overview of TopGit"
731 elif [ -r "@cmddir@"/tg-$1 -o -r "@sharedir@/tg-$1.txt" ] ; then
732 if [ -n "$_www" ]; then
733 nohtml=
734 if ! [ -r "@sharedir@/topgit.html" ]; then
735 echo "`basename $0`: missing html help file:" \
736 "@sharedir@/topgit.html" 1>&2
737 nohtml=1
739 if ! [ -r "@sharedir@/tg-$1.html" ]; then
740 echo "`basename $0`: missing html help file:" \
741 "@sharedir@/tg-$1.html" 1>&2
742 nohtml=1
744 if [ -n "$nohtml" ]; then
745 echo "`basename $0`: use" \
746 "\"`basename $0` help $1\" instead" 1>&2
747 exit 1
749 git web--browse -c help.browser "@sharedir@/tg-$1.html"
750 exit
752 setup_pager
754 if [ -r "@cmddir@"/tg-$1 ] ; then
755 "@cmddir@"/tg-$1 -h 2>&1 || :
756 echo
758 if [ -r "@sharedir@/tg-$1.txt" ] ; then
759 cat "@sharedir@/tg-$1.txt"
761 } | eval "$TG_PAGER"
762 else
763 echo "`basename $0`: no help for $1" 1>&2
764 do_help
765 exit 1
769 ## Pager stuff
771 # isatty FD
772 isatty()
774 test -t $1
777 # pass "diff" to get pager.diff
778 # if pager.$1 is a boolean false returns cat
779 # if set to true or unset fails
780 # otherwise succeeds and returns the value
781 get_pager()
783 if _x="$(git config --bool "pager.$1" 2>/dev/null)"; then
784 [ "$_x" != "true" ] || return 1
785 echo "cat"
786 return 0
788 if _x="$(git config "pager.$1" 2>/dev/null)"; then
789 echo "$_x"
790 return 0
792 return 1
795 # setup_pager
796 # Set TG_PAGER to a valid executable
797 # After calling, code to be paged should be surrounded with {...} | eval "$TG_PAGER"
798 # Preference is (same as Git):
799 # 1. GIT_PAGER
800 # 2. pager.$USE_PAGER_TYPE (but only if USE_PAGER_TYPE is set and so is pager.$USE_PAGER_TYPE)
801 # 3. core.pager (only if set)
802 # 4. PAGER
803 # 5. less
804 setup_pager()
806 isatty 1 || { TG_PAGER=cat; return 0; }
808 if [ -z "$TG_PAGER_IN_USE" ]; then
809 # TG_PAGER = GIT_PAGER | PAGER | less
810 # NOTE: GIT_PAGER='' is significant
811 if [ -n "${GIT_PAGER+set}" ]; then
812 TG_PAGER="$GIT_PAGER"
813 elif [ -n "$USE_PAGER_TYPE" ] && _dp="$(get_pager "$USE_PAGER_TYPE")"; then
814 TG_PAGER="$_dp"
815 elif _cp="$(git config core.pager 2>/dev/null)"; then
816 TG_PAGER="$_cp"
817 elif [ -n "${PAGER+set}" ]; then
818 TG_PAGER="$PAGER"
819 else
820 TG_PAGER="less"
822 : ${TG_PAGER:=cat}
823 else
824 TG_PAGER=cat
827 # Set pager default environment variables
828 # see pager.c:setup_pager
829 if [ -z "${LESS+set}" ]; then
830 LESS="-FRSX"
831 export LESS
833 if [ -z "${LV+set}" ]; then
834 LV="-c"
835 export LV
838 # this is needed so e.g. `git diff` will still colorize it's output if
839 # requested in ~/.gitconfig with color.diff=auto
840 GIT_PAGER_IN_USE=1
841 export GIT_PAGER_IN_USE
843 # this is needed so we don't get nested pagers
844 TG_PAGER_IN_USE=1
845 export TG_PAGER_IN_USE
848 # get_temp NAME [-d]
849 # creates a new temporary file (or directory with -d) in the global
850 # temporary directory $tg_tmp_dir with pattern prefix NAME
851 get_temp()
853 mktemp $2 "$tg_tmp_dir/$1.XXXXXX"
856 ## Initial setup
857 initial_setup()
859 # suppress the merge log editor feature since git 1.7.10
861 GIT_MERGE_AUTOEDIT=no
862 export GIT_MERGE_AUTOEDIT
863 git_dir="$(git rev-parse --git-dir)"
864 root_dir="$(git rev-parse --show-cdup)"; root_dir="${root_dir:-.}"
865 logrefupdates="$(git config --bool core.logallrefupdates 2>/dev/null || :)"
866 [ "$logrefupdates" = "true" ] || logrefupdates=
868 # make sure root_dir doesn't end with a trailing slash.
870 root_dir="${root_dir%/}"
871 [ -n "$base_remote" ] || base_remote="$(git config topgit.remote 2>/dev/null)" || :
873 # make sure global cache directory exists inside GIT_DIR
875 tg_cache_dir="$git_dir/tg-cache"
876 [ -d "$tg_cache_dir" ] || mkdir "$tg_cache_dir"
878 # create global temporary directories, inside GIT_DIR
880 tg_tmp_dir=
881 trap 'rm -rf "$tg_tmp_dir"' EXIT
882 trap 'exit 129' HUP
883 trap 'exit 130' INT
884 trap 'exit 131' QUIT
885 trap 'exit 134' ABRT
886 trap 'exit 143' TERM
887 tg_tmp_dir="$(mktemp -d "$git_dir/tg-tmp.XXXXXX")"
888 tg_ref_cache="$tg_tmp_dir/tg~ref-cache"
891 # return the "realpath" for the item except the leaf is not resolved if it's
892 # a symbolic link. The directory part must exist, but the basename need not.
893 get_abs_path()
895 [ -n "$1" -a -d "$(dirname -- "$1")" ] || return 1
896 printf '%s' "$(cd -- "$(dirname -- "$1")" && pwd -P)/$(basename -- "$1")"
899 ## Startup
901 [ -d "@cmddir@" ] ||
902 die "No command directory: '@cmddir@'"
904 if [ -n "$tg__include" ]; then
906 # We were sourced from another script for our utility functions;
907 # this is set by hooks. Skip the rest of the file. A simple return doesn't
908 # work as expected in every shell. See http://bugs.debian.org/516188
910 # ensure setup happens
912 initial_setup
914 else
916 set -e
918 tg="$0"
919 tgdir="$(dirname -- "$tg")/"
920 tgname="$(basename -- "$tg")"
921 [ "$0" != "$tgname" ] || tgdir=""
923 # If tg contains a '/' but does not start with one then replace it with an absolute path
925 case "$0" in /*) :;; */*)
926 tgdir="$(cd "$(dirname -- "$0")" && pwd -P)/"
927 tg="$tgdir$tgname"
928 esac
930 # If the tg in the PATH is the same as "$tg" just display the basename
931 # tgdisplay will include any explicit -C <dir> option whereas tg will not
933 tgdisplaydir="$tgdir"
934 tgdisplay="$tg"
935 if [ "$(get_abs_path "$tg")" = "$(get_abs_path "$(which "$tgname" || :)" || :)" ]; then
936 tgdisplaydir=""
937 tgdisplay="$tgname"
940 explicit_remote=
941 explicit_dir=
942 gitcdopt=
943 noremote=
945 cmd=
946 while :; do case "$1" in
948 help|--help|-h)
949 cmd=help
950 shift
951 break;;
953 --hooks-path)
954 cmd=hooks-path
955 shift
956 break;;
959 shift
960 if [ -z "$1" ]; then
961 echo "Option -r requires an argument." >&2
962 do_help
963 exit 1
965 unset noremote
966 base_remote="$1"
967 explicit_remote="$base_remote"
968 tg="$tgdir$tgname -r $explicit_remote"
969 tgdisplay="$tgdisplaydir$tgname"
970 [ -z "$explicit_dir" ] || tgdisplay="$tgdisplay -C \"$explicit_dir\""
971 tgdisplay="$tgdisplay -r $explicit_remote"
972 shift;;
975 unset base_remote explicit_remote
976 noremote=1
977 tg="$tgdir$tgname -u"
978 tgdisplay="$tgdisplaydir$tgname"
979 [ -z "$explicit_dir" ] || tgdisplay="$tgdisplay -C \"$explicit_dir\""
980 tgdisplay="$tgdisplay -u"
981 shift;;
984 shift
985 if [ -z "$1" ]; then
986 echo "Option -C requires an argument." >&2
987 do_help
988 exit 1
990 cd "$1"
991 unset GIT_DIR
992 explicit_dir="$1"
993 gitcdopt=" -C \"$explicit_dir\""
994 tg="$tgdir$tgname"
995 tgdisplay="$tgdisplaydir$tgname -C \"$explicit_dir\""
996 [ -z "$explicit_remote" ] || tg="$tg -r $explicit_remote"
997 [ -z "$explicit_remote" ] || tgdisplay="$tgdisplay -r $explicit_remote"
998 [ -z "$noremote" ] || tg="$tg -u"
999 [ -z "$noremote" ] || tg="$tgdisplay -u"
1000 shift;;
1003 shift
1004 break;;
1007 echo "Invalid option $1 (subcommand options must appear AFTER the subcommand)." >&2
1008 do_help
1009 exit 1;;
1012 break;;
1014 esac; done
1016 [ -n "$cmd" ] || { cmd="$1"; shift || :; }
1018 ## Dispatch
1020 [ -n "$cmd" ] || { do_help; exit 1; }
1022 case "$cmd" in
1024 help)
1025 do_help "$@"
1026 exit 0;;
1028 hooks-path)
1029 # Internal command
1030 echo "@hooksdir@";;
1033 [ -r "@cmddir@"/tg-$cmd ] || {
1034 echo "Unknown subcommand: $cmd" >&2
1035 do_help
1036 exit 1
1039 initial_setup
1040 [ -z "$noremote" ] || unset base_remote
1042 # make sure merging the .top* files will always behave sanely
1044 setup_ours
1045 setup_hook "pre-commit"
1047 _use_ref_cache=
1048 tg_read_only=1
1049 case "$cmd" in
1050 summary|info|export)
1051 _use_ref_cache=1;;
1052 annihilate|create|delete|depend|import|update)
1053 tg_read_only=;;
1054 esac
1055 [ -z "$_use_ref_cache" ] || create_ref_cache >/dev/null
1057 . "@cmddir@"/tg-$cmd;;
1058 esac
1062 # vim:noet