Skip to contentSkip to conversation

diff --check line number bug

PATCH In review Jay Soffian 2 participants 4 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

% mkdir git-ws
% cd git-ws
% git init
% touch foo
% git add foo
% printf '\n\nline 3 \n\nline 5 \nline 6\n' > foo
% cat -n foo

     1	
     2	
     3	line 3
     4	
     5	line 5
     6	line 6

% sed -ne l foo
$
$
line 3 $
$
line 5 $
line 6$
% git diff --check foo
foo:1: trailing whitespace.
+line 3
foo:2: trailing whitespace.
+line 5
% git version
git version 1.5.4.1.1281.g75df

Apparently somewhere in the bowels of the "Crazy xdl interfaces", empty lines are skipped over, thus the line number counting in checkdiff_consume() is off? I dunno, I briefly looked into fixing it but it didn't seem like a quick fix.

j.

#1 lore raw

PATCH 1/1 Fix line numbers emitted by "git diff --check"

Fixes a problem where git diff --check was emitting incorrect lines numbers. Adds a test to verify the fix.

---
 diff.c                     |    4 +++-
 t/t4015-diff-whitespace.sh |    9 +++++++++
 2 files changed, 12 insertions(+), 1 deletions(-)
diff.c +3 -1
index cd8bc4d..a002573 100644
@@ -1023,8 +1023,10 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 	if (line[0] == '+') {
 		data->status = check_and_emit_line(line + 1, len - 1,
 		    data->ws_rule, NULL, NULL, NULL, NULL);
-		if (!data->status)
+		if (!data->status) {
+			data->lineno++;
 			return;
+		}
 		err = whitespace_error_string(data->status);
 		printf("%s:%d: %s.\n", data->filename, data->lineno, err);
 		free(err);
t/t4015-diff-whitespace.sh +9 -0
index d30169f..83c54b7 100755
@@ -326,4 +326,13 @@ test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab:
 	! git diff --check
 
 '
+
+test_expect_success 'line numbers in --check output are correct' '
+
+	echo "" > x &&
+	echo "foo(); " >> x &&
+	git diff --check | grep "x:2:"
+
+'
+
 test_done
Signature · 1 line
1.5.4.1.1281.g75df
#2 lore raw

Re: [PATCH] Fix line numbers emitted by "git diff --check"

Quoted 4 lines · Jay Soffian

Fixes a problem where git diff --check was emitting incorrect lines numbers. Adds a test to verify the fix.

Ahh, our mails crossed, and our fixes are equivalent, but yours has an additional test, which is always good.

Will steal the test part and squash it in (I've already patched 'maint' with the one I sent out), if you do not mind.

#3 lore raw

[PATCH/maint] diff: Fix miscounting of --check output

In reply to Jay Soffian
Quoted 4 lines · Jay Soffian

Apparently somewhere in the bowels of the "Crazy xdl interfaces", empty lines are skipped over, thus the line number counting in checkdiff_consume() is off? I dunno, I briefly looked into fixing it but it didn't seem like a quick fix.

Good catch, thanks.

This is not xdl interface, but simply a miscounting by the user of xdl interface. Here is a fix.

-- >8 --
diff: Fix miscounting of --check output

c1795bb (Unify whitespace checking) incorrectly made the checking function return without incrementing the line numbers when there is no whitespace problem is found on a '+' line.

This resurrects the earlier behaviour.

Noticed and reported by Jay Soffian.

---
 diff.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
diff.c +2 -2
index 047310f..90af600 100644
@@ -1021,6 +1021,7 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 	char *err;
 
 	if (line[0] == '+') {
+		data->lineno++;
 		data->status = check_and_emit_line(line + 1, len - 1,
 		    data->ws_rule, NULL, NULL, NULL, NULL);
 		if (!data->status)
@@ -1031,13 +1032,12 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
 		emit_line(set, reset, line, 1);
 		(void)check_and_emit_line(line + 1, len - 1, data->ws_rule,
 		    stdout, set, reset, ws);
-		data->lineno++;
 	} else if (line[0] == ' ')
 		data->lineno++;
 	else if (line[0] == '@') {
 		char *plus = strchr(line, '+');
 		if (plus)
-			data->lineno = strtol(plus, NULL, 10);
+			data->lineno = strtol(plus, NULL, 10) - 1;
 		else
 			die("invalid diff");
 	}