Guilt v0.31.1
[guilt.git] / guilt
blobe3a34128471a766a8e8c3f212e957109398fc7f0
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
6 GUILT_VERSION="0.31.1"
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
11 case $1 in
12 -h|--h|--he|--hel|--help)
13 shift
14 exec "guilt-help" "`basename $0`"
15 exit
17 -V|--ver|--versi|--versio|--version)
18 echo "Guilt version $GUILT_VERSION"
19 exit
21 esac
23 # we change directories ourselves
24 SUBDIRECTORY_OK=1
26 . "$(git --exec-path)/git-sh-setup"
29 # Git version check
31 gitver=`git --version | cut -d' ' -f3 | sed -e 's/^debian\.//'`
32 case "$gitver" in
33 1.5.*) ;; # git config
34 1.6.*) ;; # git config
35 *) die "Unsupported version of git ($gitver)" ;;
36 esac
39 # Shell library
42 # echo -n is a bashism, use printf instead
43 _disp()
45 printf "%b" "$*"
48 # echo -e is a bashism, use printf instead
49 disp()
51 printf "%b\n" "$*"
54 noerr()
56 "$@" 2>/dev/null
59 silent()
61 "$@" >/dev/null 2>/dev/null
64 ########
66 guilt_commands()
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
75 cmd=
77 if [ $# -ne 0 ]; then
78 # take first arg, and try to execute it
80 arg="$1"
81 dir=`dirname $0`
83 if [ -x "$dir/guilt-$arg" ]; then
84 cmd=$arg
85 else
86 # might be a short handed
87 for command in $(guilt_commands); do
88 case $command in
89 $arg*)
90 if [ -x "$dir/guilt-$command" ]; then
91 cmd=$command
94 esac
95 done
97 if [ -n "$cmd" ]; then
98 shift
99 exec "$dir/guilt-$cmd" "$@"
101 # this is not reached because of the exec
102 die "Exec failed! Something is terribly wrong!"
103 else
104 disp "Command $arg not found" >&2
105 disp "" >&2
109 # no args passed or invalid command entered, just output help summary
111 disp "Guilt v$GUILT_VERSION"
112 disp ""
113 disp "Pick a command:"
114 guilt_commands | sort | column | column -t | sed -e 's/^/ /'
116 disp ""
117 disp "Example:"
118 disp "\tguilt-push"
119 disp "or"
120 disp "\tguilt push"
122 # now, let's exit
123 exit 1
126 ########
129 # Library goodies
132 # usage: valid_patchname <patchname>
133 valid_patchname()
135 case "$1" in
136 /*|./*|../*|*/./*|*/../*|*/.|*/..|*/|*\ *|*\ *)
137 return 1;;
139 return 0;;
140 esac
143 get_branch()
145 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
148 verify_branch()
150 [ ! -d "$GIT_DIR/patches" ] &&
151 die "Patches directory doesn't exist, try guilt-init"
152 [ ! -d "$GIT_DIR/patches/$branch" ] &&
153 die "Branch $branch is not initialized, try guilt-init"
154 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
155 die "Branch $branch does not have a series file"
156 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
157 die "Branch $branch does not have a status file"
158 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
159 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
162 get_top()
164 tail -1 "$GUILT_DIR/$branch/status"
167 get_prev()
169 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
170 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
174 get_series()
176 # ignore all lines matching:
177 # - empty lines
178 # - whitespace only
179 # - optional whitespace followed by '#' followed by more
180 # optional whitespace
181 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
184 # usage: do_make_header <hash>
185 do_make_header()
187 # we should try to work with commit objects only
188 if [ `git cat-file -t "$1"` != "commit" ]; then
189 disp "Hash $1 is not a commit object" >&2
190 disp "Aborting..." >&2
191 exit 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]*$/, "");
199 author=$0
201 !headers {
202 print
203 if (firstline) {
204 firstline = 0;
205 print "\nFrom: " author;
208 /^$/ && headers { headers = 0 }
212 # usage: do_get_patch patchfile
213 do_get_patch()
215 cat "$1" | awk '
216 BEGIN{}
217 /^(diff |---)/,/END{}/
221 # usage: do_get_header patchfile
222 do_get_header()
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
230 cat "$1" | awk '
231 BEGIN{skip=0}
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}
235 /^(diff |---)/{exit}
236 {print $0}
237 END{}
241 # usage: do_get_full_header patchfile
242 do_get_full_header()
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
246 cat "$1" | awk '
247 BEGIN{}
248 /^(diff |---)/{exit}
249 {print $0}
250 END{}
254 # usage: assert_head_check
255 assert_head_check()
257 if ! head_check refs/patches/$branch/`get_top`; then
258 die "aborting..."
262 # usage: head_check <expected hash>
263 head_check()
265 # make sure we're not doing funky things to commits that don't
266 # belong to us
268 case "$1" in
270 # the expected hash is empty
271 return 0 ;;
272 refs/patches/$branch/)
273 # the expected hash is an invalid rev
274 return 0 ;;
275 esac
277 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
278 disp "Expected HEAD commit $1" >&2
279 disp " got `git rev-parse refs/heads/$branch`" >&2
280 return 1
282 return 0
285 # usage: series_insert_patch <patchname>
286 series_insert_patch()
288 awk -v top="`get_top`" -v new="$1" \
289 'BEGIN{if (top == "") print new;}
291 print $0;
292 if (top != "" && top == $0) print new;
293 }' "$series" > "$series.tmp"
294 mv "$series.tmp" "$series"
297 # usage: series_remove_patch <patchname>
298 series_remove_patch()
300 grep -v "^$1\$" < "$series" > "$series.tmp"
301 mv "$series.tmp" "$series"
304 # usage: series_rename_patch <oldname> <newname>
305 series_rename_patch()
307 awk -v old="$1" -v new="$2" \
308 '{ if ($0 == old) print new; else print $0 }' \
309 "$series" > "$series.tmp"
311 mv "$series.tmp" "$series"
314 # usage: series_rename_patch <oldpatchname> <newpatchname>
315 ref_rename_patch()
317 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
318 remove_ref "refs/patches/$branch/$1"
321 # Beware! This is one of the few (only?) places where we modify the applied
322 # file directly
324 # usage: applied_rename_patch <oldname> <newname>
325 applied_rename_patch()
327 awk -v old="$1" -v new="$2" \
328 'BEGIN{FS=":"}
329 { if ($0 == old)
330 print new;
331 else
332 print;
333 }' "$applied" > "$applied.tmp"
335 mv "$applied.tmp" "$applied"
338 # usage: remove_patch_refs
339 # reads patch names from stdin
340 remove_patch_refs()
342 while read pname; do
343 remove_ref "refs/patches/$branch/$pname"
344 done
347 # usage: pop_many_patches <commitish> <number of patches>
348 pop_many_patches()
350 assert_head_check
353 cd "$TOP_DIR"
355 # remove the patches refs
356 tail -$2 < "$applied" | remove_patch_refs
358 git reset --hard "$1" > /dev/null
359 head -n "-$2" < "$applied" > "$applied.tmp"
360 mv "$applied.tmp" "$applied"
363 # update references to top, bottom, and base
364 update_stack_tags
367 # usage: pop_all_patches
368 pop_all_patches()
370 pop_many_patches \
371 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
372 `wc -l < "$applied"`
375 # usage: remove_ref <refname>
376 remove_ref()
379 # does the ref exist?
380 r=`git show-ref --verify -s "$1" 2> /dev/null`
381 [ $? -ne 0 ] && exit 0
383 # remove it
384 git update-ref -d "$1" "$r"
388 # usage: update_stack_tags [force]
390 # if [force] is non-empty, then do not check for autotagging being enabled,
391 # just assume it is
392 update_stack_tags()
394 # bail if autotagging is not enabled
395 if [ -z "$1" -a $autotag -eq 0 ]; then
396 return 0
399 if [ -s "$applied" ]; then
400 # there are patches applied, therefore we must get the top,
401 # bottom and base hashes, and update the tags
403 git update-ref "refs/tags/${branch}_top" `git rev-parse HEAD`
404 git update-ref "refs/tags/${branch}_bottom" `git rev-parse refs/patches/$branch/$(head -1 < $applied)`
405 git update-ref "refs/tags/${branch}_base" `git rev-parse refs/patches/$branch/$(head -1 < $applied)^`
406 else
407 # there are no patches applied, therefore we must remove the
408 # tags to old top, bottom, and base
410 remove_ref "refs/tags/${branch}_top"
411 remove_ref "refs/tags/${branch}_bottom"
412 remove_ref "refs/tags/${branch}_base"
416 # usage: push_patch patchname [bail_action]
417 push_patch()
419 __push_patch_bail=0
422 TMP_LOG=`get_tmp_file log`
423 TMP_MSG=`get_tmp_file msg`
425 p="$GUILT_DIR/$branch/$1"
426 pname="$1"
427 bail_action="$2"
428 reject="--reject"
430 assert_head_check
431 cd "$TOP_DIR"
433 # apply the patch if and only if there is something to apply
434 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
435 if [ "$bail_action" = abort ]; then
436 reject=""
438 git apply -C$guilt_push_diff_context --index \
439 $reject "$p" > /dev/null 2> "$TMP_LOG"
440 __push_patch_bail=$?
442 if [ $__push_patch_bail -ne 0 ]; then
443 cat "$TMP_LOG" >&2
444 if [ "$bail_action" = "abort" ]; then
445 rm -f "$TMP_LOG" "$TMP_MSG"
446 return $__push_patch_bail
451 # grab a commit message out of the patch
452 do_get_header "$p" > "$TMP_MSG"
454 # make a default commit message if patch doesn't contain one
455 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
457 # extract a From line from the patch header, and set
458 # GIT_AUTHOR_{NAME,EMAIL}
459 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---)/ q' "$p"`
460 if [ ! -z "$author_str" ]; then
461 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
462 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
463 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
466 # must strip nano-second part otherwise git gets very
467 # confused, and makes up strange timestamps from the past
468 # (chances are it decides to interpret it as a unix
469 # timestamp).
470 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
471 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/'`"
472 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
474 # commit
475 treeish=`git write-tree`
476 commitish=`git commit-tree $treeish -p HEAD < "$TMP_MSG"`
477 git update-ref HEAD $commitish
479 # mark patch as applied
480 git update-ref "refs/patches/$branch/$pname" $commitish ""
482 echo "$pname" >> $applied
484 rm -f "$TMP_MSG" "$TMP_LOG"
487 # sub-shell funky-ness
488 __push_patch_bail=$?
490 # update references to top, bottom, and base of the stack
491 update_stack_tags
493 return $__push_patch_bail
496 # usage: must_commit_first
497 must_commit_first()
499 git update-index --refresh --unmerged -q > /dev/null
500 [ `git diff-files | wc -l` -eq 0 ]
501 return $?
504 # usage: fold_patch patchname
505 fold_patch()
507 set -- "$1" "`get_top`"
509 assert_head_check
511 push_patch "$1"
513 __refresh_patch "$2" HEAD^^ 2 "" ""
515 series_remove_patch "$1"
518 # usage: refresh_patch patchname gengitdiff incldiffstat
519 refresh_patch()
521 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
524 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
525 # incldiffstat
526 __refresh_patch()
528 assert_head_check
531 TMP_DIFF=`get_tmp_file diff`
533 cd "$TOP_DIR"
534 p="$GUILT_DIR/$branch/$1"
536 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
538 # get the patch header
539 do_get_full_header "$p" > "$TMP_DIFF"
541 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
543 if [ ! -z "$5" ]; then
545 echo "---"
546 git diff --stat $diffopts "$2"
547 echo ""
548 ) >> "$TMP_DIFF"
551 # get the new patch
552 git diff $diffopts "$2" >> "$TMP_DIFF"
554 # move the new patch in
555 mv "$p" "$p~"
556 mv "$TMP_DIFF" $p
559 # drop the currently applied patch, pop_many_patches does it's own
560 # cd $TOP_DIR
561 pop_many_patches "$2" "$3"
563 # push_patch does it's own cd $TOP_DIR
564 push_patch "$1"
567 # usage: munge_hash_range <hash range>
569 # this means:
570 # <hash> - one commit
571 # <hash>.. - hash until head (excludes hash, includes head)
572 # ..<hash> - until hash (includes hash)
573 # <hash1>..<hash2> - from hash to hash (inclusive)
575 # The output of this function is suitable to be passed to "git rev-list"
576 munge_hash_range()
578 case "$1" in
579 *..*..*|*\ *)
580 # double .. or space is illegal
581 return 1;;
582 ..*)
583 # e.g., "..v0.10"
584 echo ${1#..};;
585 *..)
586 # e.g., "v0.19.."
587 echo ${1%..}..HEAD;;
588 *..*)
589 # e.g., "v0.19-rc1..v0.19"
590 echo ${1%%..*}..${1#*..};;
592 # e.g., "v0.19"
593 echo $1^..$1;;
594 *) # empty
595 return 1;;
596 esac
597 return 0
600 # usage: get_tmp_file <prefix> [<opts>]
602 # Get a unique filename and create the file in a non-racy way
603 get_tmp_file()
605 while true; do
606 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
607 done
610 # usage: guilt_hook <hook name> <args....>
611 guilt_hook()
613 __hookname="$1"
614 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
616 shift
618 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
619 return $?
623 # Some constants
626 # used for: git apply -C <val>
627 guilt_push_diff_context=1
629 # default autotag value
630 AUTOTAG_DEFAULT=1
633 # Parse any part of .git/config that belongs to us
636 # autotag?
637 autotag=`git config guilt.autotag`
638 [ -z "$autotag" ] && autotag=$AUTOTAG_DEFAULT
641 # The following gets run every time this file is source'd
644 TOP_DIR=`git rev-parse --show-cdup`
645 if [ -z "$TOP_DIR" ]; then
646 TOP_DIR="./"
649 GUILT_DIR="$GIT_DIR/patches"
651 branch=`get_branch`
653 # most of the time we want to verify that the repo's branch has been
654 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
655 # we must avoid the checks
656 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
657 verify_branch
659 # do not check the status file format (guilt-repair needs this,
660 # otherwise nothing can do what's necessary to bring the repo into a
661 # useable state)
662 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
663 [ -s "$GIT_DIR/patches/$branch/status" ] &&
664 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
665 die "Status file appears to use old format, try guilt-repair --status"
669 # very useful files
670 series="$GUILT_DIR/$branch/series"
671 applied="$GUILT_DIR/$branch/status"
673 # determine an editor to use for anything interactive (fall back to vi)
674 editor="vi"
675 [ ! -z "$EDITOR" ] && editor="$EDITOR"
677 # determine a pager to use for anything interactive (fall back to more)
678 pager="more"
679 [ ! -z "$PAGER" ] && pager="$PAGER"