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
14 while [ $# -gt 0 ]; do
32 # "guilt-pop" or "guilt-pop foo" or "guilt-pop -n foo"
33 if [ -z "$all" ] && [ $# -gt 1 ]; then
38 if [ ! -z "$num" ]; then
39 if [ $# -gt 1 ] ||
[ $# -eq 0 ]; then
45 if [ ! -z "$all" ] && [ $# -gt 0 ]; then
50 [ ! -z "$all" ] && patch="-a"
52 # Treat "guilt pop" as "guilt pop -n 1".
53 if [ -z "$patch" ]; then
58 if [ ! -s "$applied" ]; then
59 disp
"No patches applied."
60 if [ "$patch" = "-a" ]; then
65 elif [ "$patch" = "-a" ]; then
66 # we are supposed to pop all patches
68 sidx
=`wc -l < "$applied"`
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`
79 [ $eidx -lt 0 ] && eidx
=0
80 [ $eidx -eq $sidx ] && die
"No patches requested to be removed."
82 # we're supposed to pop only up to a patch, make sure the patch is
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"
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
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`
107 [ ! -z "$p" ] && disp
"Now at $p." || disp
"All patches popped."