update
[rrq/reviewtool.git] / git-show-i
1 #!/bin/zsh
2 #
3 # Show differences between $1 and $2 with selection
4
5 TMP=/tmp/git-show-i-$$
6 mkdir -p $TMP
7 function cleanup() {
8     rm -rf $TMP
9     trap "" 0
10     exit
11 }
12 trap "cleanup" 0 2 15
13
14 git diff --no-prefix $* 2>/dev/null | \
15     csplit -z -b %04d -f $TMP/hunk# - "/^diff --git/" "{*}" > /dev/null
16
17 if [ ! -f $TMP/hunk#0000 ] ; then
18     read 'x?Push enter'
19     exit 0
20 fi
21
22 LIST=( ${(f)"$(head -qn1 $TMP/* 2>/dev/null | awk '{print "<s>" $3;}')"} )
23
24 if [ -z "$LIST" ] ; then
25     read 'x?Nothing to show. Push enter'
26     exit 0
27 fi
28
29 # translate a given selection index, if for a submodule, into the
30 # commit ids for the submodule
31 function submodule_delta() {
32     # Determine which diff-hunk is selected
33     HUNK="$(grep -lF "diff --git $1 " $TMP/* )"
34     # If the hunk ends with "+Subproject commit ", then it's a sub project
35     tail -n 2 $HUNK | grep "^.Subproject commit " | sed 's/.* //'
36 }
37
38 PROJECT="$(git rev-parse --show-toplevel| sed 's/.*\///')"
39 if [ -n "$FOCUS" ] ; then
40     # point at the focused item
41     LAST=${$(echo "${(F)LIST}" | grep -nF "<s>$FOCUS")%%:*}
42 fi
43 [ -z "$LAST" ] && LAST=1
44 while true ; do
45     X="$(iselect -f -n "git-show-i" -K -t "$PROJECT: commit $1" -p $LAST \
46           $LIST "<s>Done" )"
47     [ -z "$X" ] && break
48     LAST=${$(echo "${(F)LIST}" | grep -nF "<s>${X#*:}")%%:*}
49     #read "x?$LAST $X"
50     case "${X%%:*}" in
51         RETURN|KEY_RIGHT)
52             [ "${X#*:}" = Done ] && exit 0
53             # If the selection is a sub module, then use git-show-i
54             # recursively for the submodule versions
55             SUBS=( $(submodule_delta "${X#*:}" ) )
56             if [ -z "$SUBS" ] ; then
57                 #git meld $* -- "${X#*:}"
58                 git-differ $* -- "${X#*:}"
59             else
60                 ( cd ${X#*:} ; $0 $SUBS )
61             fi
62             continue
63             ;;
64     esac
65 done