cmd-list.sh: Using built-ins to calculate the length and get the substring.
[guilt/mob.git] / guilt-push
blob8c16628c07414280b02ae04450f1120698c87ae9
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 USAGE="[ -f ] [-a | --all | -n <num> | <patchname>]"
7 . `dirname $0`/guilt
9 abort_flag="abort"
11 while [ $# -gt 0 ]; do
12 case "$1" in
13 -f)
14 abort_flag=""
16 -a|--all)
17 all=t
19 -n)
20 num=t
23 break
25 esac
26 shift
27 done
30 if [ $# -gt 1 ]; then
31 usage
34 # "guilt-push" or "guilt-push foo"
35 if [ -z "$all" ] && [ $# -gt 1 ]; then
36 usage
39 # "guilt-push -n foo"
40 if [ ! -z "$num" ]; then
41 if [ $# -gt 1 ] || [ $# -eq 0 ]; then
42 usage
46 # "guilt-push -a"
47 if [ ! -z "$all" ] && [ $# -gt 0 ]; then
48 usage
51 patch="$1"
52 [ ! -z "$all" ] && patch="-a"
54 if [ "$patch" = "-a" ]; then
55 # we are supposed to push all patches, get the last one out of
56 # series
58 eidx=`get_series | wc -l`
59 if [ $eidx -eq 0 ]; then
60 die "There are no patches to push."
62 elif [ ! -z "$num" ]; then
63 # we are supposed to pop a set number of patches
65 [ "$patch" -lt 0 ] && die "Invalid number of patches to push."
67 eidx=`get_series | wc -l`
69 # calculate end index from current
70 tidx=`wc -l < $applied`
71 tidx=`expr $tidx + $patch`
73 # clamp to minimum
74 [ $tidx -lt $eidx ] && eidx=$tidx
76 elif [ -z "$patch" ]; then
77 # we are supposed to push only the next patch onto the stack
79 eidx=`wc -l < $applied`
80 eidx=`expr $eidx + 1`
81 else
82 # we're supposed to push only up to a patch, make sure the patch is
83 # in the series
85 eidx=`get_series | grep -ne "^$patch\$" | cut -d: -f1`
86 if [ -z "$eidx" ]; then
87 die "Patch $patch is not in the series or is guarded."
91 # make sure that there are no unapplied changes
92 if ! must_commit_first; then
93 die "Uncommited changes detected. Refresh first."
96 # now, find the starting patch
97 sidx=`wc -l < $applied`
98 sidx=`expr $sidx + 1`
100 # do we actually have to push anything?
101 if [ "$sidx" -gt "$eidx" ]; then
102 if [ "$sidx" -le "`get_series | wc -l`" ]; then
103 disp "Patch is already applied."
104 else
105 disp "File series fully applied, ends at patch `get_series | tail -n 1`"
107 exit 0
110 get_series | sed -n -e "${sidx},${eidx}p" | while read p
112 disp "Applying patch..$p"
113 if [ ! -f "$GUILT_DIR/$branch/$p" ]; then
114 die "Patch $p does not exist. Aborting."
117 push_patch "$p" $abort_flag
119 # bail if necessary
120 if [ $? -eq 0 ]; then
121 disp "Patch applied."
122 elif [ -z "$abort_flag" ]; then
123 die "Patch applied with rejects. Fix it up, and refresh."
124 else
125 die "To force apply this patch, use 'guilt push -f'"
127 done