[PATCH] guilt: improve patch header handling
[guilt/mob.git] / guilt-pop
bloba520dcc73c6f9f551524fae4ba6e323ea9f054d7
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 USAGE="[-f] [-a | --all | -n <num> | <patchname>]"
7 . `dirname $0`/guilt
9 while [ $# -gt 0 ]; do
10 case "$1" in
11 -f)
12 force=t
14 -a|--all)
15 all=t
17 -n)
18 num=t
21 break
23 esac
24 shift
25 done
27 # "guilt-pop" or "guilt-pop foo" or "guilt-pop -n foo"
28 if [ -z "$all" ] && [ $# -gt 1 ]; then
29 usage
32 # "guilt-pop -n foo"
33 if [ ! -z "$num" ]; then
34 if [ $# -gt 1 ] || [ $# -eq 0 ]; then
35 usage
39 # "guilt-pop -a"
40 if [ ! -z "$all" ] && [ $# -gt 0 ]; then
41 usage
44 patch="$1"
45 [ ! -z "$all" ] && patch="-a"
47 if [ ! -s "$applied" ]; then
48 disp "No patches applied."
49 exit 0
50 elif [ "$patch" = "-a" ]; then
51 # we are supposed to pop all patches
53 sidx=`wc -l < $applied`
54 eidx=0
55 elif [ ! -z "$num" ]; then
56 # we are supposed to pop a set number of patches
58 [ "$patch" -lt 0 ] && die "Invalid number of patches to pop."
60 sidx=`wc -l < $applied`
61 eidx=`expr $sidx - $patch`
63 # catch underflow
64 [ $eidx -lt 0 ] && eidx=0
65 [ $eidx -eq $sidx ] && die "No patches requested to be removed."
66 elif [ -z "$patch" ]; then
67 # we are supposed to pop only the current patch on the stack
69 sidx=`wc -l < $applied`
70 eidx=`expr $sidx - 1`
71 else
72 # we're supposed to pop only up to a patch, make sure the patch is
73 # in the series
75 eidx=`cat $applied | grep -ne "^$patch$" | cut -d: -f 1`
76 if [ -z "$eidx" ]; then
77 die "Patch $patch is not in the series/is not applied"
80 eidx=`expr $eidx - 1`
81 sidx=`wc -l < $applied`
84 # make sure that there are no unapplied changes
85 # if we are forcing the pop, reset first
86 if [ ! -z "$force" ]; then
87 git reset --hard
88 elif ! must_commit_first; then
89 die "Uncommited changes detected. Refresh first."
92 l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < $applied`
94 pop_many_patches `git rev-parse refs/patches/$branch/$l^` `expr $sidx - $eidx`
96 p=`get_top`
97 [ ! -z "$p" ] && disp "Now at $p." || disp "All patches popped."