[PATCH] Remove bashisms - easy commands
[guilt/mob.git] / guilt-pop
blob89ad02104408be18c8c581d6de0aa0a81990d2c6
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 [ "$patch" = "-a" ]; then
38 # we are supposed to pop all patches
40 sidx=`wc -l < $applied`
41 eidx=0
42 elif [ -z "$patch" ]; then
43 # we are supposed to pop only the current patch on the stack
45 sidx=`wc -l < $applied`
46 eidx=`expr $sidx - 1`
47 else
48 # we're supposed to pop only up to a patch, make sure the patch is
49 # in the series
51 eidx=`cat $applied | grep -ne "^[0-9a-f]\{40\}:$patch\$" | cut -d: -f 1`
52 if [ -z "$eidx" ]; then
53 die "Patch $patch is not in the series/is not applied"
56 eidx=`expr $eidx - 1`
57 sidx=`wc -l < $applied`
60 # make sure that there are no unapplied changes
61 # if we are forcing the pop, reset first
62 if [ ! -z "$force" ]; then
63 cd "$TOP_DIR" >&2 >/dev/null
64 git-reset --hard
65 cd - >&2 >/dev/null
66 elif ! must_commit_first; then
67 die "Uncommited changes detected. Refresh first."
70 l=`awk "BEGIN{n=0}(n==$eidx){print \\$0; exit}{n=n+1}END{}" < $applied`
72 pop_many_patches `echo $l | cut -d: -f1`^ `expr $sidx - $eidx`
74 p=`get_top`
75 [ ! -z "$p" ] && echo "Now at $p." || echo "All patches popped."