]> git.ipfire.org Git - thirdparty/git.git/blobdiff - git-am.sh
Allow git-merge to select the default strategy.
[thirdparty/git.git] / git-am.sh
index 4232e27411036e339f8102e45b7cb699c808a798..c3bbd78eabf6f3d5eaa0f31bb0c240a26f28e0c5 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -6,6 +6,7 @@ USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
   [--interactive] [--whitespace=<option>] <mbox>...
   or, when resuming [--skip | --resolved]'
 . git-sh-setup
+set_reflog_action am
 
 git var GIT_COMMITTER_IDENT >/dev/null || exit
 
@@ -45,6 +46,12 @@ go_next () {
        this=$next
 }
 
+cannot_fallback () {
+       echo "$1"
+       echo "Cannot fall back to three-way merge."
+       exit 1
+}
+
 fall_back_3way () {
     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
 
@@ -52,19 +59,23 @@ fall_back_3way () {
     mkdir "$dotest/patch-merge-tmp-dir"
 
     # First see if the patch records the index info that we can use.
-    if git-apply -z --index-info "$dotest/patch" \
-       >"$dotest/patch-merge-index-info" 2>/dev/null &&
-       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
-       git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
-       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
-       git-write-tree >"$dotest/patch-merge-base+" &&
-       # index has the base tree now.
-       GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+    git-apply -z --index-info "$dotest/patch" \
+       >"$dotest/patch-merge-index-info" &&
+    GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+    git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
+    GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
+    git-write-tree >"$dotest/patch-merge-base+" ||
+    cannot_fallback "Patch does not record usable index information."
+
+    echo Using index info to reconstruct a base tree...
+    if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
        git-apply $binary --cached <"$dotest/patch"
     then
-       echo Using index info to reconstruct a base tree...
        mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
        mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
+    else
+        cannot_fallback "Did you hand edit your patch?
+It does not apply to blobs recorded in its index."
     fi
 
     test -f "$dotest/patch-merge-index" &&
@@ -77,7 +88,7 @@ fall_back_3way () {
     # This is not so wrong.  Depending on which base we picked,
     # orig_tree may be wildly different from ours, but his_tree
     # has the same set of wildly different changes in parts the
-    # patch did not touch, so resolve ends up cancelling them,
+    # patch did not touch, so resolve ends up canceling them,
     # saying that we reverted all those changes.
 
     git-merge-resolve $orig_tree -- HEAD $his_tree || {
@@ -97,7 +108,7 @@ while case "$#" in 0) break;; esac
 do
        case "$1" in
        -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
-       dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
+       dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
        -d|--d|--do|--dot|--dote|--dotes|--dotest)
        case "$#" in 1) usage ;; esac; shift
        dotest="$1"; shift;;
@@ -152,7 +163,24 @@ fi
 
 if test -d "$dotest"
 then
-       test ",$#," = ",0," ||
+       case "$#,$skip$resolved" in
+       0,*t*)
+               # Explicit resume command and we do not have file, so
+               # we are happy.
+               : ;;
+       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
+               # unreliable -- stdin could be /dev/null for example
+               # and the caller did not intend to feed us a patch but
+               # wanted to continue unattended.
+               tty -s
+               ;;
+       *)
+               false
+               ;;
+       esac ||
        die "previous dotest directory $dotest still exists but mbox given."
        resume=yes
 else
@@ -215,6 +243,10 @@ last=`cat "$dotest/last"`
 this=`cat "$dotest/next"`
 if test "$skip" = t
 then
+       if test -d "$GIT_DIR/rr-cache"
+       then
+               git-rerere clear
+       fi
        this=`expr "$this" + 1`
        resume=
 fi
@@ -366,17 +398,21 @@ do
                changed="$(git-diff-index --cached --name-only HEAD)"
                if test '' = "$changed"
                then
-                       echo "No changes - did you forget update-index?"
+                       echo "No changes - did you forget to use 'git add'?"
                        stop_here_user_resolve $this
                fi
                unmerged=$(git-ls-files -u)
                if test -n "$unmerged"
                then
                        echo "You still have unmerged paths in your index"
-                       echo "did you forget update-index?"
+                       echo "did you forget to use 'git add'?"
                        stop_here_user_resolve $this
                fi
                apply_status=0
+               if test -d "$GIT_DIR/rr-cache"
+               then
+                       git rerere
+               fi
                ;;
        esac
 
@@ -413,7 +449,7 @@ do
        parent=$(git-rev-parse --verify HEAD) &&
        commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
        echo Committed: $commit &&
-       git-update-ref -m "am: $SUBJECT" HEAD $commit $parent ||
+       git-update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
        stop_here $this
 
        if test -x "$GIT_DIR"/hooks/post-applypatch