git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: `git apply -p` doesn't remove source prefix in 'rename' diff entries.

From
IIago-lito <iago-lito@cleb.io>
Date
Nov 4, 2024, 20:35 UTC
Message-ID
<d67e133c-f287-4009-9244-eaeffd84daeb@cleb.io>
In-Reply-To
<xmqq4j4r4enh.fsf@gitster.g>
Hello @Junio, and thank you for feedback :)

If I understand correctly, you are suggesting that `git diff`/`git apply` are behaving correctly here, but they might not be the right tool for the job? I sort of sense that `git` could achieve this "z = x + 𝛥y" folder operation (because it has all the machinery required to do so), but I'm not sure how to leverage it correctly. Do I need to get into "plumbing"?

-- Iago-lito

On 01/11/2024 08:05, Junio C Hamano wrote:
Show 78 quoted lines
> Iago-lito <iago-lito@cleb.io> writes:
>
>> Hello,
>>
>> I have troubles understanding the behaviour of `git apply` with respect to renames in a `--no-index` context.
>>
>> Let us craft a toy folder:
>> ```sh
>> $ mkdir x
>> $ echo a > x/a # To be modified.
>> $ echo b > x/b # To be renamed.
>> ```
>>
>> Duplicate it twice to get three identical folders `x = y = z`.
>>
>> ```sh
>> $ cp -r x y
>> $ cp -r x z
>> ```
>>
>> Modify `y`:
>> ```sh
>> $ echo newline >> y/a # Edit.
>> $ mv y/b y/c # Rename.
>> ```
>>
>> Now I would like to use git as a "better GNU patch".
>> Calculate the diff from `x` to `y`:
>> ```sh
>> $ git diff --no-prefix x y | tee patch
>> diff --git x/a y/a
>> index 7898192..4030aa5 100644
>> --- x/a
>> +++ y/a
>> @@ -1 +1,2 @@
>> a
>> +newline
>> diff --git x/b y/c
>> similarity index 100%
>> rename from x/b
>> rename to y/c
>> ```
> Interesting.  I think "git diff --no-index" is "wrong" here, but
> "wrong" is not in the sense that it is _incorrect_, but in the sense
> that it merely is incompatible with what you are trying to achieve.
>
> If you had files a and b, modified a in place and renamed b to c,
> and if you did this in a repository with tracked files, you would
> have seen
>
>     $ git diff --src-prefix=x --dst-prefix=y
>     diff --git x/a y/a
>     index ...
>     --- x/a
>     +++ y/a
>     ...
>     diff --git x/b y/c
>     similarity index 100%
>     rename from b
>     rename to c
>
> Notice how "rename from" and "rename to" information is different in
> the "real" git-diff output, as opposed to "git diff --no-index"
> output you have?
>
> At least when comparing two directories, "git diff --no-index X Y"
> could be interpreted as a request to treat as if X and Y were
> checkouts from two Git commits X and Y, and by comparing them the
> output should be identical to the result of comparing the contents
> of commits X and Y.
>
> But feeding "git apply" however may not be the only reason why
> people are using "diff --no-index X Y", and there may be a
> legitimate reason why peole want to see "from x/b to y/c" in the
> output, so unconditionally changing the output may very likely break
> workflows of other people.
>
>
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 4 in “`git apply -p` doesn't remove source prefix in 'rename' diff entries.”
  1. Iago-litoNov 1, 2024
  2. Junio C HamanoNov 1, 2024
  3. Iago-litoNov 4, 2024
  4. Junio C HamanoNov 5, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.