[PATCH] allow guilt to handle binary files
[guilt.git] / guilt
blob17a6288f7200c8c09532969ee6af9412483844fa
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2009
6 GUILT_VERSION="0.32"
7 GUILT_NAME="Refugee"
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 awk -v pname="$1" '
225 ($1 == pname) {
226 guards = "";
228 for(i=2; i<=NF; i++) {
229 sub(/#[^+-]*/, "", $i);
230 if (length($i)) {
231 if (length(guards))
232 guards = guards " " $i;
233 else
234 guards = $i;
238 print guards;
239 }' < "$series"
242 # usage: set_guards <patch> <guards...>
243 set_guards()
246 p="$1"
247 shift
248 for x in "$@"; do
249 case "$x" in
250 [+-]*)
251 awk -v pname="$p" -v newguard="$x" '{
252 if ($1 == pname)
253 print $0 " #" newguard;
254 else
255 print $0;
256 }' < "$series" > "$series.tmp"
257 mv "$series.tmp" "$series"
260 echo "'$x' is not a valid guard name" >&2
262 esac
263 done
267 # usage: unset_guards <patch> <guards...>
268 unset_guards()
271 p="$1"
272 shift
273 for x in "$@"; do
274 awk -v pname="$p" -v oldguard="$x" '{
275 if ($1 == pname) {
276 guards = "";
278 for(i=2; i<=NF; i++) {
279 if ($i == "#" oldguard)
280 continue;
282 if (length(guards))
283 guards = guards " " $i;
284 else
285 guards = $i;
288 if (length(guards))
289 print $1 " " guards;
290 else
291 print $1;
292 } else
293 print $0;
294 }' < "$series" > "$series.tmp"
295 mv "$series.tmp" "$series"
296 done
300 # usage: do_make_header <hash>
301 do_make_header()
303 # we should try to work with commit objects only
304 if [ `git cat-file -t "$1"` != "commit" ]; then
305 disp "Hash $1 is not a commit object" >&2
306 disp "Aborting..." >&2
307 exit 2
310 git cat-file -p "$1" | awk '
311 BEGIN{headers=1; firstline=1}
312 /^author / && headers {
313 sub(/^author +/, "");
314 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
315 author=$0
317 !headers {
318 print
319 if (firstline) {
320 firstline = 0;
321 print "\nFrom: " author;
324 /^$/ && headers { headers = 0 }
328 # usage: do_get_patch patchfile
329 do_get_patch()
331 cat "$1" | awk '
332 BEGIN{}
333 /^(diff |---$|--- )/,/END{}/
337 # usage: do_get_header patchfile
338 do_get_header()
340 # The complexity arises from the fact that we want to ignore the
341 # From line and the empty line after it if it exists
343 # 2nd line skips the From line
344 # 3rd line skips the empty line right after a From line
345 # 4th line terminates execution when we encounter the diff
346 cat "$1" | awk '
347 BEGIN{skip=0}
348 /^Subject:/ && (NR==1){print substr($0, 10); next}
349 /^From:/{skip=1; next}
350 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
351 /^(diff |---$|--- )/{exit}
352 {print $0}
353 END{}
357 # usage: do_get_full_header patchfile
358 do_get_full_header()
360 # 2nd line checks for the begining of a patch
361 # 3rd line outputs the line if it didn't get pruned by the above rules
362 cat "$1" | awk '
363 BEGIN{}
364 /^(diff |---$|--- )/{exit}
365 {print $0}
366 END{}
370 # usage: assert_head_check
371 assert_head_check()
373 if ! head_check refs/patches/$branch/`get_top`; then
374 die "aborting..."
378 # usage: head_check <expected hash>
379 head_check()
381 # make sure we're not doing funky things to commits that don't
382 # belong to us
384 case "$1" in
386 # the expected hash is empty
387 return 0 ;;
388 refs/patches/$branch/)
389 # the expected hash is an invalid rev
390 return 0 ;;
391 esac
393 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
394 disp "Expected HEAD commit $1" >&2
395 disp " got `git rev-parse refs/heads/$branch`" >&2
396 return 1
398 return 0
401 # usage: series_insert_patch <patchname>
402 series_insert_patch()
404 awk -v top="`get_top`" -v new="$1" \
405 'BEGIN{if (top == "") print new;}
407 print $0;
408 if (top != "" && top == $0) print new;
409 }' "$series" > "$series.tmp"
410 mv "$series.tmp" "$series"
413 # usage: series_remove_patch <patchname>
414 series_remove_patch()
416 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
417 mv "$series.tmp" "$series"
420 # usage: series_rename_patch <oldname> <newname>
421 series_rename_patch()
423 # Rename the patch, but preserve comments on the line
424 awk -v old="$1" -v new="$2" '
426 if (index($0, old) == 1)
427 print new substr($0, length(old) + 1);
428 else
429 print $0;
430 }' "$series" > "$series.tmp"
432 mv "$series.tmp" "$series"
435 # usage: series_rename_patch <oldpatchname> <newpatchname>
436 ref_rename_patch()
438 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
439 remove_ref "refs/patches/$branch/$1"
442 # Beware! This is one of the few (only?) places where we modify the applied
443 # file directly
445 # usage: applied_rename_patch <oldname> <newname>
446 applied_rename_patch()
448 awk -v old="$1" -v new="$2" \
449 'BEGIN{FS=":"}
450 { if ($0 == old)
451 print new;
452 else
453 print;
454 }' "$applied" > "$applied.tmp"
456 mv "$applied.tmp" "$applied"
459 # usage: remove_patch_refs
460 # reads patch names from stdin
461 remove_patch_refs()
463 while read pname; do
464 remove_ref "refs/patches/$branch/$pname"
465 done
468 # usage: pop_many_patches <commitish> <number of patches>
469 pop_many_patches()
471 assert_head_check
474 cd_to_toplevel
476 # remove the patches refs
477 tail -$2 < "$applied" | remove_patch_refs
479 git reset --hard "$1" > /dev/null
480 head -n "-$2" < "$applied" > "$applied.tmp"
481 mv "$applied.tmp" "$applied"
485 # usage: pop_all_patches
486 pop_all_patches()
488 pop_many_patches \
489 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
490 `wc -l < "$applied"`
493 # usage: remove_ref <refname>
494 remove_ref()
497 # does the ref exist?
498 r=`git show-ref --verify -s "$1" 2> /dev/null`
499 [ $? -ne 0 ] && exit 0
501 # remove it
502 git update-ref -d "$1" "$r"
506 # usage: commit patchname parent
507 commit()
510 TMP_MSG=`get_tmp_file msg`
512 p="$GUILT_DIR/$branch/$1"
513 pname="$1"
514 cd_to_toplevel
516 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
518 # grab a commit message out of the patch
519 do_get_header "$p" > "$TMP_MSG"
521 # make a default commit message if patch doesn't contain one
522 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
524 # extract a From line from the patch header, and set
525 # GIT_AUTHOR_{NAME,EMAIL}
526 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
527 if [ ! -z "$author_str" ]; then
528 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
529 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
530 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
533 # must strip nano-second part otherwise git gets very
534 # confused, and makes up strange timestamps from the past
535 # (chances are it decides to interpret it as a unix
536 # timestamp).
537 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
538 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/'`"
539 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
541 # commit
542 treeish=`git write-tree`
543 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
544 git update-ref HEAD $commitish
546 # mark patch as applied
547 git update-ref "refs/patches/$branch/$pname" HEAD
549 rm -f "$TMP_MSG"
553 # usage: push_patch patchname [bail_action]
554 push_patch()
556 __push_patch_bail=0
559 TMP_LOG=`get_tmp_file log`
561 p="$GUILT_DIR/$branch/$1"
562 pname="$1"
563 bail_action="$2"
564 reject="--reject"
566 assert_head_check
567 cd_to_toplevel
569 # apply the patch if and only if there is something to apply
570 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
571 if [ "$bail_action" = abort ]; then
572 reject=""
574 git apply -C$guilt_push_diff_context --index \
575 $reject "$p" > /dev/null 2> "$TMP_LOG"
576 __push_patch_bail=$?
578 if [ $__push_patch_bail -ne 0 ]; then
579 cat "$TMP_LOG" >&2
580 if [ "$bail_action" = "abort" ]; then
581 rm -f "$TMP_LOG" "$TMP_MSG"
582 return $__push_patch_bail
587 commit "$pname" HEAD
589 echo "$pname" >> $applied
591 rm -f "$TMP_LOG"
594 # sub-shell funky-ness
595 __push_patch_bail=$?
597 return $__push_patch_bail
600 # usage: must_commit_first
601 must_commit_first()
603 git update-index --refresh --unmerged -q > /dev/null
604 [ `git diff-files | wc -l` -eq 0 ] || return $?
605 [ `git diff-index HEAD | wc -l` -eq 0 ]
606 return $?
609 # usage: fold_patch patchname
610 fold_patch()
612 set -- "$1" "`get_top`"
614 assert_head_check
616 push_patch "$1"
618 # merge the patch headers
620 pcur="$GUILT_DIR/$branch/$2"
621 pnext="$GUILT_DIR/$branch/$1"
622 TMP_CUR=`get_tmp_file diff-cur`
623 TMP_NEXT=`get_tmp_file diff-next`
624 TMP_DIFF=`get_tmp_file diff`
625 do_get_full_header "$pcur" > "$TMP_CUR"
626 do_get_full_header "$pnext" > "$TMP_NEXT"
627 do_get_patch "$pcur" > "$TMP_DIFF"
629 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
630 *,0)
631 # since the new patch header is empty, we
632 # don't have to do anything
634 0,*)
635 # current is empty; new is not
636 mv "$pcur" "$pcur~"
637 cat "$TMP_NEXT" > "$pcur"
638 cat "$TMP_DIFF" >> "$pcur"
640 *,*)
641 mv "$pcur" "$pcur~"
642 cat "$TMP_CUR" > "$pcur"
643 echo >> "$pcur"
644 echo "Header from folded patch '$1':" >> "$pcur"
645 echo >> "$pcur"
646 cat "$TMP_NEXT" >> "$pcur"
647 cat "$TMP_DIFF" >> "$pcur"
649 esac
651 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
654 __refresh_patch "$2" HEAD^^ 2 "" ""
656 series_remove_patch "$1"
659 # usage: refresh_patch patchname gengitdiff incldiffstat
660 refresh_patch()
662 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
665 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
666 # incldiffstat
667 __refresh_patch()
669 assert_head_check
672 TMP_DIFF=`get_tmp_file diff`
674 cd_to_toplevel
675 p="$GUILT_DIR/$branch/$1"
676 pname="$1"
678 # get the patch header
679 do_get_full_header "$p" > "$TMP_DIFF"
681 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
683 if [ -n "$5" -o $diffstat = "true" ]; then
685 echo "---"
686 git diff --stat $diffopts "$2"
687 echo ""
688 ) >> "$TMP_DIFF"
691 # get the new patch
692 git diff --binary $diffopts "$2" >> "$TMP_DIFF"
694 # move the new patch in
695 mv "$p" "$p~"
696 mv "$TMP_DIFF" $p
698 # commit
699 commit "$pname" "HEAD~$3"
701 # drop folded patches
702 N=`expr "$3" - 1`
704 # remove the patches refs
705 tail -$N < "$applied" | remove_patch_refs
707 head -n "-$N" < "$applied" > "$applied.tmp"
708 mv "$applied.tmp" "$applied"
712 # usage: munge_hash_range <hash range>
714 # this means:
715 # <hash> - one commit
716 # <hash>.. - hash until head (excludes hash, includes head)
717 # ..<hash> - until hash (includes hash)
718 # <hash1>..<hash2> - from hash to hash (inclusive)
720 # The output of this function is suitable to be passed to "git rev-list"
721 munge_hash_range()
723 case "$1" in
724 *..*..*|*\ *)
725 # double .. or space is illegal
726 return 1;;
727 ..*)
728 # e.g., "..v0.10"
729 echo ${1#..};;
730 *..)
731 # e.g., "v0.19.."
732 echo ${1%..}..HEAD;;
733 *..*)
734 # e.g., "v0.19-rc1..v0.19"
735 echo ${1%%..*}..${1#*..};;
737 # e.g., "v0.19"
738 echo $1^..$1;;
739 *) # empty
740 return 1;;
741 esac
742 return 0
745 # usage: get_tmp_file <prefix> [<opts>]
747 # Get a unique filename and create the file in a non-racy way
748 get_tmp_file()
750 while true; do
751 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
752 done
755 # usage: guilt_hook <hook name> <args....>
756 guilt_hook()
758 __hookname="$1"
759 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
761 shift
763 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
764 return $?
768 # Some constants
771 # used for: git apply -C <val>
772 guilt_push_diff_context=1
774 # default diffstat value: true or false
775 DIFFSTAT_DEFAULT="false"
778 # Parse any part of .git/config that belongs to us
781 # generate diffstat?
782 diffstat=`git config --bool guilt.diffstat`
783 [ -z "$diffstat" ] && diffstat=$DIFFSTAT_DEFAULT
786 # The following gets run every time this file is source'd
789 GUILT_DIR="$GIT_DIR/patches"
791 branch=`get_branch`
793 # most of the time we want to verify that the repo's branch has been
794 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
795 # we must avoid the checks
796 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
797 verify_branch
799 # do not check the status file format (guilt-repair needs this,
800 # otherwise nothing can do what's necessary to bring the repo into a
801 # useable state)
802 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
803 [ -s "$GIT_DIR/patches/$branch/status" ] &&
804 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
805 die "Status file appears to use old format, try guilt-repair --status"
809 # very useful files
810 series="$GUILT_DIR/$branch/series"
811 applied="$GUILT_DIR/$branch/status"
812 guards_file="$GUILT_DIR/$branch/guards"
814 # determine a pager to use for anything interactive (fall back to more)
815 pager="more"
816 [ ! -z "$PAGER" ] && pager="$PAGER"