guilt: remove a useless cat
[guilt.git] / guilt
blob6dfd8ee517d384d1f47a993b6a097d3bc2f62a31
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2011
6 GUILT_VERSION="0.35"
7 GUILT_NAME="Gloria"
9 GUILT="$(basename "$0")"
11 # If the first argument is one of the below, display the man page instead of
12 # the rather silly and mostly useless usage string
13 case $1 in
14 -h|--h|--he|--hel|--help)
15 shift
16 exec "guilt help" "$GUILT"
17 exit
19 -V|--ver|--versi|--versio|--version)
20 echo "Guilt version $GUILT_VERSION"
21 exit
23 esac
25 # we change directories ourselves
26 SUBDIRECTORY_OK=1
28 . "$(git --exec-path)/git-sh-setup"
31 # Git version check
33 gitver=`git --version | cut -d' ' -f3 | sed -e 's/^debian\.//'`
34 case "$gitver" in
35 1.5.*) ;; # git config
36 1.6.*) ;; # git config
37 1.7.*) ;; # git config
38 *) die "Unsupported version of git ($gitver)" ;;
39 esac
42 # Shell library
44 usage()
46 echo "Usage: guilt $CMDNAME $USAGE" >&2
47 exit 1
50 # echo -n is a bashism, use printf instead
51 _disp()
53 printf "%b" "$*"
56 # echo -e is a bashism, use printf instead
57 disp()
59 printf "%b\n" "$*"
62 noerr()
64 "$@" 2>/dev/null
67 silent()
69 "$@" >/dev/null 2>/dev/null
72 ########
74 GUILT_PATH="$(dirname "$0")"
76 guilt_commands()
78 find "$GUILT_PATH/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f -perm +111 2> /dev/null | sed -e "s/.*\\/$GUILT-//"
79 find "$GUILT_PATH" -maxdepth 1 -name "guilt-*" -type f -perm +111 | sed -e "s/.*\\/$GUILT-//"
82 # by default, we shouldn't fail
83 cmd=
85 if [ $# -ne 0 ]; then
86 # take first arg, and try to execute it
88 arg="$1"
89 dir="$GUILT_PATH"
90 libdir="$GUILT_PATH/../lib/guilt"
92 if [ -x "$dir/guilt-$arg" ]; then
93 cmd="$dir/guilt-$arg"
94 CMDNAME=$arg
95 elif [ -x "$libdir/guilt-$arg" ]; then
96 cmd="$libdir/guilt-$arg"
97 CMDNAME=$arg
98 else
99 # might be a short handed
100 for command in $(guilt_commands); do
101 case $command in
102 $arg*)
103 if [ -x "$dir/guilt-$command" ]; then
104 cmd="$dir/guilt-$command"
105 CMDNAME=$command
106 elif [ -x "$libdir/guilt-$command" ]; then
107 cmd="$libdir/guilt-$command"
108 CMDNAME=$command
111 esac
112 done
114 if [ -z "$cmd" ]; then
115 disp "Command $arg not found" >&2
116 disp "" >&2
117 exit 1
120 shift
121 else
122 # no args passed or invalid command entered, just output help summary
124 disp "Guilt v$GUILT_VERSION"
125 disp ""
126 disp "Pick a command:"
127 guilt_commands | sort | column | column -t | sed -e 's/^/ /'
129 disp ""
130 disp "Example:"
131 disp "\tguilt push"
133 # now, let's exit
134 exit 1
137 ########
140 # Library goodies
143 # usage: valid_patchname <patchname>
144 valid_patchname()
146 case "$1" in
147 /*|./*|../*|*/./*|*/../*|*/.|*/..|*/|*\ *|*\ *)
148 return 1;;
149 *:*)
150 return 1;;
152 return 0;;
153 esac
156 get_branch()
158 silent git symbolic-ref HEAD || \
159 die "Working on a detached HEAD is unsupported."
161 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
164 verify_branch()
166 [ ! -d "$GIT_DIR/patches" ] &&
167 die "Patches directory doesn't exist, try guilt init"
168 [ ! -d "$GIT_DIR/patches/$branch" ] &&
169 die "Branch $branch is not initialized, try guilt init"
170 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
171 die "Branch $branch does not have a series file"
172 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
173 die "Branch $branch does not have a status file"
174 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
175 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
178 get_top()
180 tail -n 1 "$GUILT_DIR/$branch/status"
183 get_prev()
185 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
186 tail -n 2 "$GUILT_DIR/$branch/status" | head_n 1
190 get_full_series()
192 # ignore all lines matching:
193 # - empty lines
194 # - whitespace only
195 # - optional whitespace followed by '#' followed by more
196 # optional whitespace
197 # also remove comments from end of lines
198 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
199 s/[[:space:]]*#.*\$//
203 " "$series"
206 get_series()
208 get_full_series | while read p; do
209 check_guards "$p" && echo "$p"
210 done
213 # usage: check_guards <patch>
214 # Returns 0 if the patch should be pushed
215 check_guards()
217 get_guards "$1" | while read guard; do
218 g=`echo $guard | sed "s/^[+-]//"`
219 case "$guard" in
221 # Push +guard *only if* guard selected
222 silent grep -F "$g" "$guards_file" || return 1
225 # Push -guard *unless* guard selected
226 silent grep -F "$g" "$guards_file" && return 1
227 true
229 esac
230 done
232 # propagate return from subshell
233 return $?
236 # usage: get_guards <patch>
237 get_guards()
239 awk -v pname="$1" '
240 ($1 == pname) {
241 guards = "";
243 for(i=2; i<=NF; i++) {
244 sub(/#[^+-]*/, "", $i);
245 if (length($i)) {
246 if (length(guards))
247 guards = guards " " $i;
248 else
249 guards = $i;
253 print guards;
254 }' < "$series"
257 # usage: set_guards <patch> <guards...>
258 set_guards()
261 p="$1"
262 shift
263 for x in "$@"; do
264 case "$x" in
265 [+-]*)
266 awk -v pname="$p" -v newguard="$x" '{
267 if ($1 == pname)
268 print $0 " #" newguard;
269 else
270 print $0;
271 }' < "$series" > "$series.tmp"
272 mv "$series.tmp" "$series"
275 echo "'$x' is not a valid guard name" >&2
277 esac
278 done
282 # usage: unset_guards <patch> <guards...>
283 unset_guards()
286 p="$1"
287 shift
288 for x in "$@"; do
289 awk -v pname="$p" -v oldguard="$x" '{
290 if ($1 == pname) {
291 guards = "";
293 for(i=2; i<=NF; i++) {
294 if ($i == "#" oldguard)
295 continue;
297 if (length(guards))
298 guards = guards " " $i;
299 else
300 guards = $i;
303 if (length(guards))
304 print $1 " " guards;
305 else
306 print $1;
307 } else
308 print $0;
309 }' < "$series" > "$series.tmp"
310 mv "$series.tmp" "$series"
311 done
315 # usage: do_make_header <hash>
316 do_make_header()
318 # we should try to work with commit objects only
319 if [ `git cat-file -t "$1"` != "commit" ]; then
320 disp "Hash $1 is not a commit object" >&2
321 disp "Aborting..." >&2
322 exit 2
325 git cat-file -p "$1" | awk '
326 BEGIN{headers=1; firstline=1}
327 /^author / && headers {
328 sub(/^author +/, "");
329 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
330 author=$0
332 !headers {
333 print
334 if (firstline) {
335 firstline = 0;
336 print "\nFrom: " author;
339 /^$/ && headers { headers = 0 }
343 # usage: do_get_patch patchfile
344 do_get_patch()
346 awk '
347 BEGIN{}
348 /^(diff |---$|--- )/ {patch = 1}
349 patch == 1 {print $0}
350 END{}
351 ' < "$1"
354 # usage: do_get_header patchfile
355 do_get_header()
357 # The complexity arises from the fact that we want to ignore all but the
358 # Subject line of the header, and any empty lines after it, if these
359 # exist, and inject only the Subject line as the first line of the
360 # commit message.
362 # We also need to strip "from:" lines from the body of the patch
363 # description as these are used by people to manually set the author of
364 # the patch to be different to their local email address and failing to
365 # strip them results in duplicate from: lines in output from guilt
366 # patchbomb.
368 # 1st line prints first encountered Subject line plus empty line.
369 # 2nd line skips standard email/git patch header lines.
370 # 3th line skips "from:" lines throughout the patch description
371 # 4rd line skips tip's additional header lines.
372 # 5th line skips any empty lines thereafter.
373 # 6th line turns off empty line skip upon seeing a non-empty line.
374 # 7th line terminates execution when we encounter the diff
375 awk '
376 BEGIN{body=0; subj=0}
377 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
378 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
379 /^From:/ {next}
380 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
381 /^[ \t\f\n\r\v]*$/ && (body==0){next}
382 /^.*$/ && (body==0){body=1}
383 /^(diff |---$|--- )/{exit}
384 {print $0}
385 END{}
386 ' < "$1"
389 # usage: do_get_full_header patchfile
390 do_get_full_header()
392 # 2nd line checks for the begining of a patch
393 # 3rd line outputs the line if it didn't get pruned by the above rules
394 awk '
395 BEGIN{}
396 /^(diff |---$|--- )/{exit}
397 {print $0}
398 END{}
399 ' < "$1"
402 # usage: assert_head_check
403 assert_head_check()
405 if ! head_check refs/patches/$branch/`get_top`; then
406 die "aborting..."
410 # usage: head_check <expected hash>
411 head_check()
413 # make sure we're not doing funky things to commits that don't
414 # belong to us
416 case "$1" in
418 # the expected hash is empty
419 return 0 ;;
420 refs/patches/$branch/)
421 # the expected hash is an invalid rev
422 return 0 ;;
423 esac
425 if [ "`git rev-parse refs/heads/$branch`" != "`git rev-parse $1`" ]; then
426 disp "Expected HEAD commit $1" >&2
427 disp " got `git rev-parse refs/heads/$branch`" >&2
428 return 1
430 return 0
433 # usage: series_insert_patch <patchname>
434 series_insert_patch()
436 awk -v top="`get_top`" -v new="$1" \
437 'BEGIN{if (top == "") print new;}
439 print $0;
440 if (top != "" && top == $0) print new;
441 }' "$series" > "$series.tmp"
442 mv "$series.tmp" "$series"
445 # usage: series_remove_patch <patchname>
446 series_remove_patch()
448 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
449 mv "$series.tmp" "$series"
452 # usage: series_rename_patch <oldname> <newname>
453 series_rename_patch()
455 # Rename the patch, but preserve comments on the line
456 awk -v old="$1" -v new="$2" '
458 if (index($0, old) == 1)
459 print new substr($0, length(old) + 1);
460 else
461 print $0;
462 }' "$series" > "$series.tmp"
464 mv "$series.tmp" "$series"
467 # usage: series_rename_patch <oldpatchname> <newpatchname>
468 ref_rename_patch()
470 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
471 remove_ref "refs/patches/$branch/$1"
474 # Beware! This is one of the few (only?) places where we modify the applied
475 # file directly
477 # usage: applied_rename_patch <oldname> <newname>
478 applied_rename_patch()
480 awk -v old="$1" -v new="$2" \
481 'BEGIN{FS=":"}
482 { if ($0 == old)
483 print new;
484 else
485 print;
486 }' "$applied" > "$applied.tmp"
488 mv "$applied.tmp" "$applied"
491 # usage: remove_patch_refs
492 # reads patch names from stdin
493 remove_patch_refs()
495 while read pname; do
496 remove_ref "refs/patches/$branch/$pname"
497 done
500 # usage: pop_many_patches <commitish> <number of patches>
501 pop_many_patches()
503 assert_head_check
506 cd_to_toplevel
508 # remove the patches refs
509 tail -n $2 < "$applied" | remove_patch_refs
511 git reset --hard "$1" > /dev/null
513 n=`wc -l < "$applied"`
514 n=`expr $n - $2`
515 head_n "$n" < "$applied" > "$applied.tmp"
516 mv "$applied.tmp" "$applied"
520 # usage: pop_all_patches
521 pop_all_patches()
523 pop_many_patches \
524 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
525 `wc -l < "$applied"`
528 # usage: remove_ref <refname>
529 remove_ref()
532 # does the ref exist?
533 r=`git show-ref --verify -s "$1" 2> /dev/null`
534 [ $? -ne 0 ] && exit 0
536 # remove it
537 git update-ref -d "$1" "$r"
541 # usage: commit patchname parent
542 commit()
545 TMP_MSG=`get_tmp_file msg`
547 p="$GUILT_DIR/$branch/$1"
548 pname="$1"
549 cd_to_toplevel
551 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
553 # grab a commit message out of the patch
554 do_get_header "$p" > "$TMP_MSG"
556 # make a default commit message if patch doesn't contain one
557 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
559 # extract author and date lines from the patch header, and set
560 # GIT_AUTHOR_{NAME,EMAIL,DATE}
561 # prefering Author/AuthorDate lines if available.
562 author_str=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
563 if [ -z "$author_str" ]; then
564 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
567 if [ ! -z "$author_str" ]; then
568 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
569 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
570 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
572 author_date_str=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
573 if [ -z "$author_date_str" ]; then
574 author_date_str=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
576 if [ ! -z "$author_date_str" ]; then
577 export GIT_AUTHOR_DATE=`echo $author_date_str`
581 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
582 ct=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
583 if [ $ct -gt `last_modified "$p"` ]; then
584 ct=`expr $ct + 60`
585 if [ $ct -gt `date +%s` ]; then
586 touch "$p"
587 else
588 touch_date $ct "$p"
592 export GIT_COMMITTER_DATE="`format_last_modified "$p"`"
594 # export GIT_AUTHOR_DATE only if a Date line was unavailable
595 if [ -z "$author_date_str" ]; then
596 export GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
599 # commit
600 treeish=`git write-tree`
601 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
602 git update-ref HEAD $commitish
604 # mark patch as applied
605 git update-ref "refs/patches/$branch/$pname" HEAD
607 rm -f "$TMP_MSG"
611 # usage: push_patch patchname [bail_action]
612 push_patch()
614 __push_patch_bail=0
617 TMP_LOG=`get_tmp_file log`
619 p="$GUILT_DIR/$branch/$1"
620 pname="$1"
621 bail_action="$2"
622 reject="--reject"
624 assert_head_check
625 cd_to_toplevel
627 # apply the patch if and only if there is something to apply
628 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
629 if [ "$bail_action" = abort ]; then
630 reject=""
632 git apply -C$guilt_push_diff_context --index \
633 $reject "$p" > /dev/null 2> "$TMP_LOG"
634 __push_patch_bail=$?
636 if [ $__push_patch_bail -ne 0 ]; then
637 cat "$TMP_LOG" >&2
638 if [ "$bail_action" = "abort" ]; then
639 rm -f "$TMP_LOG" "$TMP_MSG"
640 return $__push_patch_bail
645 commit "$pname" HEAD
647 echo "$pname" >> "$applied"
649 rm -f "$TMP_LOG"
652 # sub-shell funky-ness
653 __push_patch_bail=$?
655 return $__push_patch_bail
658 # usage: must_commit_first
659 must_commit_first()
661 git update-index --refresh --unmerged -q > /dev/null
662 [ `git diff-files | wc -l` -eq 0 ] || return $?
663 [ `git diff-index HEAD | wc -l` -eq 0 ]
664 return $?
667 # usage: fold_patch patchname
668 fold_patch()
670 set -- "$1" "`get_top`"
672 assert_head_check
674 push_patch "$1"
676 # merge the patch headers
678 pcur="$GUILT_DIR/$branch/$2"
679 pnext="$GUILT_DIR/$branch/$1"
680 TMP_CUR=`get_tmp_file diff-cur`
681 TMP_NEXT=`get_tmp_file diff-next`
682 TMP_DIFF=`get_tmp_file diff`
683 do_get_full_header "$pcur" > "$TMP_CUR"
684 do_get_full_header "$pnext" > "$TMP_NEXT"
685 do_get_patch "$pcur" > "$TMP_DIFF"
687 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
688 *,0)
689 # since the new patch header is empty, we
690 # don't have to do anything
692 0,*)
693 # current is empty; new is not
694 mv "$pcur" "$pcur~"
695 cat "$TMP_NEXT" > "$pcur"
696 cat "$TMP_DIFF" >> "$pcur"
698 *,*)
699 mv "$pcur" "$pcur~"
700 cat "$TMP_CUR" > "$pcur"
701 echo >> "$pcur"
702 echo "Header from folded patch '$1':" >> "$pcur"
703 echo >> "$pcur"
704 cat "$TMP_NEXT" >> "$pcur"
705 cat "$TMP_DIFF" >> "$pcur"
707 esac
709 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
712 __refresh_patch "$2" HEAD^^ 2 "" ""
714 series_remove_patch "$1"
717 # usage: refresh_patch patchname gengitdiff incldiffstat
718 refresh_patch()
720 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
723 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
724 # incldiffstat
725 __refresh_patch()
727 assert_head_check
730 TMP_DIFF=`get_tmp_file diff`
732 cd_to_toplevel
733 p="$GUILT_DIR/$branch/$1"
734 pname="$1"
736 # get the patch header
737 do_get_full_header "$p" > "$TMP_DIFF"
739 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
741 if [ -n "$5" -o $diffstat = "true" ]; then
743 echo "---"
744 git diff --stat $diffopts "$2"
745 echo ""
746 ) >> "$TMP_DIFF"
749 # get the new patch
750 git diff --binary $diffopts "$2" >> "$TMP_DIFF"
752 # move the new patch in
753 mv "$p" "$p~"
754 mv "$TMP_DIFF" "$p"
756 # commit
757 commit "$pname" "HEAD~$3"
759 # drop folded patches
760 N=`expr "$3" - 1`
762 # remove the patches refs
763 tail -n $N < "$applied" | remove_patch_refs
765 n=`wc -l < "$applied"`
766 n=`expr $n - $N`
767 head_n "$n" < "$applied" > "$applied.tmp"
768 mv "$applied.tmp" "$applied"
772 # usage: munge_hash_range <hash range>
774 # this means:
775 # <hash> - one commit
776 # <hash>.. - hash until head (excludes hash, includes head)
777 # ..<hash> - until hash (includes hash)
778 # <hash1>..<hash2> - from hash to hash (inclusive)
780 # The output of this function is suitable to be passed to "git rev-list"
781 munge_hash_range()
783 case "$1" in
784 *..*..*|*\ *)
785 # double .. or space is illegal
786 return 1;;
787 ..*)
788 # e.g., "..v0.10"
789 echo ${1#..};;
790 *..)
791 # e.g., "v0.19.."
792 echo ${1%..}..HEAD;;
793 *..*)
794 # e.g., "v0.19-rc1..v0.19"
795 echo ${1%%..*}..${1#*..};;
797 # e.g., "v0.19"
798 echo $1^..$1;;
799 *) # empty
800 return 1;;
801 esac
802 return 0
805 # usage: get_tmp_file <prefix> [<opts>]
807 # Get a unique filename and create the file in a non-racy way
808 get_tmp_file()
810 while true; do
811 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
812 done
815 # usage: guilt_hook <hook name> <args....>
816 guilt_hook()
818 __hookname="$1"
819 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
821 shift
823 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
824 return $?
828 # source the command
830 . "$cmd"
833 # Some constants
836 # used for: git apply -C <val>
837 guilt_push_diff_context=1
839 # default diffstat value: true or false
840 DIFFSTAT_DEFAULT="false"
843 # Parse any part of .git/config that belongs to us
846 # generate diffstat?
847 diffstat=`git config --bool guilt.diffstat`
848 [ -z "$diffstat" ] && diffstat=$DIFFSTAT_DEFAULT
851 # The following gets run every time this file is source'd
854 GUILT_DIR="$GIT_DIR/patches"
856 branch=`get_branch`
858 # most of the time we want to verify that the repo's branch has been
859 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
860 # we must avoid the checks
861 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
862 verify_branch
864 # do not check the status file format (guilt repair needs this,
865 # otherwise nothing can do what's necessary to bring the repo into a
866 # useable state)
867 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
868 [ -s "$GIT_DIR/patches/$branch/status" ] &&
869 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
870 die "Status file appears to use old format, try guilt repair --status"
874 # very useful files
875 series="$GUILT_DIR/$branch/series"
876 applied="$GUILT_DIR/$branch/status"
877 guards_file="$GUILT_DIR/$branch/guards"
879 # determine a pager to use for anything interactive (fall back to more)
880 pager="more"
881 [ ! -z "$PAGER" ] && pager="$PAGER"
883 UNAME_S=`uname -s`
885 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
886 . "$GUILT_PATH/os.$UNAME_S"
887 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
888 . "$GUILT_PATH/../lib/guilt/os.$UNAME_S"
889 else
890 die "Unsupported operating system: $UNAME_S"
893 _main "$@"