chiark / gitweb /
Merge commit 'refs/top-bases/fixes/export--b-deps' into fixes/export--b-deps
authorUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Tue, 3 Mar 2009 10:21:56 +0000 (11:21 +0100)
committerUwe Kleine-König <u.kleine-koenig@pengutronix.de>
Tue, 3 Mar 2009 10:21:56 +0000 (11:21 +0100)
Conflicts:
tg-export.sh

15 files changed:
README
contrib/tg-completion.bash
create-help.sh
tg-create.sh
tg-delete.sh
tg-depend.sh
tg-export.sh
tg-import.sh
tg-info.sh
tg-mail.sh
tg-patch.sh
tg-remote.sh
tg-summary.sh
tg-update.sh
tg.sh

diff --git a/README b/README
index 5bfe3ee14793c9591e78095e213039de3847d0ea..d2f095ddd164fdcca7c9515e8c27a8246ffe8ea9 100644 (file)
--- a/README
+++ b/README
@@ -22,7 +22,7 @@ version control of patches (reordering of patches is not
 version-controlled at all). But there are several disadvantages -
 for one, these tools (especially StGIT) do not actually fit well
 with plain Git at all: it is basically impossible to take advantage
-of the index efectively when using StGIT. But more importantly,
+of the index effectively when using StGIT. But more importantly,
 these tools horribly fail in the face of distributed environment.
 
 TopGit has been designed around three main tenets:
@@ -45,7 +45,7 @@ them.
 
 As mentioned above, the main intended use-case for TopGit is tracking
 third-party patches, where each patch is effectively a single topic
-branch.  In order to flexibly accomodate even complex scenarios when
+branch.  In order to flexibly accommodate even complex scenarios when
 you track many patches where many are independent but some depend
 on others, TopGit ignores the ancient Quilt heritage of patch series
 and instead allows the patches to freely form graphs (DAGs just like
@@ -222,7 +222,7 @@ tg create
 
        After `tg create`, you should insert the patch description
        to the '.topmsg' file, which will already contain some
-       pre-filled bits. You can set topgit.to, topgit.cc and topgit.bcc
+       prefilled bits. You can set topgit.to, topgit.cc and topgit.bcc
        configuration variables in order to have `tg create`
        add these headers with given default values to '.topmsg'.
 
@@ -284,8 +284,9 @@ tg patch
        tg patch will be able to automatically send the patches by mail
        or save them to files. (TODO)
 
-       TODO: tg patch -i to base at index instead of branch,
-               -w for working tree
+       Options:
+         -i            base patch generation on index instead of branch
+         -w            base patch generation on working tree instead of branch
 
 tg mail
 ~~~~~~~
@@ -350,11 +351,12 @@ tg export
        in the cleaned up history (corresponding basically exactly
        to `tg patch` output for the topic branch).
 
-       The command has two posible outputs now - either a Git branch
-       with the collapsed history, or a quilt series in new directory.
+       The command has three possible outputs now - either a Git branch with
+       the collapsed history, a Git branch with a linearized history, or a
+       quilt series in new directory.
 
        In case of producing collapsed history in new branch,
-       You can use this collapsed structure either for providing
+       you can use this collapsed structure either for providing
        a pull source for upstream, or further linearization e.g.
        for creation of a quilt series using git log:
 
@@ -377,6 +379,21 @@ tg export
                     `- t/bar/good <,-------------------'/
                     `- t/baz      ---------------------'
 
+       In case of using the linearize mode:
+
+       master$ tg export --linearize for-linus
+
+       you get a linear history respecting the dependencies of your patches in
+       a new branch for-linus.  The result should be more or less the same as
+       using quilt mode and reimporting it into a Git branch.  (More or less
+       because the topologic order can usually be extended in more than one
+       way into a complete ordering and the two methods may choose different
+       one's.)  The result might be more appropriate for merging upstream as
+       it contains fewer merges.
+
+       Note that you might get conflicts during linearization because the
+       patches are reordered to get a linear history.
+
        In case of the quilt mode,
 
        master$ tg export --quilt for-linus
@@ -409,15 +426,23 @@ tg export
        a comma-separated explicit list of branches to export. This
        mode of operation is currently not supported with collapse.
 
-       Usage: tg export ([--collapse] BRANCH | --quilt DIR)
+       In '--quilt' mode the patches are named like the originating topgit
+       branch.  So usually they end up in subdirectories of the output
+       directory.  With option '--flatten' the names are mangled such that
+       they end up directly in the output dir (i.e. slashed are substituted by
+       underscores).  With '--numbered' (which implies '--flatten') the patch
+       names get a number as prefix to allow getting the order without
+       consulting the series file, which eases sending out the patches.
+
+       Usage: tg export ([(--collapse | --linearize)] BRANCH | --quilt DIR)
 
        TODO: Make stripping of non-essential headers configurable
        TODO: Make stripping of [PATCH] and other prefixes configurable
        TODO: --mbox option for other mode of operation
-       TODO: -n option to prevent exporting of empty patches
        TODO: -a option to export all branches
-       TODO: For quilt exporting, use a temporary branch and remove it when
-             done - this would allow producing conflict-less series
+       TODO: For quilt exporting, export the linearized history created in a
+             temporary branch---this would allow producing conflict-less
+             series
 
 tg import
 ~~~~~~~~~
@@ -455,7 +480,6 @@ tg update
 
        TODO: tg update -a for updating all topic branches
 
-TODO: tg depend for adding/removing dependencies smoothly
 TODO: tg rename
 
 
@@ -485,7 +509,7 @@ whatever Cc headers you choose or the post-three-dashes message.
 When mailing out your patch, basically only few extra headers
 mail headers are inserted and the patch itself is appended.
 Thus, as your patches evolve, you can record nuances like whether
-the paricular patch should have To-list/Cc-maintainer or vice
+the particular patch should have To-list/Cc-maintainer or vice
 versa and similar nuances, if your project is into that.
 From is prefilled from your current GIT_AUTHOR_IDENT, other headers
 can be prefilled from various optional topgit.* config options.
index 67f820e8d55068697490ac537f27cede4ba46fd2..de8a7b55d76bcf5059ed4b979e44efd4aaa9918c 100755 (executable)
@@ -359,6 +359,12 @@ _tg_patch ()
        local cur="${COMP_WORDS[COMP_CWORD]}"
 
        case "$cur" in
+       -*)
+               __tgcomp "
+                       -i
+                       -w
+               "
+               ;;
        *)
                __tgcomp "$(__tg_topics)"
        esac
@@ -369,6 +375,11 @@ _tg_remote ()
        local cur="${COMP_WORDS[COMP_CWORD]}"
 
        case "$cur" in
+       -*)
+               __tgcomp "
+                       --populate
+               "
+               ;;
        *)
                __tgcomp "$(__tg_remotes)"
        esac
@@ -376,7 +387,15 @@ _tg_remote ()
 
 _tg_summary ()
 {
-       COMPREPLY=()
+       local cur="${COMP_WORDS[COMP_CWORD]}"
+
+       case "$cur" in
+       *)
+               __tgcomp "
+                       --graphviz
+                       -t
+               "
+       esac
 }
 
 _tg_update ()
index 766e8d4304ff24c1ff1251bef1d0d6ea4bc3b864..fbd9a093d926b1a6ce53f3f138b624d008da168d 100755 (executable)
@@ -15,3 +15,4 @@ fi
        { if (incommand) { print $0; } }
 '  > tg-"$1".txt
 
+# vim:noet
index 6ee3f027d23e797ac80e07c1024b22aaedc461f5..2edd5379d54c9cc533a0904dafc51b6f566e728c 100644 (file)
@@ -78,9 +78,9 @@ done
 rm -f "$git_dir/top-name" "$git_dir/top-deps" "$git_dir/top-merge"
 
 
-## Create base
+## Find starting commit to create the base
 
-if [ -n "$merge" ]; then
+if [ -n "$merge" -a -z "$restarted" ]; then
        # Unshift the first item from the to-merge list
        branch="${merge%% *}"
        merge="${merge#* }"
@@ -141,3 +141,5 @@ git add -f "$root_dir/.topmsg"
 
 info "Topic branch $name set up. Please fill .topmsg now and make initial commit."
 info "To abort: git rm -f .top* && git checkout ${deps%% *} && $tg delete $name"
+
+# vim:noet
index 7f7ede7068d71bd5f50e10686ea87398f26f1b41..ab121c2597c11534ff251944ade590490e151b23 100644 (file)
@@ -44,3 +44,5 @@ branch_empty "$name" || nonempty=1
 
 git update-ref -d "refs/top-bases/$name" "$baserev"
 git update-ref -d "refs/heads/$name" "$branchrev"
+
+# vim:noet
index ff4541c5980df606ab293cb224867c00577f5f65..ef5f94ff2919c605f268ff7b12f702b1f63e5e5d 100644 (file)
@@ -47,3 +47,5 @@ echo "$name" >>"$root_dir/.topdeps"
 git add -f "$root_dir/.topdeps"
 git commit -m"New TopGit dependency: $name"
 $tg update
+
+# vim:noet
index ae32bf48f6cde617cc31d919395cf598b68102f6..90f7c6a6dfcf739a6f89061e4e89f33aa810a292 100644 (file)
@@ -7,6 +7,8 @@ name=
 branches=
 output=
 driver=collapse
+flatten=false
+numbered=false
 
 
 ## Parse options
@@ -16,12 +18,19 @@ while [ -n "$1" ]; do
        case "$arg" in
        -b)
                branches="$1"; shift;;
+       --flatten)
+               flatten=true;;
+       --numbered)
+               flatten=true;
+               numbered=true;;
        --quilt)
                driver=quilt;;
        --collapse)
                driver=collapse;;
+       --linearize)
+               driver=linearize;;
        -*)
-               echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY)" >&2
+               echo "Usage: tg [...] export ([--collapse] NEWBRANCH | [-b BRANCH1,BRANCH2...] --quilt DIRECTORY | --linearize NEWBRANCH)" >&2
                exit 1;;
        *)
                [ -z "$output" ] || die "output already specified ($output)"
@@ -30,6 +39,16 @@ while [ -n "$1" ]; do
 done
 
 
+
+[ -z "$branches" -o "$driver" = "quilt" ] ||
+       die "-b works only with the quilt driver"
+
+[ "$driver" = "quilt" ] || ! "$numbered" ||
+       die "--numbered works only with the quilt driver";
+
+[ "$driver" = "quilt" ] || ! "$flatten" ||
+       die "--flatten works only with the quilt driver"
+
 if [ -z "$branches" ]; then
        # this check is only needed when no branches have been passed
        name="$(git symbolic-ref HEAD | sed 's#^refs/heads/##')"
@@ -60,14 +79,11 @@ pretty_tree()
         git write-tree)
 }
 
-# collapsed_commit NAME
-# Produce a collapsed commit of branch NAME.
-collapsed_commit()
+create_tg_commit()
 {
-       local name; name="$1"
-
-       rm -f "$playground/^pre" "$playground/^post"
-       >"$playground/^body"
+       name="$1"
+       tree="$2"
+       parent="$3"
 
        # Get commit message and authorship information
        git cat-file blob "$name:.topmsg" | git mailinfo "$playground/^msg" /dev/null > "$playground/^info"
@@ -81,6 +97,20 @@ collapsed_commit()
        test -n "$GIT_AUTHOR_EMAIL" && export GIT_AUTHOR_EMAIL
        test -n "$GIT_AUTHOR_DATE" && export GIT_AUTHOR_DATE
 
+       (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
+       git stripspace |
+       git commit-tree "$tree" -p "$parent"
+}
+
+# collapsed_commit NAME
+# Produce a collapsed commit of branch NAME.
+collapsed_commit()
+{
+       local name; name="$1"
+
+       rm -f "$playground/^pre" "$playground/^post"
+       >"$playground/^body"
+
        # Determine parent
        parent="$(cut -f 1 "$playground/$name^parents")"
        if [ "$(cat "$playground/$name^parents" | wc -l)" -gt 1 ]; then
@@ -93,9 +123,11 @@ collapsed_commit()
                        $(for p in $parent; do echo -p $p; done))"
        fi
 
-       (printf '%s\n\n' "$SUBJECT"; cat "$playground/^msg") |
-       git stripspace |
-       git commit-tree "$(pretty_tree "$name")" -p "$parent"
+       if branch_empty "$name"; then
+               echo "$parent";
+       else
+               create_tg_commit "$name" "$(pretty_tree $name)" "$parent"
+       fi;
 
        echo "$name" >>"$playground/^ticker"
 }
@@ -136,22 +168,95 @@ quilt()
                return
        fi
 
-       filename="$output/$_dep.diff"
-       if [ -e "$filename" ]; then
+       if "$flatten"; then
+               bn="$(echo "$_dep.diff" | sed -e 's#_#__#g' -e 's#/#_#g')";
+               dn="";
+       else
+               bn="$(basename "$_dep.diff")";
+               dn="$(dirname "$_dep.diff")/";
+               if [ "x$dn" = "x./" ]; then
+                       dn="";
+               fi;
+       fi;
+
+       if [ -e "$playground/$_dep" ]; then
                # We've already seen this dep
                return
        fi
 
-       echo "Exporting $_dep"
-       mkdir -p "$(dirname "$filename")"
-       $tg patch "$_dep" >"$filename"
-       echo "$_dep.diff -p1" >>"$output/series"
+       mkdir -p "$playground/$(dirname "$_dep")";
+       touch "$playground/$_dep";
+
+       if branch_empty "$_dep"; then
+               echo "Skip empty patch $_dep";
+       else
+               if "$numbered"; then
+                       number="$(printf "%04u" $(($(cat "$playground/^number" 2>/dev/null) + 1)))";
+                       bn="$number-$bn";
+                       echo "$number" >"$playground/^number";
+               fi;
+
+               echo "Exporting $_dep"
+               mkdir -p "$output/$dn";
+               $tg patch "$_dep" >"$output/$dn$bn"
+               echo "$dn$bn -p1" >>"$output/series"
+       fi
 }
 
+linearize()
+{
+       if test ! -f "$playground/^BASE"; then
+               head="$(git rev-parse --verify "$_dep")"
+               echo "$head" > "$playground/^BASE"
+               git checkout -q "$head"
+               return;
+       fi;
+
+       head=$(git rev-parse --verify HEAD)
+
+       if [ -z "$_dep_is_tgish" ]; then
+               # merge in $_dep unless already included
+               rev="$(git rev-parse --verify "$_dep")";
+               common="$(git merge-base --all HEAD "$_dep")";
+               if test "$rev" = "$common"; then
+                       # already included, just skip
+                       :;
+               else
+                       git merge -s recursive "$_dep";
+                       retmerge="$?";
+                       if test "x$retmerge" != "x0"; then
+                               echo fix up the merge, commit and then exit;
+                               #todo error handling
+                               sh -i
+                       fi;
+               fi;
+       else
+               git merge-recursive "$(pretty_tree "refs/top-bases/$_dep")" -- HEAD "$(pretty_tree "refs/heads/$_dep")";
+               retmerge="$?";
+
+               if test "x$retmerge" != "x0"; then
+                       echo "fix up the merge and update the index.  Don't commit!"
+                       #todo error handling
+                       sh -i
+               fi
+
+               result_tree=$(git write-tree)
+               # testing branch_empty might not always give the right answer.
+               # It can happen that the patch is non-empty but still after
+               # linearizing there is no change.  So compare the trees.
+               if test "x$result_tree" = "x$(git rev-parse $head^{tree})"; then
+                       echo "skip empty commit $_dep";
+               else
+                       newcommit=$(create_tg_commit "$_dep" "$result_tree" HEAD)
+                       git update-ref HEAD $newcommit $head
+                       echo "exported commit $_dep";
+               fi
+       fi
+}
 
 ## Machinery
 
-if [ "$driver" = "collapse" ]; then
+if [ "$driver" = "collapse" ] || [ "$driver" = "linearize" ]; then
        [ -n "$output" ] ||
                die "no target branch specified"
        ! ref_exists "$output"  ||
@@ -193,4 +298,17 @@ if [ "$driver" = "collapse" ]; then
 elif [ "$driver" = "quilt" ]; then
        depcount="$(cat "$output/series" | wc -l)"
        echo "Exported topic branch $name (total $depcount topics) to directory $output"
+
+elif [ "$driver" = "linearize" ]; then
+       git checkout -q -b $output
+
+       echo $name
+       if test $(git rev-parse "$(pretty_tree $name)^{tree}") != $(git rev-parse "HEAD^{tree}"); then
+               echo "Warning: Exported result doesn't match";
+               echo "tg-head=$(git rev-parse "$name"), exported=$(git rev-parse "HEAD")";
+               #git diff $head HEAD;
+       fi;
+
 fi
+
+# vim:noet
index b036b8645f184f54ca14878cdc02312f4f925eaf..a8d3be5b3098d73c9b1dabfc9e1d509ae533a533 100644 (file)
@@ -116,3 +116,5 @@ do
                esac
        done
 done
+
+# vim:noet
index 90762479eb44f3b0ef2f366fc4a7865e05087c76..7d6a34ca617bc8d6ffc44f81613d30715905688c 100644 (file)
@@ -73,3 +73,5 @@ else
        echo "Up-to-date."
 fi
 rm "$depcheck"
+
+# vim:noet
index 7b8f7ffdd61b9b9b802df04790be5d51ffa67c2a..4a684644a3977d0c2174e7aa7a8f7f5eeed97031 100644 (file)
@@ -29,6 +29,10 @@ done
 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
        die "not a TopGit-controlled branch"
 
+if ! git send-email --help >/dev/null 2>&1; then
+       die "git send-email command not available"
+fi
+
 if [ -n "$in_reply_to" ]; then
        send_email_args="$send_email_args --in-reply-to=$in_reply_to"
 fi
@@ -55,3 +59,5 @@ people=
 eval git send-email $send_email_args "$people" "$patchfile"
 
 rm "$patchfile"
+
+# vim:noet
index 97338ab63ef77f52eb876c77ca50b669c458f333..d701c5440705a609e833e10503f05441e61d1b1c 100644 (file)
@@ -5,14 +5,25 @@
 
 name=
 
+topic=
+diff_opts=
+diff_committed_only=yes        # will be unset for index/worktree
+
 
 ## Parse options
 
 while [ -n "$1" ]; do
        arg="$1"; shift
        case "$arg" in
+       -i)
+               topic='(i)'
+               diff_opts="$diff_opts --cached";
+               diff_committed_only=;;
+       -w)
+               topic='(w)'
+               diff_committed_only=;;
        -*)
-               echo "Usage: tg [...] patch [NAME]" >&2
+               echo "Usage: tg [...] patch [-i | -w] [NAME]" >&2
                exit 1;;
        *)
                [ -z "$name" ] || die "name already specified ($name)"
@@ -20,27 +31,40 @@ while [ -n "$1" ]; do
        esac
 done
 
+
+[ -n "$name"  -a  -z "$diff_committed_only" ]  &&
+       die "-i/-w are mutually exclusive with NAME"
+
 [ -n "$name" ] || name="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
 base_rev="$(git rev-parse --short --verify "refs/top-bases/$name" 2>/dev/null)" ||
        die "not a TopGit-controlled branch"
 
-git cat-file blob "$name:.topmsg"
+# if not index/worktree, topic is current branch
+[ -z "$topic" ] && topic="$name"
+
+
+
+setup_pager
+
+cat_file "$topic:.topmsg"
 echo
-[ -n "$(git grep '^[-]--' "$name" -- ".topmsg")" ] || echo '---'
+[ -n "$(git grep $diff_opts '^[-]--' ${diff_committed_only:+"$name"} -- ".topmsg")" ] || echo '---'
 
 # Evil obnoxious hack to work around the lack of git diff --exclude
 git_is_stupid="$(mktemp -t tg-patch-changes.XXXXXX)"
-git diff-tree --name-only "$base_rev" "$name" |
+git diff --name-only $diff_opts "$base_rev" ${diff_committed_only:+"$name"} -- |
        fgrep -vx ".topdeps" |
        fgrep -vx ".topmsg" >"$git_is_stupid" || : # fgrep likes to fail randomly?
 if [ -s "$git_is_stupid" ]; then
-       cat "$git_is_stupid" | xargs git diff --patch-with-stat "$base_rev" "$name" --
+       cat "$git_is_stupid" | xargs git diff --patch-with-stat $diff_opts "$base_rev" ${diff_committed_only:+"$name"} --
 else
        echo "No changes."
 fi
 rm "$git_is_stupid"
 
 echo '-- '
-echo "tg: ($base_rev..) $name (depends on: $(git cat-file blob "$name:.topdeps" | paste -s -d' '))"
+echo "tg: ($base_rev..) $name (depends on: $(cat_file "$topic:.topdeps" | paste -s -d' '))"
 branch_contains "$name" "$base_rev" ||
        echo "tg: The patch is out-of-date wrt. the base! Run \`$tg update\`."
+
+# vim:noet
index 4f60c7394b7fcca6ddc4e4c3fabb25f59237e359..3a40081b6b32435837c3b597ee4593f38e79de08 100644 (file)
@@ -27,9 +27,9 @@ git config "remote.$name.url" >/dev/null || die "unknown remote '$name'"
 
 ## Configure the remote
 
-git config --add "remote.$name.fetch" "+refs/top-bases/*:refs/remotes/$name/top-bases/*"
-git config --add "remote.$name.push" "+refs/top-bases/*:refs/top-bases/*"
-git config --add "remote.$name.push" "+refs/heads/*:refs/heads/*"
+git config --replace-all "remote.$name.fetch" "+refs/top-bases/*:refs/remotes/$name/top-bases/*" "\\+refs/top-bases/\\*:refs/remotes/$name/top-bases/\\*"
+git config --replace-all "remote.$name.push" "+refs/top-bases/*:refs/top-bases/*" "\\+refs/top-bases/\\*:refs/top-bases/\\*"
+git config --replace-all "remote.$name.push" "+refs/heads/*:refs/heads/*" "\\+refs/heads/\\*:refs/heads/\\*"
 
 info "Remote $name can now follow TopGit topic branches."
 if [ -z "$populate" ]; then
@@ -59,3 +59,5 @@ git for-each-ref "refs/remotes/$name/top-bases" |
 
 git config "topgit.remote" "$name"
 info "The remote '$name' is now the default source of topic branches."
+
+# vim:noet
index 3c90a6b5952e526bac9da80e84386d6abf6246a8..50ee8832090c6646c9bd114d6f5926657dcc07db 100644 (file)
@@ -24,6 +24,9 @@ done
 
 curname="$(git symbolic-ref HEAD | sed 's#^refs/\(heads\|top-bases\)/##')"
 
+! [ -n "$terse" -a -n "$graphviz" ] ||
+       die "-t and --graphviz options are mutual exclusive"
+
 if [ -n "$graphviz" ]; then
        cat <<EOT
 # GraphViz output; pipe to:
@@ -50,13 +53,22 @@ fi
 git for-each-ref refs/top-bases |
        while read rev type ref; do
                name="${ref#refs/top-bases/}"
+               if branch_annihilated "$name"; then
+                       continue;
+               fi;
+
                if [ -n "$terse" ]; then
                        echo "$name"
                        continue
                fi
                if [ -n "$graphviz" ]; then
                        git cat-file blob "$name:.topdeps" | while read dep; do
-                               echo "\"$name\" -> \"$dep\";"
+                               dep_is_tgish=true
+                               ref_exists "refs/top-bases/$dep"  ||
+                                       dep_is_tgish=false
+                               if ! "$dep_is_tgish" || ! branch_annihilated $dep; then
+                                       echo "\"$name\" -> \"$dep\";"
+                               fi
                        done
                        continue
                fi
@@ -98,3 +110,5 @@ git for-each-ref refs/top-bases |
 if [ -n "$graphviz" ]; then
        echo '}'
 fi
+
+# vim:noet
index 040800d6fb3805ef92519e436fe9cbb353cdbb77..288ec14f849495734fec17b839abf678f366ad78 100644 (file)
@@ -142,3 +142,5 @@ if ! git merge "$merge_with"; then
        fi
        exit 3
 fi
+
+# vim:noet
diff --git a/tg.sh b/tg.sh
index 4dcc15eff90bcc581980336b94d44f40e2385759..f0496f1b6ade99b1f9290f3cc5fe7096fc4373ba 100644 (file)
--- a/tg.sh
+++ b/tg.sh
@@ -3,6 +3,7 @@
 # (c) Petr Baudis <pasky@suse.cz>  2008
 # GPLv2
 
+TG_VERSION=0.6
 
 ## Auxiliary functions
 
@@ -17,6 +18,27 @@ die()
        exit 1
 }
 
+# cat_file "topic:file"
+# Like `git cat-file blob $1`, but topics '(i)' and '(w)' means index and worktree
+cat_file()
+{
+       arg="$1"
+       case "$arg" in
+       '(w):'*)
+               arg=$(echo "$arg" | tail --bytes=+5)
+               cat "$arg"
+               return
+               ;;
+       '(i):'*)
+               # ':file' means cat from index
+               arg=$(echo "$arg" | tail --bytes=+5)
+               git cat-file blob ":$arg"
+               ;;
+       *)
+               git cat-file blob "$arg"
+       esac
+}
+
 # setup_hook NAME
 setup_hook()
 {
@@ -63,8 +85,8 @@ measure_branch()
        _bname="$1"; _base="$2"
        [ -n "$_base" ] || _base="refs/top-bases/$_bname"
        # The caller should've verified $name is valid
-       _commits="$(git rev-list "$_bname" ^"$_base" | wc -l)"
-       _nmcommits="$(git rev-list --no-merges "$_bname" ^"$_base" | wc -l)"
+       _commits="$(git rev-list "$_bname" ^"$_base" -- | wc -l)"
+       _nmcommits="$(git rev-list --no-merges "$_bname" ^"$_base" -- | wc -l)"
        if [ $_commits -gt 1 ]; then
                _suffix="commits"
        else
@@ -77,7 +99,7 @@ measure_branch()
 # Whether B1 is a superset of B2.
 branch_contains()
 {
-       [ -z "$(git rev-list ^"$1" "$2" --)" ]
+       [ -z "$(git rev-list --max-count=1 ^"$1" "$2" --)" ]
 }
 
 # ref_exists REF
@@ -94,6 +116,16 @@ has_remote()
        [ -n "$base_remote" ] && ref_exists "remotes/$base_remote/$1"
 }
 
+branch_annihilated()
+{
+       _name="$1";
+
+       # use the merge base in case the base is ahead.
+       mb="$(git merge-base "refs/top-bases/$_name" "$_name")";
+
+       test "$(git rev-parse "$mb^{tree}")" = "$(git rev-parse "$_name^{tree}")";
+}
+
 # recurse_deps CMD NAME [BRANCHPATH...]
 # Recursively eval CMD on all dependencies of NAME.
 # CMD can refer to $_name for queried branch name,
@@ -116,7 +148,12 @@ recurse_deps()
        if has_remote "top-bases/$_name"; then
                echo "refs/remotes/$base_remote/top-bases/$_name" >>"$_depsfile"
        fi
-       git cat-file blob "$_name:.topdeps" >>"$_depsfile"
+
+       # if the branch was annihilated, there exists no .topdeps file
+       if ! branch_annihilated "$_name"; then
+               #TODO: handle nonexisting .topdeps?
+               git cat-file blob "$_name:.topdeps" >>"$_depsfile";
+       fi;
 
        _ret=0
        while read _dep; do
@@ -209,6 +246,12 @@ switch_to_base()
 do_help()
 {
        if [ -z "$1" ] ; then
+               # This is currently invoked in all kinds of circumstances,
+               # including when the user made a usage error. Should we end up
+               # providing more than a short help message, then we should
+               # differentiate.
+               # Petr's comment: http://marc.info/?l=git&m=122718711327376&w=2
+
                ## Build available commands list for help output
 
                cmds=
@@ -222,7 +265,7 @@ do_help()
                        sep="|"
                done
 
-               echo "TopGit v0.5 - A different patch queue manager"
+               echo "TopGit v$TG_VERSION - A different patch queue manager"
                echo "Usage: tg [-r REMOTE] ($cmds|help) ..."
        elif [ -r "@cmddir@"/tg-$1 ] ; then
                @cmddir@/tg-$1 -h || :
@@ -232,9 +275,49 @@ do_help()
                fi
        else
                echo "`basename $0`: no help for $1" 1>&2
+               do_help
+               exit 1
        fi
 }
 
+## Pager stuff
+
+# isatty FD
+isatty()
+{
+       test -t $1
+}
+
+# setup_pager
+# Spawn pager process and redirect the rest of our output to it
+setup_pager()
+{
+       isatty 1 || return 0
+
+       # TG_PAGER = GIT_PAGER | PAGER | less
+       # NOTE: GIT_PAGER='' is significant
+       TG_PAGER=${GIT_PAGER-${PAGER-less}}
+
+       [ -z "$TG_PAGER"  -o  "$TG_PAGER" = "cat" ]  && return 0
+
+
+       # now spawn pager
+       export LESS=${LESS:-FRSX}       # as in pager.c:pager_preexec()
+
+       _pager_fifo_dir="$(mktemp -t -d tg-pager-fifo.XXXXXX)"
+       _pager_fifo="$_pager_fifo_dir/0"
+       mkfifo -m 600 "$_pager_fifo"
+
+       "$TG_PAGER" < "$_pager_fifo" &
+       exec > "$_pager_fifo"           # dup2(pager_fifo.in, 1)
+
+       # this is needed so e.g. `git diff` will still colorize it's output if
+       # requested in ~/.gitconfig with color.diff=auto
+       export GIT_PAGER_IN_USE=1
+
+       # atexit(close(1); wait pager)
+       trap "exec >&-; rm \"$_pager_fifo\"; rmdir \"$_pager_fifo_dir\"; wait" EXIT
+}
 
 ## Startup
 
@@ -261,7 +344,13 @@ setup_hook "pre-commit"
 [ -z "$tg__include" ] || return 0
 
 if [ "$1" = "-r" ]; then
-       shift; base_remote="$1"; shift
+       shift
+       if [ -z "$1" ]; then
+               echo "Option -r requires an argument." >&2
+               do_help
+               exit 1
+       fi
+       base_remote="$1"; shift
        tg="$tg -r $base_remote"
 fi
 
@@ -279,7 +368,10 @@ help|--help|-h)
 *)
        [ -r "@cmddir@"/tg-$cmd ] || {
                echo "Unknown subcommand: $cmd" >&2
+               do_help
                exit 1
        }
        . "@cmddir@"/tg-$cmd;;
 esac
+
+# vim:noet