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

[PATCH] git-apply: allow empty patch text

From
Jerry Zhang <jerry@skydio.com>
Date
Apr 27, 2021, 01:12 UTC
Message-ID
<20210427011246.28054-1-jerry@skydio.com>

"git diff" produces no patch text if there is no diff, but "git apply" exits with code 128 if the patch text is empty.

Since every valid "git diff" should result in a successful patch application when applied to the same preimage as the diff, allow "git apply" to handle these empty diffs by doing nothing and returning 0.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
---
 apply.c                | 2 ++
 t/t4126-apply-empty.sh | 7 +++++++
 t/t7006-pager.sh       | 2 +-
 3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/apply.c b/apply.c
index 5ea237232d..2abf2e8a61 100644
--- a/apply.c
+++ b/apply.c
@@ -4685,6 +4685,8 @@ static int apply_patch(struct apply_state *state,
 	state->patch_input_file = filename;
 	if (read_patch_file(&buf, fd) < 0)
 		return -128;
+	if (!buf.len)
+		goto end;
 	offset = 0;
 	while (offset < buf.len) {
 		struct patch *patch;
diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh
index ceb6a79fe0..37351be609 100755
--- a/t/t4126-apply-empty.sh
+++ b/t/t4126-apply-empty.sh
@@ -31,6 +31,13 @@ test_expect_success 'apply empty' '
 	test_cmp expect empty
 '
 
+test_expect_success 'apply empty diff' '
+	git reset --hard &&
+	git diff >empty.patch &&
+	git apply empty.patch &&
+	git diff | git apply -
+'
+
 test_expect_success 'apply --index empty' '
 	git reset --hard &&
 	rm -f missing &&
diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh
index 0e7cf75435..2c0ada6bf6 100755
--- a/t/t7006-pager.sh
+++ b/t/t7006-pager.sh
@@ -567,7 +567,7 @@ test_default_pager        expect_success 'git -p shortlog'
 test_core_pager_subdir    expect_success 'git -p shortlog'
 
 test_core_pager_subdir    expect_success test_must_fail \
-					 'git -p apply </dev/null'
+					 'git -p log !'
 
 test_expect_success TTY 'command-specific pager' '
 	sane_unset PAGER GIT_PAGER &&
-- 
2.29.0
Next: Eric Wong
Message 1 of 13 in “git-apply: allow empty patch text”
  1. git-apply: allow empty patch textJerry Zhang, Apr 27, 2021
  2. Eric WongApr 27, 2021
  3. Junio C HamanoApr 27, 2021
  4. git-apply: add --allow-empty flagJerry Zhang, Apr 27, 2021
  5. Junio C HamanoApr 28, 2021
  6. Jerry ZhangApr 28, 2021
  7. Ævar Arnfjörð BjarmasonApr 29, 2021
  8. git-apply: add --allow-empty flagJerry Zhang, Dec 11, 2021
  9. Bagas SanjayaDec 11, 2021
  10. git-apply: add --allow-empty flagJerry Zhang, Dec 13, 2021
  11. Junio C HamanoDec 13, 2021
  12. Jerry ZhangDec 13, 2021
  13. Junio C HamanoDec 13, 2021

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.