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

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

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jun 27, 2008, 17:43 UTC
Message-ID
<alpine.DEB.1.00.0806271823220.9925@racer>
In-Reply-To
<7vfxr29zju.fsf@gitster.siamese.dyndns.org>
Hi,
On Tue, 24 Jun 2008, Junio C Hamano wrote:
Show 22 quoted lines
> Thomas Rast <trast@student.ethz.ch> writes:
> 
> > diff --git a/builtin-apply.c b/builtin-apply.c
> > index c497889..34c220f 100644
> > --- a/builtin-apply.c
> > +++ b/builtin-apply.c
> > @@ -153,6 +153,7 @@ struct patch {
> >  	unsigned int is_binary:1;
> >  	unsigned int is_copy:1;
> >  	unsigned int is_rename:1;
> > +	unsigned int recount:1;
> >  	struct fragment *fragments;
> >  	char *result;
> >  	size_t resultsize;
> 
> Why doesn't anybody find this quite wrong?
> 
> What is a "struct patch"?  It describes a change to a single file
> (i.e. information contained from one "diff --git" til next "diff --git"),
> groups the hunks (called "fragments") together and holds the postimage
> after applying these hunks.  Is this new "recount" field a per file
> attribute?

Actually, it is not. But then, it is an attribute of the patch: it will be recounted.

In addition, the patch would get quite large and unwieldy with "recount" being passed between the functions, because we do not have "apply_options" yet.

I was even briefly working on apply_options, but this would be a _huge_ patch (I had an initial working version, but given my limited time, I could not clean it up for submission anyway, so I scrapped it).

> > +	fragment->oldpos = 2;
> > +	fragment->oldlines = fragment->newlines = 0;
> 
> Why is this discarding the position information?
Sorry, I forgot.
Show 11 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 recount should not cause parse_fragment() to fail out either.  If 
> you miscounted, the codepath that follows this part knows how to handle 
> broken patch correctly anyway.
Okay.
> I think I've already mentioned the above two points when this was 
> originally posted.
> 
> Somewhat disgusted...
Sorry.

This is my updated patch (making changes to my "add -e" patch, which I may submit later), according to your comments so far:

-- snipsnap -- [PATCH] Allow git-apply to recount the lines in a hunk (AKA recountdiff)

Sometimes, the easiest way to fix up a patch is to edit it directly, even adding or deleting lines. Now, many people are not as divine as certain benevolent dictators as to update the hunk headers correctly at the first try.

So teach the tool to do it for us.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 Documentation/git-apply.txt |    7 +++-
 builtin-apply.c             |   76 +++++++++++++++++++++++++++++++++++++++----
 2 files changed, 75 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
index c834763..c5ee636 100644
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -12,7 +12,7 @@ SYNOPSIS
 'git-apply' [--stat] [--numstat] [--summary] [--check] [--index]
 	  [--apply] [--no-add] [--build-fake-ancestor <file>] [-R | --reverse]
 	  [--allow-binary-replacement | --binary] [--reject] [-z]
-	  [-pNUM] [-CNUM] [--inaccurate-eof] [--cached]
+	  [-pNUM] [-CNUM] [--inaccurate-eof] [--recount] [--cached]
 	  [--whitespace=<nowarn|warn|fix|error|error-all>]
 	  [--exclude=PATH] [--verbose] [<patch>...]
 
@@ -177,6 +177,11 @@ behavior:
 	current patch being applied will be printed. This option will cause
 	additional information to be reported.
 
+--recount::
+	Do not trust the line counts in the hunk headers, but infer them
+	by inspecting the patch (e.g. after editing the patch without
+	adjusting the hunk headers appropriately).
+
 Configuration
 -------------
 
diff --git a/builtin-apply.c b/builtin-apply.c
index c497889..c819652 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -153,6 +153,7 @@ struct patch {
 	unsigned int is_binary:1;
 	unsigned int is_copy:1;
 	unsigned int is_rename:1;
+	unsigned int recount:1;
 	struct fragment *fragments;
 	char *result;
 	size_t resultsize;
@@ -882,6 +883,57 @@ static int parse_range(const char *line, int len, int offset, const char *expect
 	return offset + ex;
 }
 
+static int recount_diff(char *line, int size, struct fragment *fragment)
+{
+	int oldlines = 0, newlines = 0, ret = 0;
+
+	if (size < 1) {
+		warning("recount: ignore empty hunk");
+		return -1;
+	}
+
+	for (;;) {
+		int len = linelen(line, size);
+		size -= len;
+		line += len;
+
+		if (size < 1)
+			break;
+
+		switch (*line) {
+		case ' ': case '\n':
+			newlines++;
+			/* fall through */
+		case '-':
+			oldlines++;
+			continue;
+		case '+':
+			newlines++;
+			continue;
+		case '\\':
+			break;
+		case '@':
+			ret = size < 3 || prefixcmp(line, "@@ ");
+			break;
+		case 'd':
+			ret = size < 5 || prefixcmp(line, "diff ");
+			break;
+		default:
+			ret = -1;
+			break;
+		}
+		if (ret) {
+			warning("recount: unexpected line: %.*s",
+				(int)linelen(line, size), line);
+			return -1;
+		}
+		break;
+	}
+	fragment->oldlines = oldlines;
+	fragment->newlines = newlines;
+	return 0;
+}
+
 /*
  * Parse a unified diff fragment header of the
  * form "@@ -a,b +c,d @@"
@@ -1013,6 +1065,8 @@ 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);
 	oldlines = fragment->oldlines;
 	newlines = fragment->newlines;
 	leading = 0;
@@ -2912,7 +2966,10 @@ static void prefix_patches(struct patch *p)
 	}
 }
 
-static int apply_patch(int fd, const char *filename, int inaccurate_eof)
+#define INACCURATE_EOF	(1<<0)
+#define RECOUNT		(1<<1)
+
+static int apply_patch(int fd, const char *filename, int options)
 {
 	size_t offset;
 	struct strbuf buf;
@@ -2928,7 +2985,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
 		int nr;
 
 		patch = xcalloc(1, sizeof(*patch));
-		patch->inaccurate_eof = inaccurate_eof;
+		patch->inaccurate_eof = !!(options & INACCURATE_EOF);
+		patch->recount =  !!(options & RECOUNT);
 		nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
 		if (nr < 0)
 			break;
@@ -2997,7 +3055,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 {
 	int i;
 	int read_stdin = 1;
-	int inaccurate_eof = 0;
+	int options = 0;
 	int errs = 0;
 	int is_not_gitdir;
 
@@ -3015,7 +3073,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 		int fd;
 
 		if (!strcmp(arg, "-")) {
-			errs |= apply_patch(0, "<stdin>", inaccurate_eof);
+			errs |= apply_patch(0, "<stdin>", options);
 			read_stdin = 0;
 			continue;
 		}
@@ -3115,7 +3173,11 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 			continue;
 		}
 		if (!strcmp(arg, "--inaccurate-eof")) {
-			inaccurate_eof = 1;
+			options |= INACCURATE_EOF;
+			continue;
+		}
+		if (!strcmp(arg, "--recount")) {
+			options |= RECOUNT;
 			continue;
 		}
 		if (0 < prefix_length)
@@ -3126,12 +3188,12 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 			die("can't open patch '%s': %s", arg, strerror(errno));
 		read_stdin = 0;
 		set_default_whitespace_mode(whitespace_option);
-		errs |= apply_patch(fd, arg, inaccurate_eof);
+		errs |= apply_patch(fd, arg, options);
 		close(fd);
 	}
 	set_default_whitespace_mode(whitespace_option);
 	if (read_stdin)
-		errs |= apply_patch(0, "<stdin>", inaccurate_eof);
+		errs |= apply_patch(0, "<stdin>", options);
 	if (whitespace_error) {
 		if (squelch_whitespace_errors &&
 		    squelch_whitespace_errors < whitespace_error) {
-- 
1.5.6.173.gde14c
Previous: Jeff KingNext: Thomas Rast
Message 58 of 62 in “git-add--interactive: manual hunk editing mode”
  1. git-add--interactive: manual hunk editing modeThomas Rast, May 23, 2008
  2. Ping YinMay 24, 2008
  3. Thomas RastMay 29, 2008
  4. Johannes SchindelinMay 29, 2008
  5. Thomas RastMay 29, 2008
  6. Thomas RastMay 29, 2008
  7. Jeff KingMay 29, 2008
  8. Johannes SchindelinMay 30, 2008
  9. Jakub NarebskiMay 30, 2008
  10. Thomas RastMay 30, 2008
  11. Junio C HamanoMay 30, 2008
  12. git-add--interactive: manual hunk editing mode v2Thomas Rast, Jun 1, 2008
  13. Thomas RastJun 1, 2008
  14. Jeff KingJun 1, 2008
  15. Jeff KingJun 5, 2008
  16. Thomas RastJun 5, 2008
  17. Jeff KingJun 5, 2008
  18. Thomas RastJun 5, 2008
  19. Jeff KingJun 5, 2008
  20. Thomas RastJun 5, 2008
  21. Jeff KingJun 5, 2008
  22. Junio C HamanoJun 5, 2008
  23. Jeff KingJun 5, 2008
  24. Johannes SchindelinJun 5, 2008
  25. Jeff KingJun 6, 2008
  26. Jeff KingJun 6, 2008
  27. Thomas RastJun 8, 2008
  28. Johannes SchindelinJun 8, 2008
  29. Johannes SchindelinJun 6, 2008
  30. Thomas RastJun 8, 2008
  31. Johannes SchindelinJun 8, 2008
  32. git-add--interactive: manual hunk editing modeThomas Rast, Jun 5, 2008
  33. git-add--interactive: manual hunk editing modeThomas Rast, Jun 8, 2008
  34. Johannes SchindelinJun 8, 2008
  35. Johan HerlandJun 9, 2008
  36. Jeff KingJun 9, 2008
  37. Johannes SchindelinJun 9, 2008
  38. Junio C HamanoJun 9, 2008
  39. Johan HerlandJun 9, 2008
  40. Jeff KingJun 9, 2008
  41. Johan HerlandJun 9, 2008
  42. Jeff KingJun 10, 2008
  43. Thomas RastJun 11, 2008
  44. Jeff KingJun 12, 2008
  45. Thomas RastJun 12, 2008
  46. Jeff KingJun 12, 2008
  47. git-add--interactive: manual hunk editing modeThomas Rast, Jun 13, 2008
  48. Jeff KingJun 23, 2008
  49. Johannes SchindelinJun 23, 2008
  50. Jeff KingJun 23, 2008
  51. apply --recount, was Re: [PATCH v4] git-add--interactive: manual hunk editing modeJohannes Schindelin, Jun 23, 2008
  52. Jeff KingJun 24, 2008
  53. 0/3 Manual editing for 'add' and 'add -p'Thomas Rast, Jun 24, 2008
  54. Miklos VajnaJun 24, 2008
  55. 1/3 Allow git-apply to ignore the hunk headers (AKA recountdiff)Thomas Rast, Jun 24, 2008
  56. Junio C HamanoJun 24, 2008
  57. Jeff KingJun 25, 2008
  58. Johannes SchindelinJun 27, 2008
  59. 2/3 git-add: introduce --edit (to edit the diff vs. the index)Thomas Rast, Jun 24, 2008
  60. 3/3 git-add--interactive: manual hunk editing modeThomas Rast, Jun 24, 2008
  61. Andreas EricssonJun 10, 2008
  62. Thomas RastJun 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.