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
Junio C Hamano <gitster@pobox.com>
Date
Nov 1, 2024, 07:05 UTC
Message-ID
<xmqq4j4r4enh.fsf@gitster.g>
In-Reply-To
<3af246eb-3473-4964-9aed-ecff62b215b7@cleb.io>
Iago-lito <iago-lito@cleb.io> writes:
Show 40 quoted lines
> 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: Iago-litoNext: Iago-lito
Message 2 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.