# diff --check is stupid about blank lines

7 messages from 2008-08-20 to 2008-08-20. Participants: Björn Steinbrink, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/15108

## Björn Steinbrink, 2008-08-20 14:05

Subject: diff --check is stupid about blank lines
Message-ID: <20080820140517.GA1304@atjola.homenet>
URL: https://gitlist.dev/e/20080820140517.GA1304%40atjola.homenet

```
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, 2008-08-20 17:28

Subject: Re: diff --check is stupid about blank lines
Message-ID: <20080820172825.GC27773@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20080820172825.GC27773%40sigill.intra.peff.net
In-Reply-To: <20080820140517.GA1304@atjola.homenet>

```
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, 2008-08-20 18:13

Subject: Re: diff --check is stupid about blank lines
Message-ID: <7v7iabsgfk.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v7iabsgfk.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20080820172825.GC27773@sigill.intra.peff.net>

```
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.

```

## Junio C Hamano, 2008-08-20 18:42

Subject: Re: diff --check is stupid about blank lines
Message-ID: <7vskszr0it.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vskszr0it.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <7v7iabsgfk.fsf@gitster.siamese.dyndns.org>

```
Junio C Hamano <gitster@pobox.com> writes:

> 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, 2008-08-20 20:02

Subject: Re: diff --check is stupid about blank lines
Message-ID: <20080820200201.GA3210@atjola.homenet>
URL: https://gitlist.dev/e/20080820200201.GA3210%40atjola.homenet
In-Reply-To: <7vskszr0it.fsf@gitster.siamese.dyndns.org>

```
On 2008.08.20 11:42:34 -0700, Junio C Hamano wrote:
> 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, 2008-08-20 22:20

Subject: Re: diff --check is stupid about blank lines
Message-ID: <20080820222048.GB8137@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20080820222048.GB8137%40coredump.intra.peff.net
In-Reply-To: <7vskszr0it.fsf@gitster.siamese.dyndns.org>

```
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?

---
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, 2008-08-20 22:39

Subject: Re: diff --check is stupid about blank lines
Message-ID: <7v8wurnwf6.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v8wurnwf6.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <20080820222048.GB8137@coredump.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> 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.

```
