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

Re: [GSoC PATCH v2] apply: report the location of corrupt patches

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 16, 2026, 18:19 UTC
Message-ID
<xmqqbjgnprhc.fsf@gitster.g>
In-Reply-To
<20260316113459.77794-1-jerrywang183@yahoo.com>
Jialong Wang <jerrywang183@yahoo.com> writes:
Show 41 quoted lines
> When parsing a corrupt patch, git apply reports only the line number.
> That does not tell the user which input the line number refers to.
>
> Include the patch input path in the error message, and reset the line
> number for each patch input so the reported location remains useful
> when multiple patch files are provided.
>
> Add tests for file input, standard input, and multiple patch inputs.
>
> Signed-off-by: Jialong Wang <jerrywang183@yahoo.com>
> ---
> Changes since v1:
> - reset the line number for each patch input
> - add a test for multiple patch inputs where one input is corrupted
>
>  apply.c               |  4 +++-
>  t/t4100-apply-stat.sh | 38 +++++++++++++++++++++++++++++++++++++-
>  2 files changed, 40 insertions(+), 2 deletions(-)
>
> diff --git a/apply.c b/apply.c
> index b6dd1066a0..b7b0a201b3 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1875,7 +1875,8 @@ static int parse_single_patch(struct apply_state *state,
>  		len = parse_fragment(state, line, size, patch, fragment);
>  		if (len <= 0) {
>  			free(fragment);
> -			return error(_("corrupt patch at line %d"), state->linenr);
> +			return error(_("corrupt patch at %s:%d"),
> +				     state->patch_input_file, state->linenr);
>  		}
>  		fragment->patch = line;
>  		fragment->size = len;
> @@ -4825,6 +4826,7 @@ static int apply_patch(struct apply_state *state,
>  	int flush_attributes = 0;
>  
>  	state->patch_input_file = filename;
> +	state->linenr = 1;
>  	if (read_patch_file(&buf, fd) < 0)
>  		return -128;
>  	offset = 0;

This change is expecially interesting; it shows that practically nobody feeds more than one patch to a single invocation of the command ("git am" certainly does not) that this has gone undetected ever since it was written by Linus 46979f56 (git-apply: improve error detection and messages, 2005-05-23) ;-)

And all the changes contained in this patch look correctly done.

Having said that, there are places in apply.c that still report errors only the line number, which we may want to address with a follow-up patch, or in an updated version of this patch. find_header() is one, parse_git_diff_header() is another. There might be more.

Thanks, will queue.
Previous: Jialong WangNext: Jialong Wang
Message 5 of 14 in “apply: report the location of corrupt patches”
  1. apply: report the location of corrupt patchesJialong Wang, Mar 15, 2026
  2. Karthik NayakMar 16, 2026
  3. Jialong WangMar 16, 2026
  4. apply: report the location of corrupt patchesJialong Wang, Mar 16, 2026
  5. Junio C HamanoMar 16, 2026
  6. apply: report the location of corrupt patchesJialong Wang, Mar 16, 2026
  7. 0/3 apply: report input file for more parse errorsJialong Wang, Mar 17, 2026
  8. 2/3 apply: report input location in header parsing errorsJialong Wang, Mar 17, 2026
  9. 3/3 apply: report input location in binary and garbage patch errorsJialong Wang, Mar 17, 2026
  10. 1/3 apply: report the location of corrupt patchesJialong Wang, Mar 17, 2026
  11. Jialong WangMar 16, 2026
  12. Junio C HamanoMar 16, 2026
  13. apply: report input location in header parsing errorsJialong Wang, Mar 16, 2026
  14. apply: report input location in header parsing errorsJialong Wang, Mar 16, 2026

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.