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

Bug: With diff.noprefix=true, `git format-patch` creates a patch that `git am` rejects

From
Keith Thompson <keith.s.thompson@gmail.com>
Date
Jan 8, 2022, 00:36 UTC
Message-ID
<CAAHpriMPdahH2xbrrQbeCJPYpLhr6tuvT6xsG3nACmskKF1v2w@mail.gmail.com>

Thank you for filling out a Git bug report! Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue) git format-patch master

What did you expect to happen? (Expected behavior) I expected `git format-patch` to create a patch consumable by `git am`.

What happened instead? (Actual behavior) `git am` choked on the patch because it was missing the `a/` and `b/` prefixes.

For a simple example, a patch affecting only a top-level file, the error was:
    Applying: Fix typo
    error: git diff header lacks filename information when removing 1
leading pathname component (line 9)
    Patch failed at 0001 Fix typo
For a patch affecting a file in a subdirectory ("DIR/README"), the error was:
    Applying: Fix typo
    error: README: does not exist in index
    Patch failed at 0001 Fix typo

What's different between what you expected and what actually happened? See above.

Anything else you want to add: The details: I had this in my $HOME/.gitconfig:

[diff]
        noprefix = true

This caused `git format-patch` *not* to generate the `a/` and `b/` prefixes in the patch file. Setting "noprefix = false" works around the problem.

Here are the first few lines of the resulting patch file:
===
cat 0001-Fix-documented-argv-and-envp-params-for-posix_spawn.patch,000
From f4a41189d86bc208a90e754387294b9c4c11cdfc Mon Sep 17 00:00:00 2001
From: Keith Thompson <Keith.S.Thompson@gmail.com>
Date: Wed, 5 Jan 2022 13:30:16 -0800
Subject: [PATCH] Fix documented argv and envp params for posix_spawn
---
 newlib/libc/posix/posix_spawn.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git newlib/libc/posix/posix_spawn.c newlib/libc/posix/posix_spawn.c
index 005471fde..85bfa6477 100644
--- newlib/libc/posix/posix_spawn.c
+++ newlib/libc/posix/posix_spawn.c
===

Feeding this patch to `git am` caused it to strip the `newlib/`
prefix from the file name.  There doesn't seem to be a way to coax
`git format-patch` to ignore the current diff.noprefix setting,
or `git am` to tolerate the missing prefixes.

Changing my .gitconfig to set diff.noprefix to false avoids the problem.
I could probably have done that in the local repo rather than in my
$HOME/.gitconfig .

Suggested possible fixes:

1. Make `git format-patch` ignore the `diff.noprefix` setting, or at
   least provide an option to do so.  In my opinion this is the best
   solution.  There's no point in letting `git format-patch` create
   a patch that `git am` can't handle.

2. Make `git am` tolerate the missing prefixes, perhaps by not
   removing prefixes that match, or at least provide an option to
   do so.  For example, given
       a/newlib/libc/posix/posix_spawn.c
       b/newlib/libc/posix/posix_spawn.c
    delete the a/ and b/ prefixes, but given
       newlib/libc/posix/posix_spawn.c
       newlib/libc/posix/posix_spawn.c
    leave the path alone because the top-level directory is identical.

3. Make `git format-patch` print a warning if diff.noprefix is true,
   letting the person creating the patch know how to avoid the problem.

4. Make `git am` recognize this issue and print a warning, letting
   the recipient of the patch know they need to manually edit the patch
   or request a new one.  (This would still be inconvenient.)

This issue is likely to affect other commands including `git apply`
and possibly others.

Please review the rest of the bug report below.
You can delete any lines you don't wish to share.


[System Info]
git version:
git version 2.33.1
cpu: x86_64
built from commit: af6d1d602a8f64164b266364339c4e936d5bbc33
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64
compiler info: gnuc: 9.3
libc info: glibc: 2.31
$SHELL (typically, interactive shell): /o/bin/bash
Next: Gwyneth Morgan
Message 1 of 2 in “Bug: With diff.noprefix=true, `git format-patch` creates a patch that `git am` rejects”
  1. Keith ThompsonJan 8, 2022
  2. Gwyneth MorganJan 8, 2022

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.