import-commit: disallow <>(), in generated patch names
[guilt.git] / guilt
blob552ec553f11cd23bab83509796c56e5a2241a81f
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2015
6 GUILT_VERSION="0.36"
7 GUILT_NAME="Irgendwann"
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 "$@"
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 # Shell library
33 usage()
35 echo "Usage: guilt $CMDNAME $USAGE" >&2
36 exit 1
39 # Print arguments, but no trailing newline.
40 # (echo -n is a bashism, use printf instead)
41 _disp()
43 printf "%s" "$*"
46 # Print arguments.
47 # (echo -E is a bashism, use printf instead)
48 disp()
50 printf "%s\n" "$*"
53 # Print arguments, processing backslash sequences.
54 # (echo -e is a bashism, use printf instead)
55 disp_e()
57 printf "%b\n" "$*"
60 noerr()
62 "$@" 2>/dev/null
65 silent()
67 "$@" >/dev/null 2>/dev/null
70 ########
72 GUILT_PATH="$(dirname "$0")"
74 guilt_commands()
76 # GNU Find no longer accepts -perm +111, even though the rest
77 # world (MacOS, Solaris, BSD, etc.) does. Sigh. Using -executable
78 # is arugably better, but it is a GNU extension. Since this isn't
79 # a fast path and guilt doesn't use autoconf, test for it as needed.
80 if find . -maxdepth 0 -executable > /dev/null 2>&1 ; then
81 exe_test="-executable"
82 else
83 exe_test="-perm +111"
85 find "$GUILT_PATH/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f $exe_test 2> /dev/null | sed -e "s/.*\\/$GUILT-//"
86 find "$GUILT_PATH" -maxdepth 1 -name "guilt-*" -type f $exe_test | sed -e "s/.*\\/$GUILT-//"
89 # by default, we shouldn't fail
90 cmd=
92 if [ $# -ne 0 ]; then
93 # take first arg, and try to execute it
95 arg="$1"
96 dir="$GUILT_PATH"
97 libdir="$GUILT_PATH/../lib/guilt"
99 if [ -x "$dir/guilt-$arg" ]; then
100 cmd="$dir/guilt-$arg"
101 CMDNAME=$arg
102 elif [ -x "$libdir/guilt-$arg" ]; then
103 cmd="$libdir/guilt-$arg"
104 CMDNAME=$arg
105 else
106 # might be a short handed
107 for command in $(guilt_commands); do
108 case $command in
109 $arg*)
110 if [ -x "$dir/guilt-$command" ]; then
111 cmd="$dir/guilt-$command"
112 CMDNAME=$command
113 elif [ -x "$libdir/guilt-$command" ]; then
114 cmd="$libdir/guilt-$command"
115 CMDNAME=$command
118 esac
119 done
121 if [ -z "$cmd" ]; then
122 disp "Command $arg not found" >&2
123 disp "" >&2
124 exit 1
126 set_reflog_action "guilt-$CMDNAME"
128 shift
129 else
130 # no args passed or invalid command entered, just output help summary
132 disp "Guilt v$GUILT_VERSION"
133 disp ""
134 disp "Pick a command:"
135 guilt_commands | sort | column | column -t | sed -e 's/^/ /'
137 disp ""
138 disp "Example:"
139 disp_e "\tguilt push"
141 # now, let's exit
142 exit 1
145 ########
148 # Library goodies
151 # usage: valid_patchname <patchname>
152 valid_patchname()
154 # Once we only support Git 1.7.8 and newer, the command below
155 # could be replaced with:
157 # git check-ref-format --allow-onelevel "$1"
159 # Instead, we arbitrarily prepend one level. The result
160 # should be the same, and this is portable to all existing
161 # versions of Git.
162 git check-ref-format a/"$1"
163 if [ $? -ne 0 ]; then
164 return 1
167 # We want to reject all names that Git 2.0.0 rejects. In case
168 # we are running an older version, we explicitly check some
169 # cases that were added to Git after version 1.5.0. This code
170 # aims to support Git version 1.5.0 and newer.
172 # Git 1.7.6.4 and newer rejects the DEL character.
173 if [ `echo "$1"|tr -d '\177'` != "$1" ]; then
174 return 1
177 # Git 1.7.8 and newer rejects refs that start or end with
178 # slash or contain multiple adjacent slashes.
179 case "$1" in
180 /*|*/|*//*)
181 return 1;;
182 esac
184 # Git 1.7.8 and newer rejects refname components that end in
185 # .lock.
186 case "$1" in
187 *.lock/*|*.lock)
188 return 1;;
189 esac
191 # Git 1.8.5 and newer rejects refnames that are made up of the
192 # single character "@".
193 if [ "$1" = "@" ]; then
194 return 1
197 return 0
200 get_branch()
202 silent git symbolic-ref HEAD || \
203 die "Working on a detached HEAD is unsupported."
205 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
208 verify_branch()
210 [ ! -d "$GIT_DIR/patches" ] &&
211 die "Patches directory doesn't exist, try guilt init"
212 [ ! -d "$GIT_DIR/patches/$branch" ] &&
213 die "Branch $branch is not initialized, try guilt init"
214 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
215 die "Branch $branch does not have a series file"
216 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
217 die "Branch $branch does not have a status file"
218 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
219 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
222 get_top()
224 tail -n 1 "$GUILT_DIR/$branch/status"
227 get_prev()
229 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
230 tail -n 2 "$GUILT_DIR/$branch/status" | head_n 1
234 get_full_series()
236 # ignore all lines matching:
237 # - empty lines
238 # - whitespace only
239 # - optional whitespace followed by '#' followed by more
240 # optional whitespace
241 # also remove comments from end of lines
242 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
243 s/[[:space:]]*#.*\$//
247 " "$series"
250 get_series()
252 get_full_series | while read p; do
253 check_guards "$p" && echo "$p"
254 done
257 # usage: check_guards <patch>
258 # Returns 0 if the patch should be pushed
259 check_guards()
261 get_guards "$1" | while read guard; do
262 g=`echo $guard | sed "s/^[+-]//"`
263 case "$guard" in
265 # Push +guard *only if* guard selected
266 silent grep -F "$g" "$guards_file" || return 1
269 # Push -guard *unless* guard selected
270 silent grep -F "$g" "$guards_file" && return 1
271 true
273 esac
274 done
276 # propagate return from subshell
277 return $?
280 # usage: get_guards <patch>
281 get_guards()
283 awk -v pname="$1" '
284 ($1 == pname) {
285 guards = "";
287 for(i=2; i<=NF; i++) {
288 sub(/#[^+-]*/, "", $i);
289 if (length($i)) {
290 if (length(guards))
291 guards = guards " " $i;
292 else
293 guards = $i;
297 print guards;
298 }' < "$series"
301 # usage: set_guards <patch> <guards...>
302 set_guards()
305 p="$1"
306 shift
307 for x in "$@"; do
308 case "$x" in
309 [+-]*)
310 awk -v pname="$p" -v newguard="$x" '{
311 if ($1 == pname)
312 print $0 " #" newguard;
313 else
314 print $0;
315 }' < "$series" > "$series.tmp"
316 mv "$series.tmp" "$series"
319 echo "'$x' is not a valid guard name" >&2
321 esac
322 done
326 # usage: unset_guards <patch> <guards...>
327 unset_guards()
330 p="$1"
331 shift
332 for x in "$@"; do
333 awk -v pname="$p" -v oldguard="$x" '{
334 if ($1 == pname) {
335 guards = "";
337 for(i=2; i<=NF; i++) {
338 if ($i == "#" oldguard)
339 continue;
341 if (length(guards))
342 guards = guards " " $i;
343 else
344 guards = $i;
347 if (length(guards))
348 print $1 " " guards;
349 else
350 print $1;
351 } else
352 print $0;
353 }' < "$series" > "$series.tmp"
354 mv "$series.tmp" "$series"
355 done
359 # usage: do_make_header <hash>
360 do_make_header()
362 # we should try to work with commit objects only
363 if [ `git cat-file -t "$1"` != "commit" ]; then
364 disp "Hash $1 is not a commit object" >&2
365 disp "Aborting..." >&2
366 exit 2
369 git cat-file -p "$1" | awk '
370 BEGIN{headers=1; firstline=1}
371 /^author / && headers {
372 sub(/^author +/, "");
373 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
374 author=$0
376 !headers {
377 print
378 if (firstline) {
379 firstline = 0;
380 print "\nFrom: " author;
383 /^$/ && headers { headers = 0 }
387 # usage: do_get_patch patchfile
388 do_get_patch()
390 awk '
391 BEGIN{}
392 /^(diff |--- )/ {patch = 1}
393 patch == 1 {print $0}
394 END{}
395 ' < "$1"
398 # usage: do_get_header patchfile
399 do_get_header()
401 # The complexity arises from the fact that we want to ignore all but the
402 # Subject line of the header, and any empty lines after it, if these
403 # exist, and inject only the Subject line as the first line of the
404 # commit message.
406 # We also need to strip "from:" lines from the body of the patch
407 # description as these are used by people to manually set the author of
408 # the patch to be different to their local email address and failing to
409 # strip them results in duplicate from: lines in output from guilt
410 # patchbomb.
412 # 1st line prints first encountered Subject line plus empty line.
413 # 2nd line skips standard email/git patch header lines.
414 # 3th line skips "from:" lines throughout the patch description
415 # 4rd line skips tip's additional header lines.
416 # 5th line skips any empty lines thereafter.
417 # 6th line turns off empty line skip upon seeing a non-empty line.
418 # 7th line terminates execution when we encounter the diff
419 awk '
420 BEGIN{body=0; subj=0}
421 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
422 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
423 /^From:/ {body=0; next}
424 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
425 /^[ \t\f\n\r\v]*$/ && (body==0){next}
426 /^.*$/ && (body==0){body=1}
427 /^(diff |---$|--- )/{exit}
428 {print $0}
429 END{}
430 ' < "$1"
433 # usage: do_get_full_header patchfile
434 do_get_full_header()
436 # 2nd line checks for the begining of a patch
437 # 3rd line outputs the line if it didn't get pruned by the above rules
438 awk '
439 BEGIN{}
440 /^(diff |---$|--- )/{exit}
441 {print $0}
442 END{}
443 ' < "$1"
446 # usage: assert_head_check
447 assert_head_check()
449 if ! head_check refs/patches/$branch/`get_top`; then
450 die "aborting..."
454 # usage: head_check <expected hash>
455 head_check()
457 # make sure we're not doing funky things to commits that don't
458 # belong to us
460 case "$1" in
462 # the expected hash is empty
463 return 0 ;;
464 refs/patches/$branch/)
465 # the expected hash is an invalid rev
466 return 0 ;;
467 esac
469 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
470 disp "Expected HEAD commit $1" >&2
471 disp " got `git rev-parse refs/heads/\`git_branch\``" >&2
472 return 1
474 return 0
477 # usage: series_insert_patch <patchname>
478 series_insert_patch()
480 awk -v top="`get_top`" -v new="$1" \
481 'BEGIN{if (top == "") print new;}
483 print $0;
484 if (top != "" && top == $0) print new;
485 }' "$series" > "$series.tmp"
486 mv "$series.tmp" "$series"
489 # usage: series_remove_patch <patchname>
490 series_remove_patch()
492 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
493 mv "$series.tmp" "$series"
496 # usage: series_rename_patch <oldname> <newname>
497 series_rename_patch()
499 # Rename the patch, but preserve comments on the line
500 awk -v old="$1" -v new="$2" '
502 if (index($0, old) == 1)
503 print new substr($0, length(old) + 1);
504 else
505 print $0;
506 }' "$series" > "$series.tmp"
508 mv "$series.tmp" "$series"
511 # usage: series_rename_patch <oldpatchname> <newpatchname>
512 ref_rename_patch()
514 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
515 remove_ref "refs/patches/$branch/$1"
518 # Beware! This is one of the few (only?) places where we modify the applied
519 # file directly
521 # usage: applied_rename_patch <oldname> <newname>
522 applied_rename_patch()
524 awk -v old="$1" -v new="$2" \
525 'BEGIN{FS=":"}
526 { if ($0 == old)
527 print new;
528 else
529 print;
530 }' "$applied" > "$applied.tmp"
532 mv "$applied.tmp" "$applied"
535 # usage: remove_patch_refs
536 # reads patch names from stdin
537 remove_patch_refs()
539 while read pname; do
540 remove_ref "refs/patches/$branch/$pname"
541 done
544 # usage: pop_many_patches <commitish> <number of patches>
545 pop_many_patches()
547 assert_head_check
550 cd_to_toplevel
552 # remove the patches refs
553 tail -n $2 < "$applied" | remove_patch_refs
555 git reset --hard "$1" > /dev/null
557 n=`wc -l < "$applied"`
558 n=`expr $n - $2`
559 head_n "$n" < "$applied" > "$applied.tmp"
560 mv "$applied.tmp" "$applied"
561 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
562 then
563 git symbolic-ref HEAD refs/heads/$branch
564 git update-ref -d refs/heads/$GUILT_PREFIX$branch
569 # usage: pop_all_patches
570 pop_all_patches()
572 pop_many_patches \
573 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
574 `wc -l < "$applied"`
577 # usage: remove_ref <refname>
578 remove_ref()
581 # does the ref exist?
582 r=`git show-ref --verify -s "$1" 2> /dev/null`
583 [ $? -ne 0 ] && exit 0
585 # remove it
586 git update-ref -d "$1" "$r"
590 # usage: commit patchname parent
591 commit()
594 TMP_MSG=`get_tmp_file msg`
596 p="$GUILT_DIR/$branch/$1"
597 pname="$1"
598 cd_to_toplevel
600 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
602 # grab a commit message out of the patch
603 do_get_header "$p" > "$TMP_MSG"
605 # make a default commit message if patch doesn't contain one
606 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
608 # extract author and date lines from the patch header, and set
609 # GIT_AUTHOR_{NAME,EMAIL,DATE}
610 # prefering Author/AuthorDate lines if available.
611 author_str=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
612 if [ -z "$author_str" ]; then
613 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
616 if [ ! -z "$author_str" ]; then
617 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
618 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
619 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
621 author_date_str=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
622 if [ -z "$author_date_str" ]; then
623 author_date_str=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
625 if [ ! -z "$author_date_str" ]; then
626 export GIT_AUTHOR_DATE="$author_date_str"
630 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
631 ct=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
632 if [ $ct -gt `last_modified "$p"` ]; then
633 ct=`expr $ct + 60`
634 if [ $ct -gt `date +%s` ]; then
635 touch "$p"
636 else
637 touch_date $ct "$p"
641 export GIT_COMMITTER_DATE="`format_last_modified "$p"`"
643 # export GIT_AUTHOR_DATE only if a Date line was unavailable
644 if [ -z "$author_date_str" ]; then
645 export GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
648 # commit
649 treeish=`git write-tree`
650 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
651 if $old_style_prefix || git rev-parse --verify --quiet refs/heads/$GUILT_PREFIX$branch >/dev/null
652 then
653 git update-ref -m "$GIT_REFLOG_ACTION" HEAD $commitish
654 else
655 git branch $GUILT_PREFIX$branch $commitish
656 git symbolic-ref HEAD refs/heads/$GUILT_PREFIX$branch
659 # mark patch as applied
660 git update-ref "refs/patches/$branch/$pname" HEAD
662 rm -f "$TMP_MSG"
666 # usage: push_patch patchname [bail_action]
667 push_patch()
669 __push_patch_bail=0
672 TMP_LOG=`get_tmp_file log`
674 p="$GUILT_DIR/$branch/$1"
675 pname="$1"
676 bail_action="$2"
677 reject="--reject"
679 assert_head_check
680 cd_to_toplevel
682 # apply the patch if and only if there is something to apply
683 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
684 if [ "$bail_action" = abort ]; then
685 reject=""
687 git apply -C$guilt_push_diff_context --index \
688 $reject "$p" > /dev/null 2> "$TMP_LOG"
689 __push_patch_bail=$?
691 if [ $__push_patch_bail -ne 0 ]; then
692 cat "$TMP_LOG" >&2
693 if [ "$bail_action" = "abort" ]; then
694 rm -f "$TMP_LOG" "$TMP_MSG"
695 return $__push_patch_bail
700 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: $pname" \
701 commit "$pname" HEAD
703 echo "$pname" >> "$applied"
705 rm -f "$TMP_LOG"
708 # sub-shell funky-ness
709 __push_patch_bail=$?
711 return $__push_patch_bail
714 # usage: must_commit_first
715 must_commit_first()
717 git update-index --refresh --unmerged -q > /dev/null
718 [ `git diff-files | wc -l` -eq 0 ] || return $?
719 [ `git diff-index HEAD | wc -l` -eq 0 ]
720 return $?
723 # usage: fold_patch patchname
724 fold_patch()
726 set -- "$1" "`get_top`"
728 assert_head_check
730 push_patch "$1"
732 # merge the patch headers
734 pcur="$GUILT_DIR/$branch/$2"
735 pnext="$GUILT_DIR/$branch/$1"
736 TMP_CUR=`get_tmp_file diff-cur`
737 TMP_NEXT=`get_tmp_file diff-next`
738 TMP_DIFF=`get_tmp_file diff`
739 do_get_full_header "$pcur" > "$TMP_CUR"
740 do_get_full_header "$pnext" > "$TMP_NEXT"
741 do_get_patch "$pcur" > "$TMP_DIFF"
743 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
744 *,0)
745 # since the new patch header is empty, we
746 # don't have to do anything
748 0,*)
749 # current is empty; new is not
750 mv "$pcur" "$pcur~"
751 cat "$TMP_NEXT" > "$pcur"
752 cat "$TMP_DIFF" >> "$pcur"
754 *,*)
755 mv "$pcur" "$pcur~"
756 cat "$TMP_CUR" > "$pcur"
757 echo >> "$pcur"
758 echo "Header from folded patch '$1':" >> "$pcur"
759 echo >> "$pcur"
760 cat "$TMP_NEXT" >> "$pcur"
761 cat "$TMP_DIFF" >> "$pcur"
763 esac
765 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
768 __refresh_patch "$2" HEAD^^ 2 "" ""
770 series_remove_patch "$1"
773 # usage: refresh_patch patchname gengitdiff incldiffstat
774 refresh_patch()
776 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
779 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
780 # incldiffstat
781 __refresh_patch()
783 assert_head_check
786 TMP_DIFF=`get_tmp_file diff`
788 cd_to_toplevel
789 p="$GUILT_DIR/$branch/$1"
790 pname="$1"
792 # get the patch header
793 do_get_full_header "$p" > "$TMP_DIFF"
795 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
797 if [ -n "$5" -o $diffstat = "true" ]; then
799 echo "---"
800 git diff --stat $diffopts "$2"
801 echo ""
802 ) >> "$TMP_DIFF"
805 # get the new patch
806 git diff --binary $diffopts "$2" >> "$TMP_DIFF"
808 # move the new patch in
809 mv "$p" "$p~"
810 mv "$TMP_DIFF" "$p"
812 # commit
813 commit "$pname" "HEAD~$3"
815 # drop folded patches
816 N=`expr "$3" - 1`
818 # remove the patches refs
819 tail -n $N < "$applied" | remove_patch_refs
821 n=`wc -l < "$applied"`
822 n=`expr $n - $N`
823 head_n "$n" < "$applied" > "$applied.tmp"
824 mv "$applied.tmp" "$applied"
828 # usage: munge_hash_range <hash range>
830 # this means:
831 # <hash> - one commit
832 # <hash>.. - hash until head (excludes hash, includes head)
833 # ..<hash> - until hash (includes hash)
834 # <hash1>..<hash2> - from hash to hash (inclusive)
836 # The output of this function is suitable to be passed to "git rev-list"
837 munge_hash_range()
839 case "$1" in
840 *..*..*|*\ *)
841 # double .. or space is illegal
842 return 1;;
843 ..*)
844 # e.g., "..v0.10"
845 echo ${1#..};;
846 *..)
847 # e.g., "v0.19.."
848 echo ${1%..}..HEAD;;
849 *..*)
850 # e.g., "v0.19-rc1..v0.19"
851 echo ${1%%..*}..${1#*..};;
853 # e.g., "v0.19"
854 echo $1^..$1;;
855 *) # empty
856 return 1;;
857 esac
858 return 0
861 # usage: get_tmp_file <prefix> [<opts>]
863 # Get a unique filename and create the file in a non-racy way
864 get_tmp_file()
866 while true; do
867 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
868 done
871 # usage: guilt_hook <hook name> <args....>
872 guilt_hook()
874 __hookname="$1"
875 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
877 shift
879 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
880 return $?
884 # source the command
886 . "$cmd"
889 # Some constants
892 # used for: git apply -C <val>
893 guilt_push_diff_context=1
895 # default diffstat value: true or false
896 DIFFSTAT_DEFAULT="false"
898 # default guilt.reusebranch value: true or false
899 REUSE_BRANCH_DEFAULT="false"
901 # Prefix for guilt branches.
902 GUILT_PREFIX=guilt/
905 # Parse any part of .git/config that belongs to us
908 # generate diffstat?
909 diffstat=`git config --bool guilt.diffstat`
910 [ -z "$diffstat" ] && diffstat=$DIFFSTAT_DEFAULT
912 # reuse Git branch?
913 reuse_branch=`git config --bool guilt.reusebranch`
914 [ -z "$reuse_branch" ] && reuse_branch=$REUSE_BRANCH_DEFAULT
917 # The following gets run every time this file is source'd
920 GUILT_DIR="$GIT_DIR/patches"
922 # To make it harder to accidentally do "git push" with a guilt patch
923 # applied, "guilt push" changes branch from e.g. "master" to
924 # "guilt/master". Set $git_branch to the full branch name, and
925 # $branch to the abbreviated name that the user sees most of the time.
926 # Note: old versions of guilt did not add the "guilt/" prefix. This
927 # code handles that case as well. The prefix will be added when you
928 # have no patches applied and do a "guilt push".
929 raw_git_branch=`get_branch`
930 branch=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
932 git_branch()
934 if $old_style_prefix
935 then
936 echo $branch
937 elif [ -z "`get_top 2>/dev/null`" ]
938 then
939 echo $branch
940 else
941 echo $GUILT_PREFIX$branch
945 # most of the time we want to verify that the repo's branch has been
946 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
947 # we must avoid the checks
948 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
949 verify_branch
951 # do not check the status file format (guilt repair needs this,
952 # otherwise nothing can do what's necessary to bring the repo into a
953 # useable state)
954 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
955 [ -s "$GIT_DIR/patches/$branch/status" ] &&
956 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
957 die "Status file appears to use old format, try guilt repair --status"
961 # very useful files
962 series="$GUILT_DIR/$branch/series"
963 applied="$GUILT_DIR/$branch/status"
964 guards_file="$GUILT_DIR/$branch/guards"
966 # determine a pager to use for anything interactive (fall back to more)
967 pager="more"
968 [ ! -z "$PAGER" ] && pager="$PAGER"
970 UNAME_S=`uname -s`
972 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
973 . "$GUILT_PATH/os.$UNAME_S"
974 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
975 . "$GUILT_PATH/../lib/guilt/os.$UNAME_S"
976 else
977 die "Unsupported operating system: $UNAME_S"
980 if [ -n "`get_top 2>/dev/null`" ]; then
981 # If there is at least one pushed patch, we set
982 # old_style_prefix according to how it was pushed. It is only
983 # possible to change the prefix style while no patches are
984 # applied.
985 if [ "$branch" = "$raw_git_branch" ]; then
986 old_style_prefix=true
987 else
988 old_style_prefix=false
990 else
991 old_style_prefix="$reuse_branch"
994 _main "$@"