3 # Copyright (c) Josef "Jeff" Sipek, 2006-2013
9 GUILT
="$(basename "$0")"
11 # If the first argument is one of the below, display the man page instead of
12 # the rather silly and mostly useless usage string
14 -h|
--h|
--he|
--hel|
--help)
19 -V|
--ver|
--versi|
--versio|
--version)
20 echo "Guilt version $GUILT_VERSION"
25 # we change directories ourselves
28 .
"$(git --exec-path)/git-sh-setup"
35 echo "Usage: guilt $CMDNAME $USAGE" >&2
39 # echo -n is a bashism, use printf instead
45 # echo -e is a bashism, use printf instead
58 "$@" >/dev
/null
2>/dev
/null
63 GUILT_PATH
="$(dirname "$0")"
67 find "$GUILT_PATH/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f
-perm +111 2> /dev
/null |
sed -e "s/.*\\/$GUILT-//"
68 find "$GUILT_PATH" -maxdepth 1 -name "guilt-*" -type f
-perm +111 |
sed -e "s/.*\\/$GUILT-//"
71 # by default, we shouldn't fail
75 # take first arg, and try to execute it
79 libdir
="$GUILT_PATH/../lib/guilt"
81 if [ -x "$dir/guilt-$arg" ]; then
84 elif [ -x "$libdir/guilt-$arg" ]; then
85 cmd
="$libdir/guilt-$arg"
88 # might be a short handed
89 for command in $
(guilt_commands
); do
92 if [ -x "$dir/guilt-$command" ]; then
93 cmd
="$dir/guilt-$command"
95 elif [ -x "$libdir/guilt-$command" ]; then
96 cmd
="$libdir/guilt-$command"
103 if [ -z "$cmd" ]; then
104 disp
"Command $arg not found" >&2
111 # no args passed or invalid command entered, just output help summary
113 disp
"Guilt v$GUILT_VERSION"
115 disp
"Pick a command:"
116 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/ /'
132 # usage: valid_patchname <patchname>
136 /*|.
/*|..
/*|
*/.
/*|
*/..
/*|
*/.|
*/..|
*/|
*\
*|
*\
*)
147 silent git symbolic-ref HEAD || \
148 die
"Working on a detached HEAD is unsupported."
150 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
155 [ ! -d "$GIT_DIR/patches" ] &&
156 die
"Patches directory doesn't exist, try guilt init"
157 [ ! -d "$GIT_DIR/patches/$branch" ] &&
158 die
"Branch $branch is not initialized, try guilt init"
159 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
160 die
"Branch $branch does not have a series file"
161 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
162 die
"Branch $branch does not have a status file"
163 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
164 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
169 tail -n 1 "$GUILT_DIR/$branch/status"
174 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
175 tail -n 2 "$GUILT_DIR/$branch/status" | head_n
1
181 # ignore all lines matching:
184 # - optional whitespace followed by '#' followed by more
185 # optional whitespace
186 # also remove comments from end of lines
187 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
188 s/[[:space:]]*#.*\$//
197 get_full_series |
while read p
; do
198 check_guards
"$p" && echo "$p"
202 # usage: check_guards <patch>
203 # Returns 0 if the patch should be pushed
206 get_guards
"$1" |
while read guard
; do
207 g
=`echo $guard | sed "s/^[+-]//"`
210 # Push +guard *only if* guard selected
211 silent
grep -F "$g" "$guards_file" ||
return 1
214 # Push -guard *unless* guard selected
215 silent
grep -F "$g" "$guards_file" && return 1
221 # propagate return from subshell
225 # usage: get_guards <patch>
232 for(i=2; i<=NF; i++) {
233 sub(/#[^+-]*/, "", $i);
236 guards = guards " " $i;
246 # usage: set_guards <patch> <guards...>
255 awk -v pname
="$p" -v newguard
="$x" '{
257 print $0 " #" newguard;
260 }' < "$series" > "$series.tmp"
261 mv "$series.tmp" "$series"
264 echo "'$x' is not a valid guard name" >&2
271 # usage: unset_guards <patch> <guards...>
278 awk -v pname
="$p" -v oldguard
="$x" '{
282 for(i=2; i<=NF; i++) {
283 if ($i == "#" oldguard)
287 guards = guards " " $i;
298 }' < "$series" > "$series.tmp"
299 mv "$series.tmp" "$series"
304 # usage: do_make_header <hash>
307 # we should try to work with commit objects only
308 if [ `git cat-file -t "$1"` != "commit" ]; then
309 disp
"Hash $1 is not a commit object" >&2
310 disp
"Aborting..." >&2
314 git cat-file
-p "$1" |
awk '
315 BEGIN{headers=1; firstline=1}
316 /^author / && headers {
317 sub(/^author +/, "");
318 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
325 print "\nFrom: " author;
328 /^$/ && headers { headers = 0 }
332 # usage: do_get_patch patchfile
337 /^(diff |---$|--- )/ {patch = 1}
338 patch == 1 {print $0}
343 # usage: do_get_header patchfile
346 # The complexity arises from the fact that we want to ignore all but the
347 # Subject line of the header, and any empty lines after it, if these
348 # exist, and inject only the Subject line as the first line of the
351 # We also need to strip "from:" lines from the body of the patch
352 # description as these are used by people to manually set the author of
353 # the patch to be different to their local email address and failing to
354 # strip them results in duplicate from: lines in output from guilt
357 # 1st line prints first encountered Subject line plus empty line.
358 # 2nd line skips standard email/git patch header lines.
359 # 3th line skips "from:" lines throughout the patch description
360 # 4rd line skips tip's additional header lines.
361 # 5th line skips any empty lines thereafter.
362 # 6th line turns off empty line skip upon seeing a non-empty line.
363 # 7th line terminates execution when we encounter the diff
365 BEGIN{body=0; subj=0}
366 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
367 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
368 /^From:/ {body=0; next}
369 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
370 /^[ \t\f\n\r\v]*$/ && (body==0){next}
371 /^.*$/ && (body==0){body=1}
372 /^(diff |---$|--- )/{exit}
378 # usage: do_get_full_header patchfile
381 # 2nd line checks for the begining of a patch
382 # 3rd line outputs the line if it didn't get pruned by the above rules
385 /^(diff |---$|--- )/{exit}
391 # usage: assert_head_check
394 if ! head_check refs
/patches
/$branch/`get_top`; then
399 # usage: head_check <expected hash>
402 # make sure we're not doing funky things to commits that don't
407 # the expected hash is empty
409 refs
/patches
/$branch/)
410 # the expected hash is an invalid rev
414 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
415 disp
"Expected HEAD commit $1" >&2
416 disp
" got `git rev-parse refs/heads/\`git_branch\``" >&2
422 # usage: series_insert_patch <patchname>
423 series_insert_patch
()
425 awk -v top
="`get_top`" -v new
="$1" \
426 'BEGIN{if (top == "") print new;}
429 if (top != "" && top == $0) print new;
430 }' "$series" > "$series.tmp"
431 mv "$series.tmp" "$series"
434 # usage: series_remove_patch <patchname>
435 series_remove_patch
()
437 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
438 mv "$series.tmp" "$series"
441 # usage: series_rename_patch <oldname> <newname>
442 series_rename_patch
()
444 # Rename the patch, but preserve comments on the line
445 awk -v old
="$1" -v new
="$2" '
447 if (index($0, old) == 1)
448 print new substr($0, length(old) + 1);
451 }' "$series" > "$series.tmp"
453 mv "$series.tmp" "$series"
456 # usage: series_rename_patch <oldpatchname> <newpatchname>
459 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
460 remove_ref
"refs/patches/$branch/$1"
463 # Beware! This is one of the few (only?) places where we modify the applied
466 # usage: applied_rename_patch <oldname> <newname>
467 applied_rename_patch
()
469 awk -v old
="$1" -v new
="$2" \
475 }' "$applied" > "$applied.tmp"
477 mv "$applied.tmp" "$applied"
480 # usage: remove_patch_refs
481 # reads patch names from stdin
485 remove_ref
"refs/patches/$branch/$pname"
489 # usage: pop_many_patches <commitish> <number of patches>
497 # remove the patches refs
498 tail -n $2 < "$applied" | remove_patch_refs
500 git
reset --hard "$1" > /dev
/null
502 n
=`wc -l < "$applied"`
504 head_n
"$n" < "$applied" > "$applied.tmp"
505 mv "$applied.tmp" "$applied"
506 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
508 git symbolic-ref HEAD refs
/heads
/$branch
509 git update-ref
-d refs
/heads
/$GUILT_PREFIX$branch
514 # usage: pop_all_patches
518 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
522 # usage: remove_ref <refname>
526 # does the ref exist?
527 r
=`git show-ref --verify -s "$1" 2> /dev/null`
528 [ $?
-ne 0 ] && exit 0
531 git update-ref
-d "$1" "$r"
535 # usage: commit patchname parent
539 TMP_MSG
=`get_tmp_file msg`
541 p
="$GUILT_DIR/$branch/$1"
545 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
547 # grab a commit message out of the patch
548 do_get_header
"$p" > "$TMP_MSG"
550 # make a default commit message if patch doesn't contain one
551 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
553 # extract author and date lines from the patch header, and set
554 # GIT_AUTHOR_{NAME,EMAIL,DATE}
555 # prefering Author/AuthorDate lines if available.
556 author_str
=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
557 if [ -z "$author_str" ]; then
558 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
561 if [ ! -z "$author_str" ]; then
562 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
563 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
564 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
566 author_date_str
=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
567 if [ -z "$author_date_str" ]; then
568 author_date_str
=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
570 if [ ! -z "$author_date_str" ]; then
571 export GIT_AUTHOR_DATE
="$author_date_str"
575 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
576 ct
=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
577 if [ $ct -gt `last_modified "$p"` ]; then
579 if [ $ct -gt `date +%s` ]; then
586 export GIT_COMMITTER_DATE
="`format_last_modified "$p"`"
588 # export GIT_AUTHOR_DATE only if a Date line was unavailable
589 if [ -z "$author_date_str" ]; then
590 export GIT_AUTHOR_DATE
="$GIT_COMMITTER_DATE"
594 treeish
=`git write-tree`
595 commitish
=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
596 if $old_style_prefix || git rev-parse
--verify --quiet refs
/heads
/$GUILT_PREFIX$branch >/dev
/null
598 git update-ref HEAD
$commitish
600 git branch
$GUILT_PREFIX$branch $commitish
601 git symbolic-ref HEAD refs
/heads
/$GUILT_PREFIX$branch
604 # mark patch as applied
605 git update-ref
"refs/patches/$branch/$pname" HEAD
611 # usage: push_patch patchname [bail_action]
617 TMP_LOG
=`get_tmp_file log`
619 p
="$GUILT_DIR/$branch/$1"
627 # apply the patch if and only if there is something to apply
628 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
629 if [ "$bail_action" = abort
]; then
632 git apply
-C$guilt_push_diff_context --index \
633 $reject "$p" > /dev
/null
2> "$TMP_LOG"
636 if [ $__push_patch_bail -ne 0 ]; then
638 if [ "$bail_action" = "abort" ]; then
639 rm -f "$TMP_LOG" "$TMP_MSG"
640 return $__push_patch_bail
647 echo "$pname" >> "$applied"
652 # sub-shell funky-ness
655 return $__push_patch_bail
658 # usage: must_commit_first
661 git update-index
--refresh --unmerged -q > /dev
/null
662 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
663 [ `git diff-index HEAD | wc -l` -eq 0 ]
667 # usage: fold_patch patchname
670 set -- "$1" "`get_top`"
676 # merge the patch headers
678 pcur
="$GUILT_DIR/$branch/$2"
679 pnext
="$GUILT_DIR/$branch/$1"
680 TMP_CUR
=`get_tmp_file diff-cur`
681 TMP_NEXT
=`get_tmp_file diff-next`
682 TMP_DIFF
=`get_tmp_file diff`
683 do_get_full_header
"$pcur" > "$TMP_CUR"
684 do_get_full_header
"$pnext" > "$TMP_NEXT"
685 do_get_patch
"$pcur" > "$TMP_DIFF"
687 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
689 # since the new patch header is empty, we
690 # don't have to do anything
693 # current is empty; new is not
695 cat "$TMP_NEXT" > "$pcur"
696 cat "$TMP_DIFF" >> "$pcur"
700 cat "$TMP_CUR" > "$pcur"
702 echo "Header from folded patch '$1':" >> "$pcur"
704 cat "$TMP_NEXT" >> "$pcur"
705 cat "$TMP_DIFF" >> "$pcur"
709 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
712 __refresh_patch
"$2" HEAD^^
2 "" ""
714 series_remove_patch
"$1"
717 # usage: refresh_patch patchname gengitdiff incldiffstat
720 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
723 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
730 TMP_DIFF
=`get_tmp_file diff`
733 p
="$GUILT_DIR/$branch/$1"
736 # get the patch header
737 do_get_full_header
"$p" > "$TMP_DIFF"
739 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
741 if [ -n "$5" -o $diffstat = "true" ]; then
744 git
diff --stat $diffopts "$2"
750 git
diff --binary $diffopts "$2" >> "$TMP_DIFF"
752 # move the new patch in
757 commit
"$pname" "HEAD~$3"
759 # drop folded patches
762 # remove the patches refs
763 tail -n $N < "$applied" | remove_patch_refs
765 n
=`wc -l < "$applied"`
767 head_n
"$n" < "$applied" > "$applied.tmp"
768 mv "$applied.tmp" "$applied"
772 # usage: munge_hash_range <hash range>
775 # <hash> - one commit
776 # <hash>.. - hash until head (excludes hash, includes head)
777 # ..<hash> - until hash (includes hash)
778 # <hash1>..<hash2> - from hash to hash (inclusive)
780 # The output of this function is suitable to be passed to "git rev-list"
785 # double .. or space is illegal
794 # e.g., "v0.19-rc1..v0.19"
795 echo ${1%%..*}..
${1#*..};;
805 # usage: get_tmp_file <prefix> [<opts>]
807 # Get a unique filename and create the file in a non-racy way
811 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
815 # usage: guilt_hook <hook name> <args....>
819 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
823 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
836 # used for: git apply -C <val>
837 guilt_push_diff_context
=1
839 # default diffstat value: true or false
840 DIFFSTAT_DEFAULT
="false"
842 # Prefix for guilt branches.
846 # Parse any part of .git/config that belongs to us
850 diffstat
=`git config --bool guilt.diffstat`
851 [ -z "$diffstat" ] && diffstat
=$DIFFSTAT_DEFAULT
854 # The following gets run every time this file is source'd
857 GUILT_DIR
="$GIT_DIR/patches"
859 # To make it harder to accidentally do "git push" with a guilt patch
860 # applied, "guilt push" changes branch from e.g. "master" to
861 # "guilt/master". Set $git_branch to the full branch name, and
862 # $branch to the abbreviated name that the user sees most of the time.
863 # Note: old versions of guilt did not add the "guilt/" prefix. This
864 # code handles that case as well. The prefix will be added when you
865 # have no patches applied and do a "guilt push".
866 raw_git_branch
=`get_branch`
867 branch
=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
874 elif [ -z "`get_top 2>/dev/null`" ]
878 echo $GUILT_PREFIX$branch
882 # most of the time we want to verify that the repo's branch has been
883 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
884 # we must avoid the checks
885 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
888 # do not check the status file format (guilt repair needs this,
889 # otherwise nothing can do what's necessary to bring the repo into a
891 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
892 [ -s "$GIT_DIR/patches/$branch/status" ] &&
893 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
894 die
"Status file appears to use old format, try guilt repair --status"
899 series
="$GUILT_DIR/$branch/series"
900 applied
="$GUILT_DIR/$branch/status"
901 guards_file
="$GUILT_DIR/$branch/guards"
903 # determine a pager to use for anything interactive (fall back to more)
905 [ ! -z "$PAGER" ] && pager
="$PAGER"
909 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
910 .
"$GUILT_PATH/os.$UNAME_S"
911 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
912 .
"$GUILT_PATH/../lib/guilt/os.$UNAME_S"
914 die
"Unsupported operating system: $UNAME_S"
917 if [ "$branch" = "$raw_git_branch" ] && [ -n "`get_top 2>/dev/null`" ]
919 # This is for compat with old repositories that still have a
920 # pushed patch without the new-style branch prefix.
921 old_style_prefix
=true
923 old_style_prefix
=false