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

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

From
IIago-lito <iago-lito@cleb.io>
Date
Nov 1, 2024, 00:17 UTC
Message-ID
<3af246eb-3473-4964-9aed-ecff62b215b7@cleb.io>
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
```

Then apply this patch to `z` so as to end up with `x != y = z`:
```sh
$ git apply -p1 --directory z patch
error: z/x/b: No such file or directory
```

I would expect `z/b` to be renamed into `z/c`, but I get the above error instead.

Although understand that `z/x/b` is not a valid path, I don't understand that git produces such a path, because I'm assuming that `-p1` is supposed to remove the `x/` part.

Interestingly, the procedure works fine without the rename. The rename also worth fine if I manually remove the `x/` and `y/` prefixes from the rename lines in the patch.

This makes the behaviour of `git apply` appear inconsistent. It is nonetheless expected ? If so, why ? If not, then is it a bug ?

Thank you for support,

[System Info]
git version:
git version 2.46.2
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
libcurl: 8.10.1
OpenSSL: OpenSSL 3.3.2 3 Sep 2024
zlib: 1.3.1
uname: Linux 6.10.10-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 12 Sep 2024
17:21:02 +0000 x86_64
compiler info: gnuc: 14.2
libc info: glibc: 2.40
$SHELL (typically, interactive shell): /usr/bin/zsh


[Enabled Hooks]
not run from a git repository - no hooks to show

--
Iago-lito
Next: Junio C Hamano
Message 1 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.