3 # Copyright (c) Josef "Jeff" Sipek, 2006-2011
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
12 -h|
--h|
--he|
--hel|
--help)
14 exec "guilt help" "`basename $0`"
17 -V|
--ver|
--versi|
--versio|
--version)
18 echo "Guilt version $GUILT_VERSION"
23 # we change directories ourselves
26 .
"$(git --exec-path)/git-sh-setup"
31 gitver
=`git --version | cut -d' ' -f3 | sed -e 's/^debian\.//'`
33 1.5.
*) ;; # git config
34 1.6.
*) ;; # git config
35 1.7.
*) ;; # git config
36 *) die
"Unsupported version of git ($gitver)" ;;
44 echo "Usage: guilt $CMDNAME $USAGE" >&2
48 # echo -n is a bashism, use printf instead
54 # echo -e is a bashism, use printf instead
67 "$@" >/dev
/null
2>/dev
/null
74 find "`dirname $0`/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f
-perm +111 2> /dev
/null |
sed -e "s/.*\\/`basename $0`-//"
75 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f
-perm +111 |
sed -e "s/.*\\/`basename $0`-//"
78 # by default, we shouldn't fail
82 # take first arg, and try to execute it
86 libdir
="`dirname $0`/../lib/guilt"
88 if [ -x "$dir/guilt-$arg" ]; then
91 elif [ -x "$libdir/guilt-$arg" ]; then
92 cmd
="$libdir/guilt-$arg"
95 # might be a short handed
96 for command in $
(guilt_commands
); do
99 if [ -x "$dir/guilt-$command" ]; then
100 cmd
="$dir/guilt-$command"
102 elif [ -x "$libdir/guilt-$command" ]; then
103 cmd
="$libdir/guilt-$command"
110 if [ -z "$cmd" ]; then
111 disp
"Command $arg not found" >&2
118 # no args passed or invalid command entered, just output help summary
120 disp
"Guilt v$GUILT_VERSION"
122 disp
"Pick a command:"
123 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/ /'
139 # usage: valid_patchname <patchname>
143 /*|.
/*|..
/*|
*/.
/*|
*/..
/*|
*/.|
*/..|
*/|
*\
*|
*\
*)
154 silent git symbolic-ref HEAD || \
155 die
"Working on a detached HEAD is unsupported."
157 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
162 [ ! -d "$GIT_DIR/patches" ] &&
163 die
"Patches directory doesn't exist, try guilt init"
164 [ ! -d "$GIT_DIR/patches/$branch" ] &&
165 die
"Branch $branch is not initialized, try guilt init"
166 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
167 die
"Branch $branch does not have a series file"
168 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
169 die
"Branch $branch does not have a status file"
170 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
171 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
176 tail -n 1 "$GUILT_DIR/$branch/status"
181 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
182 tail -n 2 "$GUILT_DIR/$branch/status" | head_n
1
188 # ignore all lines matching:
191 # - optional whitespace followed by '#' followed by more
192 # optional whitespace
193 # also remove comments from end of lines
194 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
195 s/[[:space:]]*#.*\$//
204 get_full_series |
while read p
; do
205 check_guards
"$p" && echo "$p"
209 # usage: check_guards <patch>
210 # Returns 0 if the patch should be pushed
213 get_guards
"$1" |
while read guard
; do
214 g
=`echo $guard | sed "s/^[+-]//"`
217 # Push +guard *only if* guard selected
218 silent
grep -F "$g" "$guards_file" ||
return 1
221 # Push -guard *unless* guard selected
222 silent
grep -F "$g" "$guards_file" && return 1
228 # propagate return from subshell
232 # usage: get_guards <patch>
239 for(i=2; i<=NF; i++) {
240 sub(/#[^+-]*/, "", $i);
243 guards = guards " " $i;
253 # usage: set_guards <patch> <guards...>
262 awk -v pname
="$p" -v newguard
="$x" '{
264 print $0 " #" newguard;
267 }' < "$series" > "$series.tmp"
268 mv "$series.tmp" "$series"
271 echo "'$x' is not a valid guard name" >&2
278 # usage: unset_guards <patch> <guards...>
285 awk -v pname
="$p" -v oldguard
="$x" '{
289 for(i=2; i<=NF; i++) {
290 if ($i == "#" oldguard)
294 guards = guards " " $i;
305 }' < "$series" > "$series.tmp"
306 mv "$series.tmp" "$series"
311 # usage: do_make_header <hash>
314 # we should try to work with commit objects only
315 if [ `git cat-file -t "$1"` != "commit" ]; then
316 disp
"Hash $1 is not a commit object" >&2
317 disp
"Aborting..." >&2
321 git cat-file
-p "$1" |
awk '
322 BEGIN{headers=1; firstline=1}
323 /^author / && headers {
324 sub(/^author +/, "");
325 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
332 print "\nFrom: " author;
335 /^$/ && headers { headers = 0 }
339 # usage: do_get_patch patchfile
344 /^(diff |---$|--- )/,/END{}/
348 # usage: do_get_header patchfile
351 # The complexity arises from the fact that we want to ignore all but the
352 # Subject line of the header, and any empty lines after it, if these
353 # exist, and inject only the Subject line as the first line of the
356 # We also need to strip "from:" lines from the body of the patch
357 # description as these are used by people to manually set the author of
358 # the patch to be different to their local email address and failing to
359 # strip them results in duplicate from: lines in output from guilt
362 # 1st line prints first encountered Subject line plus empty line.
363 # 2nd line skips standard email/git patch header lines.
364 # 3th line skips "from:" lines throughout the patch description
365 # 4rd line skips tip's additional header lines.
366 # 5th line skips any empty lines thereafter.
367 # 6th line turns off empty line skip upon seeing a non-empty line.
368 # 7th line terminates execution when we encounter the diff
370 BEGIN{body=0; subj=0}
371 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
372 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
374 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
375 /^[ \t\f\n\r\v]*$/ && (body==0){next}
376 /^.*$/ && (body==0){body=1}
377 /^(diff |---$|--- )/{exit}
383 # usage: do_get_full_header patchfile
386 # 2nd line checks for the begining of a patch
387 # 3rd line outputs the line if it didn't get pruned by the above rules
390 /^(diff |---$|--- )/{exit}
396 # usage: assert_head_check
399 if ! head_check refs
/patches
/$branch/`get_top`; then
404 # usage: head_check <expected hash>
407 # make sure we're not doing funky things to commits that don't
412 # the expected hash is empty
414 refs
/patches
/$branch/)
415 # the expected hash is an invalid rev
419 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
420 disp
"Expected HEAD commit $1" >&2
421 disp
" got `git rev-parse refs/heads/$branch`" >&2
427 # usage: series_insert_patch <patchname>
428 series_insert_patch
()
430 awk -v top
="`get_top`" -v new
="$1" \
431 'BEGIN{if (top == "") print new;}
434 if (top != "" && top == $0) print new;
435 }' "$series" > "$series.tmp"
436 mv "$series.tmp" "$series"
439 # usage: series_remove_patch <patchname>
440 series_remove_patch
()
442 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
443 mv "$series.tmp" "$series"
446 # usage: series_rename_patch <oldname> <newname>
447 series_rename_patch
()
449 # Rename the patch, but preserve comments on the line
450 awk -v old
="$1" -v new
="$2" '
452 if (index($0, old) == 1)
453 print new substr($0, length(old) + 1);
456 }' "$series" > "$series.tmp"
458 mv "$series.tmp" "$series"
461 # usage: series_rename_patch <oldpatchname> <newpatchname>
464 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
465 remove_ref
"refs/patches/$branch/$1"
468 # Beware! This is one of the few (only?) places where we modify the applied
471 # usage: applied_rename_patch <oldname> <newname>
472 applied_rename_patch
()
474 awk -v old
="$1" -v new
="$2" \
480 }' "$applied" > "$applied.tmp"
482 mv "$applied.tmp" "$applied"
485 # usage: remove_patch_refs
486 # reads patch names from stdin
490 remove_ref
"refs/patches/$branch/$pname"
494 # usage: pop_many_patches <commitish> <number of patches>
502 # remove the patches refs
503 tail -n $2 < "$applied" | remove_patch_refs
505 git
reset --hard "$1" > /dev
/null
507 n
=`wc -l < "$applied"`
509 head_n
"$n" < "$applied" > "$applied.tmp"
510 mv "$applied.tmp" "$applied"
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
=`echo $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 git update-ref HEAD
$commitish
598 # mark patch as applied
599 git update-ref
"refs/patches/$branch/$pname" HEAD
605 # usage: push_patch patchname [bail_action]
611 TMP_LOG
=`get_tmp_file log`
613 p
="$GUILT_DIR/$branch/$1"
621 # apply the patch if and only if there is something to apply
622 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
623 if [ "$bail_action" = abort
]; then
626 git apply
-C$guilt_push_diff_context --index \
627 $reject "$p" > /dev
/null
2> "$TMP_LOG"
630 if [ $__push_patch_bail -ne 0 ]; then
632 if [ "$bail_action" = "abort" ]; then
633 rm -f "$TMP_LOG" "$TMP_MSG"
634 return $__push_patch_bail
641 echo "$pname" >> $applied
646 # sub-shell funky-ness
649 return $__push_patch_bail
652 # usage: must_commit_first
655 git update-index
--refresh --unmerged -q > /dev
/null
656 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
657 [ `git diff-index HEAD | wc -l` -eq 0 ]
661 # usage: fold_patch patchname
664 set -- "$1" "`get_top`"
670 # merge the patch headers
672 pcur
="$GUILT_DIR/$branch/$2"
673 pnext
="$GUILT_DIR/$branch/$1"
674 TMP_CUR
=`get_tmp_file diff-cur`
675 TMP_NEXT
=`get_tmp_file diff-next`
676 TMP_DIFF
=`get_tmp_file diff`
677 do_get_full_header
"$pcur" > "$TMP_CUR"
678 do_get_full_header
"$pnext" > "$TMP_NEXT"
679 do_get_patch
"$pcur" > "$TMP_DIFF"
681 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
683 # since the new patch header is empty, we
684 # don't have to do anything
687 # current is empty; new is not
689 cat "$TMP_NEXT" > "$pcur"
690 cat "$TMP_DIFF" >> "$pcur"
694 cat "$TMP_CUR" > "$pcur"
696 echo "Header from folded patch '$1':" >> "$pcur"
698 cat "$TMP_NEXT" >> "$pcur"
699 cat "$TMP_DIFF" >> "$pcur"
703 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
706 __refresh_patch
"$2" HEAD^^
2 "" ""
708 series_remove_patch
"$1"
711 # usage: refresh_patch patchname gengitdiff incldiffstat
714 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
717 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
724 TMP_DIFF
=`get_tmp_file diff`
727 p
="$GUILT_DIR/$branch/$1"
730 # get the patch header
731 do_get_full_header
"$p" > "$TMP_DIFF"
733 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
735 if [ -n "$5" -o $diffstat = "true" ]; then
738 git
diff --stat $diffopts "$2"
744 git
diff --binary $diffopts "$2" >> "$TMP_DIFF"
746 # move the new patch in
751 commit
"$pname" "HEAD~$3"
753 # drop folded patches
756 # remove the patches refs
757 tail -n $N < "$applied" | remove_patch_refs
759 n
=`wc -l < "$applied"`
761 head_n
"$n" < "$applied" > "$applied.tmp"
762 mv "$applied.tmp" "$applied"
766 # usage: munge_hash_range <hash range>
769 # <hash> - one commit
770 # <hash>.. - hash until head (excludes hash, includes head)
771 # ..<hash> - until hash (includes hash)
772 # <hash1>..<hash2> - from hash to hash (inclusive)
774 # The output of this function is suitable to be passed to "git rev-list"
779 # double .. or space is illegal
788 # e.g., "v0.19-rc1..v0.19"
789 echo ${1%%..*}..
${1#*..};;
799 # usage: get_tmp_file <prefix> [<opts>]
801 # Get a unique filename and create the file in a non-racy way
805 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
809 # usage: guilt_hook <hook name> <args....>
813 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
817 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
830 # used for: git apply -C <val>
831 guilt_push_diff_context
=1
833 # default diffstat value: true or false
834 DIFFSTAT_DEFAULT
="false"
837 # Parse any part of .git/config that belongs to us
841 diffstat
=`git config --bool guilt.diffstat`
842 [ -z "$diffstat" ] && diffstat
=$DIFFSTAT_DEFAULT
845 # The following gets run every time this file is source'd
848 GUILT_DIR
="$GIT_DIR/patches"
852 # most of the time we want to verify that the repo's branch has been
853 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
854 # we must avoid the checks
855 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
858 # do not check the status file format (guilt repair needs this,
859 # otherwise nothing can do what's necessary to bring the repo into a
861 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
862 [ -s "$GIT_DIR/patches/$branch/status" ] &&
863 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
864 die
"Status file appears to use old format, try guilt repair --status"
869 series
="$GUILT_DIR/$branch/series"
870 applied
="$GUILT_DIR/$branch/status"
871 guards_file
="$GUILT_DIR/$branch/guards"
873 # determine a pager to use for anything interactive (fall back to more)
875 [ ! -z "$PAGER" ] && pager
="$PAGER"
879 if [ -r "`dirname $0`/os.$UNAME_S" ]; then
880 .
"`dirname $0`/os.$UNAME_S"
881 elif [ -r "`dirname $0`/../lib/guilt/os.$UNAME_S" ]; then
882 .
"`dirname $0`/../lib/guilt/os.$UNAME_S"
884 die
"Unsupported operating system: $UNAME_S"