3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
7 GUILT_NAME
="Ciuleandra"
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 *) die
"Unsupported version of git ($gitver)" ;;
42 # echo -n is a bashism, use printf instead
48 # echo -e is a bashism, use printf instead
61 "$@" >/dev
/null
2>/dev
/null
68 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f
-perm +111 |
sed -e "s/.*\\/`basename $0`-//"
71 if [ "`basename $0`" = "guilt" ]; then
72 # being run as standalone
74 # by default, we shouldn't fail
78 # take first arg, and try to execute it
83 if [ -x "$dir/guilt-$arg" ]; then
86 # might be a short handed
87 for command in $
(guilt_commands
); do
90 if [ -x "$dir/guilt-$command" ]; then
97 if [ -n "$cmd" ]; then
99 exec "$dir/guilt-$cmd" "$@"
101 # this is not reached because of the exec
102 die
"Exec failed! Something is terribly wrong!"
104 disp
"Command $arg not found" >&2
109 # no args passed or invalid command entered, just output help summary
111 disp
"Guilt v$GUILT_VERSION"
113 disp
"Pick a command:"
114 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/ /'
132 # usage: valid_patchname <patchname>
136 /*|.
/*|..
/*|
*/.
/*|
*/..
/*|
*/.|
*/..|
*/|
*\
*|
*\
*)
145 silent git symbolic-ref HEAD || \
146 die
"Working on a detached HEAD is unsupported."
148 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
153 [ ! -d "$GIT_DIR/patches" ] &&
154 die
"Patches directory doesn't exist, try guilt-init"
155 [ ! -d "$GIT_DIR/patches/$branch" ] &&
156 die
"Branch $branch is not initialized, try guilt-init"
157 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
158 die
"Branch $branch does not have a series file"
159 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
160 die
"Branch $branch does not have a status file"
161 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
162 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
167 tail -1 "$GUILT_DIR/$branch/status"
172 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
173 tail -n 2 "$GUILT_DIR/$branch/status" |
head -n 1
179 # ignore all lines matching:
182 # - optional whitespace followed by '#' followed by more
183 # optional whitespace
184 # also remove comments from end of lines
185 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
186 s/[[:space:]]*#.*\$//
195 get_series |
while read p
; do
196 check_guards
"$p" && echo "$p"
200 # usage: check_guards <patch>
201 # Returns 0 if the patch should be pushed
204 get_guards
"$1" |
while read guard
; do
207 # Push +guard *only if* guard selected
208 silent
grep -e "^$guard\$" "$guards_file" ||
return 1
211 # Push -guard *unless* guard selected
212 silent
grep -e "^$guard\$" "$guards_file" && return 1
217 # propagate return from subshell
221 # usage: get_guards <patch>
225 \,^$1[[:space:]]*#, {
233 # usage: set_guards <patch> <guards...>
242 sed -e "s,^$p\([[:space:]].*\)\?\$,$p\1 #$x," "$series" > "$series.tmp"
243 mv "$series.tmp" "$series"
246 echo "'$x' is not a valid guard name" >&2
253 # usage: unset_guards <patch> <guards...>
260 sed -e "\,^$p[[:space:]], { s/ #$x\([[:space:]].*\)\?$/\1/ }" "$series" > "$series.tmp"
261 mv "$series.tmp" "$series"
266 # usage: do_make_header <hash>
269 # we should try to work with commit objects only
270 if [ `git cat-file -t "$1"` != "commit" ]; then
271 disp
"Hash $1 is not a commit object" >&2
272 disp
"Aborting..." >&2
276 git cat-file
-p "$1" |
awk '
277 BEGIN{headers=1; firstline=1}
278 /^author / && headers {
279 sub(/^author +/, "");
280 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
287 print "\nFrom: " author;
290 /^$/ && headers { headers = 0 }
294 # usage: do_get_patch patchfile
299 /^(diff |---)/,/END{}/
303 # usage: do_get_header patchfile
306 # The complexity arises from the fact that we want to ignore the
307 # From line and the empty line after it if it exists
309 # 2nd line skips the From line
310 # 3rd line skips the empty line right after a From line
311 # 4th line terminates execution when we encounter the diff
314 /^Subject:/ && (NR==1){print substr($0, 10); next}
315 /^From:/{skip=1; next}
316 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
323 # usage: do_get_full_header patchfile
326 # 2nd line checks for the begining of a patch
327 # 3rd line outputs the line if it didn't get pruned by the above rules
336 # usage: assert_head_check
339 if ! head_check refs
/patches
/$branch/`get_top`; then
344 # usage: head_check <expected hash>
347 # make sure we're not doing funky things to commits that don't
352 # the expected hash is empty
354 refs
/patches
/$branch/)
355 # the expected hash is an invalid rev
359 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
360 disp
"Expected HEAD commit $1" >&2
361 disp
" got `git rev-parse refs/heads/$branch`" >&2
367 # usage: series_insert_patch <patchname>
368 series_insert_patch
()
370 awk -v top
="`get_top`" -v new
="$1" \
371 'BEGIN{if (top == "") print new;}
374 if (top != "" && top == $0) print new;
375 }' "$series" > "$series.tmp"
376 mv "$series.tmp" "$series"
379 # usage: series_remove_patch <patchname>
380 series_remove_patch
()
382 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
383 mv "$series.tmp" "$series"
386 # usage: series_rename_patch <oldname> <newname>
387 series_rename_patch
()
389 # Rename the patch, but preserve comments on the line
390 awk -v old
="$1" -v new
="$2" '
392 if (index($0, old) == 1)
393 print new substr($0, length(old) + 1);
396 }' "$series" > "$series.tmp"
398 mv "$series.tmp" "$series"
401 # usage: series_rename_patch <oldpatchname> <newpatchname>
404 git update-ref
"refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
405 remove_ref
"refs/patches/$branch/$1"
408 # Beware! This is one of the few (only?) places where we modify the applied
411 # usage: applied_rename_patch <oldname> <newname>
412 applied_rename_patch
()
414 awk -v old
="$1" -v new
="$2" \
420 }' "$applied" > "$applied.tmp"
422 mv "$applied.tmp" "$applied"
425 # usage: remove_patch_refs
426 # reads patch names from stdin
430 remove_ref
"refs/patches/$branch/$pname"
434 # usage: pop_many_patches <commitish> <number of patches>
442 # remove the patches refs
443 tail -$2 < "$applied" | remove_patch_refs
445 git
reset --hard "$1" > /dev
/null
446 head -n "-$2" < "$applied" > "$applied.tmp"
447 mv "$applied.tmp" "$applied"
451 # usage: pop_all_patches
455 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
459 # usage: remove_ref <refname>
463 # does the ref exist?
464 r
=`git show-ref --verify -s "$1" 2> /dev/null`
465 [ $?
-ne 0 ] && exit 0
468 git update-ref
-d "$1" "$r"
472 # usage: commit patchname parent
476 TMP_MSG
=`get_tmp_file msg`
478 p
="$GUILT_DIR/$branch/$1"
482 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
484 # grab a commit message out of the patch
485 do_get_header
"$p" > "$TMP_MSG"
487 # make a default commit message if patch doesn't contain one
488 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
490 # extract a From line from the patch header, and set
491 # GIT_AUTHOR_{NAME,EMAIL}
492 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---)/ q' "$p"`
493 if [ ! -z "$author_str" ]; then
494 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
495 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
496 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
499 # must strip nano-second part otherwise git gets very
500 # confused, and makes up strange timestamps from the past
501 # (chances are it decides to interpret it as a unix
503 export GIT_AUTHOR_DATE
="`stat -c %y "$p" | sed -e '
504 s/^\([0-9]\{4\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\) \([0-9]\{2\}\):\([0-9]\{2\}\):\([0-9]\{2\}\)\.[0-9]* \(.*\)$/\1-\2-\3 \4:\5:\6 \7/'`"
505 export GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
508 treeish
=`git write-tree`
509 commitish
=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
510 git update-ref HEAD
$commitish
512 # mark patch as applied
513 git update-ref
"refs/patches/$branch/$pname" HEAD
519 # usage: push_patch patchname [bail_action]
525 TMP_LOG
=`get_tmp_file log`
527 p
="$GUILT_DIR/$branch/$1"
535 # apply the patch if and only if there is something to apply
536 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
537 if [ "$bail_action" = abort
]; then
540 git apply
-C$guilt_push_diff_context --index \
541 $reject "$p" > /dev
/null
2> "$TMP_LOG"
544 if [ $__push_patch_bail -ne 0 ]; then
546 if [ "$bail_action" = "abort" ]; then
547 rm -f "$TMP_LOG" "$TMP_MSG"
548 return $__push_patch_bail
555 echo "$pname" >> $applied
560 # sub-shell funky-ness
563 return $__push_patch_bail
566 # usage: must_commit_first
569 git update-index
--refresh --unmerged -q > /dev
/null
570 [ `git diff-files | wc -l` -eq 0 ] ||
return $?
571 [ `git diff-index HEAD | wc -l` -eq 0 ]
575 # usage: fold_patch patchname
578 set -- "$1" "`get_top`"
584 __refresh_patch
"$2" HEAD^^
2 "" ""
586 series_remove_patch
"$1"
589 # usage: refresh_patch patchname gengitdiff incldiffstat
592 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
595 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
602 TMP_DIFF
=`get_tmp_file diff`
605 p
="$GUILT_DIR/$branch/$1"
608 # get the patch header
609 do_get_full_header
"$p" > "$TMP_DIFF"
611 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
613 if [ ! -z "$5" ]; then
616 git
diff --stat $diffopts "$2"
622 git
diff $diffopts "$2" >> "$TMP_DIFF"
624 # move the new patch in
629 commit
"$pname" "HEAD~$3"
631 # drop folded patches
634 # remove the patches refs
635 tail -$N < "$applied" | remove_patch_refs
637 head -n "-$N" < "$applied" > "$applied.tmp"
638 mv "$applied.tmp" "$applied"
642 # usage: munge_hash_range <hash range>
645 # <hash> - one commit
646 # <hash>.. - hash until head (excludes hash, includes head)
647 # ..<hash> - until hash (includes hash)
648 # <hash1>..<hash2> - from hash to hash (inclusive)
650 # The output of this function is suitable to be passed to "git rev-list"
655 # double .. or space is illegal
664 # e.g., "v0.19-rc1..v0.19"
665 echo ${1%%..*}..
${1#*..};;
675 # usage: get_tmp_file <prefix> [<opts>]
677 # Get a unique filename and create the file in a non-racy way
681 mktemp
$2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
685 # usage: guilt_hook <hook name> <args....>
689 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
693 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
701 # used for: git apply -C <val>
702 guilt_push_diff_context
=1
705 # Parse any part of .git/config that belongs to us
709 # The following gets run every time this file is source'd
712 GUILT_DIR
="$GIT_DIR/patches"
716 # most of the time we want to verify that the repo's branch has been
717 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
718 # we must avoid the checks
719 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
722 # do not check the status file format (guilt-repair needs this,
723 # otherwise nothing can do what's necessary to bring the repo into a
725 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
726 [ -s "$GIT_DIR/patches/$branch/status" ] &&
727 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
728 die
"Status file appears to use old format, try guilt-repair --status"
733 series
="$GUILT_DIR/$branch/series"
734 applied
="$GUILT_DIR/$branch/status"
735 guards_file
="$GUILT_DIR/$branch/guards"
737 # determine an editor to use for anything interactive (fall back to vi)
739 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
741 # determine a pager to use for anything interactive (fall back to more)
743 [ ! -z "$PAGER" ] && pager
="$PAGER"