# Patches generated by "git format-patch -M -B" can't be applied with "git apply"?

3 messages from 2015-09-14 to 2015-09-14. Participants: Dan Kegel, Junio C Hamano.
Thread: https://gitlist.dev/t/40351

## Dan Kegel, 2015-09-14 22:21

Subject: Patches generated by "git format-patch -M -B" can't be applied with "git apply"?
Message-ID: <CAPF-yOb4dRWGLmL==x8=ifvU4Znsut9XQWMLPxBCPq7TzgJ84A@mail.gmail.com>
URL: https://gitlist.dev/e/CAPF-yOb4dRWGLmL%3D%3Dx8%3DifvU4Znsut9XQWMLPxBCPq7TzgJ84A%40mail.gmail.com

```
A bot I wrote uses 'git format-patch -M -B' to generate a patch
to send off to be applied with 'git am' on a remote machine.
Alas, renames do not appear to work; git am rejects them with
 error: file2: already exists in working directory
This happens both with the git that ships with Ubuntu 14.04
and with master ( git version 2.6.0.rc1.16.g1962994 ).

I noticed that git has a similar test case already,
t4130-apply-criss-cross-rename.sh
so I munged that to reproduce the error,
http://kegel.com/git/t4154-apply-half-criss-cross-rename.sh.txt
Running
 ./t4154-apply-half-criss-cross-rename.sh -v
fails for me reliably with
 error: file2: already exists in working directory
with either v2.6.0-rc2 or random local git on local Mac and Linux.
is this behavior indeed a bug, or am I breaking some rule?
Thanks!
- Dan

p.s. Here's the test case inline, but if email munges whitespace,
the url above might be better.

#!/bin/sh

test_description='git apply handling half-criss-cross rename patch.'
. ./test-lib.sh

create_file() {
        cnt=0
        while test $cnt -le 100
        do
                cnt=$(($cnt + 1))
                echo "$2" >> "$1"
        done
}

test_expect_success 'setup' '
        create_file file1 "File1 contents" &&
        create_file file2 "File2 contents" &&
        git add file1 file2 &&
        git commit -m 1
'

test_expect_success 'half criss-cross rename' '
        mv file1 file2 &&
        git rm file1 &&
        git add file2 &&
        git commit -m 2
'

test_expect_success 'diff -M -B' '
        git format-patch -M -B HEAD^ --stdout > diff &&
        git reset --hard HEAD^
'

test_expect_success 'apply' '
        git apply --check diff
'

test_done

```

## Junio C Hamano, 2015-09-14 22:32

Subject: Re: Patches generated by "git format-patch -M -B" can't be applied with "git apply"?
Message-ID: <xmqqio7cvdgy.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqio7cvdgy.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAPF-yOb4dRWGLmL==x8=ifvU4Znsut9XQWMLPxBCPq7TzgJ84A@mail.gmail.com>

```
Dan Kegel <dank@kegel.com> writes:

Yes, "diff -M -B" is known to be problematic, unfortunately.  It
incorrectly creates an irreversible patch under certain conditions.

http://thread.gmane.org/gmane.linux.kernel/1879635

```

## Junio C Hamano, 2015-09-14 22:32

Subject: Re: Patches generated by "git format-patch -M -B" can't be applied with "git apply"?
Message-ID: <xmqqh9mwvdgu.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqh9mwvdgu.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <CAPF-yOb4dRWGLmL==x8=ifvU4Znsut9XQWMLPxBCPq7TzgJ84A@mail.gmail.com>

```
Yes, "diff -M -B" is known to be problematic, unfortunately.  It
incorrectly creates an irreversible patch under certain conditions.

http://thread.gmane.org/gmane.linux.kernel/1879635

```
