X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=topgit.git;a=blobdiff_plain;f=tg.sh;fp=tg.sh;h=6282250ee0d4e63fcca583a52cd47fc39aefc53e;hp=258f8ce4fd6e2e145300bf85c81f869786222e7b;hb=6e92736dd815e29c3715362970380f5e9a759a11;hpb=54b04c5351928dd6d6f4c55110866ba59c9bfe2b diff --git a/tg.sh b/tg.sh index 258f8ce..6282250 100644 --- a/tg.sh +++ b/tg.sh @@ -3,6 +3,7 @@ # (c) Petr Baudis 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 @@ -209,6 +231,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 +250,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,6 +260,8 @@ do_help() fi else echo "`basename $0`: no help for $1" 1>&2 + do_help + exit 1 fi } @@ -258,6 +288,45 @@ should_do_help() test -z "$prev" } +## 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 [ -d "@cmddir@" ] || @@ -286,7 +355,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 @@ -301,7 +376,10 @@ case "$cmd" in *) [ -r "@cmddir@"/tg-$cmd ] || { echo "Unknown subcommand: $cmd" >&2 + do_help exit 1 } . "@cmddir@"/tg-$cmd;; esac + +# vim:noet