threads / discuss / 15108

diff --check is stupid about blank lines

Subject: diff --check is stupid about blank lines

## tl;dr

7 messages between Aug 20, 2008 and Aug 20, 2008.

replies: 6people: 3as markdown or json

Björn Steinbrink· Aug 20, 2008, 14:05 UTC · lore
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

diff --git a/git.c b/git.c
index 37b1d76..3fa1aeb 100644
--- a/git.c
+++ b/git.c
@@ -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
Jeff King· Aug 20, 2008, 17:28 UTC · re: Björn Steinbrink · lore

Re: diff --check is stupid about blank lines

On Wed, Aug 20, 2008 at 04:05:17PM +0200, Björn Steinbrink wrote:
> 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
Junio C Hamano· Aug 20, 2008, 18:13 UTC · re: Jeff King · lore

Re: diff --check is stupid about blank lines

Jeff King <peff@peff.net> writes:
Show 13 quoted lines
> ... 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...".

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.

Junio C Hamano· Aug 20, 2008, 18:42 UTC · re: Junio C Hamano · lore

Re: diff --check is stupid about blank lines

Junio C Hamano <gitster@pobox.com> writes:
Show 21 quoted lines
> Jeff King <peff@peff.net> writes:
>
>> ... 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...".
>
> 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 --git i/diff.c w/diff.c
index 10d5440..5923fe2 100644
--- i/diff.c
+++ w/diff.c
@@ -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);
Björn Steinbrink· Aug 20, 2008, 20:02 UTC · re: Junio C Hamano · lore

Re: diff --check is stupid about blank lines

On 2008.08.20 11:42:34 -0700, Junio C Hamano wrote:
Show 13 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> > 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.
Thanks, works for me :-)
Björn
Jeff King· Aug 20, 2008, 22:20 UTC · re: Junio C Hamano · lore

Re: diff --check is stupid about blank lines

On Wed, Aug 20, 2008 at 11:42:34AM -0700, Junio C Hamano wrote:
Show 6 quoted 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.
Ah, that was much easier than I expected. ;)
Maybe this should be squashed in?
---
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 6110566..974efd0 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -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
Junio C Hamano· Aug 20, 2008, 22:39 UTC · re: Jeff King · lore

Re: diff --check is stupid about blank lines

Jeff King <peff@peff.net> writes:
Show 12 quoted lines
> On Wed, Aug 20, 2008 at 11:42:34AM -0700, Junio C Hamano wrote:
>
>> 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?
Thanks but I've done that myself already.

← back to recent threads