3 # Copyright (c) Josef "Jeff" Sipek, 2006, 2007
6 GUILT_VERSION
="0.26-rc1"
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" "$@"
17 -V|
--ver|
--versi|
--versio|
--version)
18 echo "Guilt version $GUILT_VERSION"
23 # we change directories ourselves
32 # echo -e is a bashism, fallback to /bin/echo if the builtin does not supports it
45 "$@" >/dev
/null
2>/dev
/null
52 find "`dirname $0`" -maxdepth 1 -name "guilt-*" -type f
-perm /111 |
sed -e "s/.*\\/`basename $0`-//"
55 if [ "`basename $0`" = "guilt" ]; then
56 # being run as standalone
58 # by default, we shouldn't fail
62 # take first arg, and try to execute it
67 if [ -x "$dir/guilt-$arg" ]; then
70 # might be a short handed
71 for command in $
(guilt_commands
); do
74 if [ -x "$dir/guilt-$command" ]; then
81 if [ -n "$cmd" ]; then
83 exec "$dir/guilt-$cmd" "$@"
85 # this is not reached because of the exec
86 die
"Exec failed! Something is terribly wrong!"
88 echo "Command $arg not found" >&2
93 # no args passed or invalid command entered, just output help summary
95 echo "Guilt v$GUILT_VERSION"
97 echo "Pick a command:"
98 guilt_commands |
sort |
column |
column -t |
sed -e 's/^/\t/'
102 echo -e "\tguilt-push"
104 echo -e "\tguilt push"
116 # usage: valid_patchname <patchname>
120 /*|.
/*|..
/*|
*/.
/*|
*/..
/*|
*/.|
*/..|
*/)
129 git-symbolic-ref HEAD |
sed -e 's,^refs/heads/,,'
134 [ ! -d "$GIT_DIR/patches" ] &&
135 echo "Patches directory doesn't exist, try guilt-init" >&2 &&
137 [ ! -d "$GIT_DIR/patches/$branch" ] &&
138 echo "Branch $branch is not initialized, try guilt-init" >&2 &&
140 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
141 echo "Branch $branch does not have a series file" >&2 &&
143 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
144 echo "Branch $branch does not have a status file" >&2 &&
146 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
147 echo "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit" >&2 &&
155 tail -1 "$GUILT_DIR/$branch/status" | cut
-d: -f 2-
160 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
161 tail -n 2 "$GUILT_DIR/$branch/status" |
head -n 1
167 # ignore all lines matching:
170 # - optional whitespace followed by '#' followed by more
171 # optional whitespace
172 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
175 # usage: do_make_header <hash>
178 # we should try to work with commit objects only
179 if [ `git-cat-file -t "$1"` != "commit" ]; then
180 echo "Hash $1 is not a commit object" >&2
181 echo "Aborting..." >&2
185 git-cat-file
-p "$1" |
awk '
186 BEGIN{headers=1; firstline=1}
187 /^author / && headers {
188 sub(/^author +/, "");
189 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
196 print "\nFrom: " author;
199 /^$/ && headers { headers = 0 }
203 # usage: do_get_header patchfile
206 # The complexity arises from the fact that we want to ignore the
207 # From line and the empty line after it if it exists
209 # 2nd line skips the From line
210 # 3rd line skips the empty line right after a From line
211 do_get_full_header
"$1" |
awk '
213 /^Subject:/ && (NR==1){print substr($0, 10); next}
214 /^From:/{skip=1; next}
215 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
221 # usage: do_get_full_header patchfile
224 # 2nd line checks for the begining of a patch
225 # 3rd line outputs the line if it didn't get pruned by the above rules
234 # usage: assert_head_check
237 if ! head_check
"`tail -1 < "$applied" | cut -d: -f 1`"; then
242 # usage: head_check <expected hash>
245 # make sure we're not doing funky things to commits that don't
247 # if the expected hash is empty, just return
248 [ -z "$1" ] && return 0
250 if [ "`cat "$GIT_DIR/refs
/heads
/$branch"`" != "$1" ]; then
251 echo "Expected HEAD commit $1" >&2
252 echo " got `cat "$GIT_DIR/refs
/heads
/$branch"`" >&2
258 # usage: series_insert_patch <patchname>
259 series_insert_patch
()
261 awk -v top
="`get_top`" -v new
="$1" \
262 'BEGIN{if (top == "") print new;}
265 if (top != "" && top == $0) print new;
266 }' "$series" > "$series.tmp"
267 mv "$series.tmp" "$series"
270 # usage: series_remove_patch <patchname>
271 series_remove_patch
()
273 grep -v "^$1\$" < "$series" > "$series.tmp"
274 mv "$series.tmp" "$series"
277 # usage: series_rename_patch <oldname> <newname>
278 series_rename_patch
()
280 awk -v old
="$1" -v new
="$2" \
281 '{ if ($0 == old) print new; else print $0 }' \
282 "$series" > "$series.tmp"
284 mv "$series.tmp" "$series"
287 # Beware! This is one of the few (only?) places where we modify the applied
290 # usage: applied_rename_patch <oldname> <newname>
291 applied_rename_patch
()
293 awk -v old
="$1" -v new
="$2" \
295 { if ($1 ~ /^[0-9a-f]*$/ && length($1) == 40 && substr($0, 42) == old)
296 print substr($0, 0, 41) new;
299 }' "$applied" > "$applied.tmp"
301 mv "$applied.tmp" "$applied"
304 # usage: pop_many_patches <commitish> <number of patches>
312 git-reset
--hard "$1" > /dev
/null
313 head -n "-$2" < "$applied" > "$applied.tmp"
314 mv "$applied.tmp" "$applied"
317 # update references to top, bottom, and base
321 # usage: pop_all_patches
325 `head -1 "$applied" | cut -d: -f1`^ \
329 # usage: update_stack_tags
332 # bail if autotagging is not enabled
333 if [ $autotag -eq 0 ]; then
337 if [ `wc -l < $applied` -gt 0 ]; then
338 # there are patches applied, therefore we must get the top,
339 # bottom and base hashes, and update the tags
341 git-rev-parse HEAD
> "$GIT_DIR/refs/tags/${branch}_top"
342 head -1 < $applied | cut
-d: -f1 > "$GIT_DIR/refs/tags/${branch}_bottom"
343 git-rev-parse $
(head -1 < $applied | cut
-d: -f1)^
> "$GIT_DIR/refs/tags/${branch}_base"
345 # there are no patches applied, therefore we must remove the
346 # tags to old top, bottom, and base
348 rm -f "$GIT_DIR/refs/tags/${branch}_top"
349 rm -f "$GIT_DIR/refs/tags/${branch}_bottom"
350 rm -f "$GIT_DIR/refs/tags/${branch}_base"
354 # usage: push_patch patchname [bail_action]
360 p
="$GUILT_DIR/$branch/$1"
368 # apply the patch if and only if there is something to apply
369 if [ `git-apply --numstat "$p" | wc -l` -gt 0 ]; then
370 if [ "$bail_action" = abort
]; then
373 git-apply
-C$guilt_push_diff_context \
374 $reject "$p" > /dev
/null
2> /tmp
/guilt.log.$$
377 if [ $__push_patch_bail -ne 0 ]; then
378 cat /tmp
/guilt.log.$$
>&2
379 if [ "$bail_action" = "abort" ]; then
380 rm -f /tmp
/guilt.log.$$
/tmp
/guilt.msg.$$
381 return $__push_patch_bail
385 # FIXME: Path munging is being done, we need to convince
386 # git-apply to just give us list of files with \0 as a
387 # delimiter, and pass -z to git-update-index
388 git-apply
--numstat "$p" | cut
-f 3- | git-update-index
--add --remove --stdin
391 # grab a commit message out of the patch
392 do_get_header
"$p" > /tmp
/guilt.msg.$$
394 # make a default commit message if patch doesn't contain one
395 [ ! -s /tmp
/guilt.msg.$$
] && echo "patch $pname" > /tmp
/guilt.msg.$$
397 # extract a From line from the patch header, and set
398 # GIT_AUTHOR_{NAME,EMAIL}
399 author_str
=`sed -n -e '/^From:/ { s/^From: //; p; q }' "$p"`
400 if [ ! -z "$author_str" ]; then
401 GIT_AUTHOR_NAME
=`echo $author_str | sed -e 's/ *<.*$//'`
402 export GIT_AUTHOR_NAME
="${GIT_AUTHOR_NAME:-" "}"
403 export GIT_AUTHOR_EMAIL
="`echo $author_str | sed -e 's/[^<]*//'`"
405 export GIT_AUTHOR_DATE
="`stat -c %y "$p"`"
406 export GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
409 treeish
=`git-write-tree`
410 commitish
=`git-commit-tree $treeish -p HEAD < /tmp/guilt.msg.$$`
411 echo $commitish > $GIT_DIR/`git-symbolic-ref HEAD`
413 # mark patch as applied
414 echo "$commitish:$pname" >> $applied
417 # update references to top, bottom, and base of the stack
420 rm -f /tmp
/guilt.msg.$$
/tmp
/guilt.log.$$
421 return $__push_patch_bail
424 # usage: must_commit_first
427 [ `git-diff-files | wc -l` -eq 0 ]
431 # usage: fold_patch patchname
434 set -- "$1" "`get_top`"
440 __refresh_patch
"$2" HEAD^^
2 ""
442 series_remove_patch
"$1"
445 # usage: refresh_patch patchname gengitdiff
448 __refresh_patch
"$1" HEAD^
1 "$2"
451 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
458 p
="$GUILT_DIR/$branch/$1"
460 git-diff-files
--name-only |
(while read n
; do git-update-index
"$n" ; done)
462 # get the patch header
463 do_get_full_header
"$p" > /tmp
/guilt.
diff.$$
465 [ ! -z "$4" ] && diffopts
="-C -M --find-copies-harder"
468 git-diff
$diffopts "$2" >> /tmp
/guilt.
diff.$$
470 # move the new patch in
472 mv /tmp
/guilt.
diff.$$
$p
475 # drop the currently applied patch, pop_many_patches does it's own
477 pop_many_patches
"$2" "$3"
479 # push_patch does it's own cd $TOP_DIR
483 # usage: munge_hash_range <hash range>
486 # <hash> - one commit
487 # <hash>.. - hash until head (excludes hash, includes head)
488 # ..<hash> - until hash (includes hash)
489 # <hash1>..<hash2> - from hash to hash (inclusive)
491 # The output of this function is suitable to be passed to git-rev-list
496 # double .. or space is illegal
505 # e.g., "v0.19-rc1..v0.19"
506 echo ${1%%..*}..
${1#*..};;
520 # used for: git-apply -C <val>
521 guilt_push_diff_context
=1
524 # Parse any part of .git/config that belongs to us
528 autotag
=`git-config guilt.autotag`
529 [ -z "$autotag" ] && autotag
=1
532 # The following gets run every time this file is source'd
535 TOP_DIR
=`git-rev-parse --show-cdup`
536 if [ -z "$TOP_DIR" ]; then
540 GUILT_DIR
="$GIT_DIR/patches"
544 # most of the time we want to verify that the repo's branch has been
545 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
546 # we must avoid the checks
547 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
548 verify_branch ||
exit 1
552 series
="$GUILT_DIR/$branch/series"
553 applied
="$GUILT_DIR/$branch/status"
555 # determine an editor to use for anything interactive (fall back to vi)
557 [ ! -z "$EDITOR" ] && editor
="$EDITOR"
559 # determine a pager to use for anything interactive (fall back to more)
561 [ ! -z "$PAGER" ] && pager
="$PAGER"