doc: git doesn't use git-foo invocations.
[guilt.git] / guilt
blobab472c508695cbc184c8253c597b206683b6d204
1 #!/bin/sh
3 # Copyright (c) Josef "Jeff" Sipek, 2006-2015
6 GUILT_VERSION="0.36-rc1"
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 "$@"
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 find "$GUILT_PATH/../lib/guilt" -maxdepth 1 -name "guilt-*" -type f -perm +111 2> /dev/null | sed -e "s/.*\\/$GUILT-//"
77 find "$GUILT_PATH" -maxdepth 1 -name "guilt-*" -type f -perm +111 | sed -e "s/.*\\/$GUILT-//"
80 # by default, we shouldn't fail
81 cmd=
83 if [ $# -ne 0 ]; then
84 # take first arg, and try to execute it
86 arg="$1"
87 dir="$GUILT_PATH"
88 libdir="$GUILT_PATH/../lib/guilt"
90 if [ -x "$dir/guilt-$arg" ]; then
91 cmd="$dir/guilt-$arg"
92 CMDNAME=$arg
93 elif [ -x "$libdir/guilt-$arg" ]; then
94 cmd="$libdir/guilt-$arg"
95 CMDNAME=$arg
96 else
97 # might be a short handed
98 for command in $(guilt_commands); do
99 case $command in
100 $arg*)
101 if [ -x "$dir/guilt-$command" ]; then
102 cmd="$dir/guilt-$command"
103 CMDNAME=$command
104 elif [ -x "$libdir/guilt-$command" ]; then
105 cmd="$libdir/guilt-$command"
106 CMDNAME=$command
109 esac
110 done
112 if [ -z "$cmd" ]; then
113 disp "Command $arg not found" >&2
114 disp "" >&2
115 exit 1
118 shift
119 else
120 # no args passed or invalid command entered, just output help summary
122 disp "Guilt v$GUILT_VERSION"
123 disp ""
124 disp "Pick a command:"
125 guilt_commands | sort | column | column -t | sed -e 's/^/ /'
127 disp ""
128 disp "Example:"
129 disp_e "\tguilt push"
131 # now, let's exit
132 exit 1
135 ########
138 # Library goodies
141 # usage: valid_patchname <patchname>
142 valid_patchname()
144 # Once we only support Git 1.7.8 and newer, the command below
145 # could be replaced with:
147 # git check-ref-format --allow-onelevel "$1"
149 # Instead, we arbitrarily prepend one level. The result
150 # should be the same, and this is portable to all existing
151 # versions of Git.
152 git check-ref-format a/"$1"
153 if [ $? -ne 0 ]; then
154 return 1
157 # We want to reject all names that Git 2.0.0 rejects. In case
158 # we are running an older version, we explicitly check some
159 # cases that were added to Git after version 1.5.0. This code
160 # aims to support Git version 1.5.0 and newer.
162 # Git 1.7.6.4 and newer rejects the DEL character.
163 if [ `echo "$1"|tr -d '\177'` != "$1" ]; then
164 return 1
167 # Git 1.7.8 and newer rejects refs that start or end with
168 # slash or contain multiple adjacent slashes.
169 case "$1" in
170 /*|*/|*//*)
171 return 1;;
172 esac
174 # Git 1.7.8 and newer rejects refname components that end in
175 # .lock.
176 case "$1" in
177 *.lock/*|*.lock)
178 return 1;;
179 esac
181 # Git 1.8.5 and newer rejects refnames that are made up of the
182 # single character "@".
183 if [ "$1" = "@" ]; then
184 return 1
187 return 0
190 get_branch()
192 silent git symbolic-ref HEAD || \
193 die "Working on a detached HEAD is unsupported."
195 git symbolic-ref HEAD | sed -e 's,^refs/heads/,,'
198 verify_branch()
200 [ ! -d "$GIT_DIR/patches" ] &&
201 die "Patches directory doesn't exist, try guilt init"
202 [ ! -d "$GIT_DIR/patches/$branch" ] &&
203 die "Branch $branch is not initialized, try guilt init"
204 [ ! -f "$GIT_DIR/patches/$branch/series" ] &&
205 die "Branch $branch does not have a series file"
206 [ ! -f "$GIT_DIR/patches/$branch/status" ] &&
207 die "Branch $branch does not have a status file"
208 [ -f "$GIT_DIR/patches/$branch/applied" ] &&
209 die "Warning: Branch $branch has 'applied' file - guilt is not compatible with stgit"
212 get_top()
214 tail -n 1 "$GUILT_DIR/$branch/status"
217 get_prev()
219 if [ `wc -l < "$GUILT_DIR/$branch/status"` -gt 1 ]; then
220 tail -n 2 "$GUILT_DIR/$branch/status" | head_n 1
224 get_full_series()
226 # ignore all lines matching:
227 # - empty lines
228 # - whitespace only
229 # - optional whitespace followed by '#' followed by more
230 # optional whitespace
231 # also remove comments from end of lines
232 sed -n -e "/^[[:space:]]*\(#.*\)*\$/ ! {
233 s/[[:space:]]*#.*\$//
237 " "$series"
240 get_series()
242 get_full_series | while read p; do
243 check_guards "$p" && echo "$p"
244 done
247 # usage: check_guards <patch>
248 # Returns 0 if the patch should be pushed
249 check_guards()
251 get_guards "$1" | while read guard; do
252 g=`echo $guard | sed "s/^[+-]//"`
253 case "$guard" in
255 # Push +guard *only if* guard selected
256 silent grep -F "$g" "$guards_file" || return 1
259 # Push -guard *unless* guard selected
260 silent grep -F "$g" "$guards_file" && return 1
261 true
263 esac
264 done
266 # propagate return from subshell
267 return $?
270 # usage: get_guards <patch>
271 get_guards()
273 awk -v pname="$1" '
274 ($1 == pname) {
275 guards = "";
277 for(i=2; i<=NF; i++) {
278 sub(/#[^+-]*/, "", $i);
279 if (length($i)) {
280 if (length(guards))
281 guards = guards " " $i;
282 else
283 guards = $i;
287 print guards;
288 }' < "$series"
291 # usage: set_guards <patch> <guards...>
292 set_guards()
295 p="$1"
296 shift
297 for x in "$@"; do
298 case "$x" in
299 [+-]*)
300 awk -v pname="$p" -v newguard="$x" '{
301 if ($1 == pname)
302 print $0 " #" newguard;
303 else
304 print $0;
305 }' < "$series" > "$series.tmp"
306 mv "$series.tmp" "$series"
309 echo "'$x' is not a valid guard name" >&2
311 esac
312 done
316 # usage: unset_guards <patch> <guards...>
317 unset_guards()
320 p="$1"
321 shift
322 for x in "$@"; do
323 awk -v pname="$p" -v oldguard="$x" '{
324 if ($1 == pname) {
325 guards = "";
327 for(i=2; i<=NF; i++) {
328 if ($i == "#" oldguard)
329 continue;
331 if (length(guards))
332 guards = guards " " $i;
333 else
334 guards = $i;
337 if (length(guards))
338 print $1 " " guards;
339 else
340 print $1;
341 } else
342 print $0;
343 }' < "$series" > "$series.tmp"
344 mv "$series.tmp" "$series"
345 done
349 # usage: do_make_header <hash>
350 do_make_header()
352 # we should try to work with commit objects only
353 if [ `git cat-file -t "$1"` != "commit" ]; then
354 disp "Hash $1 is not a commit object" >&2
355 disp "Aborting..." >&2
356 exit 2
359 git cat-file -p "$1" | awk '
360 BEGIN{headers=1; firstline=1}
361 /^author / && headers {
362 sub(/^author +/, "");
363 sub(/ [0-9]* [+-]*[0-9][0-9]*$/, "");
364 author=$0
366 !headers {
367 print
368 if (firstline) {
369 firstline = 0;
370 print "\nFrom: " author;
373 /^$/ && headers { headers = 0 }
377 # usage: do_get_patch patchfile
378 do_get_patch()
380 awk '
381 BEGIN{}
382 /^(diff |--- )/ {patch = 1}
383 patch == 1 {print $0}
384 END{}
385 ' < "$1"
388 # usage: do_get_header patchfile
389 do_get_header()
391 # The complexity arises from the fact that we want to ignore all but the
392 # Subject line of the header, and any empty lines after it, if these
393 # exist, and inject only the Subject line as the first line of the
394 # commit message.
396 # We also need to strip "from:" lines from the body of the patch
397 # description as these are used by people to manually set the author of
398 # the patch to be different to their local email address and failing to
399 # strip them results in duplicate from: lines in output from guilt
400 # patchbomb.
402 # 1st line prints first encountered Subject line plus empty line.
403 # 2nd line skips standard email/git patch header lines.
404 # 3th line skips "from:" lines throughout the patch description
405 # 4rd line skips tip's additional header lines.
406 # 5th line skips any empty lines thereafter.
407 # 6th line turns off empty line skip upon seeing a non-empty line.
408 # 7th line terminates execution when we encounter the diff
409 awk '
410 BEGIN{body=0; subj=0}
411 /^Subject:/ && (body == 0 && subj == 0){subj=1; print substr($0, 10) "\n"; next}
412 /^(Subject:|Author:|Date:|commit)/ && (body == 0){next}
413 /^From:/ {body=0; next}
414 /^(Commit-ID:|Gitweb:|AuthorDate:|Committer:CommitDate:)/ && (body == 0){next}
415 /^[ \t\f\n\r\v]*$/ && (body==0){next}
416 /^.*$/ && (body==0){body=1}
417 /^(diff |---$|--- )/{exit}
418 {print $0}
419 END{}
420 ' < "$1"
423 # usage: do_get_full_header patchfile
424 do_get_full_header()
426 # 2nd line checks for the begining of a patch
427 # 3rd line outputs the line if it didn't get pruned by the above rules
428 awk '
429 BEGIN{}
430 /^(diff |---$|--- )/{exit}
431 {print $0}
432 END{}
433 ' < "$1"
436 # usage: assert_head_check
437 assert_head_check()
439 if ! head_check refs/patches/$branch/`get_top`; then
440 die "aborting..."
444 # usage: head_check <expected hash>
445 head_check()
447 # make sure we're not doing funky things to commits that don't
448 # belong to us
450 case "$1" in
452 # the expected hash is empty
453 return 0 ;;
454 refs/patches/$branch/)
455 # the expected hash is an invalid rev
456 return 0 ;;
457 esac
459 if [ "`git rev-parse refs/heads/\`git_branch\``" != "`git rev-parse $1`" ]; then
460 disp "Expected HEAD commit $1" >&2
461 disp " got `git rev-parse refs/heads/\`git_branch\``" >&2
462 return 1
464 return 0
467 # usage: series_insert_patch <patchname>
468 series_insert_patch()
470 awk -v top="`get_top`" -v new="$1" \
471 'BEGIN{if (top == "") print new;}
473 print $0;
474 if (top != "" && top == $0) print new;
475 }' "$series" > "$series.tmp"
476 mv "$series.tmp" "$series"
479 # usage: series_remove_patch <patchname>
480 series_remove_patch()
482 grep -v "^$1\([[:space:]].*\)\?$" < "$series" > "$series.tmp"
483 mv "$series.tmp" "$series"
486 # usage: series_rename_patch <oldname> <newname>
487 series_rename_patch()
489 # Rename the patch, but preserve comments on the line
490 awk -v old="$1" -v new="$2" '
492 if (index($0, old) == 1)
493 print new substr($0, length(old) + 1);
494 else
495 print $0;
496 }' "$series" > "$series.tmp"
498 mv "$series.tmp" "$series"
501 # usage: series_rename_patch <oldpatchname> <newpatchname>
502 ref_rename_patch()
504 git update-ref "refs/patches/$branch/$2" `git rev-parse "refs/patches/$branch/$1"`
505 remove_ref "refs/patches/$branch/$1"
508 # Beware! This is one of the few (only?) places where we modify the applied
509 # file directly
511 # usage: applied_rename_patch <oldname> <newname>
512 applied_rename_patch()
514 awk -v old="$1" -v new="$2" \
515 'BEGIN{FS=":"}
516 { if ($0 == old)
517 print new;
518 else
519 print;
520 }' "$applied" > "$applied.tmp"
522 mv "$applied.tmp" "$applied"
525 # usage: remove_patch_refs
526 # reads patch names from stdin
527 remove_patch_refs()
529 while read pname; do
530 remove_ref "refs/patches/$branch/$pname"
531 done
534 # usage: pop_many_patches <commitish> <number of patches>
535 pop_many_patches()
537 assert_head_check
540 cd_to_toplevel
542 # remove the patches refs
543 tail -n $2 < "$applied" | remove_patch_refs
545 git reset --hard "$1" > /dev/null
547 n=`wc -l < "$applied"`
548 n=`expr $n - $2`
549 head_n "$n" < "$applied" > "$applied.tmp"
550 mv "$applied.tmp" "$applied"
551 if [ -z "`get_top 2>/dev/null`" ] && [ "`git symbolic-ref HEAD`" = "refs/heads/$GUILT_PREFIX$branch" ] && ! $old_style_prefix
552 then
553 git symbolic-ref HEAD refs/heads/$branch
554 git update-ref -d refs/heads/$GUILT_PREFIX$branch
559 # usage: pop_all_patches
560 pop_all_patches()
562 pop_many_patches \
563 `git rev-parse refs/patches/$branch/$(head_n 1 "$applied")^` \
564 `wc -l < "$applied"`
567 # usage: remove_ref <refname>
568 remove_ref()
571 # does the ref exist?
572 r=`git show-ref --verify -s "$1" 2> /dev/null`
573 [ $? -ne 0 ] && exit 0
575 # remove it
576 git update-ref -d "$1" "$r"
580 # usage: commit patchname parent
581 commit()
584 TMP_MSG=`get_tmp_file msg`
586 p="$GUILT_DIR/$branch/$1"
587 pname="$1"
588 cd_to_toplevel
590 git diff-files --name-only | (while read n; do git update-index "$n" ; done)
592 # grab a commit message out of the patch
593 do_get_header "$p" > "$TMP_MSG"
595 # make a default commit message if patch doesn't contain one
596 [ ! -s "$TMP_MSG" ] && echo "patch $pname" > "$TMP_MSG"
598 # extract author and date lines from the patch header, and set
599 # GIT_AUTHOR_{NAME,EMAIL,DATE}
600 # prefering Author/AuthorDate lines if available.
601 author_str=`sed -n -e '/^Author:/ { s/^Author: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
602 if [ -z "$author_str" ]; then
603 author_str=`sed -n -e '/^From:/ { s/^From: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
606 if [ ! -z "$author_str" ]; then
607 GIT_AUTHOR_NAME=`echo $author_str | sed -e 's/ *<.*$//'`
608 export GIT_AUTHOR_NAME="${GIT_AUTHOR_NAME:-" "}"
609 export GIT_AUTHOR_EMAIL="`echo $author_str | sed -e 's/[^<]*//'`"
611 author_date_str=`sed -n -e '/^AuthorDate:/ { s/^AuthorDate: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
612 if [ -z "$author_date_str" ]; then
613 author_date_str=`sed -n -e '/^Date:/ { s/^Date: //; p; q; }; /^(diff |---$|--- )/ q' "$p"`
615 if [ ! -z "$author_date_str" ]; then
616 export GIT_AUTHOR_DATE="$author_date_str"
620 # `git log -1 --pretty=%ct` doesn't work on Git 1.5.x
621 ct=`git cat-file commit HEAD | awk '/^committer /{ print $(NF-1); exit; }'`
622 if [ $ct -gt `last_modified "$p"` ]; then
623 ct=`expr $ct + 60`
624 if [ $ct -gt `date +%s` ]; then
625 touch "$p"
626 else
627 touch_date $ct "$p"
631 export GIT_COMMITTER_DATE="`format_last_modified "$p"`"
633 # export GIT_AUTHOR_DATE only if a Date line was unavailable
634 if [ -z "$author_date_str" ]; then
635 export GIT_AUTHOR_DATE="$GIT_COMMITTER_DATE"
638 # commit
639 treeish=`git write-tree`
640 commitish=`git commit-tree $treeish -p $2 < "$TMP_MSG"`
641 if $old_style_prefix || git rev-parse --verify --quiet refs/heads/$GUILT_PREFIX$branch >/dev/null
642 then
643 git update-ref HEAD $commitish
644 else
645 git branch $GUILT_PREFIX$branch $commitish
646 git symbolic-ref HEAD refs/heads/$GUILT_PREFIX$branch
649 # mark patch as applied
650 git update-ref "refs/patches/$branch/$pname" HEAD
652 rm -f "$TMP_MSG"
656 # usage: push_patch patchname [bail_action]
657 push_patch()
659 __push_patch_bail=0
662 TMP_LOG=`get_tmp_file log`
664 p="$GUILT_DIR/$branch/$1"
665 pname="$1"
666 bail_action="$2"
667 reject="--reject"
669 assert_head_check
670 cd_to_toplevel
672 # apply the patch if and only if there is something to apply
673 if [ `do_get_patch "$p" | wc -l` -gt 0 ]; then
674 if [ "$bail_action" = abort ]; then
675 reject=""
677 git apply -C$guilt_push_diff_context --index \
678 $reject "$p" > /dev/null 2> "$TMP_LOG"
679 __push_patch_bail=$?
681 if [ $__push_patch_bail -ne 0 ]; then
682 cat "$TMP_LOG" >&2
683 if [ "$bail_action" = "abort" ]; then
684 rm -f "$TMP_LOG" "$TMP_MSG"
685 return $__push_patch_bail
690 commit "$pname" HEAD
692 echo "$pname" >> "$applied"
694 rm -f "$TMP_LOG"
697 # sub-shell funky-ness
698 __push_patch_bail=$?
700 return $__push_patch_bail
703 # usage: must_commit_first
704 must_commit_first()
706 git update-index --refresh --unmerged -q > /dev/null
707 [ `git diff-files | wc -l` -eq 0 ] || return $?
708 [ `git diff-index HEAD | wc -l` -eq 0 ]
709 return $?
712 # usage: fold_patch patchname
713 fold_patch()
715 set -- "$1" "`get_top`"
717 assert_head_check
719 push_patch "$1"
721 # merge the patch headers
723 pcur="$GUILT_DIR/$branch/$2"
724 pnext="$GUILT_DIR/$branch/$1"
725 TMP_CUR=`get_tmp_file diff-cur`
726 TMP_NEXT=`get_tmp_file diff-next`
727 TMP_DIFF=`get_tmp_file diff`
728 do_get_full_header "$pcur" > "$TMP_CUR"
729 do_get_full_header "$pnext" > "$TMP_NEXT"
730 do_get_patch "$pcur" > "$TMP_DIFF"
732 case "`stat -c %s \"$TMP_CUR\"`,`stat -c %s \"$TMP_NEXT\"`" in
733 *,0)
734 # since the new patch header is empty, we
735 # don't have to do anything
737 0,*)
738 # current is empty; new is not
739 mv "$pcur" "$pcur~"
740 cat "$TMP_NEXT" > "$pcur"
741 cat "$TMP_DIFF" >> "$pcur"
743 *,*)
744 mv "$pcur" "$pcur~"
745 cat "$TMP_CUR" > "$pcur"
746 echo >> "$pcur"
747 echo "Header from folded patch '$1':" >> "$pcur"
748 echo >> "$pcur"
749 cat "$TMP_NEXT" >> "$pcur"
750 cat "$TMP_DIFF" >> "$pcur"
752 esac
754 rm -f "$TMP_CUR" "$TMP_NEXT" "$TMP_DIFF"
757 __refresh_patch "$2" HEAD^^ 2 "" ""
759 series_remove_patch "$1"
762 # usage: refresh_patch patchname gengitdiff incldiffstat
763 refresh_patch()
765 __refresh_patch "$1" HEAD^ 1 "$2" "$3"
768 # usage: __refresh_patch patchname commitish number_of_commits gengitdiff
769 # incldiffstat
770 __refresh_patch()
772 assert_head_check
775 TMP_DIFF=`get_tmp_file diff`
777 cd_to_toplevel
778 p="$GUILT_DIR/$branch/$1"
779 pname="$1"
781 # get the patch header
782 do_get_full_header "$p" > "$TMP_DIFF"
784 [ ! -z "$4" ] && diffopts="-C -M --find-copies-harder"
786 if [ -n "$5" -o $diffstat = "true" ]; then
788 echo "---"
789 git diff --stat $diffopts "$2"
790 echo ""
791 ) >> "$TMP_DIFF"
794 # get the new patch
795 git diff --binary $diffopts "$2" >> "$TMP_DIFF"
797 # move the new patch in
798 mv "$p" "$p~"
799 mv "$TMP_DIFF" "$p"
801 # commit
802 commit "$pname" "HEAD~$3"
804 # drop folded patches
805 N=`expr "$3" - 1`
807 # remove the patches refs
808 tail -n $N < "$applied" | remove_patch_refs
810 n=`wc -l < "$applied"`
811 n=`expr $n - $N`
812 head_n "$n" < "$applied" > "$applied.tmp"
813 mv "$applied.tmp" "$applied"
817 # usage: munge_hash_range <hash range>
819 # this means:
820 # <hash> - one commit
821 # <hash>.. - hash until head (excludes hash, includes head)
822 # ..<hash> - until hash (includes hash)
823 # <hash1>..<hash2> - from hash to hash (inclusive)
825 # The output of this function is suitable to be passed to "git rev-list"
826 munge_hash_range()
828 case "$1" in
829 *..*..*|*\ *)
830 # double .. or space is illegal
831 return 1;;
832 ..*)
833 # e.g., "..v0.10"
834 echo ${1#..};;
835 *..)
836 # e.g., "v0.19.."
837 echo ${1%..}..HEAD;;
838 *..*)
839 # e.g., "v0.19-rc1..v0.19"
840 echo ${1%%..*}..${1#*..};;
842 # e.g., "v0.19"
843 echo $1^..$1;;
844 *) # empty
845 return 1;;
846 esac
847 return 0
850 # usage: get_tmp_file <prefix> [<opts>]
852 # Get a unique filename and create the file in a non-racy way
853 get_tmp_file()
855 while true; do
856 mktemp $2 "/tmp/guilt.$1.XXXXXXXXXXXXXXX" && break
857 done
860 # usage: guilt_hook <hook name> <args....>
861 guilt_hook()
863 __hookname="$1"
864 [ ! -x "$GIT_DIR/hooks/guilt/$__hookname" ] && return 0
866 shift
868 "$GIT_DIR/hooks/guilt/$__hookname" "$@"
869 return $?
873 # source the command
875 . "$cmd"
878 # Some constants
881 # used for: git apply -C <val>
882 guilt_push_diff_context=1
884 # default diffstat value: true or false
885 DIFFSTAT_DEFAULT="false"
887 # default guilt.reusebranch value: true or false
888 REUSE_BRANCH_DEFAULT="false"
890 # Prefix for guilt branches.
891 GUILT_PREFIX=guilt/
894 # Parse any part of .git/config that belongs to us
897 # generate diffstat?
898 diffstat=`git config --bool guilt.diffstat`
899 [ -z "$diffstat" ] && diffstat=$DIFFSTAT_DEFAULT
901 # reuse Git branch?
902 reuse_branch=`git config --bool guilt.reusebranch`
903 [ -z "$reuse_branch" ] && reuse_branch=$REUSE_BRANCH_DEFAULT
906 # The following gets run every time this file is source'd
909 GUILT_DIR="$GIT_DIR/patches"
911 # To make it harder to accidentally do "git push" with a guilt patch
912 # applied, "guilt push" changes branch from e.g. "master" to
913 # "guilt/master". Set $git_branch to the full branch name, and
914 # $branch to the abbreviated name that the user sees most of the time.
915 # Note: old versions of guilt did not add the "guilt/" prefix. This
916 # code handles that case as well. The prefix will be added when you
917 # have no patches applied and do a "guilt push".
918 raw_git_branch=`get_branch`
919 branch=`echo "$raw_git_branch" | sed -e 's,^'$GUILT_PREFIX',,'`
921 git_branch()
923 if $old_style_prefix
924 then
925 echo $branch
926 elif [ -z "`get_top 2>/dev/null`" ]
927 then
928 echo $branch
929 else
930 echo $GUILT_PREFIX$branch
934 # most of the time we want to verify that the repo's branch has been
935 # initialized, but every once in a blue moon (e.g., we want to run guilt init),
936 # we must avoid the checks
937 if [ -z "$DO_NOT_CHECK_BRANCH_EXISTENCE" ]; then
938 verify_branch
940 # do not check the status file format (guilt repair needs this,
941 # otherwise nothing can do what's necessary to bring the repo into a
942 # useable state)
943 if [ -z "$DO_NOT_CHECK_STATUS_FILE_FORMAT" ]; then
944 [ -s "$GIT_DIR/patches/$branch/status" ] &&
945 grep "^[0-9a-f]\{40\}:" "$GIT_DIR/patches/$branch/status" > /dev/null &&
946 die "Status file appears to use old format, try guilt repair --status"
950 # very useful files
951 series="$GUILT_DIR/$branch/series"
952 applied="$GUILT_DIR/$branch/status"
953 guards_file="$GUILT_DIR/$branch/guards"
955 # determine a pager to use for anything interactive (fall back to more)
956 pager="more"
957 [ ! -z "$PAGER" ] && pager="$PAGER"
959 UNAME_S=`uname -s`
961 if [ -r "$GUILT_PATH/os.$UNAME_S" ]; then
962 . "$GUILT_PATH/os.$UNAME_S"
963 elif [ -r "$GUILT_PATH/../lib/guilt/os.$UNAME_S" ]; then
964 . "$GUILT_PATH/../lib/guilt/os.$UNAME_S"
965 else
966 die "Unsupported operating system: $UNAME_S"
969 if [ -n "`get_top 2>/dev/null`" ]; then
970 # If there is at least one pushed patch, we set
971 # old_style_prefix according to how it was pushed. It is only
972 # possible to change the prefix style while no patches are
973 # applied.
974 if [ "$branch" = "$raw_git_branch" ]; then
975 old_style_prefix=true
976 else
977 old_style_prefix=false
979 else
980 old_style_prefix="$reuse_branch"
983 _main "$@"