{"thread":{"id":"64609","subject":"[PATCH] add-patch: stop getting confused by deletions of binary files","startedAt":"2025-12-10T23:50:17Z","lastAt":"2025-12-10T23:50:17Z","messageCount":1,"participants":["Ahelenia Ziemiańska"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"532007","messageId":"lfh7rez2sgajamk632sea2s6sm65vbllxuvi6shihdgmtwvdmh@tarta.nabijaczleweli.xyz","threadId":"64609","inReplyTo":null,"subject":"[PATCH] add-patch: stop getting confused by deletions of binary files","fromName":"Ahelenia Ziemiańska","fromEmail":"nabijaczleweli@nabijaczleweli.xyz","sentAt":"2025-12-10T23:50:07Z","receivedAt":"2025-12-10T23:50:17Z","isPatch":true,"sender":{"key":"nabijaczleweli@nabijaczleweli.xyz","avatar":"https://avatars.githubusercontent.com/u/6709544?v=4"},"body":"git add -p can't handle binary files and ignores them:\n  $ git init\n  $ printf 'a\\0' > a\n  $ printf 'b\\0' > b\n  $ git add .\n  $ git commit -m a\n  $ printf 'A\\0' > a\n  $ rm b\n  $ git add -p\n  diff --git a/b b/b\n  deleted file mode 100644\n  index 28eacf2..0000000\n  Binary files a/b and /dev/null differ\n  (1/1) Stage deletion [y,n,q,a,d,?]? y\n  error: cannot apply binary patch to 'b' without full index line\n  error: b: patch does not apply\n  error: 'git apply' failed\nbut didn't do this for deletions of binary files.\n\nThe parsed diff looks like\n  diff --git a/a b/a\n  index 90802fe..e2b43e3 100644\n  Binary files a/a and b/a differ\n  diff --git a/b b/b\n  deleted file mode 100644\n  index 28eacf2..0000000\n  Binary files a/b and /dev/null differ\nwhich ended up with\n  a: binary=1 deleted=0 hunk_nr=0\n  b: binary=0 deleted=1 hunk_nr=1\n\nSeeing \"deleted file\" inserts a hunk and prevented detecting\nthe \"Binary files\" header, so detect it even in this case:\n  b: binary=1 deleted=1 hunk_nr=1\n\nWe can't do anything useful to binary files anyway,\nso skip them regardless of how many hunks they seem to have.\n\nThis makes the final add -p in the scenario above exit with\n\"Only binary files changed.\".\n\nCloses: https://bugs.debian.org/1122502\nSigned-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>\n---\n add-patch.c | 10 ++++++----\n 1 file changed, 6 insertions(+), 4 deletions(-)\n\ndiff --git add-patch.c add-patch.c\nindex 173a53241e..2e1ea2cb33 100644\n--- add-patch.c\n+++ add-patch.c\n@@ -518,9 +518,11 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)\n \t\t} else if (p == plain->buf)\n \t\t\tBUG(\"diff starts with unexpected line:\\n\"\n \t\t\t    \"%.*s\\n\", (int)(eol - p), p);\n-\t\telse if (file_diff->deleted)\n-\t\t\t; /* keep the rest of the file in a single \"hunk\" */\n-\t\telse if (starts_with(p, \"@@ \") ||\n+\t\telse if (file_diff->deleted) {\n+\t\t\t/* keep the rest of the file in a single \"hunk\" */\n+\t\t\tif (starts_with(p, \"Binary files \"))\n+\t\t\t\tfile_diff->binary = 1;\n+\t\t} else if (starts_with(p, \"@@ \") ||\n \t\t\t (hunk == &file_diff->head &&\n \t\t\t  (skip_prefix(p, \"deleted file\", &deleted)))) {\n \t\t\tif (marker == '-' || marker == '+')\n@@ -1853,7 +1855,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,\n \t}\n \n \tfor (i = 0; i < s.file_diff_nr; i++)\n-\t\tif (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)\n+\t\tif (s.file_diff[i].binary)\n \t\t\tbinary_count++;\n \t\telse if (patch_update_file(&s, s.file_diff + i))\n \t\t\tbreak;\n-- \n2.39.5\n"}]}