[GUILT 3/6] Handle paths that contain spaces
[guilt.git] / guilt-push
blobd9a8590bd46cd6aece515fdbdf50f1e1a55fc0ab
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2011
6 USAGE="[ -f ] [-a | --all | -n <num> | <patchname>]"
7 if [ -z "$GUILT_VERSION" ]; then
8 echo "Invoking `basename $0` directly is no longer supported." >&2
9 exit 1
12 _main() {
14 abort_flag="abort"
16 while [ $# -gt 0 ]; do
17 case "$1" in
18 -f)
19 abort_flag=""
21 -a|--all)
22 all=t
24 -n)
25 num=t
28 break
30 esac
31 shift
32 done
35 if [ $# -gt 1 ]; then
36 usage
39 # "guilt-push" or "guilt-push foo"
40 if [ -z "$all" ] && [ $# -gt 1 ]; then
41 usage
44 # "guilt-push -n foo"
45 if [ ! -z "$num" ]; then
46 if [ $# -gt 1 ] || [ $# -eq 0 ]; then
47 usage
51 # "guilt-push -a"
52 if [ ! -z "$all" ] && [ $# -gt 0 ]; then
53 usage
56 patch="$1"
57 [ ! -z "$all" ] && patch="-a"
59 if [ "$patch" = "-a" ]; then
60 # we are supposed to push all patches, get the last one out of
61 # series
63 eidx=`get_series | wc -l`
64 if [ $eidx -eq 0 ]; then
65 die "There are no patches to push."
67 elif [ ! -z "$num" ]; then
68 # we are supposed to pop a set number of patches
70 [ "$patch" -lt 0 ] && die "Invalid number of patches to push."
72 eidx=`get_series | wc -l`
74 # calculate end index from current
75 tidx=`wc -l < "$applied"`
76 tidx=`expr $tidx + $patch`
78 # clamp to minimum
79 [ $tidx -lt $eidx ] && eidx=$tidx
81 elif [ -z "$patch" ]; then
82 # we are supposed to push only the next patch onto the stack
84 eidx=`wc -l < "$applied"`
85 eidx=`expr $eidx + 1`
86 else
87 # we're supposed to push only up to a patch, make sure the patch is
88 # in the series
90 eidx=`get_series | grep -ne "^$patch\$" | cut -d: -f1`
91 if [ -z "$eidx" ]; then
92 die "Patch $patch is not in the series or is guarded."
96 # make sure that there are no unapplied changes
97 if ! must_commit_first; then
98 die "Uncommited changes detected. Refresh first."
101 # now, find the starting patch
102 sidx=`wc -l < "$applied"`
103 sidx=`expr $sidx + 1`
105 # do we actually have to push anything?
106 if [ "$sidx" -gt "$eidx" ]; then
107 if [ "$sidx" -le "`get_series | wc -l`" ]; then
108 disp "Patch is already applied."
109 else
110 disp "File series fully applied, ends at patch `get_series | tail -n 1`"
112 exit 0
115 get_series | sed -n -e "${sidx},${eidx}p" | while read p
117 disp "Applying patch..$p"
118 if [ ! -f "$GUILT_DIR/$branch/$p" ]; then
119 die "Patch $p does not exist. Aborting."
122 push_patch "$p" $abort_flag
124 # bail if necessary
125 if [ $? -eq 0 ]; then
126 disp "Patch applied."
127 elif [ -z "$abort_flag" ]; then
128 die "Patch applied with rejects. Fix it up, and refresh."
129 else
130 die "To force apply this patch, use 'guilt push -f'"
132 done