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 find "$GUILT_PATH/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f
-perm +111 2> /dev
/null |
sed -e "s/.*\\/$GUILT-//"
77 find "$GUILT_PATH" -maxdepth 1 -name "guilt-*" -type f
-perm +111 |
sed -e "s/.*\\/$GUILT-//"
80 # by default, we shouldn't fail
84 # take first arg, and try to execute it
88 libdir
="$GUILT_PATH/../lib/guilt"
90 if [ -x "$dir/guilt-$arg" ]; then
93 elif [ -x "$libdir/guilt-$arg" ]; then
94 cmd
="$libdir/guilt-$arg"
97 # might be a short handed
98 for command in $
(guilt_commands
); do
101 if [ -x "$dir/guilt-$command" ]; then
102 cmd
="$dir/guilt-$command"
104 elif [ -x "$libdir/guilt-$command" ]; then
105 cmd
="$libdir/guilt-$command"
112 if [ -z "$cmd" ]; then
113 disp
"Command $arg not found" >&2
120 # no args passed or invalid command entered, just output help summary
122 disp
"Guilt v$GUILT_VERSION"
124 disp
"Pick a command:"
125 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/ /'
129 disp_e
"\tguilt push"
141 # usage: valid_patchname <patchname>
144 # Once we only support Git 1.7.8 and newer, the command below
145 # could be replaced with:
147 # git check-ref-format --allow-onelevel "$1"
149 # Instead, we arbitrarily prepend one level. The result
150 # should be the same, and this is portable to all existing
152 git check-ref-format a
/"$1"
153 if [ $?
-ne 0 ]; then
157 # We want to reject all names that Git 2.0.0 rejects. In case
158 # we are running an older version, we explicitly check some
159 # cases that were added to Git after version 1.5.0. This code
160 # aims to support Git version 1.5.0 and newer.
162 # Git 1.7.6.4 and newer rejects the DEL character.
163 if [ `echo "$1"|tr -d '\177'` != "$1" ]; then
167 # Git 1.7.8 and newer rejects refs that start or end with
168 # slash or contain multiple adjacent slashes.
174 # Git 1.7.8 and newer rejects refname components that end in
181 # Git 1.8.5 and newer rejects refnames that are made up of the
182 # single character "@".
183 if [ "$1" = "@" ]; then
192 silent git symbolic-ref HEAD || \
193 die
"Working on a detached HEAD is unsupported."
195 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
200 [ ! -d "$GIT_DIR/patches" ] &&
201 die
"Patches directory doesn't exist, try guilt init"
202 [ ! -d "$GIT_DIR/patches/$branch" ] &&
203 die
"Branch $branch is not initialized, try guilt init"
204 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
205 die
"Branch $branch does not have a series file"
206 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
207 die
"Branch $branch does not have a status file"
208 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
209 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
214 tail -n 1 "$GUILT_DIR/$branch/status"
219 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
220 tail -n 2 "$GUILT_DIR/$branch/status" | head_n
1
226 # ignore all lines matching:
229 # - optional whitespace followed by '#' followed by more
230 # optional whitespace
231 # also remove comments from end of lines
232 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
233 s/[[:space:]]*#.*\$//
242 get_full_series |
while read p
; do
243 check_guards
"$p" && echo "$p"
247 # usage: check_guards <patch>
248 # Returns 0 if the patch should be pushed
251 get_guards
"$1" |
while read guard
; do
252 g
=`echo $guard | sed "s/^[+-]//"`
255 # Push +guard *only if* guard selected
256 silent
grep -F "$g" "$guards_file" ||
return 1
259 # Push -guard *unless* guard selected
260 silent
grep -F "$g" "$guards_file" && return 1
266 # propagate return from subshell
270 # usage: get_guards <patch>
277 for(i=2; i<=NF; i++) {
278 sub(/#[^+-]*/, "", $i);
281 guards = guards " " $i;
291 # usage: set_guards <patch> <guards...>
300 awk -v pname
="$p" -v newguard
="$x" '{
302 print $0 " #" newguard;
305 }' < "$series" > "$series.tmp"
306 mv "$series.tmp" "$series"
309 echo "'$x' is not a valid guard name" >&2
316 # usage: unset_guards <patch> <guards...>
323 awk -v pname
="$p" -v oldguard
="$x" '{
327 for(i=2; i<=NF; i++) {
328 if ($i == "#" oldguard)
332 guards = guards " " $i;
343 }' < "$series" > "$series.tmp"
344 mv "$series.tmp" "$series"
349 # usage: do_make_header <hash>
352 # we should try to work with commit objects only
353 if [ `git cat-file -t "$1"` != "commit" ]; then
354 disp
"Hash $1 is not a commit object" >&2
355 disp
"Aborting..." >&2
359 git cat-file
-p "$1" |
awk '
360 BEGIN{headers=1; firstline=1}
361 /^author / && headers {
362 sub(/^author +/, "");
363 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
370 print "\nFrom: " author;
373 /^$/ && headers { headers = 0 }
377 # usage: do_get_patch patchfile
382 /^(diff |--- )/ {patch = 1}
383 patch == 1 {print $0}
388 # usage: do_get_header patchfile
391 # The complexity arises from the fact that we want to ignore all but the
392 # Subject line of the header, and any empty lines after it, if these
393 # exist, and inject only the Subject line as the first line of the
396 # We also need to strip "from:" lines from the body of the patch
397 # description as these are used by people to manually set the author of
398 # the patch to be different to their local email address and failing to
399 # strip them results in duplicate from: lines in output from guilt
402 # 1st line prints first encountered Subject line plus empty line.
403 # 2nd line skips standard email/git patch header lines.
404 # 3th line skips "from:" lines throughout the patch description
405 # 4rd line skips tip's additional header lines.
406 # 5th line skips any empty lines thereafter.
407 # 6th line turns off empty line skip upon seeing a non-empty line.
408 # 7th line terminates execution when we encounter the diff
410 BEGIN{body=0; subj=0}
411 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
412 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
413 /^From:/ {body=0; next}
414 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
415 /^[ \t\f\n\r\v]*$/ && (body==0){next}
416 /^.*$/ && (body==0){body=1}
417 /^(diff |---$|--- )/{exit}
423 # usage: do_get_full_header patchfile
426 # 2nd line checks for the begining of a patch
427 # 3rd line outputs the line if it didn't get pruned by the above rules
430 /^(diff |---$|--- )/{exit}
436 # usage: assert_head_check
439 if ! head_check refs
/patches
/$branch/`get_top`; then
444 # usage: head_check <expected hash>
447 # make sure we're not doing funky things to commits that don't
452 # the expected hash is empty
454 refs
/patches
/$branch/)
455 # the expected hash is an invalid rev
459 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
460 disp
"Expected HEAD commit $1" >&2
461 disp
" got `git rev-parse refs/heads/\`git_branch\``" >&2
467 # usage: series_insert_patch <patchname>
468 series_insert_patch
()
470 awk -v top
="`get_top`" -v new
="$1" \
471 'BEGIN{if (top == "") print new;}
474 if (top != "" && top == $0) print new;
475 }' "$series" > "$series.tmp"
476 mv "$series.tmp" "$series"
479 # usage: series_remove_patch <patchname>
480 series_remove_patch
()
482 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
483 mv "$series.tmp" "$series"
486 # usage: series_rename_patch <oldname> <newname>
487 series_rename_patch
()
489 # Rename the patch, but preserve comments on the line
490 awk -v old
="$1" -v new
="$2" '
492 if (index($0, old) == 1)
493 print new substr($0, length(old) + 1);
496 }' "$series" > "$series.tmp"
498 mv "$series.tmp" "$series"
501 # usage: series_rename_patch <oldpatchname> <newpatchname>
504 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
505 remove_ref
"refs/patches/$branch/$1"
508 # Beware! This is one of the few (only?) places where we modify the applied
511 # usage: applied_rename_patch <oldname> <newname>
512 applied_rename_patch
()
514 awk -v old
="$1" -v new
="$2" \
520 }' "$applied" > "$applied.tmp"
522 mv "$applied.tmp" "$applied"
525 # usage: remove_patch_refs
526 # reads patch names from stdin
530 remove_ref
"refs/patches/$branch/$pname"
534 # usage: pop_many_patches <commitish> <number of patches>
542 # remove the patches refs
543 tail -n $2 < "$applied" | remove_patch_refs
545 git
reset --hard "$1" > /dev
/null
547 n
=`wc -l < "$applied"`
549 head_n
"$n" < "$applied" > "$applied.tmp"
550 mv "$applied.tmp" "$applied"
551 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
553 git symbolic-ref HEAD refs
/heads
/$branch
554 git update-ref
-d refs
/heads
/$GUILT_PREFIX$branch
559 # usage: pop_all_patches
563 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
567 # usage: remove_ref <refname>
571 # does the ref exist?
572 r
=`git show-ref --verify -s "$1" 2> /dev/null`
573 [ $?
-ne 0 ] && exit 0
576 git update-ref
-d "$1" "$r"
580 # usage: commit patchname parent
584 TMP_MSG
=`get_tmp_file msg`
586 p
="$GUILT_DIR/$branch/$1"
590 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
592 # grab a commit message out of the patch
593 do_get_header
"$p" > "$TMP_MSG"
595 # make a default commit message if patch doesn't contain one
596 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
598 # extract author and date lines from the patch header, and set
599 # GIT_AUTHOR_{NAME,EMAIL,DATE}
600 # prefering Author/AuthorDate lines if available.
601 author_str
=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
602 if [ -z "$author_str" ]; then
603 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
606 if [ ! -z "$author_str" ]; then
607 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
608 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
609 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
611 author_date_str
=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
612 if [ -z "$author_date_str" ]; then
613 author_date_str
=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
615 if [ ! -z "$author_date_str" ]; then
616 export GIT_AUTHOR_DATE
="$author_date_str"
620 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
621 ct
=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
622 if [ $ct -gt `last_modified "$p"` ]; then
624 if [ $ct -gt `date +%s` ]; then
631 export GIT_COMMITTER_DATE
="`format_last_modified "$p"`"
633 # export GIT_AUTHOR_DATE only if a Date line was unavailable
634 if [ -z "$author_date_str" ]; then
635 export GIT_AUTHOR_DATE
="$GIT_COMMITTER_DATE"
639 treeish
=`git write-tree`
640 commitish
=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
641 if $old_style_prefix || git rev-parse
--verify --quiet refs
/heads
/$GUILT_PREFIX$branch >/dev
/null
643 git update-ref HEAD
$commitish
645 git branch
$GUILT_PREFIX$branch $commitish
646 git symbolic-ref HEAD refs
/heads
/$GUILT_PREFIX$branch
649 # mark patch as applied
650 git update-ref
"refs/patches/$branch/$pname" HEAD
656 # usage: push_patch patchname [bail_action]
662 TMP_LOG
=`get_tmp_file log`
664 p
="$GUILT_DIR/$branch/$1"
672 # apply the patch if and only if there is something to apply
673 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
674 if [ "$bail_action" = abort
]; then
677 git apply
-C$guilt_push_diff_context --index \
678 $reject "$p" > /dev
/null
2> "$TMP_LOG"
681 if [ $__push_patch_bail -ne 0 ]; then
683 if [ "$bail_action" = "abort" ]; then
684 rm -f "$TMP_LOG" "$TMP_MSG"
685 return $__push_patch_bail
692 echo "$pname" >> "$applied"
697 # sub-shell funky-ness
700 return $__push_patch_bail
703 # usage: must_commit_first
706 git update-index
--refresh --unmerged -q > /dev
/null
707 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
708 [ `git diff-index HEAD | wc -l` -eq 0 ]
712 # usage: fold_patch patchname
715 set -- "$1" "`get_top`"
721 # merge the patch headers
723 pcur
="$GUILT_DIR/$branch/$2"
724 pnext
="$GUILT_DIR/$branch/$1"
725 TMP_CUR
=`get_tmp_file diff-cur`
726 TMP_NEXT
=`get_tmp_file diff-next`
727 TMP_DIFF
=`get_tmp_file diff`
728 do_get_full_header
"$pcur" > "$TMP_CUR"
729 do_get_full_header
"$pnext" > "$TMP_NEXT"
730 do_get_patch
"$pcur" > "$TMP_DIFF"
732 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
734 # since the new patch header is empty, we
735 # don't have to do anything
738 # current is empty; new is not
740 cat "$TMP_NEXT" > "$pcur"
741 cat "$TMP_DIFF" >> "$pcur"
745 cat "$TMP_CUR" > "$pcur"
747 echo "Header from folded patch '$1':" >> "$pcur"
749 cat "$TMP_NEXT" >> "$pcur"
750 cat "$TMP_DIFF" >> "$pcur"
754 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
757 __refresh_patch
"$2" HEAD^^
2 "" ""
759 series_remove_patch
"$1"
762 # usage: refresh_patch patchname gengitdiff incldiffstat
765 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
768 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
775 TMP_DIFF
=`get_tmp_file diff`
778 p
="$GUILT_DIR/$branch/$1"
781 # get the patch header
782 do_get_full_header
"$p" > "$TMP_DIFF"
784 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
786 if [ -n "$5" -o $diffstat = "true" ]; then
789 git
diff --stat $diffopts "$2"
795 git
diff --binary $diffopts "$2" >> "$TMP_DIFF"
797 # move the new patch in
802 commit
"$pname" "HEAD~$3"
804 # drop folded patches
807 # remove the patches refs
808 tail -n $N < "$applied" | remove_patch_refs
810 n
=`wc -l < "$applied"`
812 head_n
"$n" < "$applied" > "$applied.tmp"
813 mv "$applied.tmp" "$applied"
817 # usage: munge_hash_range <hash range>
820 # <hash> - one commit
821 # <hash>.. - hash until head (excludes hash, includes head)
822 # ..<hash> - until hash (includes hash)
823 # <hash1>..<hash2> - from hash to hash (inclusive)
825 # The output of this function is suitable to be passed to "git rev-list"
830 # double .. or space is illegal
839 # e.g., "v0.19-rc1..v0.19"
840 echo ${1%%..*}..
${1#*..};;
850 # usage: get_tmp_file <prefix> [<opts>]
852 # Get a unique filename and create the file in a non-racy way
856 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
860 # usage: guilt_hook <hook name> <args....>
864 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
868 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
881 # used for: git apply -C <val>
882 guilt_push_diff_context
=1
884 # default diffstat value: true or false
885 DIFFSTAT_DEFAULT
="false"
887 # default guilt.reusebranch value: true or false
888 REUSE_BRANCH_DEFAULT
="false"
890 # Prefix for guilt branches.
894 # Parse any part of .git/config that belongs to us
898 diffstat
=`git config --bool guilt.diffstat`
899 [ -z "$diffstat" ] && diffstat
=$DIFFSTAT_DEFAULT
902 reuse_branch
=`git config --bool guilt.reusebranch`
903 [ -z "$reuse_branch" ] && reuse_branch
=$REUSE_BRANCH_DEFAULT
906 # The following gets run every time this file is source'd
909 GUILT_DIR
="$GIT_DIR/patches"
911 # To make it harder to accidentally do "git push" with a guilt patch
912 # applied, "guilt push" changes branch from e.g. "master" to
913 # "guilt/master". Set $git_branch to the full branch name, and
914 # $branch to the abbreviated name that the user sees most of the time.
915 # Note: old versions of guilt did not add the "guilt/" prefix. This
916 # code handles that case as well. The prefix will be added when you
917 # have no patches applied and do a "guilt push".
918 raw_git_branch
=`get_branch`
919 branch
=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
926 elif [ -z "`get_top 2>/dev/null`" ]
930 echo $GUILT_PREFIX$branch
934 # most of the time we want to verify that the repo's branch has been
935 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
936 # we must avoid the checks
937 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
940 # do not check the status file format (guilt repair needs this,
941 # otherwise nothing can do what's necessary to bring the repo into a
943 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
944 [ -s "$GIT_DIR/patches/$branch/status" ] &&
945 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
946 die
"Status file appears to use old format, try guilt repair --status"
951 series
="$GUILT_DIR/$branch/series"
952 applied
="$GUILT_DIR/$branch/status"
953 guards_file
="$GUILT_DIR/$branch/guards"
955 # determine a pager to use for anything interactive (fall back to more)
957 [ ! -z "$PAGER" ] && pager
="$PAGER"
961 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
962 .
"$GUILT_PATH/os.$UNAME_S"
963 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
964 .
"$GUILT_PATH/../lib/guilt/os.$UNAME_S"
966 die
"Unsupported operating system: $UNAME_S"
969 if [ -n "`get_top 2>/dev/null`" ]; then
970 # If there is at least one pushed patch, we set
971 # old_style_prefix according to how it was pushed. It is only
972 # possible to change the prefix style while no patches are
974 if [ "$branch" = "$raw_git_branch" ]; then
975 old_style_prefix
=true
977 old_style_prefix
=false
980 old_style_prefix
="$reuse_branch"