How to 'git-am' apply a patch created with 'git-format-patch --no-prefix'?
Asked Answered
V

2

7

I have a reason¹ to create my git commits as patch files using git-format-patch with the --no-prefix option.

This option changes the patch output to not add the git-diff-specific prefixes a/ / b/ in file paths in the patch files. It allows tools like patch to apply the patch files without the need to pass -p1 as argument.

All cool so far. However, it seems that I cannot actually apply them anymore with Git itself (git-am):

$ git am path/to/0001-patch.patch
Applying: <commit message subject>
error: <path>: does not exist in index
Patch failed at 0001
[...]

How do I apply them with git-am now, while maintaining plain patch compatibility?

¹ It allows me to use it as patch files in Bazel without custom patch commands, as you'd need to apply patches with patch -p1 [...].

Variole answered 3/1, 2019 at 13:3 Comment(0)
V
4

git-am passes on a few options to git-apply, including the -p option, which does the same as it does with patch. Ie., apply such patches with -p0:

git am -p0 path/to/0001-patch.patch

It would have been great if git-format-patch was able to hint git-am within the auxiliary data of the patch file created, that it should apply them without the assumption of a path prefix — just like it can hint git-am with base tree information nowadays (see --base option).

Variole answered 3/1, 2019 at 13:3 Comment(1)
First solution to this I've found which actually workedCharacterization
G
1

The git am -p0 can be illustrated by its latest doc update, from Git 2.41 (Q2 2023):

See commit 9b0c7f3 (21 Mar 2023) by Junio C Hamano (gitster).
(Merged by Junio C Hamano -- gitster -- in commit a15b845, 30 Mar 2023)

am: refer to format-patch in the documentation

Suggested-by: Kai Grossjohann

There were two reasons we didn't do this.
As "git am"(man) is designed to grok e-mailed patches, not necessarily taken out of a Git repostiory or even if it came from a Git repository not necessarily produced with format-patch, we didn't want to single it out as the "blessed" input producer to the command.
Also, in the original workflow that "git am" was invented for, the user of "am" was expected to be a different person than the users of "format-patch".

But this is a very safe change to make in 2023.

Thanks to the effort by many contributors, Git ended up becoming a bit more popular than we initially thought it would be, and "format-patch", which took me a few weeks to pursuade Linus to take in 2005, seems to have become the de-facto standard tool to produce patch e-mails.

Interestingly, the documentation for "git apply"(man), which is listed in SEE ALSO section of "git am" documentation, does mention "am" and "format-patch" as two things that are related but different from "apply" in an early part.

git am now includes in its man page:

You could think of it as a reverse operation of git format-patch run on a branch with a straight history without merges.

git am now includes in its man page:

git apply, git format-patch.

Glasscock answered 25/4, 2023 at 21:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.