chiark / gitweb /
6ddcfb1c321342eb6657eb2979dcd66f150abeab
[stgit] / contrib / stg-gitk
1 #!/bin/sh
2 set -e
3
4 # stg-gitk - helper script to graphically display an StGIT stack
5
6 # Displays given branches and stacks, without getting disturbed by
7 # patch logs.
8
9 # LIMITATIONS:
10 # - no support for spaces in branch names
11
12 # Copyright (c) 2007 Yann Dirson <ydirson@altern.org>
13 # Subject to the GNU GPL, version 2.
14
15 usage()
16 {
17     echo "Usage: $(basename $0) [<branches>|--all]"
18     exit 1
19 }
20
21 allbranches=0
22 refsonly=0
23 while [ "$#" -gt 0 ]; do
24     case "$1" in
25         --refs) refsonly=1 ;;
26         --all) allbranches=1 ;;
27         --*) usage ;;
28         *) break ;;
29     esac
30     shift
31 done
32
33 if [ $allbranches = 1 ] && [ "$#" -gt 0 ]; then
34     usage
35 fi
36
37 GIT_DIR=$(git-rev-parse --git-dir)
38 GIT_DIR_SPKIPLEN=$(printf "$GIT_DIR/X" | wc -c)
39
40 refdirs=''
41 if [ $allbranches = 1 ]; then
42     refdirs="$GIT_DIR/refs"
43 else
44     if [ "$#" = 0 ]; then
45         set -- "$(stg branch)"
46     fi
47
48     for b in "$@"; do
49         if [ -e "$GIT_DIR/refs/patches/$b" ]; then
50             # StGIT branch: show all patches
51             refdirs="$refdirs $GIT_DIR/refs/heads/$b $GIT_DIR/refs/patches/$b"
52         elif [ -e "$GIT_DIR/refs/heads/$b" ]; then
53             # other GIT branch
54             refdirs="$refdirs $GIT_DIR/refs/heads/$b"
55         elif [ $(git-for-each-ref "refs/$b" | wc -l) != 0 ]; then
56             # other ref
57             refdirs="$refdirs $(git-for-each-ref --format="$GIT_DIR/%(refname)" "refs/$b")"
58         else
59             echo >&2 "ERROR: no such ref '$b'"
60             usage
61         fi
62     done
63 fi
64
65 printrefs()
66 {
67     find $refdirs -type f -not -name '*.log' | cut -c${GIT_DIR_SPKIPLEN}-
68 }
69
70 if [ $refsonly = 1 ]; then
71     printrefs
72 elif grep -q -- --argscmd $(which gitk); then
73     # This gitk supports --argscmd.
74     # Let's use a hack to pass --all, which was consumed during command-line parsing
75     if [ $allbranches = 1 ]; then
76         gitk --argscmd="$0 --refs --all"
77     else
78         gitk --argscmd="$0 --refs $*"
79     fi
80 else
81     # This gitk does not support --argscmd, just compute refs onces
82     gitk $(printrefs)
83 fi