doc: git doesn't use git-foo invocations.
[guilt.git] / guilt-pop
blob191313ed9160ddb06dd9d8a8070f11453e75e987
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2013
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 # Treat "guilt pop" as "guilt pop -n 1".
53 if [ -z "$patch" ]; then
54 patch=1
55 num=t
58 if [ ! -s "$applied" ]; then
59 disp "No patches applied."
60 if [ "$patch" = "-a" ]; then
61 exit 0
62 else
63 exit 1
65 elif [ "$patch" = "-a" ]; then
66 # we are supposed to pop all patches
68 sidx=`wc -l < "$applied"`
69 eidx=0
70 elif [ ! -z "$num" ]; then
71 # we are supposed to pop a set number of patches
73 [ "$patch" -lt 0 ] && die "Invalid number of patches to pop."
75 sidx=`wc -l < "$applied"`
76 eidx=`expr $sidx - $patch`
78 # catch underflow
79 [ $eidx -lt 0 ] && eidx=0
80 [ $eidx -eq $sidx ] && die "No patches requested to be removed."
81 else
82 # we're supposed to pop only up to a patch, make sure the patch is
83 # in the series
85 eidx=`cat "$applied" | grep -ne "^$patch$" | cut -d: -f 1`
86 if [ -z "$eidx" ]; then
87 die "Patch $patch is not in the series/is not applied"
90 eidx=`expr $eidx - 1`
91 sidx=`wc -l < "$applied"`
94 # make sure that there are no unapplied changes
95 # if we are forcing the pop, reset first
96 if [ ! -z "$force" ]; then
97 git reset --hard
98 elif ! must_commit_first; then
99 die "Uncommited changes detected. Refresh first."
102 l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < "$applied"`
104 pop_many_patches `git rev-parse refs/patches/$branch/$l^` `expr $sidx - $eidx`
106 p=`get_top`
107 [ ! -z "$p" ] && disp "Now at $p." || disp "All patches popped."