git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] git-apply: allow empty patch text
@ 2021-04-27  1:12 Jerry Zhang
  2021-04-27  5:46 ` Eric Wong
  2021-04-27 19:40 ` [PATCH V2] git-apply: add --allow-empty flag Jerry Zhang
  0 siblings, 2 replies; 13+ messages in thread
From: Jerry Zhang @ 2021-04-27  1:12 UTC (permalink / raw)
  To: git, gitster; +Cc: ross, abe, brian.kubisiak, Jerry Zhang

"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


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] git-apply: allow empty patch text
  2021-04-27  1:12 [PATCH] git-apply: allow empty patch text Jerry Zhang
@ 2021-04-27  5:46 ` 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
  1 sibling, 1 reply; 13+ messages in thread
From: Eric Wong @ 2021-04-27  5:46 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git, gitster, ross, abe, brian.kubisiak

Jerry Zhang <jerry@skydio.com> wrote:
> "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,

Should it result in successful patch application? (Why?)

I fear this change can cause errors in pipelines to go
undetected (since "set -o pipefail" is not POSIX).
In my experience, zero-byte files is also a common failure mode
for some filesystems, even after fsck marked them as clean.

Perhaps guarding this behavior with --allow-empty (as commit and
cherry-pick do) is safer.

> 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 -
> +'

It shouldn't be necessary to use "git diff" to generate empty
output.  Tests are too slow for me on Linux, even.  Something
like:

	>empty.patch &&
	git apply empty.patch &&
	git apply - <empty.patch

...ought to be realistic enough, but /dev/null in place of
empty.patch is probably portable enough, too.  Thanks.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH] git-apply: allow empty patch text
  2021-04-27  5:46 ` Eric Wong
@ 2021-04-27  6:28   ` Junio C Hamano
  0 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2021-04-27  6:28 UTC (permalink / raw)
  To: Eric Wong; +Cc: Jerry Zhang, git, ross, abe, brian.kubisiak

Eric Wong <e@80x24.org> writes:

> I fear this change can cause errors in pipelines to go
> undetected (since "set -o pipefail" is not POSIX).
> In my experience, zero-byte files is also a common failure mode
> for some filesystems, even after fsck marked them as clean.

Thanks for saving me time to say it.

It would be a grave regression to any workflow automation to change
the behaviour to silently succeed a non-patch application as a
noop.  And it does not take filesystem corruption.

Some people seem to gpg sign their patches sent to the list, which
is not very useful at this point as I don't bother to collect their
public keys anyway, but feeding such a piece of e-mail from GNUS to
"git am -s" takes "\M-i r |" prefix instead of the regular "|" to
"pipe the message to an external command" to properly get it
processed.  I was saved by the "empty input is wrong" behaviour a
number of times.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH V2] git-apply: add --allow-empty flag
  2021-04-27  1:12 [PATCH] git-apply: allow empty patch text Jerry Zhang
  2021-04-27  5:46 ` Eric Wong
@ 2021-04-27 19:40 ` Jerry Zhang
  2021-04-28  5:08   ` Junio C Hamano
                     ` (2 more replies)
  1 sibling, 3 replies; 13+ messages in thread
From: Jerry Zhang @ 2021-04-27 19:40 UTC (permalink / raw)
  To: git, gitster; +Cc: ross, abe, brian.kubisiak, Jerry Zhang

Some users or scripts will pipe "git diff"
output to "git apply" when replaying diffs
or commits. In these cases, they will rely
on the return value of "git apply" to know
whether the diff was applied successfully.

However, for empty commits, "git apply" will
fail. This complicates scripts since they
have to either buffer the diff and check
its length, or run diff again with "exit-code",
essentially doing the diff twice.

Add the "--allow-empty" flag to "git apply"
which allows it to handle both empty diffs
and empty commits created by "git format-patch
--always" by doing nothing and returning 0.

Add tests for both with and without --allow-empty.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
---
This patch applies on top of "git-apply: add --quiet flag".
The conflict is in Documentation -> Synopsis and is
trivial to solve.

V1 -> V2:
- Moved behavior under a flag
- Added tests for both cases

 Documentation/git-apply.txt |  6 +++++-
 apply.c                     |  8 ++++++--
 apply.h                     |  1 +
 t/t4126-apply-empty.sh      | 16 ++++++++++++++++
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
index a32ad64718..b6d77f4206 100644
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -16,7 +16,7 @@ SYNOPSIS
 	  [--ignore-space-change | --ignore-whitespace]
 	  [--whitespace=(nowarn|warn|fix|error|error-all)]
 	  [--exclude=<path>] [--include=<path>] [--directory=<root>]
-	  [--verbose | --quiet] [--unsafe-paths] [<patch>...]
+	  [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
 
 DESCRIPTION
 -----------
@@ -256,6 +256,10 @@ When `git apply` is used as a "better GNU patch", the user can pass
 the `--unsafe-paths` option to override this safety check.  This option
 has no effect when `--index` or `--cached` is in use.
 
+--allow-empty::
+	Don't return error for patches containing no diff. This includes
+	empty patches and patches with commit text only.
+
 CONFIGURATION
 -------------
 
diff --git a/apply.c b/apply.c
index 918e0988bb..0ddde5e8a8 100644
--- a/apply.c
+++ b/apply.c
@@ -4732,8 +4732,10 @@ static int apply_patch(struct apply_state *state,
 	}
 
 	if (!list && !skipped_patch) {
-		error(_("unrecognized input"));
-		res = -128;
+		if (!state->allow_empty) {
+			error(_("unrecognized input"));
+			res = -128;
+		}
 		goto end;
 	}
 
@@ -5061,6 +5063,8 @@ int apply_parse_options(int argc, const char **argv,
 		OPT_CALLBACK(0, "directory", state, N_("root"),
 			N_("prepend <root> to all filenames"),
 			apply_option_parse_directory),
+		OPT_BOOL(0, "allow-empty", &state->allow_empty,
+			N_("don't return error for empty patches")),
 		OPT_END()
 	};
 
diff --git a/apply.h b/apply.h
index da3d95fa50..16202da160 100644
--- a/apply.h
+++ b/apply.h
@@ -66,6 +66,7 @@ struct apply_state {
 	int threeway;
 	int unidiff_zero;
 	int unsafe_paths;
+	int allow_empty;
 
 	/* Other non boolean parameters */
 	struct repository *repo;
diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh
index ceb6a79fe0..f89c53c5f6 100755
--- a/t/t4126-apply-empty.sh
+++ b/t/t4126-apply-empty.sh
@@ -31,6 +31,22 @@ test_expect_success 'apply empty' '
 	test_cmp expect empty
 '
 
+test_expect_success 'apply empty patch fails' '
+	git reset --hard &&
+	git commit --allow-empty -m "empty commit" &&
+	git format-patch --always HEAD~ >empty.patch &&
+	test_must_fail git apply empty.patch &&
+	test_must_fail git apply - </dev/null
+'
+
+test_expect_success 'apply with --allow-empty succeeds' '
+	git reset --hard &&
+	git commit --allow-empty -m "empty commit" &&
+	git format-patch --always HEAD~ >empty.patch &&
+	git apply --allow-empty empty.patch &&
+	git apply --allow-empty - </dev/null
+'
+
 test_expect_success 'apply --index empty' '
 	git reset --hard &&
 	rm -f missing &&
-- 
2.29.0


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] git-apply: add --allow-empty flag
  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
  2 siblings, 1 reply; 13+ messages in thread
From: Junio C Hamano @ 2021-04-28  5:08 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git, ross, abe, brian.kubisiak

Jerry Zhang <jerry@skydio.com> writes:

> Some users or scripts will pipe "git diff"
> output to "git apply" when replaying diffs
> or commits. In these cases, they will rely
> on the return value of "git apply" to know
> whether the diff was applied successfully.
>
> However, for empty commits, "git apply" will
> fail. This complicates scripts since they
> have to either buffer the diff and check
> its length, or run diff again with "exit-code",
> essentially doing the diff twice.
>
> Add the "--allow-empty" flag to "git apply"
> which allows it to handle both empty diffs
> and empty commits created by "git format-patch
> --always" by doing nothing and returning 0.
>
> Add tests for both with and without --allow-empty.
>
> Signed-off-by: Jerry Zhang <jerry@skydio.com>
> ---
> This patch applies on top of "git-apply: add --quiet flag".
> The conflict is in Documentation -> Synopsis and is
> trivial to solve.

> V1 -> V2:
> - Moved behavior under a flag
> - Added tests for both cases

When people add a flag (a boolean option), it becomes tempting to
add a corresponding configuration variable.

I wonder it this step should start calling

	git apply --no-allow-empty

when "git am" drives it, so that a futre end-user configuration
variable would not break it?



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] git-apply: add --allow-empty flag
  2021-04-28  5:08   ` Junio C Hamano
@ 2021-04-28 18:40     ` Jerry Zhang
  0 siblings, 0 replies; 13+ messages in thread
From: Jerry Zhang @ 2021-04-28 18:40 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Git Mailing List, Ross Yeager, Abraham Bachrach, Brian Kubisiak

On Tue, Apr 27, 2021 at 10:08 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Jerry Zhang <jerry@skydio.com> writes:
>
> > Some users or scripts will pipe "git diff"
> > output to "git apply" when replaying diffs
> > or commits. In these cases, they will rely
> > on the return value of "git apply" to know
> > whether the diff was applied successfully.
> >
> > However, for empty commits, "git apply" will
> > fail. This complicates scripts since they
> > have to either buffer the diff and check
> > its length, or run diff again with "exit-code",
> > essentially doing the diff twice.
> >
> > Add the "--allow-empty" flag to "git apply"
> > which allows it to handle both empty diffs
> > and empty commits created by "git format-patch
> > --always" by doing nothing and returning 0.
> >
> > Add tests for both with and without --allow-empty.
> >
> > Signed-off-by: Jerry Zhang <jerry@skydio.com>
> > ---
> > This patch applies on top of "git-apply: add --quiet flag".
> > The conflict is in Documentation -> Synopsis and is
> > trivial to solve.
>
> > V1 -> V2:
> > - Moved behavior under a flag
> > - Added tests for both cases
>
> When people add a flag (a boolean option), it becomes tempting to
> add a corresponding configuration variable.
>
> I wonder it this step should start calling
>
>         git apply --no-allow-empty
>
> when "git am" drives it, so that a futre end-user configuration
> variable would not break it?
>
That's currently not necessary, since "git am" can catch empty patches
before it ever calls into apply.c:

     if (is_empty_or_missing_file(am_path(state, "patch"))) {
         printf_ln(_("Patch is empty."));
         die_user_resolve(state);
     }

If someone does make a config variable I think it might be convenient to
teach "git am" to use "allow-empty" as well, since applying empty commits
can be useful for documenting the history. The config variable could then
control the behavior of both "am" and "apply" since they are used for very
similar things. Of course that's up to the decision of that hypothetical person
since I don't currently have a need to change "am".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V2] git-apply: add --allow-empty flag
  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-29  0:32   ` Ævar Arnfjörð Bjarmason
  2021-12-11  3:13   ` [PATCH V3] " Jerry Zhang
  2 siblings, 0 replies; 13+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2021-04-29  0:32 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git, gitster, ross, abe, brian.kubisiak


On Tue, Apr 27 2021, Jerry Zhang wrote:

> Some users or scripts will pipe "git diff"
> output to "git apply" when replaying diffs
> or commits. In these cases, they will rely
> on the return value of "git apply" to know
> whether the diff was applied successfully.
>
> However, for empty commits, "git apply" will
> fail. This complicates scripts since they
> have to either buffer the diff and check
> its length, or run diff again with "exit-code",
> essentially doing the diff twice.
>
> Add the "--allow-empty" flag to "git apply"
> which allows it to handle both empty diffs
> and empty commits created by "git format-patch
> --always" by doing nothing and returning 0.
>
> Add tests for both with and without --allow-empty.
>
> Signed-off-by: Jerry Zhang <jerry@skydio.com>
> ---
> This patch applies on top of "git-apply: add --quiet flag".
> The conflict is in Documentation -> Synopsis and is
> trivial to solve.
>
> V1 -> V2:
> - Moved behavior under a flag
> - Added tests for both cases
>
>  Documentation/git-apply.txt |  6 +++++-
>  apply.c                     |  8 ++++++--
>  apply.h                     |  1 +
>  t/t4126-apply-empty.sh      | 16 ++++++++++++++++
>  4 files changed, 28 insertions(+), 3 deletions(-)
>
> diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
> index a32ad64718..b6d77f4206 100644
> --- a/Documentation/git-apply.txt
> +++ b/Documentation/git-apply.txt
> @@ -16,7 +16,7 @@ SYNOPSIS
>  	  [--ignore-space-change | --ignore-whitespace]
>  	  [--whitespace=(nowarn|warn|fix|error|error-all)]
>  	  [--exclude=<path>] [--include=<path>] [--directory=<root>]
> -	  [--verbose | --quiet] [--unsafe-paths] [<patch>...]
> +	  [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
>  
>  DESCRIPTION
>  -----------
> @@ -256,6 +256,10 @@ When `git apply` is used as a "better GNU patch", the user can pass
>  the `--unsafe-paths` option to override this safety check.  This option
>  has no effect when `--index` or `--cached` is in use.
>  
> +--allow-empty::
> +	Don't return error for patches containing no diff. This includes
> +	empty patches and patches with commit text only.
> +
>  CONFIGURATION
>  -------------
>  
> diff --git a/apply.c b/apply.c
> index 918e0988bb..0ddde5e8a8 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -4732,8 +4732,10 @@ static int apply_patch(struct apply_state *state,
>  	}
>  
>  	if (!list && !skipped_patch) {
> -		error(_("unrecognized input"));
> -		res = -128;
> +		if (!state->allow_empty) {
> +			error(_("unrecognized input"));
> +			res = -128;
> +		}
>  		goto end;
>  	}
>  
> @@ -5061,6 +5063,8 @@ int apply_parse_options(int argc, const char **argv,
>  		OPT_CALLBACK(0, "directory", state, N_("root"),
>  			N_("prepend <root> to all filenames"),
>  			apply_option_parse_directory),
> +		OPT_BOOL(0, "allow-empty", &state->allow_empty,
> +			N_("don't return error for empty patches")),
>  		OPT_END()
>  	};
>  
> diff --git a/apply.h b/apply.h
> index da3d95fa50..16202da160 100644
> --- a/apply.h
> +++ b/apply.h
> @@ -66,6 +66,7 @@ struct apply_state {
>  	int threeway;
>  	int unidiff_zero;
>  	int unsafe_paths;
> +	int allow_empty;
>  
>  	/* Other non boolean parameters */
>  	struct repository *repo;
> diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh
> index ceb6a79fe0..f89c53c5f6 100755
> --- a/t/t4126-apply-empty.sh
> +++ b/t/t4126-apply-empty.sh
> @@ -31,6 +31,22 @@ test_expect_success 'apply empty' '
>  	test_cmp expect empty
>  '
>  
> +test_expect_success 'apply empty patch fails' '
> +	git reset --hard &&
> +	git commit --allow-empty -m "empty commit" &&
> +	git format-patch --always HEAD~ >empty.patch &&
> +	test_must_fail git apply empty.patch &&
> +	test_must_fail git apply - </dev/null
> +'
> +
> +test_expect_success 'apply with --allow-empty succeeds' '
> +	git reset --hard &&
> +	git commit --allow-empty -m "empty commit" &&

Rather than making two --allow-empty commits, shouldn't this be added to
the earlier "setup" routine, so both of these tests can use the same
empty commit?

The setup routine sets up a patch0 and patch1, just add an empty-patch ?

And for the "git reset --hard" I see you're copy/pasting the existing
convention in that file, but FWIW it's better to use 'test_when_finished
"git reset --hard"' in the appropriate spot in the test (i.e. just
before we'd need the reset if we fail), so a test cleans up after
itself. As opposed to "I'm cleaning in case an earlier test didn't clean
up".

> +	git format-patch --always HEAD~ >empty.patch &&
> +	git apply --allow-empty empty.patch &&
> +	git apply --allow-empty - </dev/null
> +'
> +
>  test_expect_success 'apply --index empty' '
>  	git reset --hard &&
>  	rm -f missing &&


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH V3] git-apply: add --allow-empty flag
  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-29  0:32   ` Ævar Arnfjörð Bjarmason
@ 2021-12-11  3:13   ` Jerry Zhang
  2021-12-11  7:36     ` Bagas Sanjaya
  2021-12-13 20:28     ` [PATCH V4] " Jerry Zhang
  2 siblings, 2 replies; 13+ messages in thread
From: Jerry Zhang @ 2021-12-11  3:13 UTC (permalink / raw)
  To: git, gitster; +Cc: Jerry Zhang

Some users or scripts will pipe "git diff"
output to "git apply" when replaying diffs
or commits. In these cases, they will rely
on the return value of "git apply" to know
whether the diff was applied successfully.

However, for empty commits, "git apply" will
fail. This complicates scripts since they
have to either buffer the diff and check
its length, or run diff again with "exit-code",
essentially doing the diff twice.

Add the "--allow-empty" flag to "git apply"
which allows it to handle both empty diffs
and empty commits created by "git format-patch
--always" by doing nothing and returning 0.

Add tests for both with and without --allow-empty.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
---
V2->V3:
- moved lines into setup routine
- switched all tests to using "test_when_finished" rather than resetting at the end

 Documentation/git-apply.txt |  6 +++++-
 apply.c                     |  8 ++++++--
 apply.h                     |  1 +
 t/t4126-apply-empty.sh      | 22 ++++++++++++++++++----
 4 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
index a32ad64718..b6d77f4206 100644
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -14,11 +14,11 @@ SYNOPSIS
 	  [--allow-binary-replacement | --binary] [--reject] [-z]
 	  [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
 	  [--ignore-space-change | --ignore-whitespace]
 	  [--whitespace=(nowarn|warn|fix|error|error-all)]
 	  [--exclude=<path>] [--include=<path>] [--directory=<root>]
-	  [--verbose | --quiet] [--unsafe-paths] [<patch>...]
+	  [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
 
 DESCRIPTION
 -----------
 Reads the supplied diff output (i.e. "a patch") and applies it to files.
 When running from a subdirectory in a repository, patched paths
@@ -254,10 +254,14 @@ running `git apply --directory=modules/git-gui`.
 +
 When `git apply` is used as a "better GNU patch", the user can pass
 the `--unsafe-paths` option to override this safety check.  This option
 has no effect when `--index` or `--cached` is in use.
 
+--allow-empty::
+	Don't return error for patches containing no diff. This includes
+	empty patches and patches with commit text only.
+
 CONFIGURATION
 -------------
 
 apply.ignoreWhitespace::
 	Set to 'change' if you want changes in whitespace to be ignored by default.
diff --git a/apply.c b/apply.c
index 9f00f882a2..f6029fd935 100644
--- a/apply.c
+++ b/apply.c
@@ -4752,12 +4752,14 @@ static int apply_patch(struct apply_state *state,
 		}
 		offset += nr;
 	}
 
 	if (!list && !skipped_patch) {
-		error(_("unrecognized input"));
-		res = -128;
+		if (!state->allow_empty) {
+			error(_("unrecognized input"));
+			res = -128;
+		}
 		goto end;
 	}
 
 	if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
 		state->apply = 0;
@@ -5081,10 +5083,12 @@ int apply_parse_options(int argc, const char **argv,
 			N_("do not trust the line counts in the hunk headers"),
 			APPLY_OPT_RECOUNT),
 		OPT_CALLBACK(0, "directory", state, N_("root"),
 			N_("prepend <root> to all filenames"),
 			apply_option_parse_directory),
+		OPT_BOOL(0, "allow-empty", &state->allow_empty,
+			N_("don't return error for empty patches")),
 		OPT_END()
 	};
 
 	return parse_options(argc, argv, state->prefix, builtin_apply_options, apply_usage, 0);
 }
diff --git a/apply.h b/apply.h
index da3d95fa50..16202da160 100644
--- a/apply.h
+++ b/apply.h
@@ -64,10 +64,11 @@ struct apply_state {
 	int apply_with_reject;
 	int no_add;
 	int threeway;
 	int unidiff_zero;
 	int unsafe_paths;
+	int allow_empty;
 
 	/* Other non boolean parameters */
 	struct repository *repo;
 	const char *index_file;
 	enum apply_verbosity apply_verbosity;
diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh
index ceb6a79fe0..949e284d14 100755
--- a/t/t4126-apply-empty.sh
+++ b/t/t4126-apply-empty.sh
@@ -7,10 +7,12 @@ test_description='apply empty'
 test_expect_success setup '
 	>empty &&
 	git add empty &&
 	test_tick &&
 	git commit -m initial &&
+	git commit --allow-empty -m "empty commit" &&
+	git format-patch --always HEAD~ >empty.patch &&
 	for i in a b c d e
 	do
 		echo $i
 	done >empty &&
 	cat empty >expect &&
@@ -23,34 +25,46 @@ test_expect_success setup '
 	>empty &&
 	git update-index --refresh
 '
 
 test_expect_success 'apply empty' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply patch0 &&
 	test_cmp expect empty
 '
 
+test_expect_success 'apply empty patch fails' '
+	test_when_finished "git reset --hard" &&
+	test_must_fail git apply empty.patch &&
+	test_must_fail git apply - </dev/null
+'
+
+test_expect_success 'apply with --allow-empty succeeds' '
+	test_when_finished "git reset --hard" &&
+	git apply --allow-empty empty.patch &&
+	git apply --allow-empty - </dev/null
+'
+
 test_expect_success 'apply --index empty' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply --index patch0 &&
 	test_cmp expect empty &&
 	git diff --exit-code
 '
 
 test_expect_success 'apply create' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply patch1 &&
 	test_cmp expect missing
 '
 
 test_expect_success 'apply --index create' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply --index patch1 &&
 	test_cmp expect missing &&
 	git diff --exit-code
 '
 
-- 
2.32.0.1314.g6ed4fcc4cc


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH V3] git-apply: add --allow-empty flag
  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
  1 sibling, 0 replies; 13+ messages in thread
From: Bagas Sanjaya @ 2021-12-11  7:36 UTC (permalink / raw)
  To: Jerry Zhang, git, gitster

On 11/12/21 10.13, Jerry Zhang wrote:
> +--allow-empty::
> +	Don't return error for patches containing no diff. This includes
> +	empty patches and patches with commit text only.
> +

Looks OK.

>   	if (!list && !skipped_patch) {
> -		error(_("unrecognized input"));
> -		res = -128;
> +		if (!state->allow_empty) {
> +			error(_("unrecognized input"));
> +			res = -128;
> +		}
>   		goto end;
>   	}

When we apply empty patch but we forget --allow-empty, we got generic
"unrecognized input". So for this case, we should disambiguate the
former case and case when malformed patch input is provided.

-- 
An old man doll... just what I always wanted! - Clara

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH V4] git-apply: add --allow-empty flag
  2021-12-11  3:13   ` [PATCH V3] " Jerry Zhang
  2021-12-11  7:36     ` Bagas Sanjaya
@ 2021-12-13 20:28     ` Jerry Zhang
  2021-12-13 21:44       ` Junio C Hamano
  1 sibling, 1 reply; 13+ messages in thread
From: Jerry Zhang @ 2021-12-13 20:28 UTC (permalink / raw)
  To: git, gitster; +Cc: Jerry Zhang

Some users or scripts will pipe "git diff"
output to "git apply" when replaying diffs
or commits. In these cases, they will rely
on the return value of "git apply" to know
whether the diff was applied successfully.

However, for empty commits, "git apply" will
fail. This complicates scripts since they
have to either buffer the diff and check
its length, or run diff again with "exit-code",
essentially doing the diff twice.

Add the "--allow-empty" flag to "git apply"
which allows it to handle both empty diffs
and empty commits created by "git format-patch
--always" by doing nothing and returning 0.

Add tests for both with and without --allow-empty.

Signed-off-by: Jerry Zhang <jerry@skydio.com>
---
V3->V4:
- Add detail to error text, and suggest --allow-empty
in case user runs into the empty patch error.

 Documentation/git-apply.txt |  6 +++++-
 apply.c                     |  8 ++++++--
 apply.h                     |  1 +
 t/t4126-apply-empty.sh      | 22 ++++++++++++++++++----
 4 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
index a32ad64718..b6d77f4206 100644
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -14,11 +14,11 @@ SYNOPSIS
 	  [--allow-binary-replacement | --binary] [--reject] [-z]
 	  [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
 	  [--ignore-space-change | --ignore-whitespace]
 	  [--whitespace=(nowarn|warn|fix|error|error-all)]
 	  [--exclude=<path>] [--include=<path>] [--directory=<root>]
-	  [--verbose | --quiet] [--unsafe-paths] [<patch>...]
+	  [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
 
 DESCRIPTION
 -----------
 Reads the supplied diff output (i.e. "a patch") and applies it to files.
 When running from a subdirectory in a repository, patched paths
@@ -254,10 +254,14 @@ running `git apply --directory=modules/git-gui`.
 +
 When `git apply` is used as a "better GNU patch", the user can pass
 the `--unsafe-paths` option to override this safety check.  This option
 has no effect when `--index` or `--cached` is in use.
 
+--allow-empty::
+	Don't return error for patches containing no diff. This includes
+	empty patches and patches with commit text only.
+
 CONFIGURATION
 -------------
 
 apply.ignoreWhitespace::
 	Set to 'change' if you want changes in whitespace to be ignored by default.
diff --git a/apply.c b/apply.c
index 9f00f882a2..afc1c6510e 100644
--- a/apply.c
+++ b/apply.c
@@ -4752,12 +4752,14 @@ static int apply_patch(struct apply_state *state,
 		}
 		offset += nr;
 	}
 
 	if (!list && !skipped_patch) {
-		error(_("unrecognized input"));
-		res = -128;
+		if (!state->allow_empty) {
+			error(_("No valid patches in input (allow with \"--allow-empty\")"));
+			res = -128;
+		}
 		goto end;
 	}
 
 	if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
 		state->apply = 0;
@@ -5081,10 +5083,12 @@ int apply_parse_options(int argc, const char **argv,
 			N_("do not trust the line counts in the hunk headers"),
 			APPLY_OPT_RECOUNT),
 		OPT_CALLBACK(0, "directory", state, N_("root"),
 			N_("prepend <root> to all filenames"),
 			apply_option_parse_directory),
+		OPT_BOOL(0, "allow-empty", &state->allow_empty,
+			N_("don't return error for empty patches")),
 		OPT_END()
 	};
 
 	return parse_options(argc, argv, state->prefix, builtin_apply_options, apply_usage, 0);
 }
diff --git a/apply.h b/apply.h
index da3d95fa50..16202da160 100644
--- a/apply.h
+++ b/apply.h
@@ -64,10 +64,11 @@ struct apply_state {
 	int apply_with_reject;
 	int no_add;
 	int threeway;
 	int unidiff_zero;
 	int unsafe_paths;
+	int allow_empty;
 
 	/* Other non boolean parameters */
 	struct repository *repo;
 	const char *index_file;
 	enum apply_verbosity apply_verbosity;
diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh
index ceb6a79fe0..949e284d14 100755
--- a/t/t4126-apply-empty.sh
+++ b/t/t4126-apply-empty.sh
@@ -7,10 +7,12 @@ test_description='apply empty'
 test_expect_success setup '
 	>empty &&
 	git add empty &&
 	test_tick &&
 	git commit -m initial &&
+	git commit --allow-empty -m "empty commit" &&
+	git format-patch --always HEAD~ >empty.patch &&
 	for i in a b c d e
 	do
 		echo $i
 	done >empty &&
 	cat empty >expect &&
@@ -23,34 +25,46 @@ test_expect_success setup '
 	>empty &&
 	git update-index --refresh
 '
 
 test_expect_success 'apply empty' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply patch0 &&
 	test_cmp expect empty
 '
 
+test_expect_success 'apply empty patch fails' '
+	test_when_finished "git reset --hard" &&
+	test_must_fail git apply empty.patch &&
+	test_must_fail git apply - </dev/null
+'
+
+test_expect_success 'apply with --allow-empty succeeds' '
+	test_when_finished "git reset --hard" &&
+	git apply --allow-empty empty.patch &&
+	git apply --allow-empty - </dev/null
+'
+
 test_expect_success 'apply --index empty' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply --index patch0 &&
 	test_cmp expect empty &&
 	git diff --exit-code
 '
 
 test_expect_success 'apply create' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply patch1 &&
 	test_cmp expect missing
 '
 
 test_expect_success 'apply --index create' '
-	git reset --hard &&
 	rm -f missing &&
+	test_when_finished "git reset --hard" &&
 	git apply --index patch1 &&
 	test_cmp expect missing &&
 	git diff --exit-code
 '
 
-- 
2.32.0.1314.g6ed4fcc4cc


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH V4] git-apply: add --allow-empty flag
  2021-12-13 20:28     ` [PATCH V4] " Jerry Zhang
@ 2021-12-13 21:44       ` Junio C Hamano
  2021-12-13 21:52         ` Jerry Zhang
  0 siblings, 1 reply; 13+ messages in thread
From: Junio C Hamano @ 2021-12-13 21:44 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git

Jerry Zhang <jerry@skydio.com> writes:

> diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
> index a32ad64718..b6d77f4206 100644
> --- a/Documentation/git-apply.txt
> +++ b/Documentation/git-apply.txt
> @@ -14,11 +14,11 @@ SYNOPSIS
>  	  [--allow-binary-replacement | --binary] [--reject] [-z]
>  	  [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
>  	  [--ignore-space-change | --ignore-whitespace]
>  	  [--whitespace=(nowarn|warn|fix|error|error-all)]
>  	  [--exclude=<path>] [--include=<path>] [--directory=<root>]
> -	  [--verbose | --quiet] [--unsafe-paths] [<patch>...]
> +	  [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
>  
>  DESCRIPTION
>  -----------
>  Reads the supplied diff output (i.e. "a patch") and applies it to files.
>  When running from a subdirectory in a repository, patched paths

Applying: git-apply: add --allow-empty flag
error: patch failed: Documentation/git-apply.txt:14
error: Documentation/git-apply.txt: patch does not apply
Patch failed at 0001 git-apply: add --allow-empty flag

Hmph....  Where did that "| --quiet" thing come from?



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V4] git-apply: add --allow-empty flag
  2021-12-13 21:44       ` Junio C Hamano
@ 2021-12-13 21:52         ` Jerry Zhang
  2021-12-13 21:57           ` Junio C Hamano
  0 siblings, 1 reply; 13+ messages in thread
From: Jerry Zhang @ 2021-12-13 21:52 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Mon, Dec 13, 2021 at 1:44 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Jerry Zhang <jerry@skydio.com> writes:
>
> > diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
> > index a32ad64718..b6d77f4206 100644
> > --- a/Documentation/git-apply.txt
> > +++ b/Documentation/git-apply.txt
> > @@ -14,11 +14,11 @@ SYNOPSIS
> >         [--allow-binary-replacement | --binary] [--reject] [-z]
> >         [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
> >         [--ignore-space-change | --ignore-whitespace]
> >         [--whitespace=(nowarn|warn|fix|error|error-all)]
> >         [--exclude=<path>] [--include=<path>] [--directory=<root>]
> > -       [--verbose | --quiet] [--unsafe-paths] [<patch>...]
> > +       [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
> >
> >  DESCRIPTION
> >  -----------
> >  Reads the supplied diff output (i.e. "a patch") and applies it to files.
> >  When running from a subdirectory in a repository, patched paths
>
> Applying: git-apply: add --allow-empty flag
> error: patch failed: Documentation/git-apply.txt:14
> error: Documentation/git-apply.txt: patch does not apply
> Patch failed at 0001 git-apply: add --allow-empty flag
>
> Hmph....  Where did that "| --quiet" thing come from?
>
>
Apologies, I overlooked a dependence on my other patch here:
https://patchwork.kernel.org/project/git/patch/20210427194106.14500-1-jerry@skydio.com/

Let me know if you'd like a clean patch off of master. Please give the
other patch
a look though, as I've addressed outstanding comments.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH V4] git-apply: add --allow-empty flag
  2021-12-13 21:52         ` Jerry Zhang
@ 2021-12-13 21:57           ` Junio C Hamano
  0 siblings, 0 replies; 13+ messages in thread
From: Junio C Hamano @ 2021-12-13 21:57 UTC (permalink / raw)
  To: Jerry Zhang; +Cc: git

Jerry Zhang <jerry@skydio.com> writes:

> On Mon, Dec 13, 2021 at 1:44 PM Junio C Hamano <gitster@pobox.com> wrote:
>>
>> Jerry Zhang <jerry@skydio.com> writes:
>>
>> > diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
>> > index a32ad64718..b6d77f4206 100644
>> > --- a/Documentation/git-apply.txt
>> > +++ b/Documentation/git-apply.txt
>> > @@ -14,11 +14,11 @@ SYNOPSIS
>> >         [--allow-binary-replacement | --binary] [--reject] [-z]
>> >         [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
>> >         [--ignore-space-change | --ignore-whitespace]
>> >         [--whitespace=(nowarn|warn|fix|error|error-all)]
>> >         [--exclude=<path>] [--include=<path>] [--directory=<root>]
>> > -       [--verbose | --quiet] [--unsafe-paths] [<patch>...]
>> > +       [--verbose | --quiet] [--unsafe-paths] [--allow-empty] [<patch>...]
>> >
>> >  DESCRIPTION
>> >  -----------
>> >  Reads the supplied diff output (i.e. "a patch") and applies it to files.
>> >  When running from a subdirectory in a repository, patched paths
>>
>> Applying: git-apply: add --allow-empty flag
>> error: patch failed: Documentation/git-apply.txt:14
>> error: Documentation/git-apply.txt: patch does not apply
>> Patch failed at 0001 git-apply: add --allow-empty flag
>>
>> Hmph....  Where did that "| --quiet" thing come from?
>>
>>
> Apologies, I overlooked a dependence on my other patch here:
> https://patchwork.kernel.org/project/git/patch/20210427194106.14500-1-jerry@skydio.com/
>
> Let me know if you'd like a clean patch off of master. Please give the
> other patch
> a look though, as I've addressed outstanding comments.

I am OK with either a single patch without the other one at all, or
N-patch series if they all have to go in together.

Thanks.


^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2021-12-13 21:57 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-27  1:12 [PATCH] git-apply: allow empty patch text Jerry Zhang
2021-04-27  5:46 ` 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

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).