Commit | Line | Data |
---|---|---|
cdd071ff YD |
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: | |
cdd071ff YD |
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 | ||
70661cb7 YD |
15 | helptext="Usage: $(basename $0) [--refs] [<branches>|--all] [-- <gitk args>]" |
16 | ||
cdd071ff YD |
17 | usage() |
18 | { | |
70661cb7 | 19 | echo >&2 "$helptext" |
cdd071ff YD |
20 | exit 1 |
21 | } | |
22 | ||
23 | allbranches=0 | |
ab2e0574 | 24 | refsonly=0 |
70661cb7 | 25 | branches='' |
ab2e0574 YD |
26 | while [ "$#" -gt 0 ]; do |
27 | case "$1" in | |
28 | --refs) refsonly=1 ;; | |
29 | --all) allbranches=1 ;; | |
70661cb7 YD |
30 | --help) echo "$helptext"; exit 0 ;; |
31 | --) shift; break ;; | |
ab2e0574 | 32 | --*) usage ;; |
70661cb7 | 33 | *) branches="$branches $1" ;; |
ab2e0574 YD |
34 | esac |
35 | shift | |
36 | done | |
70661cb7 | 37 | # Now any remaining stuff in $@ are additional options for gitk |
cdd071ff | 38 | |
70661cb7 | 39 | if [ $allbranches = 1 ] && [ "$branches" != "" ]; then |
cdd071ff YD |
40 | usage |
41 | fi | |
42 | ||
04b44217 | 43 | GIT_DIR=$(git rev-parse --git-dir) |
cdd071ff YD |
44 | GIT_DIR_SPKIPLEN=$(printf "$GIT_DIR/X" | wc -c) |
45 | ||
46 | refdirs='' | |
47 | if [ $allbranches = 1 ]; then | |
48 | refdirs="$GIT_DIR/refs" | |
49 | else | |
70661cb7 YD |
50 | # default to current branch |
51 | if [ "$branches" == "" ]; then | |
52 | branches="$(stg branch)" | |
53 | fi | |
54 | if [ "$branches" == "" ]; then | |
55 | echo >&2 "ERROR: cannot find current branch." | |
56 | exit 1 | |
cdd071ff YD |
57 | fi |
58 | ||
70661cb7 YD |
59 | # expand patches for each named branch |
60 | for b in $branches; do | |
cdd071ff YD |
61 | if [ -e "$GIT_DIR/refs/patches/$b" ]; then |
62 | # StGIT branch: show all patches | |
63 | refdirs="$refdirs $GIT_DIR/refs/heads/$b $GIT_DIR/refs/patches/$b" | |
64 | elif [ -e "$GIT_DIR/refs/heads/$b" ]; then | |
65 | # other GIT branch | |
66 | refdirs="$refdirs $GIT_DIR/refs/heads/$b" | |
04b44217 | 67 | elif [ $(git for-each-ref "refs/$b" | wc -l) != 0 ]; then |
cdd071ff | 68 | # other ref |
04b44217 | 69 | refdirs="$refdirs $(git for-each-ref --format="$GIT_DIR/%(refname)" "refs/$b")" |
cdd071ff YD |
70 | else |
71 | echo >&2 "ERROR: no such ref '$b'" | |
72 | usage | |
73 | fi | |
74 | done | |
75 | fi | |
76 | ||
ab2e0574 YD |
77 | printrefs() |
78 | { | |
79 | find $refdirs -type f -not -name '*.log' | cut -c${GIT_DIR_SPKIPLEN}- | |
80 | } | |
81 | ||
82 | if [ $refsonly = 1 ]; then | |
83 | printrefs | |
84 | elif grep -q -- --argscmd $(which gitk); then | |
85 | # This gitk supports --argscmd. | |
86 | # Let's use a hack to pass --all, which was consumed during command-line parsing | |
87 | if [ $allbranches = 1 ]; then | |
70661cb7 | 88 | gitk --argscmd="$0 --refs --all" "$@" |
ab2e0574 | 89 | else |
70661cb7 | 90 | gitk --argscmd="$0 --refs $branches" "$@" |
ab2e0574 YD |
91 | fi |
92 | else | |
93 | # This gitk does not support --argscmd, just compute refs onces | |
70661cb7 | 94 | gitk $(printrefs) "$@" |
ab2e0574 | 95 | fi |