guilt-fold: merge headers of the 2 patches
[guilt.git] / guilt
blob7eaeefde1b3fbbca7def2f0a8cf2fcd98ddc75b5
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
6 GUILT_VERSION="0.31.2"
7 GUILT_NAME="Ciuleandra"
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 --exec-path)/git-sh-setup"
29 # Git version check
31 gitver=`git --version | cut -d' ' -f3 | sed -e 's/^debian\.//'`
32 case "$gitver" in
33 1.5.*) ;; # git config
34 1.6.*) ;; # git config
35 *) die "Unsupported version of git ($gitver)" ;;
36 esac
39 # Shell library
42 # echo -n is a bashism, use printf instead
43 _disp()
45 printf "%b" "$*"
48 # echo -e is a bashism, use printf instead
49 disp()
51 printf "%b\n" "$*"
54 noerr()
56 "$@" 2>/dev/null
59 silent()
61 "$@" >/dev/null 2>/dev/null
64 ########
66 guilt_commands()
68 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f -perm +111 | sed -e "s/.*\\/`basename $0`-//"
71 if [ "`basename $0`" = "guilt" ]; then
72 # being run as standalone
74 # by default, we shouldn't fail
75 cmd=
77 if [ $# -ne 0 ]; then
78 # take first arg, and try to execute it
80 arg="$1"
81 dir=`dirname $0`
83 if [ -x "$dir/guilt-$arg" ]; then
84 cmd=$arg
85 else
86 # might be a short handed
87 for command in $(guilt_commands); do
88 case $command in
89 $arg*)
90 if [ -x "$dir/guilt-$command" ]; then
91 cmd=$command
94 esac
95 done
97 if [ -n "$cmd" ]; then
98 shift
99 exec "$dir/guilt-$cmd" "$@"
101 # this is not reached because of the exec
102 die "Exec failed! Something is terribly wrong!"
103 else
104 disp "Command $arg not found" >&2
105 disp "" >&2
109 # no args passed or invalid command entered, just output help summary
111 disp "Guilt v$GUILT_VERSION"
112 disp ""
113 disp "Pick a command:"
114 guilt_commands | sort | column | column -t | sed -e 's/^/ /'
116 disp ""
117 disp "Example:"
118 disp "\tguilt-push"
119 disp "or"
120 disp "\tguilt push"
122 # now, let's exit
123 exit 1
126 ########
129 # Library goodies
132 # usage: valid_patchname <patchname>
133 valid_patchname()
135 case "$1" in
136 /*|./*|../*|*/./*|*/../*|*/.|*/..|*/|*\ *|*\ *)
137 return 1;;
139 return 0;;
140 esac
143 get_branch()
145 silent git symbolic-ref HEAD || \
146 die "Working on a detached HEAD is unsupported."
148 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
151 verify_branch()
153 [ ! -d "$GIT_DIR/patches" ] &&
154 die "Patches directory doesn't exist, try guilt-init"
155 [ ! -d "$GIT_DIR/patches/$branch" ] &&
156 die "Branch $branch is not initialized, try guilt-init"
157 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
158 die "Branch $branch does not have a series file"
159 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
160 die "Branch $branch does not have a status file"
161 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
162 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
165 get_top()
167 tail -1 "$GUILT_DIR/$branch/status"
170 get_prev()
172 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
173 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
177 get_full_series()
179 # ignore all lines matching:
180 # - empty lines
181 # - whitespace only
182 # - optional whitespace followed by '#' followed by more
183 # optional whitespace
184 # also remove comments from end of lines
185 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
186 s/[[:space:]]*#.*\$//
190 " $series
193 get_series()
195 get_full_series | while read p; do
196 check_guards "$p" && echo "$p"
197 done
200 # usage: check_guards <patch>
201 # Returns 0 if the patch should be pushed
202 check_guards()
204 get_guards "$1" | while read guard; do
205 case "$guard" in
207 # Push +guard *only if* guard selected
208 silent grep -e "^$guard\$" "$guards_file" || return 1
211 # Push -guard *unless* guard selected
212 silent grep -e "^$guard\$" "$guards_file" && return 1
214 esac
215 done
217 # propagate return from subshell
218 return $?
221 # usage: get_guards <patch>
222 get_guards()
224 sed -n -e "
225 \,^$1[[:space:]]*#, {
226 s,^$1[[:space:]]*,,
227 s,#[^+-]*,,g
230 }" "$series"
233 # usage: set_guards <patch> <guards...>
234 set_guards()
237 p="$1"
238 shift
239 for x in "$@"; do
240 case "$x" in
241 [+-]*)
242 sed -e "s,^$p\([[:space:]].*\)\?\$,$p\1 #$x," "$series" > "$series.tmp"
243 mv "$series.tmp" "$series"
246 echo "'$x' is not a valid guard name" >&2
248 esac
249 done
253 # usage: unset_guards <patch> <guards...>
254 unset_guards()
257 p="$1"
258 shift
259 for x in "$@"; do
260 sed -e "\,^$p[[:space:]], { s/ #$x\([[:space:]].*\)\?$/\1/ }" "$series" > "$series.tmp"
261 mv "$series.tmp" "$series"
262 done
266 # usage: do_make_header <hash>
267 do_make_header()
269 # we should try to work with commit objects only
270 if [ `git cat-file -t "$1"` != "commit" ]; then
271 disp "Hash $1 is not a commit object" >&2
272 disp "Aborting..." >&2
273 exit 2
276 git cat-file -p "$1" | awk '
277 BEGIN{headers=1; firstline=1}
278 /^author / && headers {
279 sub(/^author +/, "");
280 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
281 author=$0
283 !headers {
284 print
285 if (firstline) {
286 firstline = 0;
287 print "\nFrom: " author;
290 /^$/ && headers { headers = 0 }
294 # usage: do_get_patch patchfile
295 do_get_patch()
297 cat "$1" | awk '
298 BEGIN{}
299 /^(diff |---)/,/END{}/
303 # usage: do_get_header patchfile
304 do_get_header()
306 # The complexity arises from the fact that we want to ignore the
307 # From line and the empty line after it if it exists
309 # 2nd line skips the From line
310 # 3rd line skips the empty line right after a From line
311 # 4th line terminates execution when we encounter the diff
312 cat "$1" | awk '
313 BEGIN{skip=0}
314 /^Subject:/ && (NR==1){print substr($0, 10); next}
315 /^From:/{skip=1; next}
316 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
317 /^(diff |---)/{exit}
318 {print $0}
319 END{}
323 # usage: do_get_full_header patchfile
324 do_get_full_header()
326 # 2nd line checks for the begining of a patch
327 # 3rd line outputs the line if it didn't get pruned by the above rules
328 cat "$1" | awk '
329 BEGIN{}
330 /^(diff |---)/{exit}
331 {print $0}
332 END{}
336 # usage: assert_head_check
337 assert_head_check()
339 if ! head_check refs/patches/$branch/`get_top`; then
340 die "aborting..."
344 # usage: head_check <expected hash>
345 head_check()
347 # make sure we're not doing funky things to commits that don't
348 # belong to us
350 case "$1" in
352 # the expected hash is empty
353 return 0 ;;
354 refs/patches/$branch/)
355 # the expected hash is an invalid rev
356 return 0 ;;
357 esac
359 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
360 disp "Expected HEAD commit $1" >&2
361 disp " got `git rev-parse refs/heads/$branch`" >&2
362 return 1
364 return 0
367 # usage: series_insert_patch <patchname>
368 series_insert_patch()
370 awk -v top="`get_top`" -v new="$1" \
371 'BEGIN{if (top == "") print new;}
373 print $0;
374 if (top != "" && top == $0) print new;
375 }' "$series" > "$series.tmp"
376 mv "$series.tmp" "$series"
379 # usage: series_remove_patch <patchname>
380 series_remove_patch()
382 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
383 mv "$series.tmp" "$series"
386 # usage: series_rename_patch <oldname> <newname>
387 series_rename_patch()
389 # Rename the patch, but preserve comments on the line
390 awk -v old="$1" -v new="$2" '
392 if (index($0, old) == 1)
393 print new substr($0, length(old) + 1);
394 else
395 print $0;
396 }' "$series" > "$series.tmp"
398 mv "$series.tmp" "$series"
401 # usage: series_rename_patch <oldpatchname> <newpatchname>
402 ref_rename_patch()
404 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
405 remove_ref "refs/patches/$branch/$1"
408 # Beware! This is one of the few (only?) places where we modify the applied
409 # file directly
411 # usage: applied_rename_patch <oldname> <newname>
412 applied_rename_patch()
414 awk -v old="$1" -v new="$2" \
415 'BEGIN{FS=":"}
416 { if ($0 == old)
417 print new;
418 else
419 print;
420 }' "$applied" > "$applied.tmp"
422 mv "$applied.tmp" "$applied"
425 # usage: remove_patch_refs
426 # reads patch names from stdin
427 remove_patch_refs()
429 while read pname; do
430 remove_ref "refs/patches/$branch/$pname"
431 done
434 # usage: pop_many_patches <commitish> <number of patches>
435 pop_many_patches()
437 assert_head_check
440 cd "$TOP_DIR"
442 # remove the patches refs
443 tail -$2 < "$applied" | remove_patch_refs
445 git reset --hard "$1" > /dev/null
446 head -n "-$2" < "$applied" > "$applied.tmp"
447 mv "$applied.tmp" "$applied"
451 # usage: pop_all_patches
452 pop_all_patches()
454 pop_many_patches \
455 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
456 `wc -l < "$applied"`
459 # usage: remove_ref <refname>
460 remove_ref()
463 # does the ref exist?
464 r=`git show-ref --verify -s "$1" 2> /dev/null`
465 [ $? -ne 0 ] && exit 0
467 # remove it
468 git update-ref -d "$1" "$r"
472 # usage: commit patchname parent
473 commit()
476 TMP_MSG=`get_tmp_file msg`
478 p="$GUILT_DIR/$branch/$1"
479 pname="$1"
480 cd_to_toplevel
482 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
484 # grab a commit message out of the patch
485 do_get_header "$p" > "$TMP_MSG"
487 # make a default commit message if patch doesn't contain one
488 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
490 # extract a From line from the patch header, and set
491 # GIT_AUTHOR_{NAME,EMAIL}
492 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---)/ q' "$p"`
493 if [ ! -z "$author_str" ]; then
494 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
495 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
496 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
499 # must strip nano-second part otherwise git gets very
500 # confused, and makes up strange timestamps from the past
501 # (chances are it decides to interpret it as a unix
502 # timestamp).
503 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
504 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/'`"
505 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
507 # commit
508 treeish=`git write-tree`
509 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
510 git update-ref HEAD $commitish
512 # mark patch as applied
513 git update-ref "refs/patches/$branch/$pname" HEAD
515 rm -f "$TMP_MSG"
519 # usage: push_patch patchname [bail_action]
520 push_patch()
522 __push_patch_bail=0
525 TMP_LOG=`get_tmp_file log`
527 p="$GUILT_DIR/$branch/$1"
528 pname="$1"
529 bail_action="$2"
530 reject="--reject"
532 assert_head_check
533 cd_to_toplevel
535 # apply the patch if and only if there is something to apply
536 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
537 if [ "$bail_action" = abort ]; then
538 reject=""
540 git apply -C$guilt_push_diff_context --index \
541 $reject "$p" > /dev/null 2> "$TMP_LOG"
542 __push_patch_bail=$?
544 if [ $__push_patch_bail -ne 0 ]; then
545 cat "$TMP_LOG" >&2
546 if [ "$bail_action" = "abort" ]; then
547 rm -f "$TMP_LOG" "$TMP_MSG"
548 return $__push_patch_bail
553 commit "$pname" HEAD
555 echo "$pname" >> $applied
557 rm -f "$TMP_LOG"
560 # sub-shell funky-ness
561 __push_patch_bail=$?
563 return $__push_patch_bail
566 # usage: must_commit_first
567 must_commit_first()
569 git update-index --refresh --unmerged -q > /dev/null
570 [ `git diff-files | wc -l` -eq 0 ] || return $?
571 [ `git diff-index HEAD | wc -l` -eq 0 ]
572 return $?
575 # usage: fold_patch patchname
576 fold_patch()
578 set -- "$1" "`get_top`"
580 assert_head_check
582 push_patch "$1"
584 # merge the patch headers
586 pcur="$GUILT_DIR/$branch/$2"
587 pnext="$GUILT_DIR/$branch/$1"
588 TMP_CUR=`get_tmp_file diff-cur`
589 TMP_NEXT=`get_tmp_file diff-next`
590 TMP_DIFF=`get_tmp_file diff`
591 do_get_full_header "$pcur" > "$TMP_CUR"
592 do_get_full_header "$pnext" > "$TMP_NEXT"
593 do_get_patch "$pcur" > "$TMP_DIFF"
595 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
596 *,0)
597 # since the new patch header is empty, we
598 # don't have to do anything
600 0,*)
601 # current is empty; new is not
602 mv "$pcur" "$pcur~"
603 cat "$TMP_NEXT" > "$pcur"
604 cat "$TMP_DIFF" >> "$pcur"
606 *,*)
607 mv "$pcur" "$pcur~"
608 cat "$TMP_CUR" > "$pcur"
609 echo >> "$pcur"
610 echo "Header from folded patch '$1':" >> "$pcur"
611 echo >> "$pcur"
612 cat "$TMP_NEXT" >> "$pcur"
613 cat "$TMP_DIFF" >> "$pcur"
615 esac
617 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
620 __refresh_patch "$2" HEAD^^ 2 "" ""
622 series_remove_patch "$1"
625 # usage: refresh_patch patchname gengitdiff incldiffstat
626 refresh_patch()
628 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
631 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
632 # incldiffstat
633 __refresh_patch()
635 assert_head_check
638 TMP_DIFF=`get_tmp_file diff`
640 cd "$TOP_DIR"
641 p="$GUILT_DIR/$branch/$1"
642 pname="$1"
644 # get the patch header
645 do_get_full_header "$p" > "$TMP_DIFF"
647 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
649 if [ ! -z "$5" ]; then
651 echo "---"
652 git diff --stat $diffopts "$2"
653 echo ""
654 ) >> "$TMP_DIFF"
657 # get the new patch
658 git diff $diffopts "$2" >> "$TMP_DIFF"
660 # move the new patch in
661 mv "$p" "$p~"
662 mv "$TMP_DIFF" $p
664 # commit
665 commit "$pname" "HEAD~$3"
667 # drop folded patches
668 N=`expr "$3" - 1`
670 # remove the patches refs
671 tail -$N < "$applied" | remove_patch_refs
673 head -n "-$N" < "$applied" > "$applied.tmp"
674 mv "$applied.tmp" "$applied"
678 # usage: munge_hash_range <hash range>
680 # this means:
681 # <hash> - one commit
682 # <hash>.. - hash until head (excludes hash, includes head)
683 # ..<hash> - until hash (includes hash)
684 # <hash1>..<hash2> - from hash to hash (inclusive)
686 # The output of this function is suitable to be passed to "git rev-list"
687 munge_hash_range()
689 case "$1" in
690 *..*..*|*\ *)
691 # double .. or space is illegal
692 return 1;;
693 ..*)
694 # e.g., "..v0.10"
695 echo ${1#..};;
696 *..)
697 # e.g., "v0.19.."
698 echo ${1%..}..HEAD;;
699 *..*)
700 # e.g., "v0.19-rc1..v0.19"
701 echo ${1%%..*}..${1#*..};;
703 # e.g., "v0.19"
704 echo $1^..$1;;
705 *) # empty
706 return 1;;
707 esac
708 return 0
711 # usage: get_tmp_file <prefix> [<opts>]
713 # Get a unique filename and create the file in a non-racy way
714 get_tmp_file()
716 while true; do
717 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
718 done
721 # usage: guilt_hook <hook name> <args....>
722 guilt_hook()
724 __hookname="$1"
725 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
727 shift
729 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
730 return $?
734 # Some constants
737 # used for: git apply -C <val>
738 guilt_push_diff_context=1
741 # Parse any part of .git/config that belongs to us
745 # The following gets run every time this file is source'd
748 GUILT_DIR="$GIT_DIR/patches"
750 branch=`get_branch`
752 # most of the time we want to verify that the repo's branch has been
753 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
754 # we must avoid the checks
755 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
756 verify_branch
758 # do not check the status file format (guilt-repair needs this,
759 # otherwise nothing can do what's necessary to bring the repo into a
760 # useable state)
761 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
762 [ -s "$GIT_DIR/patches/$branch/status" ] &&
763 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
764 die "Status file appears to use old format, try guilt-repair --status"
768 # very useful files
769 series="$GUILT_DIR/$branch/series"
770 applied="$GUILT_DIR/$branch/status"
771 guards_file="$GUILT_DIR/$branch/guards"
773 # determine an editor to use for anything interactive (fall back to vi)
774 editor="vi"
775 [ ! -z "$EDITOR" ] && editor="$EDITOR"
777 # determine a pager to use for anything interactive (fall back to more)
778 pager="more"
779 [ ! -z "$PAGER" ] && pager="$PAGER"