chiark / gitweb /
README: clearify note describing short-comings of tg log
[topgit.git] / tg.sh
diff --git a/tg.sh b/tg.sh
index b848f7863ae6f482dd7d37d4df9ebd07e91dc509..f1b323b7cbe543994f373523ab2b84b814fd0adc 100644 (file)
--- a/tg.sh
+++ b/tg.sh
@@ -14,7 +14,7 @@ info()
 
 die()
 {
-       info "fatal: $*"
+       info "fatal: $*" >&2
        exit 1
 }
 
@@ -25,20 +25,67 @@ cat_file()
        arg="$1"
        case "$arg" in
        '(w):'*)
-               arg=$(echo "$arg" | tail --bytes=+5)
-               cat "$arg"
-               return
+               cat "${arg#(w):}"
                ;;
        '(i):'*)
                # ':file' means cat from index
-               arg=$(echo "$arg" | tail --bytes=+5)
-               git cat-file blob ":$arg"
+               git cat-file blob "${arg#(i)}"
                ;;
        *)
                git cat-file blob "$arg"
+               ;;
        esac
 }
 
+# get tree for the committed topic
+get_tree_()
+{
+       echo "$1"
+}
+
+# get tree for the base
+get_tree_b()
+{
+       echo "refs/top-bases/$1"
+}
+
+# get tree for the index
+get_tree_i()
+{
+       git write-tree
+}
+
+# get tree for the worktree
+get_tree_w()
+{
+       i_tree=$(git write-tree)
+       (
+               # the file for --index-output needs to sit next to the
+               # current index file
+               : ${GIT_INDEX_FILE:="$git_dir/index"}
+               TMP_INDEX="$(mktemp "${GIT_INDEX_FILE}-tg.XXXXXX")"
+               git read-tree -m $i_tree --index-output="$TMP_INDEX" &&
+               GIT_INDEX_FILE="$TMP_INDEX" &&
+               export GIT_INDEX_FILE &&
+               git diff --name-only -z HEAD |
+                       git update-index -z --add --remove --stdin &&
+               git write-tree &&
+               rm -f "$TMP_INDEX"
+       )
+}
+
+# pretty_tree NAME [-b | -i | -w]
+# Output tree ID of a cleaned-up tree without tg's artifacts.
+# NAME will be ignored for -i and -w, but needs to be present
+pretty_tree()
+{
+       name=$1
+       source=${2#?}
+       git ls-tree --full-tree "$(get_tree_$source "$name")" |
+               awk -F '        ' '$2 !~ /^.top/' |
+               git mktree
+}
+
 # setup_hook NAME
 setup_hook()
 {