push_patch: look at diff stats instead of number of lines in patch
[guilt.git] / guilt
blobc162832cc0c5ed9a76a6781601b0cb9b5316f7ed
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.17"
7 GUILT_NAME="Strange Brew"
9 if [ `basename $0` = "guilt" ]; then
10 # being run as standalone
12 # by default, we shouldn't fail
13 fail=0
15 if [ $# -ne 0 ]; then
16 # take first arg, and try to execute it
18 cmd="$1"
19 dir=`dirname $0`
21 if [ ! -x "$dir/guilt-$cmd" ]; then
22 echo "Command $cmd not found" >&2
23 echo "" >&2
24 fail=1
25 else
26 shift
27 exec "$dir/guilt-$cmd" "$@"
29 # this is not reached because of the exec
30 echo "Exec failed! Something is terribly wrong!" >&2
31 exit 1
35 # no args passed or invalid command entered, just output help summary
37 echo "Guilt v$GUILT_VERSION"
38 echo ""
39 echo "Pick a command:"
40 for x in `dirname $0`/guilt-*; do
41 [ -x $x ] && echo -e "\t`basename $x`"
42 done
44 echo ""
45 echo "Example:"
46 echo -e "\tguilt-push"
47 echo "or"
48 echo -e "\tguilt push"
50 # now, let's exit
51 exit $fail
54 ########
57 # Library goodies
60 function print_usage
62 echo "$USAGE" >&2
65 function find_git_dir
67 local d=`git-rev-parse --git-dir`
69 if [ $? -ne 0 -o -z "$d" ]; then
70 echo "Not a git repository" >&2
71 return 1
74 echo "$d"
77 function get_branch
79 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
82 function verify_branch
84 local b=$branch
86 [ ! -d "$GIT_DIR/patches" ] &&
87 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
88 return 1
89 [ ! -d "$GIT_DIR/patches/$b" ] &&
90 echo "Branch $b is not initialized, try guilt-init" >&2 &&
91 return 1
92 [ ! -f "$GIT_DIR/patches/$b/series" ] &&
93 echo "Branch $b does not have a series file" >&2 &&
94 return 1
95 [ ! -f "$GIT_DIR/patches/$b/status" ] &&
96 echo "Branch $b does not have a status file" >&2 &&
97 return 1
98 [ -f "$GIT_DIR/patches/$b/applied" ] &&
99 echo "Warning: Branch $b has 'applied' file - guilt is not compatible with stgit" >&2 &&
100 return 1
102 return 0
105 function get_top
107 tail -1 $GUILT_DIR/$branch/status | cut -d: -f 2-
110 function get_prev
112 local n=`wc -l < $GUILT_DIR/$branch/status`
113 local n=`expr $n - 1`
115 local idx=0
116 for p in `cat $GUILT_DIR/$branch/status`; do
117 idx=`expr $idx + 1`
118 [ $idx -lt $n ] && continue
119 [ $idx -gt $n ] && break
121 echo $p
122 done
125 function get_series
127 # ignore all lines matching:
128 # - empty lines
129 # - whitespace only
130 # - optional whitespace followed by '#' followed by more
131 # optional whitespace
132 grep -ve '^[[:space:]]*\(#.*\)*$' < $series
135 # usage: index_update_magic
136 function index_update_magic
138 while read l; do
139 fil=`echo $l | cut -d: -f 2`
140 git-update-index --add --remove "$fil"
141 done
144 # usage: do_get_header patchfile
145 function do_get_header
147 # The complexity arises from the fact that we want to ignore the
148 # From line and the empty line after it if it exists
150 # 2nd line skips the From line
151 # 3rd line skips the empty line right after a From line
152 do_get_full_header $1 | awk '
153 BEGIN{skip=0}
154 /^From:/{skip=1; next}
155 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
156 {print $0}
157 END{}
161 # usage: do_get_full_header patchfile
162 function do_get_full_header
164 # 2nd line checks for the begining of a patch
165 # 3rd line outputs the line if it didn't get pruned by the above rules
166 cat $1 | awk '
167 BEGIN{ok=1}
168 /^(diff|---)/{ok=0}
169 (ok==1){print $0}
170 END{}
174 # usage: pop_patch
175 function pop_patch
177 pop_many_patches HEAD^ 1
180 # usage: pop_many_patches <commitish> <number of patches>
181 function pop_many_patches
183 cd $TOP_DIR
185 git-reset --hard $1 > /dev/null
186 head -n -$2 < $applied > $applied.tmp
187 mv $applied{.tmp,}
189 cd - 2>&1 >/dev/null
192 # usage: push_patch patchname
193 function push_patch
195 local p="$GUILT_DIR/$branch/$1"
196 local pname="$1"
198 local bail=0
200 cd $TOP_DIR
202 # apply the patch if and only if there is something to apply
203 if [ `git-apply --numstat $p | wc -l` -gt 0 ]; then
204 git-apply -C$guilt_push_diff_context \
205 --reject $p > /dev/null 2> /tmp/guilt.log.$$
206 bail=$?
208 [ $bail -ne 0 ] && cat /tmp/guilt.log.$$ >&2
210 ( git-apply --numstat $p | awk '{print "changed:" $3}';
211 #git-apply --summary $p | awk '
212 # /^ (create|delete)/{print $1 ":" $4}
213 # /^ mode change/{print "mode:" $6}'
214 )| index_update_magic
217 # grab a commit message out of the patch
218 do_get_header $p > /tmp/guilt.msg.$$
220 # make a default commit message if patch doesn't contain one
221 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
223 # extract a From line from the patch header, and set
224 # GIT_AUTHOR_{NAME,EMAIL}
225 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
226 if [ ! -z "$author_str" ]; then
227 local backup_author_name="$GIT_AUTHOR_NAME"
228 local backup_author_email="$GIT_AUTHOR_EMAIL"
229 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
230 GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
232 if [ -z "$GIT_AUTHOR_NAME" ]; then
233 GIT_AUTHOR_NAME=" "
236 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
239 # commit
240 local treeish=`git-write-tree`
241 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
242 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
244 # mark patch as applied
245 echo "$commitish:$pname" >> $applied
247 # restore original GIT_AUTHOR_{NAME,EMAIL}
248 if [ ! -z "$author_str" ]; then
249 if [ ! -z "$backup_author_name" ]; then
250 export GIT_AUTHOR_NAME="$backup_author_name"
251 else
252 unset GIT_AUTHOR_NAME
255 if [ ! -z "$backup_author_name" ]; then
256 export GIT_AUTHOR_EMAIL="$backup_author_email"
257 else
258 unset GIT_AUTHOR_EMAIL
262 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
264 cd - 2>&1 >/dev/null
266 return $bail
269 # usage: must_commit_first
270 function must_commit_first
272 [ `git-diff-files | wc -l` -eq 0 ]
273 return $?
276 # usage: refresh_patch patchname
277 function refresh_patch
279 local p="$GUILT_DIR/$branch/$1"
281 cd $TOP_DIR
283 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
285 # get the patch header
286 do_get_full_header $p > /tmp/guilt.diff.$$
288 # get the new patch
289 git-diff HEAD^ >> /tmp/guilt.diff.$$
291 # move the new patch in
292 mv $p $p.prev
293 mv /tmp/guilt.diff.$$ $p
295 cd - 2>&1 >/dev/null
297 # drop the currently applied patch, pop_patch does it's own cd
298 # $TOP_DIR
299 pop_patch
301 # push_patch does it's own cd $TOP_DIR
302 push_patch $1
305 # Some constants
307 # used for: git-apply -C <val>
308 guilt_push_diff_context=1
311 # The following gets run every time this file is source'd
314 GIT_DIR=`find_git_dir`
315 [ $? -ne 0 ] && exit 1
317 TOP_DIR=`git-rev-parse --show-cdup`
318 if [ -z "$TOP_DIR" ]; then
319 TOP_DIR="./"
322 GUILT_DIR="$GIT_DIR/patches"
324 branch=`get_branch`
326 # most of the time we want to verify that the repo's branch has been
327 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
328 # we must avoid the checks
329 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
330 verify_branch || exit 1
333 # very useful files
334 series="$GUILT_DIR/$branch/series"
335 applied="$GUILT_DIR/$branch/status"
337 USAGE="Usage: `basename $0`"