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 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
380 print "\nFrom: " author;
383 /^$/ && headers { headers = 0 }
387 # usage: do_get_patch patchfile
392 /^(diff |--- )/ {patch = 1}
393 patch == 1 {print $0}
398 # usage: do_get_header patchfile
401 # The complexity arises from the fact that we want to ignore all but the
402 # Subject line of the header, and any empty lines after it, if these
403 # exist, and inject only the Subject line as the first line of the
406 # We also need to strip "from:" lines from the body of the patch
407 # description as these are used by people to manually set the author of
408 # the patch to be different to their local email address and failing to
409 # strip them results in duplicate from: lines in output from guilt
412 # 1st line prints first encountered Subject line plus empty line.
413 # 2nd line skips standard email/git patch header lines.
414 # 3th line skips "from:" lines throughout the patch description
415 # 4rd line skips tip's additional header lines.
416 # 5th line skips any empty lines thereafter.
417 # 6th line turns off empty line skip upon seeing a non-empty line.
418 # 7th line terminates execution when we encounter the diff
420 BEGIN{body=0; subj=0}
421 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
422 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
423 /^From:/ {body=0; next}
424 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
425 /^[ \t\f\n\r\v]*$/ && (body==0){next}
426 /^.*$/ && (body==0){body=1}
427 /^(diff |---$|--- )/{exit}
433 # usage: do_get_full_header patchfile
436 # 2nd line checks for the begining of a patch
437 # 3rd line outputs the line if it didn't get pruned by the above rules
440 /^(diff |---$|--- )/{exit}
446 # usage: assert_head_check
449 if ! head_check refs
/patches
/$branch/`get_top`; then
454 # usage: head_check <expected hash>
457 # make sure we're not doing funky things to commits that don't
462 # the expected hash is empty
464 refs
/patches
/$branch/)
465 # the expected hash is an invalid rev
469 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
470 disp
"Expected HEAD commit $1" >&2
471 disp
" got `git rev-parse refs/heads/\`git_branch\``" >&2
477 # usage: series_insert_patch <patchname>
478 series_insert_patch
()
480 awk -v top
="`get_top`" -v new
="$1" \
481 'BEGIN{if (top == "") print new;}
484 if (top != "" && top == $0) print new;
485 }' "$series" > "$series.tmp"
486 mv "$series.tmp" "$series"
489 # usage: series_remove_patch <patchname>
490 series_remove_patch
()
492 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
493 mv "$series.tmp" "$series"
496 # usage: series_rename_patch <oldname> <newname>
497 series_rename_patch
()
499 # Rename the patch, but preserve comments on the line
500 awk -v old
="$1" -v new
="$2" '
502 if (index($0, old) == 1)
503 print new substr($0, length(old) + 1);
506 }' "$series" > "$series.tmp"
508 mv "$series.tmp" "$series"
511 # usage: series_rename_patch <oldpatchname> <newpatchname>
514 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
515 remove_ref
"refs/patches/$branch/$1"
518 # Beware! This is one of the few (only?) places where we modify the applied
521 # usage: applied_rename_patch <oldname> <newname>
522 applied_rename_patch
()
524 awk -v old
="$1" -v new
="$2" \
530 }' "$applied" > "$applied.tmp"
532 mv "$applied.tmp" "$applied"
535 # usage: remove_patch_refs
536 # reads patch names from stdin
540 remove_ref
"refs/patches/$branch/$pname"
544 # usage: pop_many_patches <commitish> <number of patches>
552 # remove the patches refs
553 tail -n $2 < "$applied" | remove_patch_refs
555 git
reset --hard "$1" > /dev
/null
557 n
=`wc -l < "$applied"`
559 head_n
"$n" < "$applied" > "$applied.tmp"
560 mv "$applied.tmp" "$applied"
561 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
563 git symbolic-ref HEAD refs
/heads
/$branch
564 git update-ref
-d refs
/heads
/$GUILT_PREFIX$branch
569 # usage: pop_all_patches
573 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
577 # usage: remove_ref <refname>
581 # does the ref exist?
582 r
=`git show-ref --verify -s "$1" 2> /dev/null`
583 [ $?
-ne 0 ] && exit 0
586 git update-ref
-d "$1" "$r"
590 # usage: commit patchname parent
594 TMP_MSG
=`get_tmp_file msg`
596 p
="$GUILT_DIR/$branch/$1"
600 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
602 # grab a commit message out of the patch
603 do_get_header
"$p" > "$TMP_MSG"
605 # make a default commit message if patch doesn't contain one
606 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
608 # extract author and date lines from the patch header, and set
609 # GIT_AUTHOR_{NAME,EMAIL,DATE}
610 # prefering Author/AuthorDate lines if available.
611 author_str
=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
612 if [ -z "$author_str" ]; then
613 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
616 if [ ! -z "$author_str" ]; then
617 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
618 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
619 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
621 author_date_str
=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
622 if [ -z "$author_date_str" ]; then
623 author_date_str
=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
625 if [ ! -z "$author_date_str" ]; then
626 export GIT_AUTHOR_DATE
="$author_date_str"
630 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
631 ct
=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
632 if [ $ct -gt `last_modified "$p"` ]; then
634 if [ $ct -gt `date +%s` ]; then
641 export GIT_COMMITTER_DATE
="`format_last_modified "$p"`"
643 # export GIT_AUTHOR_DATE only if a Date line was unavailable
644 if [ -z "$author_date_str" ]; then
645 export GIT_AUTHOR_DATE
="$GIT_COMMITTER_DATE"
649 treeish
=`git write-tree`
650 commitish
=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
651 if $old_style_prefix || git rev-parse
--verify --quiet refs
/heads
/$GUILT_PREFIX$branch >/dev
/null
653 git update-ref
-m "$GIT_REFLOG_ACTION" HEAD
$commitish
655 git branch
$GUILT_PREFIX$branch $commitish
656 git symbolic-ref HEAD refs
/heads
/$GUILT_PREFIX$branch
659 # mark patch as applied
660 git update-ref
"refs/patches/$branch/$pname" HEAD
666 # usage: push_patch patchname [bail_action]
672 TMP_LOG
=`get_tmp_file log`
674 p
="$GUILT_DIR/$branch/$1"
682 # apply the patch if and only if there is something to apply
683 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
684 if [ "$bail_action" = abort
]; then
687 git apply
-C$guilt_push_diff_context --index \
688 $reject "$p" > /dev
/null
2> "$TMP_LOG"
691 if [ $__push_patch_bail -ne 0 ]; then
693 if [ "$bail_action" = "abort" ]; then
694 rm -f "$TMP_LOG" "$TMP_MSG"
695 return $__push_patch_bail
700 GIT_REFLOG_ACTION
="$GIT_REFLOG_ACTION: $pname" \
703 echo "$pname" >> "$applied"
708 # sub-shell funky-ness
711 return $__push_patch_bail
714 # usage: must_commit_first
717 git update-index
--refresh --unmerged -q > /dev
/null
718 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
719 [ `git diff-index HEAD | wc -l` -eq 0 ]
723 # usage: fold_patch patchname
726 set -- "$1" "`get_top`"
732 # merge the patch headers
734 pcur
="$GUILT_DIR/$branch/$2"
735 pnext
="$GUILT_DIR/$branch/$1"
736 TMP_CUR
=`get_tmp_file diff-cur`
737 TMP_NEXT
=`get_tmp_file diff-next`
738 TMP_DIFF
=`get_tmp_file diff`
739 do_get_full_header
"$pcur" > "$TMP_CUR"
740 do_get_full_header
"$pnext" > "$TMP_NEXT"
741 do_get_patch
"$pcur" > "$TMP_DIFF"
743 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
745 # since the new patch header is empty, we
746 # don't have to do anything
749 # current is empty; new is not
751 cat "$TMP_NEXT" > "$pcur"
752 cat "$TMP_DIFF" >> "$pcur"
756 cat "$TMP_CUR" > "$pcur"
758 echo "Header from folded patch '$1':" >> "$pcur"
760 cat "$TMP_NEXT" >> "$pcur"
761 cat "$TMP_DIFF" >> "$pcur"
765 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
768 __refresh_patch
"$2" HEAD^^
2 "" ""
770 series_remove_patch
"$1"
773 # usage: refresh_patch patchname gengitdiff incldiffstat
776 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
779 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
786 TMP_DIFF
=`get_tmp_file diff`
789 p
="$GUILT_DIR/$branch/$1"
792 # get the patch header
793 do_get_full_header
"$p" > "$TMP_DIFF"
795 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
797 if [ -n "$5" -o $diffstat = "true" ]; then
800 git
diff --stat $diffopts "$2"
806 git
diff --binary $diffopts "$2" >> "$TMP_DIFF"
808 # move the new patch in
813 commit
"$pname" "HEAD~$3"
815 # drop folded patches
818 # remove the patches refs
819 tail -n $N < "$applied" | remove_patch_refs
821 n
=`wc -l < "$applied"`
823 head_n
"$n" < "$applied" > "$applied.tmp"
824 mv "$applied.tmp" "$applied"
828 # usage: munge_hash_range <hash range>
831 # <hash> - one commit
832 # <hash>.. - hash until head (excludes hash, includes head)
833 # ..<hash> - until hash (includes hash)
834 # <hash1>..<hash2> - from hash to hash (inclusive)
836 # The output of this function is suitable to be passed to "git rev-list"
841 # double .. or space is illegal
850 # e.g., "v0.19-rc1..v0.19"
851 echo ${1%%..*}..
${1#*..};;
861 # usage: get_tmp_file <prefix> [<opts>]
863 # Get a unique filename and create the file in a non-racy way
867 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
871 # usage: guilt_hook <hook name> <args....>
875 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
879 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
892 # used for: git apply -C <val>
893 guilt_push_diff_context
=1
895 # default diffstat value: true or false
896 DIFFSTAT_DEFAULT
="false"
898 # default guilt.reusebranch value: true or false
899 REUSE_BRANCH_DEFAULT
="false"
901 # Prefix for guilt branches.
905 # Parse any part of .git/config that belongs to us
909 diffstat
=`git config --bool guilt.diffstat`
910 [ -z "$diffstat" ] && diffstat
=$DIFFSTAT_DEFAULT
913 reuse_branch
=`git config --bool guilt.reusebranch`
914 [ -z "$reuse_branch" ] && reuse_branch
=$REUSE_BRANCH_DEFAULT
917 # The following gets run every time this file is source'd
920 GUILT_DIR
="$GIT_DIR/patches"
922 # To make it harder to accidentally do "git push" with a guilt patch
923 # applied, "guilt push" changes branch from e.g. "master" to
924 # "guilt/master". Set $git_branch to the full branch name, and
925 # $branch to the abbreviated name that the user sees most of the time.
926 # Note: old versions of guilt did not add the "guilt/" prefix. This
927 # code handles that case as well. The prefix will be added when you
928 # have no patches applied and do a "guilt push".
929 raw_git_branch
=`get_branch`
930 branch
=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
937 elif [ -z "`get_top 2>/dev/null`" ]
941 echo $GUILT_PREFIX$branch
945 # most of the time we want to verify that the repo's branch has been
946 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
947 # we must avoid the checks
948 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
951 # do not check the status file format (guilt repair needs this,
952 # otherwise nothing can do what's necessary to bring the repo into a
954 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
955 [ -s "$GIT_DIR/patches/$branch/status" ] &&
956 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
957 die
"Status file appears to use old format, try guilt repair --status"
962 series
="$GUILT_DIR/$branch/series"
963 applied
="$GUILT_DIR/$branch/status"
964 guards_file
="$GUILT_DIR/$branch/guards"
966 # determine a pager to use for anything interactive (fall back to more)
968 [ ! -z "$PAGER" ] && pager
="$PAGER"
972 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
973 .
"$GUILT_PATH/os.$UNAME_S"
974 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
975 .
"$GUILT_PATH/../lib/guilt/os.$UNAME_S"
977 die
"Unsupported operating system: $UNAME_S"
980 if [ -n "`get_top 2>/dev/null`" ]; then
981 # If there is at least one pushed patch, we set
982 # old_style_prefix according to how it was pushed. It is only
983 # possible to change the prefix style while no patches are
985 if [ "$branch" = "$raw_git_branch" ]; then
986 old_style_prefix
=true
988 old_style_prefix
=false
991 old_style_prefix
="$reuse_branch"