Tests redirect to err file when grepping stderr
[stgit.git] / contrib / stg-gitk
blobc26a131acc2209e39a6ba4d0271424124bb106dc
1 #!/bin/sh
2 set -e
4 # stg-gitk - helper script to graphically display an StGit stack
6 # Displays given branches and stacks, without getting disturbed by
7 # patch logs.
9 # LIMITATIONS:
10 # - no support for spaces in branch names
12 # Copyright (c) 2007 Yann Dirson <ydirson@altern.org>
13 # Subject to the GNU GPL, version 2.
15 helptext="Usage: $(basename "$0") [--refs] [<branches>|--all] [-- <gitk args>]"
17 usage()
19 echo >&2 "$helptext"
20 exit 1
23 allbranches=0
24 refsonly=0
25 branches=''
26 while [ "$#" -gt 0 ]; do
27 case "$1" in
28 --refs) refsonly=1 ;;
29 --all) allbranches=1 ;;
30 --help) echo "$helptext"; exit 0 ;;
31 --) shift; break ;;
32 --*) usage ;;
33 *) branches="$branches $1" ;;
34 esac
35 shift
36 done
37 # Now any remaining stuff in $@ are additional options for gitk
39 if [ $allbranches = 1 ] && [ "$branches" != "" ]; then
40 usage
43 GIT_DIR=$(git rev-parse --git-dir)
44 GIT_DIR_SKIPLEN=$(printf "%s/X" "$GIT_DIR" | wc -c)
46 refdirs=''
47 if [ $allbranches = 1 ]; then
48 refdirs="$GIT_DIR/refs"
49 else
50 # default to current branch
51 if [ "$branches" = "" ]; then
52 branches="$(stg branch)"
54 if [ "$branches" = "" ]; then
55 echo >&2 "ERROR: cannot find current branch."
56 exit 1
59 # expand patches for each named branch
60 for b in $branches; do
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"
67 elif [ "$(git for-each-ref "refs/$b" | wc -l)" != 0 ]; then
68 # other ref
69 refdirs="$refdirs $(git for-each-ref --format="$GIT_DIR/%(refname)" "refs/$b")"
70 else
71 echo >&2 "ERROR: no such ref '$b'"
72 usage
74 done
77 printrefs()
79 # shellcheck disable=SC2086
80 find $refdirs -type f -not -name '*.log' | cut -c"${GIT_DIR_SKIPLEN}"-
83 if [ $refsonly = 1 ]; then
84 printrefs
85 elif grep -q -- --argscmd "$(command -v gitk)"; then
86 # This gitk supports --argscmd.
87 # Let's use a hack to pass --all, which was consumed during command-line parsing
88 if [ $allbranches = 1 ]; then
89 gitk --argscmd="$0 --refs --all" "$@"
90 else
91 gitk --argscmd="$0 --refs $branches" "$@"
93 else
94 # This gitk does not support --argscmd, just compute refs onces
95 # shellcheck disable=SC2046
96 gitk $(printrefs) "$@"