pop: Check if no patches are applied
[guilt.git] / guilt-pop
blob8246292dd0c9d545a7c4f8482c040272d20b14cc
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 USAGE="[-f] [-a | --all | <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
18 break
20 esac
21 shift
22 done
24 # "guilt-pop" or "guilt-pop foo"
25 if [ -z "$all" ] && [ $# -gt 1 ]; then
26 usage
29 # "guilt-pop -a"
30 if [ ! -z "$all" ] && [ $# -gt 0 ]; then
31 usage
34 patch="$1"
35 [ ! -z "$all" ] && patch="-a"
37 if [ ! -s "$applied" ]; then
38 echo "No patches applied."
39 exit 0
40 elif [ "$patch" = "-a" ]; then
41 # we are supposed to pop all patches
43 sidx=`wc -l < $applied`
44 eidx=0
45 elif [ -z "$patch" ]; then
46 # we are supposed to pop only the current patch on the stack
48 sidx=`wc -l < $applied`
49 eidx=`expr $sidx - 1`
50 else
51 # we're supposed to pop only up to a patch, make sure the patch is
52 # in the series
54 eidx=`cat $applied | grep -ne "^[0-9a-f]\{40\}:$patch\$" | cut -d: -f 1`
55 if [ -z "$eidx" ]; then
56 die "Patch $patch is not in the series/is not applied"
59 eidx=`expr $eidx - 1`
60 sidx=`wc -l < $applied`
63 # make sure that there are no unapplied changes
64 # if we are forcing the pop, reset first
65 if [ ! -z "$force" ]; then
66 cd "$TOP_DIR" >&2 >/dev/null
67 git-reset --hard
68 cd - >&2 >/dev/null
69 elif ! must_commit_first; then
70 die "Uncommited changes detected. Refresh first."
73 l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < $applied`
75 pop_many_patches `echo $l | cut -d: -f1`^ `expr $sidx - $eidx`
77 p=`get_top`
78 [ ! -z "$p" ] && echo "Now at $p." || echo "All patches popped."