fork: fix regression caused by status format change
[guilt.git] / guilt
blob346b9cbfdac6b8c4441500e5226df44b041c40bd
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
6 GUILT_VERSION="0.29"
7 GUILT_NAME="Bleecker Street"
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" "`basename $0`"
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 die "Patches directory doesn't exist, try guilt-init"
145 [ ! -d "$GIT_DIR/patches/$branch" ] &&
146 die "Branch $branch is not initialized, try guilt-init"
147 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
148 die "Branch $branch does not have a series file"
149 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
150 die "Branch $branch does not have a status file"
151 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
152 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
155 get_top()
157 tail -1 "$GUILT_DIR/$branch/status"
160 get_prev()
162 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
163 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
167 get_series()
169 # ignore all lines matching:
170 # - empty lines
171 # - whitespace only
172 # - optional whitespace followed by '#' followed by more
173 # optional whitespace
174 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
177 # usage: do_make_header <hash>
178 do_make_header()
180 # we should try to work with commit objects only
181 if [ `git cat-file -t "$1"` != "commit" ]; then
182 echo "Hash $1 is not a commit object" >&2
183 echo "Aborting..." >&2
184 exit 2
187 git cat-file -p "$1" | awk '
188 BEGIN{headers=1; firstline=1}
189 /^author / && headers {
190 sub(/^author +/, "");
191 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
192 author=$0
194 !headers {
195 print
196 if (firstline) {
197 firstline = 0;
198 print "\nFrom: " author;
201 /^$/ && headers { headers = 0 }
205 # usage: do_get_patch patchfile
206 do_get_patch()
208 cat "$1" | awk '
209 BEGIN{}
210 /^(diff |---)/,/END{}/
214 # usage: do_get_header patchfile
215 do_get_header()
217 # The complexity arises from the fact that we want to ignore the
218 # From line and the empty line after it if it exists
220 # 2nd line skips the From line
221 # 3rd line skips the empty line right after a From line
222 # 4th line terminates execution when we encounter the diff
223 cat "$1" | awk '
224 BEGIN{skip=0}
225 /^Subject:/ && (NR==1){print substr($0, 10); next}
226 /^From:/{skip=1; next}
227 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
228 /^(diff |---)/{exit}
229 {print $0}
230 END{}
234 # usage: do_get_full_header patchfile
235 do_get_full_header()
237 # 2nd line checks for the begining of a patch
238 # 3rd line outputs the line if it didn't get pruned by the above rules
239 cat "$1" | awk '
240 BEGIN{}
241 /^(diff |---)/{exit}
242 {print $0}
243 END{}
247 # usage: assert_head_check
248 assert_head_check()
250 if ! head_check refs/patches/$branch/`get_top`; then
251 die "aborting..."
255 # usage: head_check <expected hash>
256 head_check()
258 # make sure we're not doing funky things to commits that don't
259 # belong to us
261 case "$1" in
263 # the expected hash is empty
264 return 0 ;;
265 refs/patches/$branch/)
266 # the expected hash is an invalid rev
267 return 0 ;;
268 esac
270 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
271 echo "Expected HEAD commit $1" >&2
272 echo " got `git rev-parse refs/heads/$branch`" >&2
273 return 1
275 return 0
278 # usage: series_insert_patch <patchname>
279 series_insert_patch()
281 awk -v top="`get_top`" -v new="$1" \
282 'BEGIN{if (top == "") print new;}
284 print $0;
285 if (top != "" && top == $0) print new;
286 }' "$series" > "$series.tmp"
287 mv "$series.tmp" "$series"
290 # usage: series_remove_patch <patchname>
291 series_remove_patch()
293 grep -v "^$1\$" < "$series" > "$series.tmp"
294 mv "$series.tmp" "$series"
297 # usage: series_rename_patch <oldname> <newname>
298 series_rename_patch()
300 awk -v old="$1" -v new="$2" \
301 '{ if ($0 == old) print new; else print $0 }' \
302 "$series" > "$series.tmp"
304 mv "$series.tmp" "$series"
307 # usage: series_rename_patch <oldname> <newname>
308 ref_rename_patch()
310 git update-ref "$2" `git rev-parse "$1"`
311 remove_ref "$1"
314 # Beware! This is one of the few (only?) places where we modify the applied
315 # file directly
317 # usage: applied_rename_patch <oldname> <newname>
318 applied_rename_patch()
320 awk -v old="$1" -v new="$2" \
321 'BEGIN{FS=":"}
322 { if ($0 == old)
323 print new;
324 else
325 print;
326 }' "$applied" > "$applied.tmp"
328 mv "$applied.tmp" "$applied"
331 # usage: remove_patch_refs
332 # reads patch names from stdin
333 remove_patch_refs()
335 while read pname; do
336 remove_ref "refs/patches/$branch/$pname"
337 done
340 # usage: pop_many_patches <commitish> <number of patches>
341 pop_many_patches()
343 assert_head_check
346 cd "$TOP_DIR"
348 # remove the patches refs
349 tail -$2 < "$applied" | remove_patch_refs
351 git reset --hard "$1" > /dev/null
352 head -n "-$2" < "$applied" > "$applied.tmp"
353 mv "$applied.tmp" "$applied"
356 # update references to top, bottom, and base
357 update_stack_tags
360 # usage: pop_all_patches
361 pop_all_patches()
363 pop_many_patches \
364 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
365 `wc -l < "$applied"`
368 # usage: remove_ref <refname>
369 remove_ref()
372 # does the ref exist?
373 r=`git show-ref --verify -s "$1" 2> /dev/null`
374 [ $? -ne 0 ] && exit 0
376 # remove it
377 git update-ref -d "$1" "$r"
381 # usage: update_stack_tags [force]
383 # if [force] is non-empty, then do not check for autotagging being enabled,
384 # just assume it is
385 update_stack_tags()
387 # bail if autotagging is not enabled
388 if [ -z "$1" -a $autotag -eq 0 ]; then
389 return 0
392 if [ -s "$applied" ]; then
393 # there are patches applied, therefore we must get the top,
394 # bottom and base hashes, and update the tags
396 git update-ref "refs/tags/${branch}_top" `git rev-parse HEAD`
397 git update-ref "refs/tags/${branch}_bottom" `git rev-parse refs/patches/$branch/$(head -1 < $applied)`
398 git update-ref "refs/tags/${branch}_base" `git rev-parse refs/patches/$branch/$(head -1 < $applied)^`
399 else
400 # there are no patches applied, therefore we must remove the
401 # tags to old top, bottom, and base
403 remove_ref "refs/tags/${branch}_top"
404 remove_ref "refs/tags/${branch}_bottom"
405 remove_ref "refs/tags/${branch}_base"
409 # usage: push_patch patchname [bail_action]
410 push_patch()
412 __push_patch_bail=0
415 TMP_LOG=`get_tmp_file log`
416 TMP_MSG=`get_tmp_file msg`
418 p="$GUILT_DIR/$branch/$1"
419 pname="$1"
420 bail_action="$2"
421 reject="--reject"
423 assert_head_check
424 cd "$TOP_DIR"
426 # apply the patch if and only if there is something to apply
427 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
428 if [ "$bail_action" = abort ]; then
429 reject=""
431 git apply -C$guilt_push_diff_context --index \
432 $reject "$p" > /dev/null 2> "$TMP_LOG"
433 __push_patch_bail=$?
435 if [ $__push_patch_bail -ne 0 ]; then
436 cat "$TMP_LOG" >&2
437 if [ "$bail_action" = "abort" ]; then
438 rm -f "$TMP_LOG" "$TMP_MSG"
439 return $__push_patch_bail
444 # grab a commit message out of the patch
445 do_get_header "$p" > "$TMP_MSG"
447 # make a default commit message if patch doesn't contain one
448 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
450 # extract a From line from the patch header, and set
451 # GIT_AUTHOR_{NAME,EMAIL}
452 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q }; /^(diff |---)/ q' "$p"`
453 if [ ! -z "$author_str" ]; then
454 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
455 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
456 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
459 # must strip nano-second part otherwise git gets very
460 # confused, and makes up strange timestamps from the past
461 # (chances are it decides to interpret it as a unix
462 # timestamp).
463 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
464 s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'`"
465 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
467 # commit
468 treeish=`git write-tree`
469 commitish=`git commit-tree $treeish -p HEAD < "$TMP_MSG"`
470 git update-ref HEAD $commitish
472 # mark patch as applied
473 git update-ref "refs/patches/$branch/$pname" $commitish ""
475 echo "$pname" >> $applied
477 rm -f "$TMP_MSG" "$TMP_LOG"
480 # sub-shell funky-ness
481 __push_patch_bail=$?
483 # update references to top, bottom, and base of the stack
484 update_stack_tags
486 return $__push_patch_bail
489 # usage: must_commit_first
490 must_commit_first()
492 [ `git diff-files | wc -l` -eq 0 ]
493 return $?
496 # usage: fold_patch patchname
497 fold_patch()
499 set -- "$1" "`get_top`"
501 assert_head_check
503 push_patch "$1"
505 __refresh_patch "$2" HEAD^^ 2 "" ""
507 series_remove_patch "$1"
510 # usage: refresh_patch patchname gengitdiff incldiffstat
511 refresh_patch()
513 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
516 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
517 # incldiffstat
518 __refresh_patch()
520 assert_head_check
523 TMP_DIFF=`get_tmp_file diff`
525 cd "$TOP_DIR"
526 p="$GUILT_DIR/$branch/$1"
528 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
530 # get the patch header
531 do_get_full_header "$p" > "$TMP_DIFF"
533 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
535 if [ ! -z "$5" ]; then
537 echo "---"
538 git diff --stat $diffopts "$2"
539 echo ""
540 ) >> "$TMP_DIFF"
543 # get the new patch
544 git diff $diffopts "$2" >> "$TMP_DIFF"
546 # move the new patch in
547 mv "$p" "$p~"
548 mv "$TMP_DIFF" $p
551 # drop the currently applied patch, pop_many_patches does it's own
552 # cd $TOP_DIR
553 pop_many_patches "$2" "$3"
555 # push_patch does it's own cd $TOP_DIR
556 push_patch "$1"
559 # usage: munge_hash_range <hash range>
561 # this means:
562 # <hash> - one commit
563 # <hash>.. - hash until head (excludes hash, includes head)
564 # ..<hash> - until hash (includes hash)
565 # <hash1>..<hash2> - from hash to hash (inclusive)
567 # The output of this function is suitable to be passed to "git rev-list"
568 munge_hash_range()
570 case "$1" in
571 *..*..*|*\ *)
572 # double .. or space is illegal
573 return 1;;
574 ..*)
575 # e.g., "..v0.10"
576 echo ${1#..};;
577 *..)
578 # e.g., "v0.19.."
579 echo ${1%..}..HEAD;;
580 *..*)
581 # e.g., "v0.19-rc1..v0.19"
582 echo ${1%%..*}..${1#*..};;
584 # e.g., "v0.19"
585 echo $1^..$1;;
586 *) # empty
587 return 1;;
588 esac
589 return 0
592 # usage: get_tmp_file <prefix>
594 # Get a unique filename and create the file in a non-racy way
595 get_tmp_file()
597 while true; do
598 mktemp "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
599 done
602 # usage: guilt_hook <hook name> <args....>
603 guilt_hook()
605 __hookname="$1"
606 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
608 shift
610 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
611 return $?
615 # Some constants
618 # used for: git apply -C <val>
619 guilt_push_diff_context=1
622 # Parse any part of .git/config that belongs to us
625 # autotag?
626 autotag=`git config guilt.autotag`
627 [ -z "$autotag" ] && autotag=1
630 # The following gets run every time this file is source'd
633 TOP_DIR=`git rev-parse --show-cdup`
634 if [ -z "$TOP_DIR" ]; then
635 TOP_DIR="./"
638 GUILT_DIR="$GIT_DIR/patches"
640 branch=`get_branch`
642 # most of the time we want to verify that the repo's branch has been
643 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
644 # we must avoid the checks
645 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
646 verify_branch
648 # do not check the status file format (guilt-repair needs this,
649 # otherwise nothing can do what's necessary to bring the repo into a
650 # useable state)
651 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
652 [ -s "$GIT_DIR/patches/$branch/status" ] &&
653 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
654 die "Status file appears to use old format, try guilt-repair --status"
658 # very useful files
659 series="$GUILT_DIR/$branch/series"
660 applied="$GUILT_DIR/$branch/status"
662 # determine an editor to use for anything interactive (fall back to vi)
663 editor="vi"
664 [ ! -z "$EDITOR" ] && editor="$EDITOR"
666 # determine a pager to use for anything interactive (fall back to more)
667 pager="more"
668 [ ! -z "$PAGER" ] && pager="$PAGER"