{"thread":{"id":"57211","subject":"Bug: With diff.noprefix=true, `git format-patch` creates a patch that `git am` rejects","startedAt":"2022-01-08T00:36:43Z","lastAt":"2022-01-08T02:56:04Z","messageCount":2,"participants":["Keith Thompson","Gwyneth Morgan"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"445765","messageId":"CAAHpriMPdahH2xbrrQbeCJPYpLhr6tuvT6xsG3nACmskKF1v2w@mail.gmail.com","threadId":"57211","inReplyTo":null,"subject":"Bug: With diff.noprefix=true, `git format-patch` creates a patch that `git am` rejects","fromName":"Keith Thompson","fromEmail":"keith.s.thompson@gmail.com","sentAt":"2022-01-08T00:36:27Z","receivedAt":"2022-01-08T00:36:43Z","isPatch":false,"sender":{"key":"keith.s.thompson@gmail.com","avatar":"https://gravatar.com/avatar/2cd597778c4fcea958f6ff673882f768bd9506a95bc509925cb7eff718601cd5?d=mp&s=160"},"body":"Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\ngit format-patch master\n\nWhat did you expect to happen? (Expected behavior)\nI expected `git format-patch` to create a patch consumable by `git am`.\n\nWhat happened instead? (Actual behavior)\n`git am` choked on the patch because it was missing the `a/` and `b/`\nprefixes.\n\nFor a simple example, a patch affecting only a top-level file, the error was:\n\n    Applying: Fix typo\n    error: git diff header lacks filename information when removing 1\nleading pathname component (line 9)\n    Patch failed at 0001 Fix typo\n\nFor a patch affecting a file in a subdirectory (\"DIR/README\"), the error was:\n\n    Applying: Fix typo\n    error: README: does not exist in index\n    Patch failed at 0001 Fix typo\n\nWhat's different between what you expected and what actually happened?\nSee above.\n\nAnything else you want to add:\nThe details: I had this in my $HOME/.gitconfig:\n\n[diff]\n        noprefix = true\n\nThis caused `git format-patch` *not* to generate the `a/` and `b/`\nprefixes in the patch file.  Setting \"noprefix = false\" works around\nthe problem.\n\nHere are the first few lines of the resulting patch file:\n===\ncat 0001-Fix-documented-argv-and-envp-params-for-posix_spawn.patch,000\nFrom f4a41189d86bc208a90e754387294b9c4c11cdfc Mon Sep 17 00:00:00 2001\nFrom: Keith Thompson <Keith.S.Thompson@gmail.com>\nDate: Wed, 5 Jan 2022 13:30:16 -0800\nSubject: [PATCH] Fix documented argv and envp params for posix_spawn\n\n---\n newlib/libc/posix/posix_spawn.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git newlib/libc/posix/posix_spawn.c newlib/libc/posix/posix_spawn.c\nindex 005471fde..85bfa6477 100644\n--- newlib/libc/posix/posix_spawn.c\n+++ newlib/libc/posix/posix_spawn.c\n===\n\nFeeding this patch to `git am` caused it to strip the `newlib/`\nprefix from the file name.  There doesn't seem to be a way to coax\n`git format-patch` to ignore the current diff.noprefix setting,\nor `git am` to tolerate the missing prefixes.\n\nChanging my .gitconfig to set diff.noprefix to false avoids the problem.\nI could probably have done that in the local repo rather than in my\n$HOME/.gitconfig .\n\nSuggested possible fixes:\n\n1. Make `git format-patch` ignore the `diff.noprefix` setting, or at\n   least provide an option to do so.  In my opinion this is the best\n   solution.  There's no point in letting `git format-patch` create\n   a patch that `git am` can't handle.\n\n2. Make `git am` tolerate the missing prefixes, perhaps by not\n   removing prefixes that match, or at least provide an option to\n   do so.  For example, given\n       a/newlib/libc/posix/posix_spawn.c\n       b/newlib/libc/posix/posix_spawn.c\n    delete the a/ and b/ prefixes, but given\n       newlib/libc/posix/posix_spawn.c\n       newlib/libc/posix/posix_spawn.c\n    leave the path alone because the top-level directory is identical.\n\n3. Make `git format-patch` print a warning if diff.noprefix is true,\n   letting the person creating the patch know how to avoid the problem.\n\n4. Make `git am` recognize this issue and print a warning, letting\n   the recipient of the patch know they need to manually edit the patch\n   or request a new one.  (This would still be inconvenient.)\n\nThis issue is likely to affect other commands including `git apply`\nand possibly others.\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n\n[System Info]\ngit version:\ngit version 2.33.1\ncpu: x86_64\nbuilt from commit: af6d1d602a8f64164b266364339c4e936d5bbc33\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64\ncompiler info: gnuc: 9.3\nlibc info: glibc: 2.31\n$SHELL (typically, interactive shell): /o/bin/bash\n"},{"id":"445778","messageId":"Ydj6aYySwK5qP/uo@tilde.club","threadId":"57211","inReplyTo":"CAAHpriMPdahH2xbrrQbeCJPYpLhr6tuvT6xsG3nACmskKF1v2w@mail.gmail.com","subject":"Re: Bug: With diff.noprefix=true, `git format-patch` creates a patch that `git am` rejects","fromName":"Gwyneth Morgan","fromEmail":"gwymor@tilde.club","sentAt":"2022-01-08T02:45:59Z","receivedAt":"2022-01-08T02:56:04Z","isPatch":false,"sender":{"key":"gwymor@tilde.club","avatar":"https://avatars.githubusercontent.com/u/87623694?v=4"},"body":"On 2022-01-07 16:36:27-0800, Keith Thompson wrote:\n> The details: I had this in my $HOME/.gitconfig:\n> \n> [diff]\n>         noprefix = true\n> \n> This caused `git format-patch` *not* to generate the `a/` and `b/`\n> prefixes in the patch file.  Setting \"noprefix = false\" works around\n> the problem.\n> \n> Here are the first few lines of the resulting patch file:\n> ===\n> cat 0001-Fix-documented-argv-and-envp-params-for-posix_spawn.patch,000\n> >From f4a41189d86bc208a90e754387294b9c4c11cdfc Mon Sep 17 00:00:00 2001\n> From: Keith Thompson <Keith.S.Thompson@gmail.com>\n> Date: Wed, 5 Jan 2022 13:30:16 -0800\n> Subject: [PATCH] Fix documented argv and envp params for posix_spawn\n> \n> ---\n>  newlib/libc/posix/posix_spawn.c | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n> \n> diff --git newlib/libc/posix/posix_spawn.c newlib/libc/posix/posix_spawn.c\n> index 005471fde..85bfa6477 100644\n> --- newlib/libc/posix/posix_spawn.c\n> +++ newlib/libc/posix/posix_spawn.c\n> ===\n> \n> Feeding this patch to `git am` caused it to strip the `newlib/`\n> prefix from the file name.  There doesn't seem to be a way to coax\n> `git format-patch` to ignore the current diff.noprefix setting,\n> or `git am` to tolerate the missing prefixes.\n\nThe -p<num> flag controls how many segments `git am` or `git apply`\nremoves. The default is -p1. `git am -p0` should apply the patch\ncorrectly.\n"}]}