3 # Copyright (c) Josef "Jeff" Sipek, 2006-2015
7 GUILT_NAME
="Irgendwann"
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 # Print arguments, but no trailing newline.
40 # (echo -n is a bashism, use printf instead)
47 # (echo -E is a bashism, use printf instead)
53 # Print arguments, processing backslash sequences.
54 # (echo -e is a bashism, use printf instead)
67 "$@" >/dev
/null
2>/dev
/null
72 GUILT_PATH
="$(dirname "$0")"
76 # GNU Find no longer accepts -perm +111, even though the rest
77 # world (MacOS, Solaris, BSD, etc.) does. Sigh. Using -executable
78 # is arugably better, but it is a GNU extension. Since this isn't
79 # a fast path and guilt doesn't use autoconf, test for it as needed.
80 if find .
-maxdepth 0 -executable > /dev
/null
2>&1 ; then
81 exe_test
="-executable"
85 find "$GUILT_PATH/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f
$exe_test 2> /dev
/null |
sed -e "s/.*\\/$GUILT-//"
86 find "$GUILT_PATH" -maxdepth 1 -name "guilt-*" -type f
$exe_test |
sed -e "s/.*\\/$GUILT-//"
89 # by default, we shouldn't fail
93 # take first arg, and try to execute it
97 libdir
="$GUILT_PATH/../lib/guilt"
99 if [ -x "$dir/guilt-$arg" ]; then
100 cmd
="$dir/guilt-$arg"
102 elif [ -x "$libdir/guilt-$arg" ]; then
103 cmd
="$libdir/guilt-$arg"
106 # might be a short handed
107 for command in $
(guilt_commands
); do
110 if [ -x "$dir/guilt-$command" ]; then
111 cmd
="$dir/guilt-$command"
113 elif [ -x "$libdir/guilt-$command" ]; then
114 cmd
="$libdir/guilt-$command"
121 if [ -z "$cmd" ]; then
122 disp
"Command $arg not found" >&2
126 set_reflog_action
"guilt-$CMDNAME"
130 # no args passed or invalid command entered, just output help summary
132 disp
"Guilt v$GUILT_VERSION"
134 disp
"Pick a command:"
135 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/ /'
139 disp_e
"\tguilt push"
151 # usage: valid_patchname <patchname>
154 # Once we only support Git 1.7.8 and newer, the command below
155 # could be replaced with:
157 # git check-ref-format --allow-onelevel "$1"
159 # Instead, we arbitrarily prepend one level. The result
160 # should be the same, and this is portable to all existing
162 git check-ref-format a
/"$1"
163 if [ $?
-ne 0 ]; then
167 # We want to reject all names that Git 2.0.0 rejects. In case
168 # we are running an older version, we explicitly check some
169 # cases that were added to Git after version 1.5.0. This code
170 # aims to support Git version 1.5.0 and newer.
172 # Git 1.7.6.4 and newer rejects the DEL character.
173 if [ `echo "$1"|tr -d '\177'` != "$1" ]; then
177 # Git 1.7.8 and newer rejects refs that start or end with
178 # slash or contain multiple adjacent slashes.
184 # Git 1.7.8 and newer rejects refname components that end in
191 # Git 1.8.5 and newer rejects refnames that are made up of the
192 # single character "@".
193 if [ "$1" = "@" ]; then
202 silent git symbolic-ref HEAD || \
203 die
"Working on a detached HEAD is unsupported."
205 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
210 [ ! -d "$GIT_DIR/patches" ] &&
211 die
"Patches directory doesn't exist, try guilt init"
212 [ ! -d "$GIT_DIR/patches/$branch" ] &&
213 die
"Branch $branch is not initialized, try guilt init"
214 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
215 die
"Branch $branch does not have a series file"
216 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
217 die
"Branch $branch does not have a status file"
218 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
219 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
224 tail -n 1 "$GUILT_DIR/$branch/status"
229 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
230 tail -n 2 "$GUILT_DIR/$branch/status" | head_n
1
236 # ignore all lines matching:
239 # - optional whitespace followed by '#' followed by more
240 # optional whitespace
241 # also remove comments from end of lines
242 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
243 s/[[:space:]]*#.*\$//
252 get_full_series |
while read p
; do
253 check_guards
"$p" && echo "$p"
257 # usage: check_guards <patch>
258 # Returns 0 if the patch should be pushed
261 get_guards
"$1" |
while read guard
; do
262 g
=`echo $guard | sed "s/^[+-]//"`
265 # Push +guard *only if* guard selected
266 silent
grep -F "$g" "$guards_file" ||
return 1
269 # Push -guard *unless* guard selected
270 silent
grep -F "$g" "$guards_file" && return 1
276 # propagate return from subshell
280 # usage: get_guards <patch>
287 for(i=2; i<=NF; i++) {
288 sub(/#[^+-]*/, "", $i);
291 guards = guards " " $i;
301 # usage: set_guards <patch> <guards...>
310 awk -v pname
="$p" -v newguard
="$x" '{
312 print $0 " #" newguard;
315 }' < "$series" > "$series.tmp"
316 mv "$series.tmp" "$series"
319 echo "'$x' is not a valid guard name" >&2
326 # usage: unset_guards <patch> <guards...>
333 awk -v pname
="$p" -v oldguard
="$x" '{
337 for(i=2; i<=NF; i++) {
338 if ($i == "#" oldguard)
342 guards = guards " " $i;
353 }' < "$series" > "$series.tmp"
354 mv "$series.tmp" "$series"
359 # usage: do_make_header <hash>
362 # we should try to work with commit objects only
363 if [ `git cat-file -t "$1"` != "commit" ]; then
364 disp
"Hash $1 is not a commit object" >&2
365 disp
"Aborting..." >&2
369 git cat-file
-p "$1" |
awk '
370 BEGIN{headers=1; firstline=1}
371 /^author / && headers {
372 sub(/^author +/, "");
373 authordate=strftime("%c", $(NF -1)) " " $NF;
374 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
381 print "\nFrom: " author;
382 print "Date: " authordate;
385 /^$/ && headers { headers = 0 }
389 # usage: do_get_patch patchfile
394 /^(diff |--- )/ {patch = 1}
395 patch == 1 {print $0}
400 # usage: do_get_header patchfile
403 # The complexity arises from the fact that we want to ignore all but the
404 # Subject line of the header, and any empty lines after it, if these
405 # exist, and inject only the Subject line as the first line of the
408 # We also need to strip "from:" lines from the body of the patch
409 # description as these are used by people to manually set the author of
410 # the patch to be different to their local email address and failing to
411 # strip them results in duplicate from: lines in output from guilt
414 # 1st line prints first encountered Subject line plus empty line.
415 # 2nd line skips standard email/git patch header lines.
416 # 3th line skips "from:" lines throughout the patch description
417 # 4rd line skips tip's additional header lines.
418 # 5th line skips any empty lines thereafter.
419 # 6th line turns off empty line skip upon seeing a non-empty line.
420 # 7th line terminates execution when we encounter the diff
422 BEGIN{body=0; subj=0}
423 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
424 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
425 /^From:/ {body=0; next}
426 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
427 /^[ \t\f\n\r\v]*$/ && (body==0){next}
428 /^.*$/ && (body==0){body=1}
429 /^(diff |---$|--- )/{exit}
435 # usage: do_get_full_header patchfile
438 # 2nd line checks for the begining of a patch
439 # 3rd line outputs the line if it didn't get pruned by the above rules
442 /^(diff |---$|--- )/{exit}
448 # usage: assert_head_check
451 if ! head_check refs
/patches
/$branch/`get_top`; then
456 # usage: head_check <expected hash>
459 # make sure we're not doing funky things to commits that don't
464 # the expected hash is empty
466 refs
/patches
/$branch/)
467 # the expected hash is an invalid rev
471 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
472 disp
"Expected HEAD commit $1" >&2
473 disp
" got `git rev-parse refs/heads/\`git_branch\``" >&2
479 # usage: series_insert_patch <patchname>
480 series_insert_patch
()
482 awk -v top
="`get_top`" -v new
="$1" \
483 'BEGIN{if (top == "") print new;}
486 if (top != "" && top == $0) print new;
487 }' "$series" > "$series.tmp"
488 mv "$series.tmp" "$series"
491 # usage: series_remove_patch <patchname>
492 series_remove_patch
()
494 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
495 mv "$series.tmp" "$series"
498 # usage: series_rename_patch <oldname> <newname>
499 series_rename_patch
()
501 # Rename the patch, but preserve comments on the line
502 awk -v old
="$1" -v new
="$2" '
504 if (index($0, old) == 1)
505 print new substr($0, length(old) + 1);
508 }' "$series" > "$series.tmp"
510 mv "$series.tmp" "$series"
513 # usage: series_rename_patch <oldpatchname> <newpatchname>
516 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
517 remove_ref
"refs/patches/$branch/$1"
520 # Beware! This is one of the few (only?) places where we modify the applied
523 # usage: applied_rename_patch <oldname> <newname>
524 applied_rename_patch
()
526 awk -v old
="$1" -v new
="$2" \
532 }' "$applied" > "$applied.tmp"
534 mv "$applied.tmp" "$applied"
537 # usage: remove_patch_refs
538 # reads patch names from stdin
542 remove_ref
"refs/patches/$branch/$pname"
546 # usage: pop_many_patches <commitish> <number of patches>
554 # remove the patches refs
555 tail -n $2 < "$applied" | remove_patch_refs
557 git
reset --hard "$1" > /dev
/null
559 n
=`wc -l < "$applied"`
561 head_n
"$n" < "$applied" > "$applied.tmp"
562 mv "$applied.tmp" "$applied"
563 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
565 git symbolic-ref HEAD refs
/heads
/$branch
566 git update-ref
-d refs
/heads
/$GUILT_PREFIX$branch
571 # usage: pop_all_patches
575 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
579 # usage: remove_ref <refname>
583 # does the ref exist?
584 r
=`git show-ref --verify -s "$1" 2> /dev/null`
585 [ $?
-ne 0 ] && exit 0
588 git update-ref
-d "$1" "$r"
592 # usage: commit patchname parent
596 TMP_MSG
=`get_tmp_file msg`
598 p
="$GUILT_DIR/$branch/$1"
602 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
604 # grab a commit message out of the patch
605 do_get_header
"$p" > "$TMP_MSG"
607 # make a default commit message if patch doesn't contain one
608 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
610 # extract author and date lines from the patch header, and set
611 # GIT_AUTHOR_{NAME,EMAIL,DATE}
612 # prefering Author/AuthorDate lines if available.
613 author_str
=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
614 if [ -z "$author_str" ]; then
615 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
618 if [ ! -z "$author_str" ]; then
619 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
620 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
621 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
623 author_date_str
=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
624 if [ -z "$author_date_str" ]; then
625 author_date_str
=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
627 if [ ! -z "$author_date_str" ]; then
628 export GIT_AUTHOR_DATE
="$author_date_str"
632 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
633 ct
=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
634 if [ $ct -gt `last_modified "$p"` ]; then
636 if [ $ct -gt `date +%s` ]; then
643 export GIT_COMMITTER_DATE
="`format_last_modified "$p"`"
645 # export GIT_AUTHOR_DATE only if a Date line was unavailable
646 if [ -z "$author_date_str" ]; then
647 export GIT_AUTHOR_DATE
="$GIT_COMMITTER_DATE"
651 treeish
=`git write-tree`
652 commitish
=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
653 if $old_style_prefix || git rev-parse
--verify --quiet refs
/heads
/$GUILT_PREFIX$branch >/dev
/null
655 git update-ref
-m "$GIT_REFLOG_ACTION" HEAD
$commitish
657 git branch
$GUILT_PREFIX$branch $commitish
658 git symbolic-ref HEAD refs
/heads
/$GUILT_PREFIX$branch
661 # mark patch as applied
662 git update-ref
"refs/patches/$branch/$pname" HEAD
668 # usage: push_patch patchname [bail_action]
674 TMP_LOG
=`get_tmp_file log`
676 p
="$GUILT_DIR/$branch/$1"
684 # apply the patch if and only if there is something to apply
685 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
686 if [ "$bail_action" = abort
]; then
689 git apply
$guilt_push_diff_context --index \
690 $reject "$p" > /dev
/null
2> "$TMP_LOG"
693 if [ $__push_patch_bail -ne 0 ]; then
695 if [ "$bail_action" = "abort" ]; then
696 rm -f "$TMP_LOG" "$TMP_MSG"
697 return $__push_patch_bail
702 GIT_REFLOG_ACTION
="$GIT_REFLOG_ACTION: $pname" \
705 echo "$pname" >> "$applied"
710 # sub-shell funky-ness
713 return $__push_patch_bail
716 # usage: must_commit_first
719 git update-index
--refresh --unmerged -q > /dev
/null
720 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
721 [ `git diff-index HEAD | wc -l` -eq 0 ]
725 # usage: fold_patch patchname
728 set -- "$1" "`get_top`"
734 # merge the patch headers
736 pcur
="$GUILT_DIR/$branch/$2"
737 pnext
="$GUILT_DIR/$branch/$1"
738 TMP_CUR
=`get_tmp_file diff-cur`
739 TMP_NEXT
=`get_tmp_file diff-next`
740 TMP_DIFF
=`get_tmp_file diff`
741 do_get_full_header
"$pcur" > "$TMP_CUR"
742 do_get_full_header
"$pnext" > "$TMP_NEXT"
743 do_get_patch
"$pcur" > "$TMP_DIFF"
745 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
747 # since the new patch header is empty, we
748 # don't have to do anything
751 # current is empty; new is not
753 cat "$TMP_NEXT" > "$pcur"
754 cat "$TMP_DIFF" >> "$pcur"
758 cat "$TMP_CUR" > "$pcur"
760 echo "Header from folded patch '$1':" >> "$pcur"
762 cat "$TMP_NEXT" >> "$pcur"
763 cat "$TMP_DIFF" >> "$pcur"
767 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
770 __refresh_patch
"$2" HEAD^^
2 "" ""
772 series_remove_patch
"$1"
775 # usage: refresh_patch patchname gengitdiff incldiffstat
778 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
781 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
788 TMP_DIFF
=`get_tmp_file diff`
791 p
="$GUILT_DIR/$branch/$1"
794 # get the patch header
795 do_get_full_header
"$p" > "$TMP_DIFF"
797 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
799 if [ -n "$5" -o $diffstat = "true" ]; then
802 git
diff --stat $diffopts "$2"
808 git
diff --binary $diffopts "$2" >> "$TMP_DIFF"
810 # move the new patch in
815 commit
"$pname" "HEAD~$3"
817 # drop folded patches
820 # remove the patches refs
821 tail -n $N < "$applied" | remove_patch_refs
823 n
=`wc -l < "$applied"`
825 head_n
"$n" < "$applied" > "$applied.tmp"
826 mv "$applied.tmp" "$applied"
830 # usage: munge_hash_range <hash range>
833 # <hash> - one commit
834 # <hash>.. - hash until head (excludes hash, includes head)
835 # ..<hash> - until hash (includes hash)
836 # <hash1>..<hash2> - from hash to hash (inclusive)
838 # The output of this function is suitable to be passed to "git rev-list"
843 # double .. or space is illegal
852 # e.g., "v0.19-rc1..v0.19"
853 echo ${1%%..*}..
${1#*..};;
863 # usage: get_tmp_file <prefix> [<opts>]
865 # Get a unique filename and create the file in a non-racy way
869 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
873 # usage: guilt_hook <hook name> <args....>
877 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
881 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
894 # default guilt.diffcontext value: int or bool
895 DIFFCONTEXT_DEFAULT
=1
897 # default diffstat value: true or false
898 DIFFSTAT_DEFAULT
="false"
900 # default guilt.reusebranch value: true or false
901 REUSE_BRANCH_DEFAULT
="false"
903 # Prefix for guilt branches.
907 # Parse any part of .git/config that belongs to us
911 diffcontext
=`git config --bool-or-int guilt.diffcontext`
912 case "$diffcontext" in
914 guilt_push_diff_context
="-C0"
917 guilt_push_diff_context
=
920 guilt_push_diff_context
="-C$DIFFCONTEXT_DEFAULT"
923 guilt_push_diff_context
="-C$diffcontext"
928 diffstat
=`git config --bool guilt.diffstat`
929 [ -z "$diffstat" ] && diffstat
=$DIFFSTAT_DEFAULT
932 reuse_branch
=`git config --bool guilt.reusebranch`
933 [ -z "$reuse_branch" ] && reuse_branch
=$REUSE_BRANCH_DEFAULT
936 # The following gets run every time this file is source'd
939 GUILT_DIR
="$GIT_DIR/patches"
941 # To make it harder to accidentally do "git push" with a guilt patch
942 # applied, "guilt push" changes branch from e.g. "master" to
943 # "guilt/master". Set $git_branch to the full branch name, and
944 # $branch to the abbreviated name that the user sees most of the time.
945 # Note: old versions of guilt did not add the "guilt/" prefix. This
946 # code handles that case as well. The prefix will be added when you
947 # have no patches applied and do a "guilt push".
948 raw_git_branch
=`get_branch`
949 branch
=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
956 elif [ -z "`get_top 2>/dev/null`" ]
960 echo $GUILT_PREFIX$branch
964 # most of the time we want to verify that the repo's branch has been
965 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
966 # we must avoid the checks
967 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
970 # do not check the status file format (guilt repair needs this,
971 # otherwise nothing can do what's necessary to bring the repo into a
973 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
974 [ -s "$GIT_DIR/patches/$branch/status" ] &&
975 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
976 die
"Status file appears to use old format, try guilt repair --status"
981 series
="$GUILT_DIR/$branch/series"
982 applied
="$GUILT_DIR/$branch/status"
983 guards_file
="$GUILT_DIR/$branch/guards"
985 # determine a pager to use for anything interactive (fall back to more)
987 [ ! -z "$PAGER" ] && pager
="$PAGER"
991 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
992 .
"$GUILT_PATH/os.$UNAME_S"
993 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
994 .
"$GUILT_PATH/../lib/guilt/os.$UNAME_S"
996 die
"Unsupported operating system: $UNAME_S"
999 if [ -n "`get_top 2>/dev/null`" ]; then
1000 # If there is at least one pushed patch, we set
1001 # old_style_prefix according to how it was pushed. It is only
1002 # possible to change the prefix style while no patches are
1004 if [ "$branch" = "$raw_git_branch" ]; then
1005 old_style_prefix
=true
1007 old_style_prefix
=false
1010 old_style_prefix
="$reuse_branch"