[PATCH] guilt-refresh: Check color.ui git config before refresh
[guilt.git] / guilt
blob25dbb26f20e205160dbdf86a376ce0f727d0d371
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 authordate=strftime("%c", $(NF -1)) " " $NF;
374 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
375 author=$0
377 !headers {
378 print
379 if (firstline) {
380 firstline = 0;
381 print "\nFrom: " author;
382 print "Date: " authordate;
385 /^$/ && headers { headers = 0 }
389 # usage: do_get_patch patchfile
390 do_get_patch()
392 awk '
393 BEGIN{}
394 /^(diff |--- )/ {patch = 1}
395 patch == 1 {print $0}
396 END{}
397 ' < "$1"
400 # usage: do_get_header patchfile
401 do_get_header()
403 # The complexity arises from the fact that we want to ignore all but the
404 # Subject line of the header, and any empty lines after it, if these
405 # exist, and inject only the Subject line as the first line of the
406 # commit message.
408 # We also need to strip "from:" lines from the body of the patch
409 # description as these are used by people to manually set the author of
410 # the patch to be different to their local email address and failing to
411 # strip them results in duplicate from: lines in output from guilt
412 # patchbomb.
414 # 1st line prints first encountered Subject line plus empty line.
415 # 2nd line skips standard email/git patch header lines.
416 # 3th line skips "from:" lines throughout the patch description
417 # 4rd line skips tip's additional header lines.
418 # 5th line skips any empty lines thereafter.
419 # 6th line turns off empty line skip upon seeing a non-empty line.
420 # 7th line terminates execution when we encounter the diff
421 awk '
422 BEGIN{body=0; subj=0}
423 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
424 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
425 /^From:/ {body=0; next}
426 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
427 /^[ \t\f\n\r\v]*$/ && (body==0){next}
428 /^.*$/ && (body==0){body=1}
429 /^(diff |---$|--- )/{exit}
430 {print $0}
431 END{}
432 ' < "$1"
435 # usage: do_get_full_header patchfile
436 do_get_full_header()
438 # 2nd line checks for the begining of a patch
439 # 3rd line outputs the line if it didn't get pruned by the above rules
440 awk '
441 BEGIN{}
442 /^(diff |---$|--- )/{exit}
443 {print $0}
444 END{}
445 ' < "$1"
448 # usage: assert_head_check
449 assert_head_check()
451 if ! head_check refs/patches/$branch/`get_top`; then
452 die "aborting..."
456 # usage: head_check <expected hash>
457 head_check()
459 # make sure we're not doing funky things to commits that don't
460 # belong to us
462 case "$1" in
464 # the expected hash is empty
465 return 0 ;;
466 refs/patches/$branch/)
467 # the expected hash is an invalid rev
468 return 0 ;;
469 esac
471 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
472 disp "Expected HEAD commit $1" >&2
473 disp " got `git rev-parse refs/heads/\`git_branch\``" >&2
474 return 1
476 return 0
479 # usage: series_insert_patch <patchname>
480 series_insert_patch()
482 awk -v top="`get_top`" -v new="$1" \
483 'BEGIN{if (top == "") print new;}
485 print $0;
486 if (top != "" && top == $0) print new;
487 }' "$series" > "$series.tmp"
488 mv "$series.tmp" "$series"
491 # usage: series_remove_patch <patchname>
492 series_remove_patch()
494 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
495 mv "$series.tmp" "$series"
498 # usage: series_rename_patch <oldname> <newname>
499 series_rename_patch()
501 # Rename the patch, but preserve comments on the line
502 awk -v old="$1" -v new="$2" '
504 if (index($0, old) == 1)
505 print new substr($0, length(old) + 1);
506 else
507 print $0;
508 }' "$series" > "$series.tmp"
510 mv "$series.tmp" "$series"
513 # usage: series_rename_patch <oldpatchname> <newpatchname>
514 ref_rename_patch()
516 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
517 remove_ref "refs/patches/$branch/$1"
520 # Beware! This is one of the few (only?) places where we modify the applied
521 # file directly
523 # usage: applied_rename_patch <oldname> <newname>
524 applied_rename_patch()
526 awk -v old="$1" -v new="$2" \
527 'BEGIN{FS=":"}
528 { if ($0 == old)
529 print new;
530 else
531 print;
532 }' "$applied" > "$applied.tmp"
534 mv "$applied.tmp" "$applied"
537 # usage: remove_patch_refs
538 # reads patch names from stdin
539 remove_patch_refs()
541 while read pname; do
542 remove_ref "refs/patches/$branch/$pname"
543 done
546 # usage: pop_many_patches <commitish> <number of patches>
547 pop_many_patches()
549 assert_head_check
552 cd_to_toplevel
554 # remove the patches refs
555 tail -n $2 < "$applied" | remove_patch_refs
557 git reset --hard "$1" > /dev/null
559 n=`wc -l < "$applied"`
560 n=`expr $n - $2`
561 head_n "$n" < "$applied" > "$applied.tmp"
562 mv "$applied.tmp" "$applied"
563 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
564 then
565 git symbolic-ref HEAD refs/heads/$branch
566 git update-ref -d refs/heads/$GUILT_PREFIX$branch
571 # usage: pop_all_patches
572 pop_all_patches()
574 pop_many_patches \
575 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
576 `wc -l < "$applied"`
579 # usage: remove_ref <refname>
580 remove_ref()
583 # does the ref exist?
584 r=`git show-ref --verify -s "$1" 2> /dev/null`
585 [ $? -ne 0 ] && exit 0
587 # remove it
588 git update-ref -d "$1" "$r"
592 # usage: commit patchname parent
593 commit()
596 TMP_MSG=`get_tmp_file msg`
598 p="$GUILT_DIR/$branch/$1"
599 pname="$1"
600 cd_to_toplevel
602 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
604 # grab a commit message out of the patch
605 do_get_header "$p" > "$TMP_MSG"
607 # make a default commit message if patch doesn't contain one
608 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
610 # extract author and date lines from the patch header, and set
611 # GIT_AUTHOR_{NAME,EMAIL,DATE}
612 # prefering Author/AuthorDate lines if available.
613 author_str=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
614 if [ -z "$author_str" ]; then
615 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
618 if [ ! -z "$author_str" ]; then
619 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
620 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
621 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
623 author_date_str=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
624 if [ -z "$author_date_str" ]; then
625 author_date_str=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
627 if [ ! -z "$author_date_str" ]; then
628 export GIT_AUTHOR_DATE="$author_date_str"
632 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
633 ct=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
634 if [ $ct -gt `last_modified "$p"` ]; then
635 ct=`expr $ct + 60`
636 if [ $ct -gt `date +%s` ]; then
637 touch "$p"
638 else
639 touch_date $ct "$p"
643 export GIT_COMMITTER_DATE="`format_last_modified "$p"`"
645 # export GIT_AUTHOR_DATE only if a Date line was unavailable
646 if [ -z "$author_date_str" ]; then
647 export GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
650 # commit
651 treeish=`git write-tree`
652 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
653 if $old_style_prefix || git rev-parse --verify --quiet refs/heads/$GUILT_PREFIX$branch >/dev/null
654 then
655 git update-ref -m "$GIT_REFLOG_ACTION" HEAD $commitish
656 else
657 git branch $GUILT_PREFIX$branch $commitish
658 git symbolic-ref HEAD refs/heads/$GUILT_PREFIX$branch
661 # mark patch as applied
662 git update-ref "refs/patches/$branch/$pname" HEAD
664 rm -f "$TMP_MSG"
668 # usage: push_patch patchname [bail_action]
669 push_patch()
671 __push_patch_bail=0
674 TMP_LOG=`get_tmp_file log`
676 p="$GUILT_DIR/$branch/$1"
677 pname="$1"
678 bail_action="$2"
679 reject="--reject"
681 assert_head_check
682 cd_to_toplevel
684 # apply the patch if and only if there is something to apply
685 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
686 if [ "$bail_action" = abort ]; then
687 reject=""
689 git apply $guilt_push_diff_context --index \
690 $reject "$p" > /dev/null 2> "$TMP_LOG"
691 __push_patch_bail=$?
693 if [ $__push_patch_bail -ne 0 ]; then
694 cat "$TMP_LOG" >&2
695 if [ "$bail_action" = "abort" ]; then
696 rm -f "$TMP_LOG" "$TMP_MSG"
697 return $__push_patch_bail
702 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: $pname" \
703 commit "$pname" HEAD
705 echo "$pname" >> "$applied"
707 rm -f "$TMP_LOG"
710 # sub-shell funky-ness
711 __push_patch_bail=$?
713 return $__push_patch_bail
716 # usage: must_commit_first
717 must_commit_first()
719 git update-index --refresh --unmerged -q > /dev/null
720 [ `git diff-files | wc -l` -eq 0 ] || return $?
721 [ `git diff-index HEAD | wc -l` -eq 0 ]
722 return $?
725 # usage: fold_patch patchname
726 fold_patch()
728 set -- "$1" "`get_top`"
730 assert_head_check
732 push_patch "$1"
734 # merge the patch headers
736 pcur="$GUILT_DIR/$branch/$2"
737 pnext="$GUILT_DIR/$branch/$1"
738 TMP_CUR=`get_tmp_file diff-cur`
739 TMP_NEXT=`get_tmp_file diff-next`
740 TMP_DIFF=`get_tmp_file diff`
741 do_get_full_header "$pcur" > "$TMP_CUR"
742 do_get_full_header "$pnext" > "$TMP_NEXT"
743 do_get_patch "$pcur" > "$TMP_DIFF"
745 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
746 *,0)
747 # since the new patch header is empty, we
748 # don't have to do anything
750 0,*)
751 # current is empty; new is not
752 mv "$pcur" "$pcur~"
753 cat "$TMP_NEXT" > "$pcur"
754 cat "$TMP_DIFF" >> "$pcur"
756 *,*)
757 mv "$pcur" "$pcur~"
758 cat "$TMP_CUR" > "$pcur"
759 echo >> "$pcur"
760 echo "Header from folded patch '$1':" >> "$pcur"
761 echo >> "$pcur"
762 cat "$TMP_NEXT" >> "$pcur"
763 cat "$TMP_DIFF" >> "$pcur"
765 esac
767 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
770 __refresh_patch "$2" HEAD^^ 2 "" ""
772 series_remove_patch "$1"
775 # usage: refresh_patch patchname gengitdiff incldiffstat
776 refresh_patch()
778 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
781 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
782 # incldiffstat
783 __refresh_patch()
785 assert_head_check
788 TMP_DIFF=`get_tmp_file diff`
790 cd_to_toplevel
791 p="$GUILT_DIR/$branch/$1"
792 pname="$1"
794 # get the patch header
795 do_get_full_header "$p" > "$TMP_DIFF"
797 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
799 if [ -n "$5" -o $diffstat = "true" ]; then
801 echo "---"
802 git diff --stat $diffopts "$2"
803 echo ""
804 ) >> "$TMP_DIFF"
807 # get the new patch
808 git diff --binary $diffopts "$2" >> "$TMP_DIFF"
810 # move the new patch in
811 mv "$p" "$p~"
812 mv "$TMP_DIFF" "$p"
814 # commit
815 commit "$pname" "HEAD~$3"
817 # drop folded patches
818 N=`expr "$3" - 1`
820 # remove the patches refs
821 tail -n $N < "$applied" | remove_patch_refs
823 n=`wc -l < "$applied"`
824 n=`expr $n - $N`
825 head_n "$n" < "$applied" > "$applied.tmp"
826 mv "$applied.tmp" "$applied"
830 # usage: munge_hash_range <hash range>
832 # this means:
833 # <hash> - one commit
834 # <hash>.. - hash until head (excludes hash, includes head)
835 # ..<hash> - until hash (includes hash)
836 # <hash1>..<hash2> - from hash to hash (inclusive)
838 # The output of this function is suitable to be passed to "git rev-list"
839 munge_hash_range()
841 case "$1" in
842 *..*..*|*\ *)
843 # double .. or space is illegal
844 return 1;;
845 ..*)
846 # e.g., "..v0.10"
847 echo ${1#..};;
848 *..)
849 # e.g., "v0.19.."
850 echo ${1%..}..HEAD;;
851 *..*)
852 # e.g., "v0.19-rc1..v0.19"
853 echo ${1%%..*}..${1#*..};;
855 # e.g., "v0.19"
856 echo $1^..$1;;
857 *) # empty
858 return 1;;
859 esac
860 return 0
863 # usage: get_tmp_file <prefix> [<opts>]
865 # Get a unique filename and create the file in a non-racy way
866 get_tmp_file()
868 while true; do
869 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
870 done
873 # usage: guilt_hook <hook name> <args....>
874 guilt_hook()
876 __hookname="$1"
877 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
879 shift
881 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
882 return $?
886 # source the command
888 . "$cmd"
891 # Some constants
894 # default guilt.diffcontext value: int or bool
895 DIFFCONTEXT_DEFAULT=1
897 # default diffstat value: true or false
898 DIFFSTAT_DEFAULT="false"
900 # default guilt.reusebranch value: true or false
901 REUSE_BRANCH_DEFAULT="false"
903 # Prefix for guilt branches.
904 GUILT_PREFIX=guilt/
907 # Parse any part of .git/config that belongs to us
910 # amount of context
911 diffcontext=`git config --bool-or-int guilt.diffcontext`
912 case "$diffcontext" in
913 false)
914 guilt_push_diff_context="-C0"
916 true)
917 guilt_push_diff_context=
920 guilt_push_diff_context="-C$DIFFCONTEXT_DEFAULT"
923 guilt_push_diff_context="-C$diffcontext"
925 esac
927 # generate diffstat?
928 diffstat=`git config --bool guilt.diffstat`
929 [ -z "$diffstat" ] && diffstat=$DIFFSTAT_DEFAULT
931 # reuse Git branch?
932 reuse_branch=`git config --bool guilt.reusebranch`
933 [ -z "$reuse_branch" ] && reuse_branch=$REUSE_BRANCH_DEFAULT
936 # The following gets run every time this file is source'd
939 GUILT_DIR="$GIT_DIR/patches"
941 # To make it harder to accidentally do "git push" with a guilt patch
942 # applied, "guilt push" changes branch from e.g. "master" to
943 # "guilt/master". Set $git_branch to the full branch name, and
944 # $branch to the abbreviated name that the user sees most of the time.
945 # Note: old versions of guilt did not add the "guilt/" prefix. This
946 # code handles that case as well. The prefix will be added when you
947 # have no patches applied and do a "guilt push".
948 raw_git_branch=`get_branch`
949 branch=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
951 git_branch()
953 if $old_style_prefix
954 then
955 echo $branch
956 elif [ -z "`get_top 2>/dev/null`" ]
957 then
958 echo $branch
959 else
960 echo $GUILT_PREFIX$branch
964 # most of the time we want to verify that the repo's branch has been
965 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
966 # we must avoid the checks
967 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
968 verify_branch
970 # do not check the status file format (guilt repair needs this,
971 # otherwise nothing can do what's necessary to bring the repo into a
972 # useable state)
973 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
974 [ -s "$GIT_DIR/patches/$branch/status" ] &&
975 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
976 die "Status file appears to use old format, try guilt repair --status"
980 # very useful files
981 series="$GUILT_DIR/$branch/series"
982 applied="$GUILT_DIR/$branch/status"
983 guards_file="$GUILT_DIR/$branch/guards"
985 # determine a pager to use for anything interactive (fall back to more)
986 pager="more"
987 [ ! -z "$PAGER" ] && pager="$PAGER"
989 UNAME_S=`uname -s`
991 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
992 . "$GUILT_PATH/os.$UNAME_S"
993 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
994 . "$GUILT_PATH/../lib/guilt/os.$UNAME_S"
995 else
996 die "Unsupported operating system: $UNAME_S"
999 if [ -n "`get_top 2>/dev/null`" ]; then
1000 # If there is at least one pushed patch, we set
1001 # old_style_prefix according to how it was pushed. It is only
1002 # possible to change the prefix style while no patches are
1003 # applied.
1004 if [ "$branch" = "$raw_git_branch" ]; then
1005 old_style_prefix=true
1006 else
1007 old_style_prefix=false
1009 else
1010 old_style_prefix="$reuse_branch"
1013 _main "$@"