3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
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 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
146 [ ! -d "$GIT_DIR/patches/$branch" ] &&
147 echo "Branch $branch is not initialized, try guilt-init" >&2 &&
149 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
150 echo "Branch $branch does not have a series file" >&2 &&
152 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
153 echo "Branch $branch does not have a status file" >&2 &&
155 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
156 echo "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit" >&2 &&
164 tail -1 "$GUILT_DIR/$branch/status" | cut
-d: -f 2-
169 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
170 tail -n 2 "$GUILT_DIR/$branch/status" |
head -n 1
176 # ignore all lines matching:
179 # - optional whitespace followed by '#' followed by more
180 # optional whitespace
181 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
184 # usage: do_make_header <hash>
187 # we should try to work with commit objects only
188 if [ `git cat-file -t "$1"` != "commit" ]; then
189 echo "Hash $1 is not a commit object" >&2
190 echo "Aborting..." >&2
194 git cat-file
-p "$1" |
awk '
195 BEGIN{headers=1; firstline=1}
196 /^author / && headers {
197 sub(/^author +/, "");
198 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
205 print "\nFrom: " author;
208 /^$/ && headers { headers = 0 }
212 # usage: do_get_patch patchfile
217 /^(diff |---)/,/END{}/
221 # usage: do_get_header patchfile
224 # The complexity arises from the fact that we want to ignore the
225 # From line and the empty line after it if it exists
227 # 2nd line skips the From line
228 # 3rd line skips the empty line right after a From line
229 # 4th line terminates execution when we encounter the diff
232 /^Subject:/ && (NR==1){print substr($0, 10); next}
233 /^From:/{skip=1; next}
234 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
241 # usage: do_get_full_header patchfile
244 # 2nd line checks for the begining of a patch
245 # 3rd line outputs the line if it didn't get pruned by the above rules
254 # usage: assert_head_check
257 if ! head_check
"`tail -1 < "$applied" | cut -d: -f 1`"; then
262 # usage: head_check <expected hash>
265 # make sure we're not doing funky things to commits that don't
267 # if the expected hash is empty, just return
268 [ -z "$1" ] && return 0
270 if [ "`git show-ref -s "refs
/heads
/$branch"`" != "$1" ]; then
271 echo "Expected HEAD commit $1" >&2
272 echo " got `cat "$GIT_DIR/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" \
315 { if ($1 ~ /^[0-9a-f]*$/ && length($1) == 40 && substr($0, 42) == old)
316 print substr($0, 0, 41) new;
319 }' "$applied" > "$applied.tmp"
321 mv "$applied.tmp" "$applied"
324 # usage: pop_many_patches <commitish> <number of patches>
332 git
reset --hard "$1" > /dev
/null
333 head -n "-$2" < "$applied" > "$applied.tmp"
334 mv "$applied.tmp" "$applied"
337 # update references to top, bottom, and base
341 # usage: pop_all_patches
345 `head -1 "$applied" | cut -d: -f1`^ \
349 # usage: update_stack_tags
352 # bail if autotagging is not enabled
353 if [ $autotag -eq 0 ]; then
357 if [ -s "$applied" ]; then
358 # there are patches applied, therefore we must get the top,
359 # bottom and base hashes, and update the tags
361 mkdir
-p `dirname "$GIT_DIR/refs/tags/${branch}_top"`
362 git rev-parse HEAD
> "$GIT_DIR/refs/tags/${branch}_top"
363 head -1 < $applied | cut
-d: -f1 > "$GIT_DIR/refs/tags/${branch}_bottom"
364 git rev-parse $
(head -1 < $applied | cut
-d: -f1)^
> "$GIT_DIR/refs/tags/${branch}_base"
366 # there are no patches applied, therefore we must remove the
367 # tags to old top, bottom, and base
369 rm -f "$GIT_DIR/refs/tags/${branch}_top"
370 rm -f "$GIT_DIR/refs/tags/${branch}_bottom"
371 rm -f "$GIT_DIR/refs/tags/${branch}_base"
375 # usage: push_patch patchname [bail_action]
381 TMP_LOG
=`get_tmp_file log`
382 TMP_MSG
=`get_tmp_file msg`
384 p
="$GUILT_DIR/$branch/$1"
392 # apply the patch if and only if there is something to apply
393 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
394 if [ "$bail_action" = abort
]; then
397 git apply
-C$guilt_push_diff_context --index \
398 $reject "$p" > /dev
/null
2> "$TMP_LOG"
401 if [ $__push_patch_bail -ne 0 ]; then
403 if [ "$bail_action" = "abort" ]; then
404 rm -f "$TMP_LOG" "$TMP_MSG"
405 return $__push_patch_bail
410 # grab a commit message out of the patch
411 do_get_header
"$p" > "$TMP_MSG"
413 # make a default commit message if patch doesn't contain one
414 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
416 # extract a From line from the patch header, and set
417 # GIT_AUTHOR_{NAME,EMAIL}
418 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q }; /^(diff |---)/ q' "$p"`
419 if [ ! -z "$author_str" ]; then
420 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
421 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
422 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
425 # must strip nano-second part otherwise git gets very
426 # confused, and makes up strange timestamps from the past
427 # (chances are it decides to interpret it as a unix
429 export GIT_AUTHOR_DATE
="`stat -c %y "$p" | sed -e '
430 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/'`"
431 export GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
434 treeish
=`git write-tree`
435 commitish
=`git commit-tree $treeish -p HEAD < "$TMP_MSG"`
436 echo $commitish > $GIT_DIR/`git symbolic-ref HEAD`
438 # mark patch as applied
439 echo "$commitish:$pname" >> $applied
441 rm -f "$TMP_MSG" "$TMP_LOG"
444 # sub-shell funky-ness
447 # update references to top, bottom, and base of the stack
450 return $__push_patch_bail
453 # usage: must_commit_first
456 [ `git diff-files | wc -l` -eq 0 ]
460 # usage: fold_patch patchname
463 set -- "$1" "`get_top`"
469 __refresh_patch
"$2" HEAD^^
2 "" ""
471 series_remove_patch
"$1"
474 # usage: refresh_patch patchname gengitdiff incldiffstat
477 __refresh_patch
"$1" HEAD^
1 "$2" "$3"
480 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
487 TMP_DIFF
=`get_tmp_file diff`
490 p
="$GUILT_DIR/$branch/$1"
492 git diff-files
--name-only |
(while read n
; do git update-index
"$n" ; done)
494 # get the patch header
495 do_get_full_header
"$p" > "$TMP_DIFF"
497 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
499 if [ ! -z "$5" ]; then
502 git
diff --stat $diffopts "$2"
508 git
diff $diffopts "$2" >> "$TMP_DIFF"
510 # move the new patch in
515 # drop the currently applied patch, pop_many_patches does it's own
517 pop_many_patches
"$2" "$3"
519 # push_patch does it's own cd $TOP_DIR
523 # usage: munge_hash_range <hash range>
526 # <hash> - one commit
527 # <hash>.. - hash until head (excludes hash, includes head)
528 # ..<hash> - until hash (includes hash)
529 # <hash1>..<hash2> - from hash to hash (inclusive)
531 # The output of this function is suitable to be passed to "git rev-list"
536 # double .. or space is illegal
545 # e.g., "v0.19-rc1..v0.19"
546 echo ${1%%..*}..
${1#*..};;
556 # usage: get_tmp_file <prefix>
558 # Get a unique filename and create the file in a non-racy way
562 mktemp
"/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
566 # usage: guilt_hook <hook name> <args....>
570 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
574 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
582 # used for: git apply -C <val>
583 guilt_push_diff_context
=1
586 # Parse any part of .git/config that belongs to us
590 autotag
=`git config guilt.autotag`
591 [ -z "$autotag" ] && autotag
=1
594 # The following gets run every time this file is source'd
597 TOP_DIR
=`git rev-parse --show-cdup`
598 if [ -z "$TOP_DIR" ]; then
602 GUILT_DIR
="$GIT_DIR/patches"
606 # most of the time we want to verify that the repo's branch has been
607 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
608 # we must avoid the checks
609 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
610 verify_branch ||
exit 1
614 series
="$GUILT_DIR/$branch/series"
615 applied
="$GUILT_DIR/$branch/status"
617 # determine an editor to use for anything interactive (fall back to vi)
619 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
621 # determine a pager to use for anything interactive (fall back to more)
623 [ ! -z "$PAGER" ] && pager
="$PAGER"