3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
7 GUILT_NAME
="Bleecker Street"
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
31 gitver
=`git --version | cut -d' ' -f3`
33 1.5.
*) ;; # git config
34 *) die
"Unsupported version of git ($gitver)" ;;
41 # echo -e is a bashism, fallback to /bin/echo if the builtin does not supports it
54 "$@" >/dev
/null
2>/dev
/null
61 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f
-perm /111 |
sed -e "s/.*\\/`basename $0`-//"
64 if [ "`basename $0`" = "guilt" ]; then
65 # being run as standalone
67 # by default, we shouldn't fail
71 # take first arg, and try to execute it
76 if [ -x "$dir/guilt-$arg" ]; then
79 # might be a short handed
80 for command in $
(guilt_commands
); do
83 if [ -x "$dir/guilt-$command" ]; then
90 if [ -n "$cmd" ]; then
92 exec "$dir/guilt-$cmd" "$@"
94 # this is not reached because of the exec
95 die
"Exec failed! Something is terribly wrong!"
97 echo "Command $arg not found" >&2
102 # no args passed or invalid command entered, just output help summary
104 echo "Guilt v$GUILT_VERSION"
106 echo "Pick a command:"
107 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/\t/'
111 echo -e "\tguilt-push"
113 echo -e "\tguilt push"
125 # usage: valid_patchname <patchname>
129 /*|.
/*|..
/*|
*/.
/*|
*/..
/*|
*/.|
*/..|
*/|
*\
*|
*\
*)
138 git symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
143 [ ! -d "$GIT_DIR/patches" ] &&
144 die
"Patches directory doesn't exist, try guilt-init"
145 [ ! -d "$GIT_DIR/patches/$branch" ] &&
146 die
"Branch $branch is not initialized, try guilt-init"
147 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
148 die
"Branch $branch does not have a series file"
149 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
150 die
"Branch $branch does not have a status file"
151 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
152 die
"Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
157 tail -1 "$GUILT_DIR/$branch/status"
162 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
163 tail -n 2 "$GUILT_DIR/$branch/status" |
head -n 1
169 # ignore all lines matching:
172 # - optional whitespace followed by '#' followed by more
173 # optional whitespace
174 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
177 # usage: do_make_header <hash>
180 # we should try to work with commit objects only
181 if [ `git cat-file -t "$1"` != "commit" ]; then
182 echo "Hash $1 is not a commit object" >&2
183 echo "Aborting..." >&2
187 git cat-file
-p "$1" |
awk '
188 BEGIN{headers=1; firstline=1}
189 /^author / && headers {
190 sub(/^author +/, "");
191 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
198 print "\nFrom: " author;
201 /^$/ && headers { headers = 0 }
205 # usage: do_get_patch patchfile
210 /^(diff |---)/,/END{}/
214 # usage: do_get_header patchfile
217 # The complexity arises from the fact that we want to ignore the
218 # From line and the empty line after it if it exists
220 # 2nd line skips the From line
221 # 3rd line skips the empty line right after a From line
222 # 4th line terminates execution when we encounter the diff
225 /^Subject:/ && (NR==1){print substr($0, 10); next}
226 /^From:/{skip=1; next}
227 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
234 # usage: do_get_full_header patchfile
237 # 2nd line checks for the begining of a patch
238 # 3rd line outputs the line if it didn't get pruned by the above rules
247 # usage: assert_head_check
250 if ! head_check refs
/patches
/$branch/`get_top`; then
255 # usage: head_check <expected hash>
258 # make sure we're not doing funky things to commits that don't
263 # the expected hash is empty
265 refs
/patches
/$branch/)
266 # the expected hash is an invalid rev
270 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
271 echo "Expected HEAD commit $1" >&2
272 echo " got `git rev-parse refs/heads/$branch`" >&2
278 # usage: series_insert_patch <patchname>
279 series_insert_patch
()
281 awk -v top
="`get_top`" -v new
="$1" \
282 'BEGIN{if (top == "") print new;}
285 if (top != "" && top == $0) print new;
286 }' "$series" > "$series.tmp"
287 mv "$series.tmp" "$series"
290 # usage: series_remove_patch <patchname>
291 series_remove_patch
()
293 grep -v "^$1\$" < "$series" > "$series.tmp"
294 mv "$series.tmp" "$series"
297 # usage: series_rename_patch <oldname> <newname>
298 series_rename_patch
()
300 awk -v old
="$1" -v new
="$2" \
301 '{ if ($0 == old) print new; else print $0 }' \
302 "$series" > "$series.tmp"
304 mv "$series.tmp" "$series"
307 # Beware! This is one of the few (only?) places where we modify the applied
310 # usage: applied_rename_patch <oldname> <newname>
311 applied_rename_patch
()
313 awk -v old
="$1" -v new
="$2" \
319 }' "$applied" > "$applied.tmp"
321 mv "$applied.tmp" "$applied"
324 # usage: remove_patch_refs
325 # reads patch names from stdin
329 git update-ref
-d "refs/patches/$branch/$pname" `git rev-parse "refs/patches/$branch/$pname"`
333 # usage: pop_many_patches <commitish> <number of patches>
341 # remove the patches refs
342 tail -$2 < "$applied" | remove_patch_refs
344 git
reset --hard "$1" > /dev
/null
345 head -n "-$2" < "$applied" > "$applied.tmp"
346 mv "$applied.tmp" "$applied"
349 # update references to top, bottom, and base
353 # usage: pop_all_patches
357 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
361 # usage: update_stack_tags
364 # bail if autotagging is not enabled
365 if [ $autotag -eq 0 ]; then
369 if [ -s "$applied" ]; then
370 # there are patches applied, therefore we must get the top,
371 # bottom and base hashes, and update the tags
373 git update-ref
"refs/tags/${branch}_top" `git rev-parse HEAD`
374 git update-ref
"refs/tags/${branch}_bottom" `git rev-parse refs/patches/$branch/$(head -1 < $applied)`
375 git update-ref
"refs/tags/${branch}_base" `git rev-parse refs/patches/$branch/$(head -1 < $applied)^`
377 # there are no patches applied, therefore we must remove the
378 # tags to old top, bottom, and base
380 git update-ref
-d "refs/tags/${branch}_top" \
381 `git rev-parse "refs/tags/${branch}_top"`
382 git update-ref
-d "refs/tags/${branch}_bottom" \
383 `git rev-parse "refs/tags/${branch}_bottom"`
384 git update-ref
-d "refs/tags/${branch}_base" \
385 `git rev-parse "refs/tags/${branch}_base"`
389 # usage: push_patch patchname [bail_action]
395 TMP_LOG
=`get_tmp_file log`
396 TMP_MSG
=`get_tmp_file msg`
398 p
="$GUILT_DIR/$branch/$1"
406 # apply the patch if and only if there is something to apply
407 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
408 if [ "$bail_action" = abort
]; then
411 git apply
-C$guilt_push_diff_context --index \
412 $reject "$p" > /dev
/null
2> "$TMP_LOG"
415 if [ $__push_patch_bail -ne 0 ]; then
417 if [ "$bail_action" = "abort" ]; then
418 rm -f "$TMP_LOG" "$TMP_MSG"
419 return $__push_patch_bail
424 # grab a commit message out of the patch
425 do_get_header
"$p" > "$TMP_MSG"
427 # make a default commit message if patch doesn't contain one
428 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
430 # extract a From line from the patch header, and set
431 # GIT_AUTHOR_{NAME,EMAIL}
432 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q }; /^(diff |---)/ q' "$p"`
433 if [ ! -z "$author_str" ]; then
434 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
435 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
436 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
439 # must strip nano-second part otherwise git gets very
440 # confused, and makes up strange timestamps from the past
441 # (chances are it decides to interpret it as a unix
443 export GIT_AUTHOR_DATE
="`stat -c %y "$p" | sed -e '
444 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/'`"
445 export GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
448 treeish
=`git write-tree`
449 commitish
=`git commit-tree $treeish -p HEAD < "$TMP_MSG"`
450 git update-ref HEAD
$commitish
452 # mark patch as applied
453 git update-ref
"refs/patches/$branch/$pname" $commitish ""
455 echo "$pname" >> $applied
457 rm -f "$TMP_MSG" "$TMP_LOG"
460 # sub-shell funky-ness
463 # update references to top, bottom, and base of the stack
466 return $__push_patch_bail
469 # usage: must_commit_first
472 [ `git diff-files | wc -l` -eq 0 ]
476 # usage: fold_patch patchname
479 set -- "$1" "`get_top`"
485 __refresh_patch
"$2" HEAD^^
2 "" ""
487 series_remove_patch
"$1"
490 # usage: refresh_patch patchname gengitdiff incldiffstat
493 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
496 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
503 TMP_DIFF
=`get_tmp_file diff`
506 p
="$GUILT_DIR/$branch/$1"
508 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
510 # get the patch header
511 do_get_full_header
"$p" > "$TMP_DIFF"
513 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
515 if [ ! -z "$5" ]; then
518 git
diff --stat $diffopts "$2"
524 git
diff $diffopts "$2" >> "$TMP_DIFF"
526 # move the new patch in
531 # drop the currently applied patch, pop_many_patches does it's own
533 pop_many_patches
"$2" "$3"
535 # push_patch does it's own cd $TOP_DIR
539 # usage: munge_hash_range <hash range>
542 # <hash> - one commit
543 # <hash>.. - hash until head (excludes hash, includes head)
544 # ..<hash> - until hash (includes hash)
545 # <hash1>..<hash2> - from hash to hash (inclusive)
547 # The output of this function is suitable to be passed to "git rev-list"
552 # double .. or space is illegal
561 # e.g., "v0.19-rc1..v0.19"
562 echo ${1%%..*}..
${1#*..};;
572 # usage: get_tmp_file <prefix>
574 # Get a unique filename and create the file in a non-racy way
578 mktemp
"/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
582 # usage: guilt_hook <hook name> <args....>
586 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
590 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
598 # used for: git apply -C <val>
599 guilt_push_diff_context
=1
602 # Parse any part of .git/config that belongs to us
606 autotag
=`git config guilt.autotag`
607 [ -z "$autotag" ] && autotag
=1
610 # The following gets run every time this file is source'd
613 TOP_DIR
=`git rev-parse --show-cdup`
614 if [ -z "$TOP_DIR" ]; then
618 GUILT_DIR
="$GIT_DIR/patches"
622 # most of the time we want to verify that the repo's branch has been
623 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
624 # we must avoid the checks
625 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
628 # do not check the status file format (guilt-repair needs this,
629 # otherwise nothing can do what's necessary to bring the repo into a
631 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
632 [ -s "$GIT_DIR/patches/$branch/status" ] &&
633 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev
/null
&&
634 die
"Status file appears to use old format, try guilt-repair --status"
639 series
="$GUILT_DIR/$branch/series"
640 applied
="$GUILT_DIR/$branch/status"
642 # determine an editor to use for anything interactive (fall back to vi)
644 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
646 # determine a pager to use for anything interactive (fall back to more)
648 [ ! -z "$PAGER" ] && pager
="$PAGER"