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

Re: [PATCH] word-diff: ignore '\ No newline at eof' marker

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 12, 2012, 19:23 UTC
Message-ID
<7vty40208g.fsf@alter.siamese.dyndns.org>
In-Reply-To
<902665ee053876c2684f5b935ee4f81e77122802.1326366909.git.trast@student.ethz.ch>
Thomas Rast <trast@student.ethz.ch> writes:
> A proper fix would defer such markers until the end of the hunk.
> However, word-diff is inherently whitespace-ignoring, so as a cheap
> fix simply ignore the marker (and hide it from the output).
Sounds like a very sensible simplification of the issue.
Show 31 quoted lines
> We use a prefix match for '\ ' to parallel the logic in
> apply.c:parse_fragment().  We currently do not localize this string
> (just accept other variants of it in git-apply), but this should be
> future-proof.
>
> Noticed-by: Ivan Shirokoff <shirokoff@yandex-team.ru>
> Signed-off-by: Thomas Rast <trast@student.ethz.ch>
> ---
>
>  diff.c                |    8 ++++++++
>  t/t4034-diff-words.sh |   14 ++++++++++++++
>  2 files changed, 22 insertions(+), 0 deletions(-)
>
> diff --git a/diff.c b/diff.c
> index a65223a..996cc60 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -1113,6 +1113,14 @@ static void fn_out_consume(void *priv, char *line, unsigned long len)
>  			diff_words_append(line, len,
>  					  &ecbdata->diff_words->plus);
>  			return;
> +		} else if (!prefixcmp(line, "\\ ")) {
> +			/*
> +			 * Silently eat the "no newline at eof" marker
> +			 * (we are diffing without regard to
> +			 * whitespace anyway), and defer processing:
> +			 * more '+' lines could be after it.
> +			 */
> +			return;
>  		}
>  		diff_words_flush(ecbdata);

It took me a while to realize "defer processing" in the comment was meant to justify the placement of the new block _before_ this flush. Perhaps rephrasing it to "return without calling diff_words_flush()" would make it more readable?

Otherwise the patch looks good.
Thanks.
Show 24 quoted lines
>  		if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
> diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
> index 6f1e5a2..5c20121 100755
> --- a/t/t4034-diff-words.sh
> +++ b/t/t4034-diff-words.sh
> @@ -334,4 +334,18 @@ test_expect_success 'word-diff with diff.sbe' '
>  	word_diff --word-diff=plain
>  '
>  
> +test_expect_success 'word-diff with no newline at EOF' '
> +	cat >expect <<-\EOF &&
> +	diff --git a/pre b/post
> +	index 7bf316e..3dd0303 100644
> +	--- a/pre
> +	+++ b/post
> +	@@ -1 +1 @@
> +	a a [-a-]{+ab+} a a
> +	EOF
> +	printf "%s" "a a a a a" >pre &&
> +	printf "%s" "a a ab a a" >post &&
> +	word_diff --word-diff=plain
> +'
> +
>  test_done
Previous: Thomas Rast
Message 3 of 3 in “git diff --word-diff problem”
  1. Ivan ShirokoffJan 12, 2012
  2. word-diff: ignore '\ No newline at eof' markerThomas Rast, Jan 12, 2012
  3. Junio C HamanoJan 12, 2012

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.