doc: git doesn't use git-foo invocations.
[guilt.git] / guilt-push
blob39c125e518979de5866bd4f1f6a8ab960a14fa38
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2013
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 # Treat "guilt push" as "guilt push -n 1".
60 if [ -z "$patch" ]; then
61 patch=1
62 num=t
65 if [ "$patch" = "-a" ]; then
66 # we are supposed to push all patches, get the last one out of
67 # series
69 eidx=`get_series | wc -l`
70 if [ $eidx -eq 0 ]; then
71 die "There are no patches to push."
73 elif [ ! -z "$num" ]; then
74 # we are supposed to push a set number of patches
76 [ "$patch" -lt 0 ] && die "Invalid number of patches to push."
78 eidx=`get_series | wc -l`
80 # calculate end index from current
81 tidx=`wc -l < "$applied"`
82 tidx=`expr $tidx + $patch`
84 # clamp to minimum
85 [ $tidx -lt $eidx ] && eidx=$tidx
87 else
88 # we're supposed to push only up to a patch, make sure the patch is
89 # in the series
91 eidx=`get_series | grep -ne "^$patch\$" | cut -d: -f1`
92 if [ -z "$eidx" ]; then
93 die "Patch $patch is not in the series or is guarded."
97 # make sure that there are no unapplied changes
98 if ! must_commit_first; then
99 die "Uncommited changes detected. Refresh first."
102 # now, find the starting patch
103 sidx=`wc -l < "$applied"`
104 sidx=`expr $sidx + 1`
106 # do we actually have to push anything?
107 if [ "$sidx" -gt "$eidx" ]; then
108 if [ "$sidx" -le "`get_series | wc -l`" ]; then
109 disp "Patch is already applied."
110 else
111 disp "File series fully applied, ends at patch `get_series | tail -n 1`"
113 if [ -n "$all" ]; then
114 exit 0
115 else
116 exit 1
120 get_series | sed -n -e "${sidx},${eidx}p" | while read p
122 disp "Applying patch..$p"
123 if [ ! -f "$GUILT_DIR/$branch/$p" ]; then
124 die "Patch $p does not exist. Aborting."
127 push_patch "$p" $abort_flag
129 # bail if necessary
130 if [ $? -eq 0 ]; then
131 disp "Patch applied."
132 elif [ -z "$abort_flag" ]; then
133 die "Patch applied with rejects. Fix it up, and refresh."
134 else
135 die "To force apply this patch, use 'guilt push -f'"
137 done