Code

revision traversal: show full history with merge simplification
[git.git] / git-am.sh
index 4dce87bdff7763cdd6e9d73ed236e40a5b3ca34c..8f91a97eb3a07908b35de086d555822c928fd2e9 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -2,13 +2,13 @@
 #
 # Copyright (c) 2005, 2006 Junio C Hamano
 
+SUBDIRECTORY_OK=Yes
 OPTIONS_KEEPDASHDASH=
 OPTIONS_SPEC="\
-git-am [options] <mbox>|<Maildir>...
-git-am [options] --resolved
-git-am [options] --skip
+git am [options] [<mbox>|<Maildir>...]
+git am [options] (--resolved | --skip | --abort)
 --
-d,dotest=       use <dir> and not .dotest
+d,dotest=       (removed -- do not use)
 i,interactive   run interactively
 b,binary        pass --allow-binary-replacement to git-apply
 3,3way          allow fall back on 3way merging if needed
@@ -20,13 +20,18 @@ C=              pass it through git-apply
 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"
+skip            skip the current patch
+abort           restore the original branch and abort the patching operation.
+rebasing        (internal use for git-rebase)"
 
 . git-sh-setup
+prefix=$(git rev-parse --show-prefix)
 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"
@@ -38,7 +43,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"
@@ -47,12 +52,9 @@ stop_here_user_resolve () {
     then
         cmdline="$cmdline -3"
     fi
-    if test '.dotest' != "$dotest"
-    then
-        cmdline="$cmdline -d=$dotest"
-    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,7 +120,8 @@ It does not apply to blobs recorded in its index."
 }
 
 prec=4
-dotest=.dotest sign= utf8=t keep= skip= interactive= resolved= binary=
+dotest="$GIT_DIR/rebase-apply"
+sign= utf8=t keep= skip= interactive= resolved= binary= rebasing= abort=
 resolvemsg= resume=
 git_apply_opt=
 
@@ -143,8 +146,13 @@ do
                resolved=t ;;
        --skip)
                skip=t ;;
+       --abort)
+               abort=t ;;
+       --rebasing)
+               rebasing=t threeway=t keep=t binary=t ;;
        -d|--dotest)
-               shift; dotest=$1;;
+               die "-d option is no longer supported.  Do not use."
+               ;;
        --resolvemsg)
                shift; resolvemsg=$1 ;;
        --whitespace)
@@ -172,7 +180,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.
@@ -180,7 +188,7 @@ then
        0,)
                # No file input but without resume parameters; catch
                # user error to feed us a patch from standard input
-               # when there is already .dotest.  This is somewhat
+               # when there is already $dotest.  This is somewhat
                # unreliable -- stdin could be /dev/null for example
                # and the caller did not intend to feed us a patch but
                # wanted to continue unattended.
@@ -190,16 +198,50 @@ 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.
        mkdir -p "$dotest" || exit
 
+       if test -n "$prefix" && test $# != 0
+       then
+               first=t
+               for arg
+               do
+                       test -n "$first" && {
+                               set x
+                               first=
+                       }
+                       case "$arg" in
+                       /*)
+                               set "$@" "$arg" ;;
+                       *)
+                               set "$@" "$prefix$arg" ;;
+                       esac
+               done
+               shift
+       fi
        git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||  {
                rm -fr "$dotest"
                exit 1
@@ -214,6 +256,13 @@ else
        echo "$utf8" >"$dotest/utf8"
        echo "$keep" >"$dotest/keep"
        echo 1 >"$dotest/next"
+       if test -n "$rebasing"
+       then
+               : >"$dotest/rebasing"
+       else
+               : >"$dotest/applying"
+               git update-ref ORIG_HEAD HEAD
+       fi
 fi
 
 case "$resolved" in
@@ -242,7 +291,7 @@ fi
 ws=`cat "$dotest/whitespace"`
 if test "$(cat "$dotest/sign")" = t
 then
-       SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
+       SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
                        s/>.*/>/
                        s/^/Signed-off-by: /'
                `
@@ -254,7 +303,6 @@ last=`cat "$dotest/last"`
 this=`cat "$dotest/next"`
 if test "$skip" = t
 then
-       git rerere clear
        this=`expr "$this" + 1`
        resume=
 fi
@@ -295,15 +343,24 @@ 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
                }
-               SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
-               case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
-
-               (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
-                       git stripspace > "$dotest/msg-clean"
+               if test -f "$dotest/rebasing" &&
+                       commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
+                               -e q "$dotest/$msgnum") &&
+                       test "$(git cat-file -t "$commit")" = commit
+               then
+                       git cat-file commit "$commit" |
+                       sed -e '1,/^$/d' >"$dotest/msg-clean"
+               else
+                       SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
+                       case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
+
+                       (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
+                               git stripspace > "$dotest/msg-clean"
+               fi
                ;;
        esac
 
@@ -326,7 +383,7 @@ do
                LAST_SIGNED_OFF_BY=`
                    sed -ne '/^Signed-off-by: /p' \
                    "$dotest/msg-clean" |
-                   tail -n 1
+                   sed -ne '$p'
                `
                ADD_SIGNOFF=`
                    test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
@@ -384,7 +441,7 @@ do
        else
            action=yes
        fi
-       FIRSTLINE=$(head -1 "$dotest/final-commit")
+       FIRSTLINE=$(sed 1q "$dotest/final-commit")
 
        if test $action = skip
        then
@@ -398,7 +455,7 @@ do
                stop_here $this
        fi
 
-       printf 'Applying %s\n' "$FIRSTLINE"
+       printf 'Applying: %s\n' "$FIRSTLINE"
 
        case "$resolved" in
        '')