Remove autotagging
[guilt.git] / guilt
blob08dc25057120c304dbaa7e56e92674cb82e5fa5f
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2008
6 GUILT_VERSION="0.31.2"
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 silent git symbolic-ref HEAD || \
146 die "Working on a detached HEAD is unsupported."
148 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
151 verify_branch()
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"
165 get_top()
167 tail -1 "$GUILT_DIR/$branch/status"
170 get_prev()
172 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
173 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
177 get_series()
179 # ignore all lines matching:
180 # - empty lines
181 # - whitespace only
182 # - optional whitespace followed by '#' followed by more
183 # optional whitespace
184 grep -ve '^[[:space:]]*\(#.*\)*$' "$series"
187 # usage: do_make_header <hash>
188 do_make_header()
190 # we should try to work with commit objects only
191 if [ `git cat-file -t "$1"` != "commit" ]; then
192 disp "Hash $1 is not a commit object" >&2
193 disp "Aborting..." >&2
194 exit 2
197 git cat-file -p "$1" | awk '
198 BEGIN{headers=1; firstline=1}
199 /^author / && headers {
200 sub(/^author +/, "");
201 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
202 author=$0
204 !headers {
205 print
206 if (firstline) {
207 firstline = 0;
208 print "\nFrom: " author;
211 /^$/ && headers { headers = 0 }
215 # usage: do_get_patch patchfile
216 do_get_patch()
218 cat "$1" | awk '
219 BEGIN{}
220 /^(diff |---)/,/END{}/
224 # usage: do_get_header patchfile
225 do_get_header()
227 # The complexity arises from the fact that we want to ignore the
228 # From line and the empty line after it if it exists
230 # 2nd line skips the From line
231 # 3rd line skips the empty line right after a From line
232 # 4th line terminates execution when we encounter the diff
233 cat "$1" | awk '
234 BEGIN{skip=0}
235 /^Subject:/ && (NR==1){print substr($0, 10); next}
236 /^From:/{skip=1; next}
237 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
238 /^(diff |---)/{exit}
239 {print $0}
240 END{}
244 # usage: do_get_full_header patchfile
245 do_get_full_header()
247 # 2nd line checks for the begining of a patch
248 # 3rd line outputs the line if it didn't get pruned by the above rules
249 cat "$1" | awk '
250 BEGIN{}
251 /^(diff |---)/{exit}
252 {print $0}
253 END{}
257 # usage: assert_head_check
258 assert_head_check()
260 if ! head_check refs/patches/$branch/`get_top`; then
261 die "aborting..."
265 # usage: head_check <expected hash>
266 head_check()
268 # make sure we're not doing funky things to commits that don't
269 # belong to us
271 case "$1" in
273 # the expected hash is empty
274 return 0 ;;
275 refs/patches/$branch/)
276 # the expected hash is an invalid rev
277 return 0 ;;
278 esac
280 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
281 disp "Expected HEAD commit $1" >&2
282 disp " got `git rev-parse refs/heads/$branch`" >&2
283 return 1
285 return 0
288 # usage: series_insert_patch <patchname>
289 series_insert_patch()
291 awk -v top="`get_top`" -v new="$1" \
292 'BEGIN{if (top == "") print new;}
294 print $0;
295 if (top != "" && top == $0) print new;
296 }' "$series" > "$series.tmp"
297 mv "$series.tmp" "$series"
300 # usage: series_remove_patch <patchname>
301 series_remove_patch()
303 grep -v "^$1\$" < "$series" > "$series.tmp"
304 mv "$series.tmp" "$series"
307 # usage: series_rename_patch <oldname> <newname>
308 series_rename_patch()
310 awk -v old="$1" -v new="$2" \
311 '{ if ($0 == old) print new; else print $0 }' \
312 "$series" > "$series.tmp"
314 mv "$series.tmp" "$series"
317 # usage: series_rename_patch <oldpatchname> <newpatchname>
318 ref_rename_patch()
320 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
321 remove_ref "refs/patches/$branch/$1"
324 # Beware! This is one of the few (only?) places where we modify the applied
325 # file directly
327 # usage: applied_rename_patch <oldname> <newname>
328 applied_rename_patch()
330 awk -v old="$1" -v new="$2" \
331 'BEGIN{FS=":"}
332 { if ($0 == old)
333 print new;
334 else
335 print;
336 }' "$applied" > "$applied.tmp"
338 mv "$applied.tmp" "$applied"
341 # usage: remove_patch_refs
342 # reads patch names from stdin
343 remove_patch_refs()
345 while read pname; do
346 remove_ref "refs/patches/$branch/$pname"
347 done
350 # usage: pop_many_patches <commitish> <number of patches>
351 pop_many_patches()
353 assert_head_check
356 cd "$TOP_DIR"
358 # remove the patches refs
359 tail -$2 < "$applied" | remove_patch_refs
361 git reset --hard "$1" > /dev/null
362 head -n "-$2" < "$applied" > "$applied.tmp"
363 mv "$applied.tmp" "$applied"
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: commit patchname parent
389 commit()
392 TMP_MSG=`get_tmp_file msg`
394 p="$GUILT_DIR/$branch/$1"
395 pname="$1"
396 cd_to_toplevel
398 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
400 # grab a commit message out of the patch
401 do_get_header "$p" > "$TMP_MSG"
403 # make a default commit message if patch doesn't contain one
404 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
406 # extract a From line from the patch header, and set
407 # GIT_AUTHOR_{NAME,EMAIL}
408 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---)/ q' "$p"`
409 if [ ! -z "$author_str" ]; then
410 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
411 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
412 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
415 # must strip nano-second part otherwise git gets very
416 # confused, and makes up strange timestamps from the past
417 # (chances are it decides to interpret it as a unix
418 # timestamp).
419 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
420 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/'`"
421 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
423 # commit
424 treeish=`git write-tree`
425 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
426 git update-ref HEAD $commitish
428 # mark patch as applied
429 git update-ref "refs/patches/$branch/$pname" HEAD
431 rm -f "$TMP_MSG"
435 # usage: push_patch patchname [bail_action]
436 push_patch()
438 __push_patch_bail=0
441 TMP_LOG=`get_tmp_file log`
443 p="$GUILT_DIR/$branch/$1"
444 pname="$1"
445 bail_action="$2"
446 reject="--reject"
448 assert_head_check
449 cd_to_toplevel
451 # apply the patch if and only if there is something to apply
452 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
453 if [ "$bail_action" = abort ]; then
454 reject=""
456 git apply -C$guilt_push_diff_context --index \
457 $reject "$p" > /dev/null 2> "$TMP_LOG"
458 __push_patch_bail=$?
460 if [ $__push_patch_bail -ne 0 ]; then
461 cat "$TMP_LOG" >&2
462 if [ "$bail_action" = "abort" ]; then
463 rm -f "$TMP_LOG" "$TMP_MSG"
464 return $__push_patch_bail
469 commit "$pname" HEAD
471 echo "$pname" >> $applied
473 rm -f "$TMP_LOG"
476 # sub-shell funky-ness
477 __push_patch_bail=$?
479 return $__push_patch_bail
482 # usage: must_commit_first
483 must_commit_first()
485 git update-index --refresh --unmerged -q > /dev/null
486 [ `git diff-files | wc -l` -eq 0 ] || return $?
487 [ `git diff-index HEAD | wc -l` -eq 0 ]
488 return $?
491 # usage: fold_patch patchname
492 fold_patch()
494 set -- "$1" "`get_top`"
496 assert_head_check
498 push_patch "$1"
500 __refresh_patch "$2" HEAD^^ 2 "" ""
502 series_remove_patch "$1"
505 # usage: refresh_patch patchname gengitdiff incldiffstat
506 refresh_patch()
508 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
511 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
512 # incldiffstat
513 __refresh_patch()
515 assert_head_check
518 TMP_DIFF=`get_tmp_file diff`
520 cd "$TOP_DIR"
521 p="$GUILT_DIR/$branch/$1"
522 pname="$1"
524 # get the patch header
525 do_get_full_header "$p" > "$TMP_DIFF"
527 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
529 if [ ! -z "$5" ]; then
531 echo "---"
532 git diff --stat $diffopts "$2"
533 echo ""
534 ) >> "$TMP_DIFF"
537 # get the new patch
538 git diff $diffopts "$2" >> "$TMP_DIFF"
540 # move the new patch in
541 mv "$p" "$p~"
542 mv "$TMP_DIFF" $p
544 # commit
545 commit "$pname" "HEAD~$3"
547 # drop folded patches
548 N=`expr "$3" - 1`
550 # remove the patches refs
551 tail -$N < "$applied" | remove_patch_refs
553 head -n "-$N" < "$applied" > "$applied.tmp"
554 mv "$applied.tmp" "$applied"
558 # usage: munge_hash_range <hash range>
560 # this means:
561 # <hash> - one commit
562 # <hash>.. - hash until head (excludes hash, includes head)
563 # ..<hash> - until hash (includes hash)
564 # <hash1>..<hash2> - from hash to hash (inclusive)
566 # The output of this function is suitable to be passed to "git rev-list"
567 munge_hash_range()
569 case "$1" in
570 *..*..*|*\ *)
571 # double .. or space is illegal
572 return 1;;
573 ..*)
574 # e.g., "..v0.10"
575 echo ${1#..};;
576 *..)
577 # e.g., "v0.19.."
578 echo ${1%..}..HEAD;;
579 *..*)
580 # e.g., "v0.19-rc1..v0.19"
581 echo ${1%%..*}..${1#*..};;
583 # e.g., "v0.19"
584 echo $1^..$1;;
585 *) # empty
586 return 1;;
587 esac
588 return 0
591 # usage: get_tmp_file <prefix> [<opts>]
593 # Get a unique filename and create the file in a non-racy way
594 get_tmp_file()
596 while true; do
597 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
598 done
601 # usage: guilt_hook <hook name> <args....>
602 guilt_hook()
604 __hookname="$1"
605 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
607 shift
609 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
610 return $?
614 # Some constants
617 # used for: git apply -C <val>
618 guilt_push_diff_context=1
621 # Parse any part of .git/config that belongs to us
625 # The following gets run every time this file is source'd
628 GUILT_DIR="$GIT_DIR/patches"
630 branch=`get_branch`
632 # most of the time we want to verify that the repo's branch has been
633 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
634 # we must avoid the checks
635 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
636 verify_branch
638 # do not check the status file format (guilt-repair needs this,
639 # otherwise nothing can do what's necessary to bring the repo into a
640 # useable state)
641 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
642 [ -s "$GIT_DIR/patches/$branch/status" ] &&
643 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
644 die "Status file appears to use old format, try guilt-repair --status"
648 # very useful files
649 series="$GUILT_DIR/$branch/series"
650 applied="$GUILT_DIR/$branch/status"
652 # determine an editor to use for anything interactive (fall back to vi)
653 editor="vi"
654 [ ! -z "$EDITOR" ] && editor="$EDITOR"
656 # determine a pager to use for anything interactive (fall back to more)
657 pager="more"
658 [ ! -z "$PAGER" ] && pager="$PAGER"