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

Re: git apply/am whitespace errors

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 26, 2011, 20:39 UTC
Message-ID
<7vr53311js.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7vzkhr142r.fsf@alter.siamese.dyndns.org>
Subject: apply --whitespace=error: correctly report new blank lines at end

Earlier 77b15bb (apply --whitespace=warn/error: diagnose blank at EOF, 2009-09-03) cheated by reporting the line number of the hunk that contains the offending line that adds new blank lines at the end of the file. All other types of whitespace errors are reported with the line number in the patch file that has the actual offending text.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * This is an interim workaround. fragment->linenr was added for the
   specific purpose of reporting this type of error in the quoted patch,
   and the field is not used for any other purpose, so it may be a better
   fix to either rename it to hunk_linenr and use it directly without
   introducing a new local variable in apply_one_fragment(), or pass
   "linenr" as a parameter to parse_fragment() and pass it down the
   callchain all the way through, or something like that.
 builtin/apply.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index f2edc52..092429a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2447,6 +2447,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 	char *old, *oldlines;
 	struct strbuf newlines;
 	int new_blank_lines_at_end = 0;
+	int found_new_blank_lines_at_end = 0;
+	int hunk_linenr = frag->linenr;
 	unsigned long leading, trailing;
 	int pos, applied_pos;
 	struct image preimage;
@@ -2540,14 +2542,18 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 				error("invalid start of line: '%c'", first);
 			return -1;
 		}
-		if (added_blank_line)
+		if (added_blank_line) {
+			if (!new_blank_lines_at_end)
+				found_new_blank_lines_at_end = hunk_linenr;
 			new_blank_lines_at_end++;
+		}
 		else if (is_blank_context)
 			;
 		else
 			new_blank_lines_at_end = 0;
 		patch += len;
 		size -= len;
+		hunk_linenr++;
 	}
 	if (inaccurate_eof &&
 	    old > oldlines && old[-1] == '\n' &&
@@ -2629,7 +2635,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 		    preimage.nr + applied_pos >= img->nr &&
 		    (ws_rule & WS_BLANK_AT_EOF) &&
 		    ws_error_action != nowarn_ws_error) {
-			record_ws_error(WS_BLANK_AT_EOF, "+", 1, frag->linenr);
+			record_ws_error(WS_BLANK_AT_EOF, "+", 1,
+					found_new_blank_lines_at_end);
 			if (ws_error_action == correct_ws_error) {
 				while (new_blank_lines_at_end--)
 					remove_last_line(&postimage);
-- 
1.7.7.rc3
Previous: Junio C Hamano
Message 3 of 3 in “git apply/am whitespace errors”
  1. neubyrSep 26, 2011
  2. Junio C HamanoSep 26, 2011
  3. Junio C HamanoSep 26, 2011

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.