{fork,import}: check patchnames for colons
[guilt.git] / guilt
blob44b9fd337deb439a0c9d3f058dea4f9706566487
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2009
6 GUILT_VERSION="0.32.1"
7 GUILT_NAME="Refugee"
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;;
138 *:*)
139 return 1;;
141 return 0;;
142 esac
145 get_branch()
147 silent git symbolic-ref HEAD || \
148 die "Working on a detached HEAD is unsupported."
150 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
153 verify_branch()
155 [ ! -d "$GIT_DIR/patches" ] &&
156 die "Patches directory doesn't exist, try guilt-init"
157 [ ! -d "$GIT_DIR/patches/$branch" ] &&
158 die "Branch $branch is not initialized, try guilt-init"
159 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
160 die "Branch $branch does not have a series file"
161 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
162 die "Branch $branch does not have a status file"
163 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
164 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
167 get_top()
169 tail -1 "$GUILT_DIR/$branch/status"
172 get_prev()
174 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
175 tail -n 2 "$GUILT_DIR/$branch/status" | head -n 1
179 get_full_series()
181 # ignore all lines matching:
182 # - empty lines
183 # - whitespace only
184 # - optional whitespace followed by '#' followed by more
185 # optional whitespace
186 # also remove comments from end of lines
187 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
188 s/[[:space:]]*#.*\$//
192 " $series
195 get_series()
197 get_full_series | while read p; do
198 check_guards "$p" && echo "$p"
199 done
202 # usage: check_guards <patch>
203 # Returns 0 if the patch should be pushed
204 check_guards()
206 get_guards "$1" | while read guard; do
207 case "$guard" in
209 # Push +guard *only if* guard selected
210 silent grep -e "^$guard\$" "$guards_file" || return 1
213 # Push -guard *unless* guard selected
214 silent grep -e "^$guard\$" "$guards_file" && return 1
216 esac
217 done
219 # propagate return from subshell
220 return $?
223 # usage: get_guards <patch>
224 get_guards()
226 awk -v pname="$1" '
227 ($1 == pname) {
228 guards = "";
230 for(i=2; i<=NF; i++) {
231 sub(/#[^+-]*/, "", $i);
232 if (length($i)) {
233 if (length(guards))
234 guards = guards " " $i;
235 else
236 guards = $i;
240 print guards;
241 }' < "$series"
244 # usage: set_guards <patch> <guards...>
245 set_guards()
248 p="$1"
249 shift
250 for x in "$@"; do
251 case "$x" in
252 [+-]*)
253 awk -v pname="$p" -v newguard="$x" '{
254 if ($1 == pname)
255 print $0 " #" newguard;
256 else
257 print $0;
258 }' < "$series" > "$series.tmp"
259 mv "$series.tmp" "$series"
262 echo "'$x' is not a valid guard name" >&2
264 esac
265 done
269 # usage: unset_guards <patch> <guards...>
270 unset_guards()
273 p="$1"
274 shift
275 for x in "$@"; do
276 awk -v pname="$p" -v oldguard="$x" '{
277 if ($1 == pname) {
278 guards = "";
280 for(i=2; i<=NF; i++) {
281 if ($i == "#" oldguard)
282 continue;
284 if (length(guards))
285 guards = guards " " $i;
286 else
287 guards = $i;
290 if (length(guards))
291 print $1 " " guards;
292 else
293 print $1;
294 } else
295 print $0;
296 }' < "$series" > "$series.tmp"
297 mv "$series.tmp" "$series"
298 done
302 # usage: do_make_header <hash>
303 do_make_header()
305 # we should try to work with commit objects only
306 if [ `git cat-file -t "$1"` != "commit" ]; then
307 disp "Hash $1 is not a commit object" >&2
308 disp "Aborting..." >&2
309 exit 2
312 git cat-file -p "$1" | awk '
313 BEGIN{headers=1; firstline=1}
314 /^author / && headers {
315 sub(/^author +/, "");
316 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
317 author=$0
319 !headers {
320 print
321 if (firstline) {
322 firstline = 0;
323 print "\nFrom: " author;
326 /^$/ && headers { headers = 0 }
330 # usage: do_get_patch patchfile
331 do_get_patch()
333 cat "$1" | awk '
334 BEGIN{}
335 /^(diff |---$|--- )/,/END{}/
339 # usage: do_get_header patchfile
340 do_get_header()
342 # The complexity arises from the fact that we want to ignore the
343 # From line and the empty line after it if it exists
345 # 2nd line skips the From line
346 # 3rd line skips the empty line right after a From line
347 # 4th line terminates execution when we encounter the diff
348 cat "$1" | awk '
349 BEGIN{skip=0}
350 /^Subject:/ && (NR==1){print substr($0, 10); next}
351 /^From:/{skip=1; next}
352 /^[ \t\f\n\r\v]*$/ && (skip==1){skip=0; next}
353 /^(diff |---$|--- )/{exit}
354 {print $0}
355 END{}
359 # usage: do_get_full_header patchfile
360 do_get_full_header()
362 # 2nd line checks for the begining of a patch
363 # 3rd line outputs the line if it didn't get pruned by the above rules
364 cat "$1" | awk '
365 BEGIN{}
366 /^(diff |---$|--- )/{exit}
367 {print $0}
368 END{}
372 # usage: assert_head_check
373 assert_head_check()
375 if ! head_check refs/patches/$branch/`get_top`; then
376 die "aborting..."
380 # usage: head_check <expected hash>
381 head_check()
383 # make sure we're not doing funky things to commits that don't
384 # belong to us
386 case "$1" in
388 # the expected hash is empty
389 return 0 ;;
390 refs/patches/$branch/)
391 # the expected hash is an invalid rev
392 return 0 ;;
393 esac
395 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
396 disp "Expected HEAD commit $1" >&2
397 disp " got `git rev-parse refs/heads/$branch`" >&2
398 return 1
400 return 0
403 # usage: series_insert_patch <patchname>
404 series_insert_patch()
406 awk -v top="`get_top`" -v new="$1" \
407 'BEGIN{if (top == "") print new;}
409 print $0;
410 if (top != "" && top == $0) print new;
411 }' "$series" > "$series.tmp"
412 mv "$series.tmp" "$series"
415 # usage: series_remove_patch <patchname>
416 series_remove_patch()
418 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
419 mv "$series.tmp" "$series"
422 # usage: series_rename_patch <oldname> <newname>
423 series_rename_patch()
425 # Rename the patch, but preserve comments on the line
426 awk -v old="$1" -v new="$2" '
428 if (index($0, old) == 1)
429 print new substr($0, length(old) + 1);
430 else
431 print $0;
432 }' "$series" > "$series.tmp"
434 mv "$series.tmp" "$series"
437 # usage: series_rename_patch <oldpatchname> <newpatchname>
438 ref_rename_patch()
440 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
441 remove_ref "refs/patches/$branch/$1"
444 # Beware! This is one of the few (only?) places where we modify the applied
445 # file directly
447 # usage: applied_rename_patch <oldname> <newname>
448 applied_rename_patch()
450 awk -v old="$1" -v new="$2" \
451 'BEGIN{FS=":"}
452 { if ($0 == old)
453 print new;
454 else
455 print;
456 }' "$applied" > "$applied.tmp"
458 mv "$applied.tmp" "$applied"
461 # usage: remove_patch_refs
462 # reads patch names from stdin
463 remove_patch_refs()
465 while read pname; do
466 remove_ref "refs/patches/$branch/$pname"
467 done
470 # usage: pop_many_patches <commitish> <number of patches>
471 pop_many_patches()
473 assert_head_check
476 cd_to_toplevel
478 # remove the patches refs
479 tail -$2 < "$applied" | remove_patch_refs
481 git reset --hard "$1" > /dev/null
482 head -n "-$2" < "$applied" > "$applied.tmp"
483 mv "$applied.tmp" "$applied"
487 # usage: pop_all_patches
488 pop_all_patches()
490 pop_many_patches \
491 `git rev-parse refs/patches/$branch/$(head -1 "$applied")^` \
492 `wc -l < "$applied"`
495 # usage: remove_ref <refname>
496 remove_ref()
499 # does the ref exist?
500 r=`git show-ref --verify -s "$1" 2> /dev/null`
501 [ $? -ne 0 ] && exit 0
503 # remove it
504 git update-ref -d "$1" "$r"
508 # usage: commit patchname parent
509 commit()
512 TMP_MSG=`get_tmp_file msg`
514 p="$GUILT_DIR/$branch/$1"
515 pname="$1"
516 cd_to_toplevel
518 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
520 # grab a commit message out of the patch
521 do_get_header "$p" > "$TMP_MSG"
523 # make a default commit message if patch doesn't contain one
524 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
526 # extract a From line from the patch header, and set
527 # GIT_AUTHOR_{NAME,EMAIL}
528 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
529 if [ ! -z "$author_str" ]; then
530 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
531 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
532 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
535 # must strip nano-second part otherwise git gets very
536 # confused, and makes up strange timestamps from the past
537 # (chances are it decides to interpret it as a unix
538 # timestamp).
539 export GIT_AUTHOR_DATE="`stat -c %y "$p" | sed -e '
540 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/'`"
541 export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
543 # commit
544 treeish=`git write-tree`
545 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
546 git update-ref HEAD $commitish
548 # mark patch as applied
549 git update-ref "refs/patches/$branch/$pname" HEAD
551 rm -f "$TMP_MSG"
555 # usage: push_patch patchname [bail_action]
556 push_patch()
558 __push_patch_bail=0
561 TMP_LOG=`get_tmp_file log`
563 p="$GUILT_DIR/$branch/$1"
564 pname="$1"
565 bail_action="$2"
566 reject="--reject"
568 assert_head_check
569 cd_to_toplevel
571 # apply the patch if and only if there is something to apply
572 if [ `git apply --numstat "$p" | wc -l` -gt 0 ]; then
573 if [ "$bail_action" = abort ]; then
574 reject=""
576 git apply -C$guilt_push_diff_context --index \
577 $reject "$p" > /dev/null 2> "$TMP_LOG"
578 __push_patch_bail=$?
580 if [ $__push_patch_bail -ne 0 ]; then
581 cat "$TMP_LOG" >&2
582 if [ "$bail_action" = "abort" ]; then
583 rm -f "$TMP_LOG" "$TMP_MSG"
584 return $__push_patch_bail
589 commit "$pname" HEAD
591 echo "$pname" >> $applied
593 rm -f "$TMP_LOG"
596 # sub-shell funky-ness
597 __push_patch_bail=$?
599 return $__push_patch_bail
602 # usage: must_commit_first
603 must_commit_first()
605 git update-index --refresh --unmerged -q > /dev/null
606 [ `git diff-files | wc -l` -eq 0 ] || return $?
607 [ `git diff-index HEAD | wc -l` -eq 0 ]
608 return $?
611 # usage: fold_patch patchname
612 fold_patch()
614 set -- "$1" "`get_top`"
616 assert_head_check
618 push_patch "$1"
620 # merge the patch headers
622 pcur="$GUILT_DIR/$branch/$2"
623 pnext="$GUILT_DIR/$branch/$1"
624 TMP_CUR=`get_tmp_file diff-cur`
625 TMP_NEXT=`get_tmp_file diff-next`
626 TMP_DIFF=`get_tmp_file diff`
627 do_get_full_header "$pcur" > "$TMP_CUR"
628 do_get_full_header "$pnext" > "$TMP_NEXT"
629 do_get_patch "$pcur" > "$TMP_DIFF"
631 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
632 *,0)
633 # since the new patch header is empty, we
634 # don't have to do anything
636 0,*)
637 # current is empty; new is not
638 mv "$pcur" "$pcur~"
639 cat "$TMP_NEXT" > "$pcur"
640 cat "$TMP_DIFF" >> "$pcur"
642 *,*)
643 mv "$pcur" "$pcur~"
644 cat "$TMP_CUR" > "$pcur"
645 echo >> "$pcur"
646 echo "Header from folded patch '$1':" >> "$pcur"
647 echo >> "$pcur"
648 cat "$TMP_NEXT" >> "$pcur"
649 cat "$TMP_DIFF" >> "$pcur"
651 esac
653 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
656 __refresh_patch "$2" HEAD^^ 2 "" ""
658 series_remove_patch "$1"
661 # usage: refresh_patch patchname gengitdiff incldiffstat
662 refresh_patch()
664 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
667 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
668 # incldiffstat
669 __refresh_patch()
671 assert_head_check
674 TMP_DIFF=`get_tmp_file diff`
676 cd_to_toplevel
677 p="$GUILT_DIR/$branch/$1"
678 pname="$1"
680 # get the patch header
681 do_get_full_header "$p" > "$TMP_DIFF"
683 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
685 if [ -n "$5" -o $diffstat = "true" ]; then
687 echo "---"
688 git diff --stat $diffopts "$2"
689 echo ""
690 ) >> "$TMP_DIFF"
693 # get the new patch
694 git diff --binary $diffopts "$2" >> "$TMP_DIFF"
696 # move the new patch in
697 mv "$p" "$p~"
698 mv "$TMP_DIFF" $p
700 # commit
701 commit "$pname" "HEAD~$3"
703 # drop folded patches
704 N=`expr "$3" - 1`
706 # remove the patches refs
707 tail -$N < "$applied" | remove_patch_refs
709 head -n "-$N" < "$applied" > "$applied.tmp"
710 mv "$applied.tmp" "$applied"
714 # usage: munge_hash_range <hash range>
716 # this means:
717 # <hash> - one commit
718 # <hash>.. - hash until head (excludes hash, includes head)
719 # ..<hash> - until hash (includes hash)
720 # <hash1>..<hash2> - from hash to hash (inclusive)
722 # The output of this function is suitable to be passed to "git rev-list"
723 munge_hash_range()
725 case "$1" in
726 *..*..*|*\ *)
727 # double .. or space is illegal
728 return 1;;
729 ..*)
730 # e.g., "..v0.10"
731 echo ${1#..};;
732 *..)
733 # e.g., "v0.19.."
734 echo ${1%..}..HEAD;;
735 *..*)
736 # e.g., "v0.19-rc1..v0.19"
737 echo ${1%%..*}..${1#*..};;
739 # e.g., "v0.19"
740 echo $1^..$1;;
741 *) # empty
742 return 1;;
743 esac
744 return 0
747 # usage: get_tmp_file <prefix> [<opts>]
749 # Get a unique filename and create the file in a non-racy way
750 get_tmp_file()
752 while true; do
753 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
754 done
757 # usage: guilt_hook <hook name> <args....>
758 guilt_hook()
760 __hookname="$1"
761 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
763 shift
765 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
766 return $?
770 # Some constants
773 # used for: git apply -C <val>
774 guilt_push_diff_context=1
776 # default diffstat value: true or false
777 DIFFSTAT_DEFAULT="false"
780 # Parse any part of .git/config that belongs to us
783 # generate diffstat?
784 diffstat=`git config --bool guilt.diffstat`
785 [ -z "$diffstat" ] && diffstat=$DIFFSTAT_DEFAULT
788 # The following gets run every time this file is source'd
791 GUILT_DIR="$GIT_DIR/patches"
793 branch=`get_branch`
795 # most of the time we want to verify that the repo's branch has been
796 # initialized, but every once in a blue moon (e.g., we want to run guilt-init),
797 # we must avoid the checks
798 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
799 verify_branch
801 # do not check the status file format (guilt-repair needs this,
802 # otherwise nothing can do what's necessary to bring the repo into a
803 # useable state)
804 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
805 [ -s "$GIT_DIR/patches/$branch/status" ] &&
806 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
807 die "Status file appears to use old format, try guilt-repair --status"
811 # very useful files
812 series="$GUILT_DIR/$branch/series"
813 applied="$GUILT_DIR/$branch/status"
814 guards_file="$GUILT_DIR/$branch/guards"
816 # determine a pager to use for anything interactive (fall back to more)
817 pager="more"
818 [ ! -z "$PAGER" ] && pager="$PAGER"