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

Re: [PATCH v3 1/2] Allow git-apply to ignore the hunk headers (AKA recountdiff)

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 6, 2008, 04:55 UTC
Message-ID
<7vve0nw4b7.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<alpine.DEB.1.00.0806060006370.21190@racer>
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> +static int recount_diff(char *line, int size, struct fragment *fragment)
> +{
> +	int line_nr = 0;

At this point, line points at the beginning of the line that immediately follows "@@ -oldpos,oldlines +newpos,newlines @@ ...\n", right?

> +	if (size < 1)
> +		return -1;
> +
> +	fragment->oldpos = 2;
Why do you discard oldpos information, and use magic number "2"?
Show 6 quoted lines
> +	fragment->oldlines = fragment->newlines = 0;
> +
> +	for (;;) {
> +		int len = linelen(line, size);
> +		size -= len;
> +		line += len;

And you look at the line of the patch, measure how long it is, and you already advance line to point at the next line without ever looking at the contents of the line (you could look at line[-len], but that is crazy).

> +		if (size < 1)
> +			return 0;

Why? It may be the last line in the hunk but you haven't done anything to the current line yet.

Show 14 quoted lines
> +		switch (*line) {
> +		case ' ': case '\n':
> +			fragment->newlines++;
> +			/* fall through */
> +		case '-':
> +			fragment->oldlines++;
> +			break;
> +		case '+':
> +			fragment->newlines++;
> +			if (line_nr == 0) {
> +				fragment->leading = 1;
> +				fragment->oldpos = 1;
> +			}
> +			fragment->trailing = 1;

Again, why muck with oldpos? Also leading and trailing? After you recount the newlines and oldlines you ignored from the hunk header, the caller will behave as if the original patch had the right numbers on the hunk header to compute leading and trailing, doesn't it?

Show 7 quoted lines
> +			break;
> +		case '@':
> +			return size < 3 || prefixcmp(line, "@@ ");
> +		case 'd':
> +			return size < 5 || prefixcmp(line, "diff ");
> +		default:
> +			return -1;

I do not understand these return values. Your caller, parse_fragment() with your patch, gives you chance to recount the old and new line count, and you are responsible for only recounting them. The change you made to parse_fragment() returns error, aborting the whole git-apply, when recount_diff() returns non-zero, but having extra lines after the patch text is perfectly fine and there is no reason to force aborting from here. IOW, this function should not even have power to do so. The one and only thing this function should do well is to reliably count the number of patch lines.

Show 7 quoted lines
> @@ -1013,6 +1058,9 @@ static int parse_fragment(char *line, unsigned long size,
>  	offset = parse_fragment_header(line, len, fragment);
>  	if (offset < 0)
>  		return -1;
> +	if (offset > 0 && patch->recount &&
> +			recount_diff(line + offset, size - offset, fragment))
> +		return -1;
... and this "return -1" is uncalled for.
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 16 of 39 in “Allow git-apply to fix up the line counts”
  1. 1/2 Allow git-apply to fix up the line countsJohannes Schindelin, Jun 5, 2008
  2. 2/2 git-add: introduce --edit (to edit the diff vs. the index)Johannes Schindelin, Jun 5, 2008
  3. Johannes SixtJun 5, 2008
  4. Johannes SchindelinJun 5, 2008
  5. Johannes SixtJun 5, 2008
  6. Johannes SchindelinJun 5, 2008
  7. Johannes SixtJun 5, 2008
  8. Johannes SchindelinJun 5, 2008
  9. Johannes SixtJun 5, 2008
  10. 0/2 git add --editJohannes Schindelin, Jun 5, 2008
  11. 1/2 Allow git-apply to ignore the hunk headersJohannes Schindelin, Jun 5, 2008
  12. Junio C HamanoJun 5, 2008
  13. Johannes SchindelinJun 5, 2008
  14. 0/2 git add --editJohannes Schindelin, Jun 5, 2008
  15. 1/2 Allow git-apply to ignore the hunk headers (AKA recountdiff)Johannes Schindelin, Jun 5, 2008
  16. Junio C HamanoJun 6, 2008
  17. Johannes SchindelinJun 6, 2008
  18. Junio C HamanoJun 6, 2008
  19. Junio C HamanoJun 6, 2008
  20. Johannes SchindelinJun 6, 2008
  21. Junio C HamanoJun 6, 2008
  22. Johannes SchindelinJun 6, 2008
  23. Govind SalinasJun 6, 2008
  24. Johannes SchindelinJun 6, 2008
  25. 2/2 git-add: introduce --edit (to edit the diff vs. the index)Johannes Schindelin, Jun 5, 2008
  26. Olivier MarinJun 6, 2008
  27. Johannes SchindelinJun 6, 2008
  28. Junio C HamanoJun 6, 2008
  29. Johannes SchindelinJun 6, 2008
  30. Junio C HamanoJun 5, 2008
  31. Johannes SchindelinJun 5, 2008
  32. Paolo BonziniJun 6, 2008
  33. Johannes SchindelinJun 6, 2008
  34. Sergei OrganovJun 6, 2008
  35. Johannes SchindelinJun 6, 2008
  36. Junio C HamanoJun 6, 2008
  37. 2/2 git-add: introduce --edit (to edit the diff vs. the index)Johannes Schindelin, Jun 5, 2008
  38. Pieter de BieJun 5, 2008
  39. Junio C HamanoJun 5, 2008

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.