push_patch: propagate return code to caller
[guilt.git] / guilt
blobf67bfb5d2493f93f751f41aa75ab52fb53b8bb42
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION="0.26"
7 GUILT_NAME="Los"
9 # If the first argument is one of the below, display the man page instead of
10 # the rather silly and mostly useless usage string
11 case $1 in
12 -h|--h|--he|--hel|--help)
13 shift
14 exec "guilt-help" "$@"
15 exit
17 -V|--ver|--versi|--versio|--version)
18 echo "Guilt version $GUILT_VERSION"
19 exit
21 esac
23 # we change directories ourselves
24 SUBDIRECTORY_OK=1
26 . git-sh-setup
29 # Git version check
31 gitver=`git --version | cut -d' ' -f3`
32 case "$gitver" in
33 1.5.*) ;; # git-config
34 *) die "Unsupported version of git ($gitver)" ;;
35 esac
38 # Shell library
41 # echo -e is a bashism, fallback to /bin/echo if the builtin does not supports it
42 echo()
44 /bin/echo "$@"
47 noerr()
49 "$@" 2>/dev/null
52 silent()
54 "$@" >/dev/null 2>/dev/null
57 ########
59 guilt_commands()
61 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f -perm /111 | sed -e "s/.*\\/`basename $0`-//"
64 if [ "`basename $0`" = "guilt" ]; then
65 # being run as standalone
67 # by default, we shouldn't fail
68 cmd=
70 if [ $# -ne 0 ]; then
71 # take first arg, and try to execute it
73 arg="$1"
74 dir=`dirname $0`
76 if [ -x "$dir/guilt-$arg" ]; then
77 cmd=$arg
78 else
79 # might be a short handed
80 for command in $(guilt_commands); do
81 case $command in
82 $arg*)
83 if [ -x "$dir/guilt-$command" ]; then
84 cmd=$command
87 esac
88 done
90 if [ -n "$cmd" ]; then
91 shift
92 exec "$dir/guilt-$cmd" "$@"
94 # this is not reached because of the exec
95 die "Exec failed! Something is terribly wrong!"
96 else
97 echo "Command $arg not found" >&2
98 echo "" >&2
102 # no args passed or invalid command entered, just output help summary
104 echo "Guilt v$GUILT_VERSION"
105 echo ""
106 echo "Pick a command:"
107 guilt_commands | sort | column | column -t | sed -e 's/^/\t/'
109 echo ""
110 echo "Example:"
111 echo -e "\tguilt-push"
112 echo "or"
113 echo -e "\tguilt push"
115 # now, let's exit
116 exit 1
119 ########
122 # Library goodies
125 # usage: valid_patchname <patchname>
126 valid_patchname()
128 case "$1" in
129 /*|./*|../*|*/./*|*/../*|*/.|*/..|*/)
130 return 1;;
132 return 0;;
133 esac
136 get_branch()
138 git-symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
141 verify_branch()
143 [ ! -d "$GIT_DIR/patches" ] &&
144 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
145 return 1
146 [ ! -d "$GIT_DIR/patches/$branch" ] &&
147 echo "Branch $branch is not initialized, try guilt-init" >&2 &&
148 return 1
149 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
150 echo "Branch $branch does not have a series file" >&2 &&
151 return 1
152 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
153 echo "Branch $branch does not have a status file" >&2 &&
154 return 1
155 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
156 echo "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit" >&2 &&
157 return 1
159 return 0
162 get_top()
164 tail -1 "$GUILT_DIR/$branch/status" | cut -d: -f 2-
167 get_prev()
169 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
170 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
174 get_series()
176 # ignore all lines matching:
177 # - empty lines
178 # - whitespace only
179 # - optional whitespace followed by '#' followed by more
180 # optional whitespace
181 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
184 # usage: do_make_header <hash>
185 do_make_header()
187 # we should try to work with commit objects only
188 if [ `git-cat-file -t "$1"` != "commit" ]; then
189 echo "Hash $1 is not a commit object" >&2
190 echo "Aborting..." >&2
191 exit 2
194 git-cat-file -p "$1" | awk '
195 BEGIN{headers=1; firstline=1}
196 /^author / && headers {
197 sub(/^author +/, "");
198 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
199 author=$0
201 !headers {
202 print
203 if (firstline) {
204 firstline = 0;
205 print "\nFrom: " author;
208 /^$/ && headers { headers = 0 }
212 # usage: do_get_header patchfile
213 do_get_header()
215 # The complexity arises from the fact that we want to ignore the
216 # From line and the empty line after it if it exists
218 # 2nd line skips the From line
219 # 3rd line skips the empty line right after a From line
220 # 4th line terminates execution when we encounter the diff
221 cat "$1" | awk '
222 BEGIN{skip=0}
223 /^Subject:/ && (NR==1){print substr($0, 10); next}
224 /^From:/{skip=1; next}
225 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
226 /^(diff|---)/{exit}
227 {print $0}
228 END{}
232 # usage: do_get_full_header patchfile
233 do_get_full_header()
235 # 2nd line checks for the begining of a patch
236 # 3rd line outputs the line if it didn't get pruned by the above rules
237 cat "$1" | awk '
238 BEGIN{}
239 /^(diff|---)/{exit}
240 {print $0}
241 END{}
245 # usage: assert_head_check
246 assert_head_check()
248 if ! head_check "`tail -1 < "$applied" | cut -d: -f 1`"; then
249 die "aborting..."
253 # usage: head_check <expected hash>
254 head_check()
256 # make sure we're not doing funky things to commits that don't
257 # belong to us
258 # if the expected hash is empty, just return
259 [ -z "$1" ] && return 0
261 if [ "`git show-ref -s "refs/heads/$branch"`" != "$1" ]; then
262 echo "Expected HEAD commit $1" >&2
263 echo " got `cat "$GIT_DIR/refs/heads/$branch"`" >&2
264 return 1
266 return 0
269 # usage: series_insert_patch <patchname>
270 series_insert_patch()
272 awk -v top="`get_top`" -v new="$1" \
273 'BEGIN{if (top == "") print new;}
275 print $0;
276 if (top != "" && top == $0) print new;
277 }' "$series" > "$series.tmp"
278 mv "$series.tmp" "$series"
281 # usage: series_remove_patch <patchname>
282 series_remove_patch()
284 grep -v "^$1\$" < "$series" > "$series.tmp"
285 mv "$series.tmp" "$series"
288 # usage: series_rename_patch <oldname> <newname>
289 series_rename_patch()
291 awk -v old="$1" -v new="$2" \
292 '{ if ($0 == old) print new; else print $0 }' \
293 "$series" > "$series.tmp"
295 mv "$series.tmp" "$series"
298 # Beware! This is one of the few (only?) places where we modify the applied
299 # file directly
301 # usage: applied_rename_patch <oldname> <newname>
302 applied_rename_patch()
304 awk -v old="$1" -v new="$2" \
305 'BEGIN{FS=":"}
306 { if ($1 ~ /^[0-9a-f]*$/ && length($1) == 40 && substr($0, 42) == old)
307 print substr($0, 0, 41) new;
308 else
309 print;
310 }' "$applied" > "$applied.tmp"
312 mv "$applied.tmp" "$applied"
315 # usage: pop_many_patches <commitish> <number of patches>
316 pop_many_patches()
318 assert_head_check
321 cd "$TOP_DIR"
323 git-reset --hard "$1" > /dev/null
324 head -n "-$2" < "$applied" > "$applied.tmp"
325 mv "$applied.tmp" "$applied"
328 # update references to top, bottom, and base
329 update_stack_tags
332 # usage: pop_all_patches
333 pop_all_patches()
335 pop_many_patches \
336 `head -1 "$applied" | cut -d: -f1`^ \
337 `wc -l < "$applied"`
340 # usage: update_stack_tags
341 update_stack_tags()
343 # bail if autotagging is not enabled
344 if [ $autotag -eq 0 ]; then
345 return 0
348 if [ -s "$applied" ]; then
349 # there are patches applied, therefore we must get the top,
350 # bottom and base hashes, and update the tags
352 mkdir -p `dirname "$GIT_DIR/refs/tags/${branch}_top"`
353 git-rev-parse HEAD > "$GIT_DIR/refs/tags/${branch}_top"
354 head -1 < $applied | cut -d: -f1 > "$GIT_DIR/refs/tags/${branch}_bottom"
355 git-rev-parse $(head -1 < $applied | cut -d: -f1)^ > "$GIT_DIR/refs/tags/${branch}_base"
356 else
357 # there are no patches applied, therefore we must remove the
358 # tags to old top, bottom, and base
360 rm -f "$GIT_DIR/refs/tags/${branch}_top"
361 rm -f "$GIT_DIR/refs/tags/${branch}_bottom"
362 rm -f "$GIT_DIR/refs/tags/${branch}_base"
366 # usage: push_patch patchname [bail_action]
367 push_patch()
369 __push_patch_bail=0
372 p="$GUILT_DIR/$branch/$1"
373 pname="$1"
374 bail_action="$2"
375 reject="--reject"
377 assert_head_check
378 cd "$TOP_DIR"
380 # apply the patch if and only if there is something to apply
381 if [ `git-apply --numstat "$p" | wc -l` -gt 0 ]; then
382 if [ "$bail_action" = abort ]; then
383 reject=""
385 git-apply -C$guilt_push_diff_context --index \
386 $reject "$p" > /dev/null 2> /tmp/guilt.log.$$
387 __push_patch_bail=$?
389 if [ $__push_patch_bail -ne 0 ]; then
390 cat /tmp/guilt.log.$$ >&2
391 if [ "$bail_action" = "abort" ]; then
392 rm -f /tmp/guilt.log.$$ /tmp/guilt.msg.$$
393 return $__push_patch_bail
398 # grab a commit message out of the patch
399 do_get_header "$p" > /tmp/guilt.msg.$$
401 # make a default commit message if patch doesn't contain one
402 [ ! -s /tmp/guilt.msg.$$ ] && echo "patch $pname" > /tmp/guilt.msg.$$
404 # extract a From line from the patch header, and set
405 # GIT_AUTHOR_{NAME,EMAIL}
406 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q }; /^(diff|---)/ q' "$p"`
407 if [ ! -z "$author_str" ]; then
408 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
409 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
410 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
412 export GIT_AUTHOR_DATE="`stat -c %y "$p"`"
413 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
415 # commit
416 treeish=`git-write-tree`
417 commitish=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
418 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
420 # mark patch as applied
421 echo "$commitish:$pname" >> $applied
424 # sub-shell funky-ness
425 __push_patch_bail=$?
427 # update references to top, bottom, and base of the stack
428 update_stack_tags
430 rm -f /tmp/guilt.msg.$$ /tmp/guilt.log.$$
431 return $__push_patch_bail
434 # usage: must_commit_first
435 must_commit_first()
437 [ `git-diff-files | wc -l` -eq 0 ]
438 return $?
441 # usage: fold_patch patchname
442 fold_patch()
444 set -- "$1" "`get_top`"
446 assert_head_check
448 push_patch "$1"
450 __refresh_patch "$2" HEAD^^ 2 "" ""
452 series_remove_patch "$1"
455 # usage: refresh_patch patchname gengitdiff incldiffstat
456 refresh_patch()
458 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
461 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
462 # incldiffstat
463 __refresh_patch()
465 assert_head_check
468 cd "$TOP_DIR"
469 p="$GUILT_DIR/$branch/$1"
471 git-diff-files --name-only | (while read n; do git-update-index "$n" ; done)
473 # get the patch header
474 do_get_full_header "$p" > /tmp/guilt.diff.$$
476 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
478 if [ ! -z "$5" ]; then
480 echo "---"
481 git-diff --stat $diffopts "$2"
482 echo ""
483 ) >> /tmp/guilt.diff.$$
486 # get the new patch
487 git-diff $diffopts "$2" >> /tmp/guilt.diff.$$
489 # move the new patch in
490 mv "$p" "$p~"
491 mv /tmp/guilt.diff.$$ $p
494 # drop the currently applied patch, pop_many_patches does it's own
495 # cd $TOP_DIR
496 pop_many_patches "$2" "$3"
498 # push_patch does it's own cd $TOP_DIR
499 push_patch "$1"
502 # usage: munge_hash_range <hash range>
504 # this means:
505 # <hash> - one commit
506 # <hash>.. - hash until head (excludes hash, includes head)
507 # ..<hash> - until hash (includes hash)
508 # <hash1>..<hash2> - from hash to hash (inclusive)
510 # The output of this function is suitable to be passed to git-rev-list
511 munge_hash_range()
513 case "$1" in
514 *..*..*|*\ *)
515 # double .. or space is illegal
516 return 1;;
517 ..*)
518 # e.g., "..v0.10"
519 echo ${1#..};;
520 *..)
521 # e.g., "v0.19.."
522 echo ${1%..}..HEAD;;
523 *..*)
524 # e.g., "v0.19-rc1..v0.19"
525 echo ${1%%..*}..${1#*..};;
527 # e.g., "v0.19"
528 echo $1^..$1;;
529 *) # empty
530 return 1;;
531 esac
532 return 0
535 # usage: guilt_hook <hook name> <args....>
536 guilt_hook()
538 __hookname="$1"
539 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
541 shift
543 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
544 return $?
548 # Some constants
551 # used for: git-apply -C <val>
552 guilt_push_diff_context=1
555 # Parse any part of .git/config that belongs to us
558 # autotag?
559 autotag=`git-config guilt.autotag`
560 [ -z "$autotag" ] && autotag=1
563 # The following gets run every time this file is source'd
566 TOP_DIR=`git-rev-parse --show-cdup`
567 if [ -z "$TOP_DIR" ]; then
568 TOP_DIR="./"
571 GUILT_DIR="$GIT_DIR/patches"
573 branch=`get_branch`
575 # most of the time we want to verify that the repo's branch has been
576 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
577 # we must avoid the checks
578 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
579 verify_branch || exit 1
582 # very useful files
583 series="$GUILT_DIR/$branch/series"
584 applied="$GUILT_DIR/$branch/status"
586 # determine an editor to use for anything interactive (fall back to vi)
587 editor="vi"
588 [ ! -z "$EDITOR" ] && editor="$EDITOR"
590 # determine a pager to use for anything interactive (fall back to more)
591 pager="more"
592 [ ! -z "$PAGER" ] && pager="$PAGER"