git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jerry Zhang <jerry@skydio.com>
To: git@vger.kernel.org, gitster@pobox.com
Cc: ross@skydio.com, abe@skydio.com, brian.kubisiak@skydio.com,
	Jerry Zhang <jerry@skydio.com>
Subject: [PATCH] git-apply: allow empty patch text
Date: Mon, 26 Apr 2021 18:12:46 -0700	[thread overview]
Message-ID: <20210427011246.28054-1-jerry@skydio.com> (raw)

"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


             reply	other threads:[~2021-04-27  1:13 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27  1:12 Jerry Zhang [this message]
2021-04-27  5:46 ` [PATCH] git-apply: allow empty patch text Eric Wong
2021-04-27  6:28   ` Junio C Hamano
2021-04-27 19:40 ` [PATCH V2] git-apply: add --allow-empty flag Jerry Zhang
2021-04-28  5:08   ` Junio C Hamano
2021-04-28 18:40     ` Jerry Zhang
2021-04-29  0:32   ` Ævar Arnfjörð Bjarmason
2021-12-11  3:13   ` [PATCH V3] " Jerry Zhang
2021-12-11  7:36     ` Bagas Sanjaya
2021-12-13 20:28     ` [PATCH V4] " Jerry Zhang
2021-12-13 21:44       ` Junio C Hamano
2021-12-13 21:52         ` Jerry Zhang
2021-12-13 21:57           ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210427011246.28054-1-jerry@skydio.com \
    --to=jerry@skydio.com \
    --cc=abe@skydio.com \
    --cc=brian.kubisiak@skydio.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=ross@skydio.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).