Skip to contentSkip to conversation

diff --check is stupid about blank lines

PATCH Björn Steinbrink 3 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hi,

diff --check triggers not only for blank lines at the end of the file, but also at the end of the changes. That seems broken to me, unless you really dislike empty lines.

For example:
git (master) $ git diff

git.c +2 -0
index 37b1d76..3fa1aeb 100644
@@ -9,6 +9,8 @@ const char git_usage_string[] =
 const char git_more_info_string[] =
        "See 'git help COMMAND' for more information on a specific command.";
 
+int new_var = 0;
+
 static int use_pager = -1;
 struct pager_config {
        const char *cmd;

git (master) $ git diff --check
git.c:13: ends with blank lines.

But that blank line was of course intentional.

I'm not quite sure why that happens though. The code in checkdiff_consume seems to reset the flag when it sees context lines, but apparently that does not work for some reason.

git version 1.6.0.36.g3814c

Björn

#1 lore raw
Quoted 3 lines · Björn Steinbrink

diff --check triggers not only for blank lines at the end of the file, but also at the end of the changes. That seems broken to me, unless you really dislike empty lines.

Hmm, yes, that seems wrong. The problem seems to be the conditional at diff.c:1622:

        if ((data.ws_rule & WS_TRAILING_SPACE) &&
            data.trailing_blanks_start) {
                fprintf(o->file, "%s:%d: ends with blank lines.\n",
                        data.filename, data.trailing_blanks_start);
                data.status = 1; /* report errors */
        }

that should probably be "if we care about trailing space, and the last thing we saw was a trailing blank, _and_ the last hunk adds to end-of-file, then...". However, I'm not sure what is the best way to get that information out of xdiff. Is there a "this hunk hits eof" signal anywhere? Is there a definitive line count we could use to calculate that it is in the chunk of final lines in the file?

-Peff

#2 lore raw
Quoted 10 lines · Jeff King

... The problem seems to be the conditional at
diff.c:1622:

        if ((data.ws_rule & WS_TRAILING_SPACE) &&
            data.trailing_blanks_start) {
                fprintf(o->file, "%s:%d: ends with blank lines.\n",
                        data.filename, data.trailing_blanks_start);
                data.status = 1; /* report errors */
        }
Quoted 3 lines

that should probably be "if we care about trailing space, and the last thing we saw was a trailing blank, _and_ the last hunk adds to end-of-file, then...".

Instead, data.trailing_blanks_start is supposed to be reset to 0 every time we see non-blank newline, a copied context line, or new hunk.

So if this triggers with -U0 I'd understand, but otherwise I do not see how.

#3 lore raw
Quoted 16 lines · Junio C Hamano
Quoted 10 lines · Jeff King

... The problem seems to be the conditional at
diff.c:1622:

        if ((data.ws_rule & WS_TRAILING_SPACE) &&
            data.trailing_blanks_start) {
                fprintf(o->file, "%s:%d: ends with blank lines.\n",
                        data.filename, data.trailing_blanks_start);
                data.status = 1; /* report errors */
        }
Quoted 3 lines

that should probably be "if we care about trailing space, and the last thing we saw was a trailing blank, _and_ the last hunk adds to end-of-file, then...".

Quoted 5 lines

Instead, data.trailing_blanks_start is supposed to be reset to 0 every time we see non-blank newline, a copied context line, or new hunk.

So if this triggers with -U0 I'd understand, but otherwise I do not see how.

Ahhh, what idiot wrote the logic for checking trailing blank lines in checkdiff_consume().

It does not ask for any context lines. Sheesh.

This should fix it.

diff.c +1 -0
index 10d5440..5923fe2 100644
@@ -1628,6 +1628,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
 		xdemitcb_t ecb;
 
 		memset(&xecfg, 0, sizeof(xecfg));
+		xecfg.ctxlen = 1; /* at least one context line */
 		xpp.flags = XDF_NEED_MINIMAL;
 		xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
 			      &xpp, &xecfg, &ecb);
#4 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Junio C Hamano

Instead, data.trailing_blanks_start is supposed to be reset to 0 every time we see non-blank newline, a copied context line, or new hunk.

So if this triggers with -U0 I'd understand, but otherwise I do not see how.

Quoted 6 lines

Ahhh, what idiot wrote the logic for checking trailing blank lines in checkdiff_consume().

It does not ask for any context lines. Sheesh.

This should fix it.

Thanks, works for me :-)

Björn

#5 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano

Ahhh, what idiot wrote the logic for checking trailing blank lines in checkdiff_consume().

It does not ask for any context lines. Sheesh.

This should fix it.

Ah, that was much easier than I expected. ;)

Maybe this should be squashed in?

---

t/t4015-diff-whitespace.sh +9 -0
index 6110566..974efd0 100755
@@ -341,4 +341,13 @@ test_expect_success 'checkdiff detects trailing blank lines' '
 	git diff --check | grep "ends with blank"
 '
 
+test_expect_success 'checkdiff does not mistake trailing blank lines in hunk' '
+	echo content >x &&
+	git add x &&
+	echo "" >x &&
+	echo content >>x &&
+	git diff --check >out &&
+	! grep "ends with blank" out
+'
+
 test_done
#6 lore raw
Quoted 5 lines · Jeff King
Quoted 3 lines · Junio C Hamano

Ahhh, what idiot wrote the logic for checking trailing blank lines in checkdiff_consume().

Quoted 7 lines
Quoted 3 lines

It does not ask for any context lines. Sheesh.

This should fix it.

Ah, that was much easier than I expected. ;)

Maybe this should be squashed in?

Thanks but I've done that myself already.