Code

commit, merge: initialize static strbuf
[git.git] / git-am.sh
index 14578ceb57645502fe9af25c4c1cfd757a852b68..f4abd9db15d7e7213e0d94a6c36e5205b7c5e0ce 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -5,7 +5,7 @@
 SUBDIRECTORY_OK=Yes
 OPTIONS_KEEPDASHDASH=
 OPTIONS_SPEC="\
-git am [options] <mbox>|<Maildir>...
+git am [options] [<mbox>|<Maildir>...]
 git am [options] --resolved
 git am [options] --skip
 --
@@ -22,6 +22,7 @@ p=              pass it through git-apply
 resolvemsg=     override error message when patch failure occurs
 r,resolved      to be used after a patch failure
 skip            skip the current patch
+abort           restore the original branch and abort the patching operation.
 rebasing        (internal use for git-rebase)"
 
 . git-sh-setup
@@ -30,7 +31,8 @@ set_reflog_action am
 require_work_tree
 cd_to_toplevel
 
-git var GIT_COMMITTER_IDENT >/dev/null || exit
+git var GIT_COMMITTER_IDENT >/dev/null ||
+       die "You need to set your committer info first"
 
 stop_here () {
     echo "$1" >"$dotest/next"
@@ -42,7 +44,7 @@ stop_here_user_resolve () {
            printf '%s\n' "$resolvemsg"
            stop_here $1
     fi
-    cmdline=$(basename $0)
+    cmdline="git am"
     if test '' != "$interactive"
     then
         cmdline="$cmdline -i"
@@ -53,6 +55,7 @@ stop_here_user_resolve () {
     fi
     echo "When you have resolved this problem run \"$cmdline --resolved\"."
     echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
+    echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
 
     stop_here $1
 }
@@ -118,8 +121,8 @@ It does not apply to blobs recorded in its index."
 }
 
 prec=4
-dotest=".dotest"
-sign= utf8=t keep= skip= interactive= resolved= binary= rebasing=
+dotest="$GIT_DIR/rebase-apply"
+sign= utf8=t keep= skip= interactive= resolved= binary= rebasing= abort=
 resolvemsg= resume=
 git_apply_opt=
 
@@ -144,6 +147,8 @@ do
                resolved=t ;;
        --skip)
                skip=t ;;
+       --abort)
+               abort=t ;;
        --rebasing)
                rebasing=t threeway=t keep=t binary=t ;;
        -d|--dotest)
@@ -176,7 +181,7 @@ fi
 
 if test -d "$dotest"
 then
-       case "$#,$skip$resolved" in
+       case "$#,$skip$resolved$abort" in
        0,*t*)
                # Explicit resume command and we do not have file, so
                # we are happy.
@@ -194,11 +199,27 @@ then
                false
                ;;
        esac ||
-       die "previous dotest directory $dotest still exists but mbox given."
+       die "previous rebase directory $dotest still exists but mbox given."
        resume=yes
+
+       case "$skip,$abort" in
+       t,)
+               git rerere clear
+               git read-tree --reset -u HEAD HEAD
+               orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
+               git reset HEAD
+               git update-ref ORIG_HEAD $orig_head
+               ;;
+       ,t)
+               git rerere clear
+               git read-tree --reset -u HEAD ORIG_HEAD
+               git reset ORIG_HEAD
+               rm -fr "$dotest"
+               exit ;;
+       esac
 else
-       # Make sure we are not given --skip nor --resolved
-       test ",$skip,$resolved," = ,,, ||
+       # Make sure we are not given --skip, --resolved, nor --abort
+       test "$skip$resolved$abort" = "" ||
                die "Resolve operation not in progress, we are not resuming."
 
        # Start afresh.
@@ -241,6 +262,7 @@ else
                : >"$dotest/rebasing"
        else
                : >"$dotest/applying"
+               git update-ref ORIG_HEAD HEAD
        fi
 fi
 
@@ -282,7 +304,6 @@ last=`cat "$dotest/last"`
 this=`cat "$dotest/next"`
 if test "$skip" = t
 then
-       git rerere clear
        this=`expr "$this" + 1`
        resume=
 fi
@@ -323,7 +344,7 @@ do
                        <"$dotest"/info >/dev/null &&
                        go_next && continue
 
-               test -s $dotest/patch || {
+               test -s "$dotest/patch" || {
                        echo "Patch is empty.  Was it split wrong?"
                        stop_here $this
                }
@@ -435,7 +456,7 @@ do
                stop_here $this
        fi
 
-       printf 'Applying %s\n' "$FIRSTLINE"
+       printf 'Applying: %s\n' "$FIRSTLINE"
 
        case "$resolved" in
        '')