repair: allow status file upgrade
[guilt.git] / guilt
blob32846228e57774d013be37507b413f3a57726f4c
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 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"
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_patch patchfile
213 do_get_patch()
215 cat "$1" | awk '
216 BEGIN{}
217 /^(diff |---)/,/END{}/
221 # usage: do_get_header patchfile
222 do_get_header()
224 # The complexity arises from the fact that we want to ignore the
225 # From line and the empty line after it if it exists
227 # 2nd line skips the From line
228 # 3rd line skips the empty line right after a From line
229 # 4th line terminates execution when we encounter the diff
230 cat "$1" | awk '
231 BEGIN{skip=0}
232 /^Subject:/ && (NR==1){print substr($0, 10); next}
233 /^From:/{skip=1; next}
234 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
235 /^(diff |---)/{exit}
236 {print $0}
237 END{}
241 # usage: do_get_full_header patchfile
242 do_get_full_header()
244 # 2nd line checks for the begining of a patch
245 # 3rd line outputs the line if it didn't get pruned by the above rules
246 cat "$1" | awk '
247 BEGIN{}
248 /^(diff |---)/{exit}
249 {print $0}
250 END{}
254 # usage: assert_head_check
255 assert_head_check()
257 if ! head_check refs/patches/$branch/`get_top`; then
258 die "aborting..."
262 # usage: head_check <expected hash>
263 head_check()
265 # make sure we're not doing funky things to commits that don't
266 # belong to us
268 case "$1" in
270 # the expected hash is empty
271 return 0 ;;
272 refs/patches/$branch/)
273 # the expected hash is an invalid rev
274 return 0 ;;
275 esac
277 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
278 echo "Expected HEAD commit $1" >&2
279 echo " got `git rev-parse refs/heads/$branch`" >&2
280 return 1
282 return 0
285 # usage: series_insert_patch <patchname>
286 series_insert_patch()
288 awk -v top="`get_top`" -v new="$1" \
289 'BEGIN{if (top == "") print new;}
291 print $0;
292 if (top != "" && top == $0) print new;
293 }' "$series" > "$series.tmp"
294 mv "$series.tmp" "$series"
297 # usage: series_remove_patch <patchname>
298 series_remove_patch()
300 grep -v "^$1\$" < "$series" > "$series.tmp"
301 mv "$series.tmp" "$series"
304 # usage: series_rename_patch <oldname> <newname>
305 series_rename_patch()
307 awk -v old="$1" -v new="$2" \
308 '{ if ($0 == old) print new; else print $0 }' \
309 "$series" > "$series.tmp"
311 mv "$series.tmp" "$series"
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 git update-ref -d "refs/patches/$branch/$pname" `git rev-parse "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: update_stack_tags
369 update_stack_tags()
371 # bail if autotagging is not enabled
372 if [ $autotag -eq 0 ]; then
373 return 0
376 if [ -s "$applied" ]; then
377 # there are patches applied, therefore we must get the top,
378 # bottom and base hashes, and update the tags
380 git update-ref "refs/tags/${branch}_top" `git rev-parse HEAD`
381 git update-ref "refs/tags/${branch}_bottom" `git rev-parse refs/patches/$branch/$(head -1 < $applied)`
382 git update-ref "refs/tags/${branch}_base" `git rev-parse refs/patches/$branch/$(head -1 < $applied)^`
383 else
384 # there are no patches applied, therefore we must remove the
385 # tags to old top, bottom, and base
387 git update-ref -d "refs/tags/${branch}_top" \
388 `git rev-parse "refs/tags/${branch}_top"`
389 git update-ref -d "refs/tags/${branch}_bottom" \
390 `git rev-parse "refs/tags/${branch}_bottom"`
391 git update-ref -d "refs/tags/${branch}_base" \
392 `git rev-parse "refs/tags/${branch}_base"`
396 # usage: push_patch patchname [bail_action]
397 push_patch()
399 __push_patch_bail=0
402 TMP_LOG=`get_tmp_file log`
403 TMP_MSG=`get_tmp_file msg`
405 p="$GUILT_DIR/$branch/$1"
406 pname="$1"
407 bail_action="$2"
408 reject="--reject"
410 assert_head_check
411 cd "$TOP_DIR"
413 # apply the patch if and only if there is something to apply
414 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
415 if [ "$bail_action" = abort ]; then
416 reject=""
418 git apply -C$guilt_push_diff_context --index \
419 $reject "$p" > /dev/null 2> "$TMP_LOG"
420 __push_patch_bail=$?
422 if [ $__push_patch_bail -ne 0 ]; then
423 cat "$TMP_LOG" >&2
424 if [ "$bail_action" = "abort" ]; then
425 rm -f "$TMP_LOG" "$TMP_MSG"
426 return $__push_patch_bail
431 # grab a commit message out of the patch
432 do_get_header "$p" > "$TMP_MSG"
434 # make a default commit message if patch doesn't contain one
435 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
437 # extract a From line from the patch header, and set
438 # GIT_AUTHOR_{NAME,EMAIL}
439 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q }; /^(diff |---)/ q' "$p"`
440 if [ ! -z "$author_str" ]; then
441 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
442 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
443 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
446 # must strip nano-second part otherwise git gets very
447 # confused, and makes up strange timestamps from the past
448 # (chances are it decides to interpret it as a unix
449 # timestamp).
450 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
451 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/'`"
452 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
454 # commit
455 treeish=`git write-tree`
456 commitish=`git commit-tree $treeish -p HEAD < "$TMP_MSG"`
457 git update-ref HEAD $commitish
459 # mark patch as applied
460 git update-ref "refs/patches/$branch/$pname" $commitish ""
462 echo "$pname" >> $applied
464 rm -f "$TMP_MSG" "$TMP_LOG"
467 # sub-shell funky-ness
468 __push_patch_bail=$?
470 # update references to top, bottom, and base of the stack
471 update_stack_tags
473 return $__push_patch_bail
476 # usage: must_commit_first
477 must_commit_first()
479 [ `git diff-files | wc -l` -eq 0 ]
480 return $?
483 # usage: fold_patch patchname
484 fold_patch()
486 set -- "$1" "`get_top`"
488 assert_head_check
490 push_patch "$1"
492 __refresh_patch "$2" HEAD^^ 2 "" ""
494 series_remove_patch "$1"
497 # usage: refresh_patch patchname gengitdiff incldiffstat
498 refresh_patch()
500 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
503 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
504 # incldiffstat
505 __refresh_patch()
507 assert_head_check
510 TMP_DIFF=`get_tmp_file diff`
512 cd "$TOP_DIR"
513 p="$GUILT_DIR/$branch/$1"
515 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
517 # get the patch header
518 do_get_full_header "$p" > "$TMP_DIFF"
520 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
522 if [ ! -z "$5" ]; then
524 echo "---"
525 git diff --stat $diffopts "$2"
526 echo ""
527 ) >> "$TMP_DIFF"
530 # get the new patch
531 git diff $diffopts "$2" >> "$TMP_DIFF"
533 # move the new patch in
534 mv "$p" "$p~"
535 mv "$TMP_DIFF" $p
538 # drop the currently applied patch, pop_many_patches does it's own
539 # cd $TOP_DIR
540 pop_many_patches "$2" "$3"
542 # push_patch does it's own cd $TOP_DIR
543 push_patch "$1"
546 # usage: munge_hash_range <hash range>
548 # this means:
549 # <hash> - one commit
550 # <hash>.. - hash until head (excludes hash, includes head)
551 # ..<hash> - until hash (includes hash)
552 # <hash1>..<hash2> - from hash to hash (inclusive)
554 # The output of this function is suitable to be passed to "git rev-list"
555 munge_hash_range()
557 case "$1" in
558 *..*..*|*\ *)
559 # double .. or space is illegal
560 return 1;;
561 ..*)
562 # e.g., "..v0.10"
563 echo ${1#..};;
564 *..)
565 # e.g., "v0.19.."
566 echo ${1%..}..HEAD;;
567 *..*)
568 # e.g., "v0.19-rc1..v0.19"
569 echo ${1%%..*}..${1#*..};;
571 # e.g., "v0.19"
572 echo $1^..$1;;
573 *) # empty
574 return 1;;
575 esac
576 return 0
579 # usage: get_tmp_file <prefix>
581 # Get a unique filename and create the file in a non-racy way
582 get_tmp_file()
584 while true; do
585 mktemp "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
586 done
589 # usage: guilt_hook <hook name> <args....>
590 guilt_hook()
592 __hookname="$1"
593 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
595 shift
597 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
598 return $?
602 # Some constants
605 # used for: git apply -C <val>
606 guilt_push_diff_context=1
609 # Parse any part of .git/config that belongs to us
612 # autotag?
613 autotag=`git config guilt.autotag`
614 [ -z "$autotag" ] && autotag=1
617 # The following gets run every time this file is source'd
620 TOP_DIR=`git rev-parse --show-cdup`
621 if [ -z "$TOP_DIR" ]; then
622 TOP_DIR="./"
625 GUILT_DIR="$GIT_DIR/patches"
627 branch=`get_branch`
629 # most of the time we want to verify that the repo's branch has been
630 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
631 # we must avoid the checks
632 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
633 verify_branch || exit 1
636 # very useful files
637 series="$GUILT_DIR/$branch/series"
638 applied="$GUILT_DIR/$branch/status"
640 # determine an editor to use for anything interactive (fall back to vi)
641 editor="vi"
642 [ ! -z "$EDITOR" ] && editor="$EDITOR"
644 # determine a pager to use for anything interactive (fall back to more)
645 pager="more"
646 [ ! -z "$PAGER" ] && pager="$PAGER"