repair: new command to repair repository state
[guilt.git] / guilt-pop
blob46a34f4a381c9b882f36589b5e91dbf4fc253a6f
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 echo "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 "^[0-9a-f]\{40\}:$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 cd "$TOP_DIR" >&2 >/dev/null
88 git-reset --hard
89 cd - >&2 >/dev/null
90 elif ! must_commit_first; then
91 die "Uncommited changes detected. Refresh first."
94 l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < $applied`
96 pop_many_patches `echo $l | cut -d: -f1`^ `expr $sidx - $eidx`
98 p=`get_top`
99 [ ! -z "$p" ] && echo "Now at $p." || echo "All patches popped."