Work around missing hard links on FAT formatted media
[git.git] / git-am.sh
index 9e41e70..115ebad 100755 (executable)
--- a/git-am.sh
+++ b/git-am.sh
@@ -35,10 +35,10 @@ 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 --show-index-info "$dotest/patch" \
+    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 --index-info <"$dotest/patch-merge-index-info" &&
+       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.
@@ -153,6 +153,7 @@ if test -d "$dotest"
 then
        test ",$#," = ",0," ||
        die "previous dotest directory $dotest still exists but mbox given."
+       resume=yes
 else
        # Make sure we are not given --skip
        test ",$skip," = ,, ||
@@ -172,7 +173,6 @@ else
        echo "$sign" >"$dotest/sign"
        echo "$utf8" >"$dotest/utf8"
        echo "$keep" >"$dotest/keep"
-       echo "$threeway" >"$dotest/3way"
        echo 1 >"$dotest/next"
 fi
 
@@ -193,7 +193,6 @@ then
 else
        SIGNOFF=
 fi
-threeway=$(cat "$dotest/3way")
 
 last=`cat "$dotest/last"`
 this=`cat "$dotest/next"`
@@ -217,15 +216,21 @@ do
                go_next
                continue
        }
-       git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
-               <"$dotest/$msgnum" >"$dotest/info" ||
-               stop_here $this
-       git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
+       case "$resume" in
+       '')
+               git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
+                       <"$dotest/$msgnum" >"$dotest/info" ||
+                       stop_here $this
+               git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
+               ;;
+       esac
+       resume=
 
        GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
        GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
        GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
        SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
+       export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 
        case "$keep_subject" in -k)  SUBJECT="[PATCH] $SUBJECT" ;; esac
        if test '' != "$SIGNOFF"
@@ -256,6 +261,8 @@ do
 
        if test "$interactive" = t
        then
+           test -t 0 ||
+           die "cannot be interactive without stdin connected to a terminal."
            action=again
            while test "$action" = again
            do
@@ -263,14 +270,17 @@ do
                echo "--------------------------"
                cat "$dotest/final-commit"
                echo "--------------------------"
-               echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
+               echo -n "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
                read reply
                case "$reply" in
-               y*|Y*) action=yes ;;
-               a*|A*) action=yes interactive= ;;
-               n*|N*) action=skip ;;
-               e*|E*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
+               [yY]*) action=yes ;;
+               [aA]*) action=yes interactive= ;;
+               [nN]*) action=skip ;;
+               [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
                       action=again ;;
+               [vV]*) action=again
+                      LESS=-S ${PAGER:-less} "$dotest/patch" ;;
+               *)     action=again ;;
                esac
            done
        else
@@ -296,15 +306,19 @@ do
        git-apply --index "$dotest/patch"; apply_status=$?
        if test $apply_status = 1 && test "$threeway" = t
        then
-               (fall_back_3way) || stop_here $this
-
-               # Applying the patch to an earlier tree and merging the
-               # result may have produced the same tree as ours.
-               if test '' = "$(git-diff-index --cached --name-only -z HEAD)"
+               if (fall_back_3way)
                then
-                       echo No changes -- Patch already applied.
-                       go_next
-                       continue
+                   # Applying the patch to an earlier tree and merging the
+                   # result may have produced the same tree as ours.
+                   changed="$(git-diff-index --cached --name-only -z HEAD)"
+                   if test '' = "$changed"
+                   then
+                           echo No changes -- Patch already applied.
+                           go_next
+                           continue
+                   fi
+                   # clear apply_status -- we have successfully merged.
+                   apply_status=0
                fi
        fi
        if test $apply_status != 0