[PATCH] Make "guilt push" match "quilt push" when the patch doesn't apply
[guilt.git] / guilt
blobf5a55fff772d86c0bfa51f377946d3178e81a198
1 #!/bin/bash
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.20"
7 GUILT_NAME="Buddy Holly"
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: do_make_header <hash>
136 function do_make_header
138 # which revision do we want to work with?
139 local rev=$1
141 # we should try to work with commit objects only
142 if [ `git-cat-file -t $rev` != "commit" ]; then
143 echo "Hash $rev is not a commit object" >&2
144 echo "Aborting..." >&2
145 exit 2
148 # get the author line from the commit object
149 local author=`git-cat-file -p $rev | grep -e '^author ' | head -1`
151 # strip the timestamp & '^author ' string
152 author=`echo $author | sed -e 's/^author //' -e 's/ [0-9]* [+-]*[0-9][0-9]*$//'`
154 git-cat-file -p $rev | awk "
155 BEGIN{ok=0}
156 (ok==1){print \$0; print \"\nFrom: $author\"; ok=2; next}
157 (ok==2){print \$0}
158 /^\$/ && (ok==0){ok=1}
162 # usage: do_get_header patchfile
163 function do_get_header
165 # The complexity arises from the fact that we want to ignore the
166 # From line and the empty line after it if it exists
168 # 2nd line skips the From line
169 # 3rd line skips the empty line right after a From line
170 do_get_full_header $1 | awk '
171 BEGIN{skip=0}
172 /^From:/{skip=1; next}
173 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
174 {print $0}
175 END{}
179 # usage: do_get_full_header patchfile
180 function do_get_full_header
182 # 2nd line checks for the begining of a patch
183 # 3rd line outputs the line if it didn't get pruned by the above rules
184 cat $1 | awk '
185 BEGIN{ok=1}
186 /^(diff|---)/{ok=0}
187 (ok==1){print $0}
188 END{}
192 # usage: assert_head_check
193 function assert_head_check
195 local eh=`tail -1 < $applied | cut -d: -f 1`
197 if ! head_check $eh; then
198 echo "aborting..." >&2
199 exit 1
202 return 0
205 # usage: head_check <expected hash>
206 function head_check
208 # make sure we're not doing funky things to commits that don't
209 # belong to us
210 local ch=`cat $GIT_DIR/refs/heads/$branch`
212 # if the expected hash is empty, just return
213 [ -z "$1" ] && return 0
215 if [ "$ch" != "$1" ]; then
216 echo "Expected HEAD commit $1" >&2
217 echo " got $ch" >&2
218 return 1
220 return 0
223 # usage: series_insert_patch <patchname>
224 function series_insert_patch
226 local top=`get_top`
228 if [ ! -z "$top" ]; then
229 sed -i -e "s,^$top\$,$top\n$1," $series
230 else
231 echo "$1" > $series.tmp
232 cat $series >> $series.tmp
233 mv $series.tmp $series
237 # usage: pop_patch
238 function pop_patch
240 pop_many_patches HEAD^ 1
243 # usage: pop_many_patches <commitish> <number of patches>
244 function pop_many_patches
246 assert_head_check
248 cd $TOP_DIR
250 git-reset --hard $1 > /dev/null
251 head -n -$2 < $applied > $applied.tmp
252 mv $applied{.tmp,}
254 cd - 2>&1 >/dev/null
257 # usage: push_patch patchname
258 function push_patch
260 local p="$GUILT_DIR/$branch/$1"
261 local pname="$1"
262 local bail_action="$2"
264 local bail=0
265 local reject="--reject"
267 assert_head_check
269 cd $TOP_DIR
271 # apply the patch if and only if there is something to apply
272 if [ `git-apply --numstat $p | wc -l` -gt 0 ]; then
273 if [ "$bail_action" = abort ]; then
274 reject=""
276 git-apply -C$guilt_push_diff_context \
277 $reject $p > /dev/null 2> /tmp/guilt.log.$$
278 bail=$?
280 if [ $bail -ne 0 ]; then
281 cat /tmp/guilt.log.$$ >&2
282 if [ "$bail_action" = abort ]; then
283 return $bail
287 # FIXME: Path munging is being done, we need to convince
288 # git-apply to just give us list of files with \0 as a
289 # delimiter, and pass -z to git-update-index
290 git-apply --numstat $p | cut -f 3- | git-update-index --add --remove --stdin
293 # grab a commit message out of the patch
294 do_get_header $p > /tmp/guilt.msg.$$
296 # make a default commit message if patch doesn't contain one
297 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
299 # extract a From line from the patch header, and set
300 # GIT_AUTHOR_{NAME,EMAIL}
301 local author_str=`cat $p | grep -e '^From: ' | sed -e 's/^From: //'`
302 if [ ! -z "$author_str" ]; then
303 local backup_author_name="$GIT_AUTHOR_NAME"
304 local backup_author_email="$GIT_AUTHOR_EMAIL"
305 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
306 GIT_AUTHOR_EMAIL=`echo $author_str | sed -e 's/[^<]*//'`
308 if [ -z "$GIT_AUTHOR_NAME" ]; then
309 GIT_AUTHOR_NAME=" "
312 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
314 local backup_author_date="$GIT_AUTHOR_DATE"
315 local backup_committer_date="$GIT_COMMITTER_DATE"
316 export GIT_AUTHOR_DATE=`stat -c %y $p`
317 export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE
319 # commit
320 local treeish=`git-write-tree`
321 local commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
322 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
324 # mark patch as applied
325 echo "$commitish:$pname" >> $applied
327 # restore original GIT_AUTHOR_{NAME,EMAIL}
328 if [ ! -z "$author_str" ]; then
329 if [ ! -z "$backup_author_name" ]; then
330 export GIT_AUTHOR_NAME="$backup_author_name"
331 else
332 unset GIT_AUTHOR_NAME
335 if [ ! -z "$backup_author_name" ]; then
336 export GIT_AUTHOR_EMAIL="$backup_author_email"
337 else
338 unset GIT_AUTHOR_EMAIL
341 if [ ! -z "$backup_author_date" ]; then
342 export GIT_AUTHOR_DATE="$backup_author_date"
343 else
344 unset GIT_AUTHOR_DATE
346 if [ ! -z "$backup_committer_date" ]; then
347 export GIT_COMMITTER_DATE="$backup_committer_date"
348 else
349 unset GIT_COMMITTER_DATE
352 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
354 cd - 2>&1 >/dev/null
356 return $bail
359 # usage: must_commit_first
360 function must_commit_first
362 [ `git-diff-files | wc -l` -eq 0 ]
363 return $?
366 # usage: refresh_patch patchname
367 function refresh_patch
369 local p="$GUILT_DIR/$branch/$1"
371 assert_head_check
373 cd $TOP_DIR
375 git-diff-files --name-only | (while read n; do git-update-index $n ; done)
377 # get the patch header
378 do_get_full_header $p > /tmp/guilt.diff.$$
380 # get the new patch
381 git-diff HEAD^ >> /tmp/guilt.diff.$$
383 # move the new patch in
384 mv $p $p.prev
385 mv /tmp/guilt.diff.$$ $p
387 cd - 2>&1 >/dev/null
389 # drop the currently applied patch, pop_patch does it's own cd
390 # $TOP_DIR
391 pop_patch
393 # push_patch does it's own cd $TOP_DIR
394 push_patch $1
397 # usage: munge_hash_range <hash range>
399 # this means:
400 # <hash> - one commit
401 # <hash>.. - hash until head (excludes hash, includes head)
402 # ..<hash> - until hash (includes hash)
403 # <hash1>..<hash2> - from hash to hash (inclusive)
405 # The output of this function is suitable to be passed to git-rev-list
406 function munge_hash_range
408 local l=`echo "$1" | sed -e 's/\.\./ /'`
410 local h1=`echo "$l" | cut -s -d' ' -f 1`
411 local h2=`echo "$l" | cut -s -d' ' -f 2`
413 if [ -z "$h1" -a -z "$h2" ]; then
414 # e.g., "v0.19"
415 echo "$l^..$l"
416 elif [ -z "$h1" ]; then
417 # e.g., "..v0.10"
418 echo "$h2"
419 elif [ -z "$h2" ]; then
420 # e.g., "v0.19.."
421 echo "$h1..HEAD"
422 else
423 # e.g., "v0.19-rc1..v0.19"
424 echo "$h1..$h2"
428 # Some constants
430 # used for: git-apply -C <val>
431 guilt_push_diff_context=1
434 # The following gets run every time this file is source'd
437 GIT_DIR=`find_git_dir`
438 [ $? -ne 0 ] && exit 1
440 TOP_DIR=`git-rev-parse --show-cdup`
441 if [ -z "$TOP_DIR" ]; then
442 TOP_DIR="./"
445 GUILT_DIR="$GIT_DIR/patches"
447 branch=`get_branch`
449 # most of the time we want to verify that the repo's branch has been
450 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
451 # we must avoid the checks
452 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
453 verify_branch || exit 1
456 # very useful files
457 series="$GUILT_DIR/$branch/series"
458 applied="$GUILT_DIR/$branch/status"
460 USAGE="Usage: `basename $0`"
462 # determine an editor to use for anything interactive (fall back to vi)
463 editor="vi"
464 [ ! -z "$EDITOR" ] && editor="$EDITOR"
466 # determine a pager to use for anything interactive (fall back to more)
467 pager="more"
468 [ -z "$pager" ] && pager="$PAGER"