3 # Copyright (c) Josef "Jeff" Sipek, 2006-2010
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)" ;;
43 # echo -n is a bashism, use printf instead
49 # echo -e is a bashism, use printf instead
62 "$@" >/dev
/null
2>/dev
/null
69 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f
-perm +111 |
sed -e "s/.*\\/`basename $0`-//"
72 if [ "`basename $0`" = "guilt" ]; then
73 # being run as standalone
75 # by default, we shouldn't fail
79 # take first arg, and try to execute it
84 if [ -x "$dir/guilt-$arg" ]; then
87 # might be a short handed
88 for command in $
(guilt_commands
); do
91 if [ -x "$dir/guilt-$command" ]; then
98 if [ -n "$cmd" ]; then
100 exec "$dir/guilt-$cmd" "$@"
102 # this is not reached because of the exec
103 die
"Exec failed! Something is terribly wrong!"
105 disp
"Command $arg not found" >&2
110 # no args passed or invalid command entered, just output help summary
112 disp
"Guilt v$GUILT_VERSION"
114 disp
"Pick a command:"
115 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/ /'
133 # usage: valid_patchname <patchname>
137 /*|.
/*|..
/*|
*/.
/*|
*/..
/*|
*/.|
*/..|
*/|
*\
*|
*\
*)
148 silent git symbolic-ref HEAD || \
149 die
"Working on a detached HEAD is unsupported."
151 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
156 [ ! -d "$GIT_DIR/patches" ] &&
157 die
"Patches directory doesn't exist, try guilt-init"
158 [ ! -d "$GIT_DIR/patches/$branch" ] &&
159 die
"Branch $branch is not initialized, try guilt-init"
160 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
161 die
"Branch $branch does not have a series file"
162 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
163 die
"Branch $branch does not have a status file"
164 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
165 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
170 tail -n 1 "$GUILT_DIR/$branch/status"
175 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
176 tail -n 2 "$GUILT_DIR/$branch/status" | head_n
1
182 # ignore all lines matching:
185 # - optional whitespace followed by '#' followed by more
186 # optional whitespace
187 # also remove comments from end of lines
188 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
189 s/[[:space:]]*#.*\$//
198 get_full_series |
while read p
; do
199 check_guards
"$p" && echo "$p"
203 # usage: check_guards <patch>
204 # Returns 0 if the patch should be pushed
207 get_guards
"$1" |
while read guard
; do
208 g
=`echo $guard | sed "s/^[+-]//"`
211 # Push +guard *only if* guard selected
212 silent
grep -F "$g" "$guards_file" ||
return 1
215 # Push -guard *unless* guard selected
216 silent
grep -F "$g" "$guards_file" && return 1
222 # propagate return from subshell
226 # usage: get_guards <patch>
233 for(i=2; i<=NF; i++) {
234 sub(/#[^+-]*/, "", $i);
237 guards = guards " " $i;
247 # usage: set_guards <patch> <guards...>
256 awk -v pname
="$p" -v newguard
="$x" '{
258 print $0 " #" newguard;
261 }' < "$series" > "$series.tmp"
262 mv "$series.tmp" "$series"
265 echo "'$x' is not a valid guard name" >&2
272 # usage: unset_guards <patch> <guards...>
279 awk -v pname
="$p" -v oldguard
="$x" '{
283 for(i=2; i<=NF; i++) {
284 if ($i == "#" oldguard)
288 guards = guards " " $i;
299 }' < "$series" > "$series.tmp"
300 mv "$series.tmp" "$series"
305 # usage: do_make_header <hash>
308 # we should try to work with commit objects only
309 if [ `git cat-file -t "$1"` != "commit" ]; then
310 disp
"Hash $1 is not a commit object" >&2
311 disp
"Aborting..." >&2
315 git cat-file
-p "$1" |
awk '
316 BEGIN{headers=1; firstline=1}
317 /^author / && headers {
318 sub(/^author +/, "");
319 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
326 print "\nFrom: " author;
329 /^$/ && headers { headers = 0 }
333 # usage: do_get_patch patchfile
338 /^(diff |---$|--- )/,/END{}/
342 # usage: do_get_header patchfile
345 # The complexity arises from the fact that we want to ignore the
346 # From line and the empty line after it if it exists
348 # 2nd line skips the From line
349 # 3rd line skips the empty line right after a From line
350 # 4th line terminates execution when we encounter the diff
353 /^Subject:/ && (NR==1){print substr($0, 10); next}
354 /^From:/{skip=1; next}
355 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
356 /^(diff |---$|--- )/{exit}
362 # usage: do_get_full_header patchfile
365 # 2nd line checks for the begining of a patch
366 # 3rd line outputs the line if it didn't get pruned by the above rules
369 /^(diff |---$|--- )/{exit}
375 # usage: assert_head_check
378 if ! head_check refs
/patches
/$branch/`get_top`; then
383 # usage: head_check <expected hash>
386 # make sure we're not doing funky things to commits that don't
391 # the expected hash is empty
393 refs
/patches
/$branch/)
394 # the expected hash is an invalid rev
398 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
399 disp
"Expected HEAD commit $1" >&2
400 disp
" got `git rev-parse refs/heads/$branch`" >&2
406 # usage: series_insert_patch <patchname>
407 series_insert_patch
()
409 awk -v top
="`get_top`" -v new
="$1" \
410 'BEGIN{if (top == "") print new;}
413 if (top != "" && top == $0) print new;
414 }' "$series" > "$series.tmp"
415 mv "$series.tmp" "$series"
418 # usage: series_remove_patch <patchname>
419 series_remove_patch
()
421 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
422 mv "$series.tmp" "$series"
425 # usage: series_rename_patch <oldname> <newname>
426 series_rename_patch
()
428 # Rename the patch, but preserve comments on the line
429 awk -v old
="$1" -v new
="$2" '
431 if (index($0, old) == 1)
432 print new substr($0, length(old) + 1);
435 }' "$series" > "$series.tmp"
437 mv "$series.tmp" "$series"
440 # usage: series_rename_patch <oldpatchname> <newpatchname>
443 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
444 remove_ref
"refs/patches/$branch/$1"
447 # Beware! This is one of the few (only?) places where we modify the applied
450 # usage: applied_rename_patch <oldname> <newname>
451 applied_rename_patch
()
453 awk -v old
="$1" -v new
="$2" \
459 }' "$applied" > "$applied.tmp"
461 mv "$applied.tmp" "$applied"
464 # usage: remove_patch_refs
465 # reads patch names from stdin
469 remove_ref
"refs/patches/$branch/$pname"
473 # usage: pop_many_patches <commitish> <number of patches>
481 # remove the patches refs
482 tail -n $2 < "$applied" | remove_patch_refs
484 git
reset --hard "$1" > /dev
/null
486 n
=`wc -l < "$applied"`
488 head_n
"$n" < "$applied" > "$applied.tmp"
489 mv "$applied.tmp" "$applied"
493 # usage: pop_all_patches
497 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
501 # usage: remove_ref <refname>
505 # does the ref exist?
506 r
=`git show-ref --verify -s "$1" 2> /dev/null`
507 [ $?
-ne 0 ] && exit 0
510 git update-ref
-d "$1" "$r"
514 # usage: commit patchname parent
518 TMP_MSG
=`get_tmp_file msg`
520 p
="$GUILT_DIR/$branch/$1"
524 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
526 # grab a commit message out of the patch
527 do_get_header
"$p" > "$TMP_MSG"
529 # make a default commit message if patch doesn't contain one
530 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
532 # extract a From line from the patch header, and set
533 # GIT_AUTHOR_{NAME,EMAIL}
534 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
535 if [ ! -z "$author_str" ]; then
536 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
537 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
538 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
541 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
542 ct
=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
543 if [ $ct -gt `last_modified "$p"` ]; then
545 if [ $ct -gt `date +%s` ]; then
552 export GIT_AUTHOR_DATE
="`format_last_modified "$p"`"
553 export GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
556 treeish
=`git write-tree`
557 commitish
=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
558 git update-ref HEAD
$commitish
560 # mark patch as applied
561 git update-ref
"refs/patches/$branch/$pname" HEAD
567 # usage: push_patch patchname [bail_action]
573 TMP_LOG
=`get_tmp_file log`
575 p
="$GUILT_DIR/$branch/$1"
583 # apply the patch if and only if there is something to apply
584 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
585 if [ "$bail_action" = abort
]; then
588 git apply
-C$guilt_push_diff_context --index \
589 $reject "$p" > /dev
/null
2> "$TMP_LOG"
592 if [ $__push_patch_bail -ne 0 ]; then
594 if [ "$bail_action" = "abort" ]; then
595 rm -f "$TMP_LOG" "$TMP_MSG"
596 return $__push_patch_bail
603 echo "$pname" >> $applied
608 # sub-shell funky-ness
611 return $__push_patch_bail
614 # usage: must_commit_first
617 git update-index
--refresh --unmerged -q > /dev
/null
618 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
619 [ `git diff-index HEAD | wc -l` -eq 0 ]
623 # usage: fold_patch patchname
626 set -- "$1" "`get_top`"
632 # merge the patch headers
634 pcur
="$GUILT_DIR/$branch/$2"
635 pnext
="$GUILT_DIR/$branch/$1"
636 TMP_CUR
=`get_tmp_file diff-cur`
637 TMP_NEXT
=`get_tmp_file diff-next`
638 TMP_DIFF
=`get_tmp_file diff`
639 do_get_full_header
"$pcur" > "$TMP_CUR"
640 do_get_full_header
"$pnext" > "$TMP_NEXT"
641 do_get_patch
"$pcur" > "$TMP_DIFF"
643 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
645 # since the new patch header is empty, we
646 # don't have to do anything
649 # current is empty; new is not
651 cat "$TMP_NEXT" > "$pcur"
652 cat "$TMP_DIFF" >> "$pcur"
656 cat "$TMP_CUR" > "$pcur"
658 echo "Header from folded patch '$1':" >> "$pcur"
660 cat "$TMP_NEXT" >> "$pcur"
661 cat "$TMP_DIFF" >> "$pcur"
665 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
668 __refresh_patch
"$2" HEAD^^
2 "" ""
670 series_remove_patch
"$1"
673 # usage: refresh_patch patchname gengitdiff incldiffstat
676 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
679 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
686 TMP_DIFF
=`get_tmp_file diff`
689 p
="$GUILT_DIR/$branch/$1"
692 # get the patch header
693 do_get_full_header
"$p" > "$TMP_DIFF"
695 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
697 if [ -n "$5" -o $diffstat = "true" ]; then
700 git
diff --stat $diffopts "$2"
706 git
diff --binary $diffopts "$2" >> "$TMP_DIFF"
708 # move the new patch in
713 commit
"$pname" "HEAD~$3"
715 # drop folded patches
718 # remove the patches refs
719 tail -n $N < "$applied" | remove_patch_refs
721 n
=`wc -l < "$applied"`
723 head_n
"$n" < "$applied" > "$applied.tmp"
724 mv "$applied.tmp" "$applied"
728 # usage: munge_hash_range <hash range>
731 # <hash> - one commit
732 # <hash>.. - hash until head (excludes hash, includes head)
733 # ..<hash> - until hash (includes hash)
734 # <hash1>..<hash2> - from hash to hash (inclusive)
736 # The output of this function is suitable to be passed to "git rev-list"
741 # double .. or space is illegal
750 # e.g., "v0.19-rc1..v0.19"
751 echo ${1%%..*}..
${1#*..};;
761 # usage: get_tmp_file <prefix> [<opts>]
763 # Get a unique filename and create the file in a non-racy way
767 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
771 # usage: guilt_hook <hook name> <args....>
775 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
779 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
787 # used for: git apply -C <val>
788 guilt_push_diff_context
=1
790 # default diffstat value: true or false
791 DIFFSTAT_DEFAULT
="false"
794 # Parse any part of .git/config that belongs to us
798 diffstat
=`git config --bool guilt.diffstat`
799 [ -z "$diffstat" ] && diffstat
=$DIFFSTAT_DEFAULT
802 # The following gets run every time this file is source'd
805 GUILT_DIR
="$GIT_DIR/patches"
809 # most of the time we want to verify that the repo's branch has been
810 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
811 # we must avoid the checks
812 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
815 # do not check the status file format (guilt-repair needs this,
816 # otherwise nothing can do what's necessary to bring the repo into a
818 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
819 [ -s "$GIT_DIR/patches/$branch/status" ] &&
820 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
821 die
"Status file appears to use old format, try guilt-repair --status"
826 series
="$GUILT_DIR/$branch/series"
827 applied
="$GUILT_DIR/$branch/status"
828 guards_file
="$GUILT_DIR/$branch/guards"
830 # determine a pager to use for anything interactive (fall back to more)
832 [ ! -z "$PAGER" ] && pager
="$PAGER"
836 if [ -r "`dirname $0`/os.$UNAME_S" ]; then
837 .
"`dirname $0`/os.$UNAME_S"
839 die
"Unsupported operating system: $UNAME_S"