[PATCH] get rid of "cat: write error: Broken pipe" error message
[guilt.git] / guilt-pop
blob77d2c8801a7a86bbf11fd69311040bee7588fad3
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2011
6 USAGE="[-f] [-a | --all | -n <num> | <patchname>]"
7 if [ -z "$GUILT_VERSION" ]; then
8 echo "Invoking `basename $0` directly is no longer supported." >&2
9 exit 1
12 _main() {
14 while [ $# -gt 0 ]; do
15 case "$1" in
16 -f)
17 force=t
19 -a|--all)
20 all=t
22 -n)
23 num=t
26 break
28 esac
29 shift
30 done
32 # "guilt-pop" or "guilt-pop foo" or "guilt-pop -n foo"
33 if [ -z "$all" ] && [ $# -gt 1 ]; then
34 usage
37 # "guilt-pop -n foo"
38 if [ ! -z "$num" ]; then
39 if [ $# -gt 1 ] || [ $# -eq 0 ]; then
40 usage
44 # "guilt-pop -a"
45 if [ ! -z "$all" ] && [ $# -gt 0 ]; then
46 usage
49 patch="$1"
50 [ ! -z "$all" ] && patch="-a"
52 if [ ! -s "$applied" ]; then
53 disp "No patches applied."
54 exit 0
55 elif [ "$patch" = "-a" ]; then
56 # we are supposed to pop all patches
58 sidx=`wc -l < $applied`
59 eidx=0
60 elif [ ! -z "$num" ]; then
61 # we are supposed to pop a set number of patches
63 [ "$patch" -lt 0 ] && die "Invalid number of patches to pop."
65 sidx=`wc -l < $applied`
66 eidx=`expr $sidx - $patch`
68 # catch underflow
69 [ $eidx -lt 0 ] && eidx=0
70 [ $eidx -eq $sidx ] && die "No patches requested to be removed."
71 elif [ -z "$patch" ]; then
72 # we are supposed to pop only the current patch on the stack
74 sidx=`wc -l < $applied`
75 eidx=`expr $sidx - 1`
76 else
77 # we're supposed to pop only up to a patch, make sure the patch is
78 # in the series
80 eidx=`cat $applied | grep -ne "^$patch$" | cut -d: -f 1`
81 if [ -z "$eidx" ]; then
82 die "Patch $patch is not in the series/is not applied"
85 eidx=`expr $eidx - 1`
86 sidx=`wc -l < $applied`
89 # make sure that there are no unapplied changes
90 # if we are forcing the pop, reset first
91 if [ ! -z "$force" ]; then
92 git reset --hard
93 elif ! must_commit_first; then
94 die "Uncommited changes detected. Refresh first."
97 l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < $applied`
99 pop_many_patches `git rev-parse refs/patches/$branch/$l^` `expr $sidx - $eidx`
101 p=`get_top`
102 [ ! -z "$p" ] && disp "Now at $p." || disp "All patches popped."