git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Phillip Wood via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Phillip Wood <phillip.wood@dunelm.org.uk>,
	Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH 02/10] diff --color-moved: avoid false short line matches and bad zerba coloring
Date: Mon, 14 Jun 2021 13:04:40 +0000	[thread overview]
Message-ID: <3d02a0a91a086417f9dec4823255f50644e3aa87.1623675889.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.981.git.1623675888.gitgitgadget@gmail.com>

From: Phillip Wood <phillip.wood@dunelm.org.uk>

When marking moved lines it is possible for a block of potential
matched lines to extend past a change in sign when there is a sequence
of added lines whose text matches the text of a sequence of deleted
and added lines. Most of the time either `match` will be NULL or
`pmb_advance_or_null()` will fail when the loop encounters a change of
sign but there are corner cases where `match` is non-NULL and
`pmb_advance_or_null()` successfully advances the moved block despite
the change in sign.

One consequence of this is highlighting a short line as moved when it
should not be. For example

-moved line  # Correctly highlighted as moved
+short line  # Wrongly highlighted as moved
 context
+moved line  # Correctly highlighted as moved
+short line
 context
-short line

The other consequence is coloring a moved addition following a moved
deletion in the wrong color. In the example below the first "+moved
line 3" should be highlighted as newMoved not newMovedAlternate.

-moved line 1 # Correctly highlighted as oldMoved
-moved line 2 # Correctly highlighted as oldMovedAlternate
+moved line 3 # Wrongly highlighted as newMovedAlternate
 context      # Everything else is highlighted correctly
+moved line 2
+moved line 3
 context
+moved line 1
-moved line 3

These false matches are more likely when using --color-moved-ws with
the exception of --color-moved-ws=allow-indentation-change which ties
the sign of the current whitespace delta to the sign of the line to
avoid this problem. The fix is to check that the sign of the new line
being matched is the same as the sign of the line that started the
block of potential matches.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 diff.c                     | 17 ++++++----
 t/t4015-diff-whitespace.sh | 65 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/diff.c b/diff.c
index cb068f8258c0..a0c43a104768 100644
--- a/diff.c
+++ b/diff.c
@@ -1142,7 +1142,7 @@ static void mark_color_as_moved(struct diff_options *o,
 	struct moved_block *pmb = NULL; /* potentially moved blocks */
 	int pmb_nr = 0, pmb_alloc = 0;
 	int n, flipped_block = 0, block_length = 0;
-	enum diff_symbol last_symbol = 0;
+	enum diff_symbol moved_symbol = 0;
 
 
 	for (n = 0; n < o->emitted_symbols->nr; n++) {
@@ -1168,7 +1168,7 @@ static void mark_color_as_moved(struct diff_options *o,
 			flipped_block = 0;
 		}
 
-		if (!match) {
+		if (pmb_nr && (!match || l->s != moved_symbol)) {
 			int i;
 
 			adjust_last_block(o, n, block_length);
@@ -1177,12 +1177,13 @@ static void mark_color_as_moved(struct diff_options *o,
 			pmb_nr = 0;
 			block_length = 0;
 			flipped_block = 0;
-			last_symbol = l->s;
+		}
+		if (!match) {
+			moved_symbol = 0;
 			continue;
 		}
 
 		if (o->color_moved == COLOR_MOVED_PLAIN) {
-			last_symbol = l->s;
 			l->flags |= DIFF_SYMBOL_MOVED_LINE;
 			continue;
 		}
@@ -1214,11 +1215,16 @@ static void mark_color_as_moved(struct diff_options *o,
 			}
 
 			if (adjust_last_block(o, n, block_length) &&
-			    pmb_nr && last_symbol == l->s)
+			    pmb_nr && moved_symbol == l->s)
 				flipped_block = (flipped_block + 1) % 2;
 			else
 				flipped_block = 0;
 
+			if (pmb_nr)
+				moved_symbol = l->s;
+			else
+				moved_symbol = 0;
+
 			block_length = 0;
 		}
 
@@ -1228,7 +1234,6 @@ static void mark_color_as_moved(struct diff_options *o,
 			if (flipped_block && o->color_moved != COLOR_MOVED_BLOCKS)
 				l->flags |= DIFF_SYMBOL_MOVED_LINE_ALT;
 		}
-		last_symbol = l->s;
 	}
 	adjust_last_block(o, n, block_length);
 
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 920114cd795c..3119a59f071d 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -1514,6 +1514,71 @@ test_expect_success 'zebra alternate color is only used when necessary' '
 	test_cmp expected actual
 '
 
+test_expect_success 'short lines of opposite sign do not get marked as moved' '
+	cat >old.txt <<-\EOF &&
+	this line should be marked as moved
+	unchanged
+	unchanged
+	unchanged
+	unchanged
+	too short
+	this line should be marked as oldMoved newMoved
+	this line should be marked as oldMovedAlternate newMoved
+	unchanged 1
+	unchanged 2
+	unchanged 3
+	unchanged 4
+	this line should be marked as oldMoved newMoved/newMovedAlternate
+	EOF
+	cat >new.txt <<-\EOF &&
+	too short
+	unchanged
+	unchanged
+	this line should be marked as moved
+	too short
+	unchanged
+	unchanged
+	this line should be marked as oldMoved newMoved/newMovedAlternate
+	unchanged 1
+	unchanged 2
+	this line should be marked as oldMovedAlternate newMoved
+	this line should be marked as oldMoved newMoved/newMovedAlternate
+	unchanged 3
+	this line should be marked as oldMoved newMoved
+	unchanged 4
+	EOF
+	test_expect_code 1 git diff --no-index --color --color-moved=zebra \
+		old.txt new.txt >output && cat output &&
+	grep -v index output | test_decode_color >actual &&
+	cat >expect <<-\EOF &&
+	<BOLD>diff --git a/old.txt b/new.txt<RESET>
+	<BOLD>--- a/old.txt<RESET>
+	<BOLD>+++ b/new.txt<RESET>
+	<CYAN>@@ -1,13 +1,15 @@<RESET>
+	<BOLD;MAGENTA>-this line should be marked as moved<RESET>
+	<GREEN>+<RESET><GREEN>too short<RESET>
+	 unchanged<RESET>
+	 unchanged<RESET>
+	<BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as moved<RESET>
+	<GREEN>+<RESET><GREEN>too short<RESET>
+	 unchanged<RESET>
+	 unchanged<RESET>
+	<RED>-too short<RESET>
+	<BOLD;MAGENTA>-this line should be marked as oldMoved newMoved<RESET>
+	<BOLD;BLUE>-this line should be marked as oldMovedAlternate newMoved<RESET>
+	<BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMoved newMoved/newMovedAlternate<RESET>
+	 unchanged 1<RESET>
+	 unchanged 2<RESET>
+	<BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMovedAlternate newMoved<RESET>
+	<BOLD;YELLOW>+<RESET><BOLD;YELLOW>this line should be marked as oldMoved newMoved/newMovedAlternate<RESET>
+	 unchanged 3<RESET>
+	<BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMoved newMoved<RESET>
+	 unchanged 4<RESET>
+	<BOLD;MAGENTA>-this line should be marked as oldMoved newMoved/newMovedAlternate<RESET>
+	EOF
+	test_cmp expect actual
+'
+
 test_expect_success 'cmd option assumes configured colored-moved' '
 	test_config color.diff.oldMoved "magenta" &&
 	test_config color.diff.newMoved "cyan" &&
-- 
gitgitgadget


  parent reply	other threads:[~2021-06-14 13:05 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-14 13:04 [PATCH 00/10] diff --color-moved[-ws] speedups Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 01/10] diff --color-moved=zerba: fix alternate coloring Phillip Wood via GitGitGadget
2021-06-15  3:24   ` Junio C Hamano
2021-06-15 11:22     ` Phillip Wood
2021-06-14 13:04 ` Phillip Wood via GitGitGadget [this message]
2021-06-14 13:04 ` [PATCH 03/10] diff: simplify allow-indentation-change delta calculation Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 04/10] diff --color-moved-ws=allow-indentation-change: simplify and optimize Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 05/10] diff --color-moved: call comparison function directly Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 06/10] diff --color-moved: unify moved block growth functions Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 07/10] diff --color-moved: shrink potential moved blocks as we go Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 08/10] diff --color-moved: stop clearing potential moved blocks Phillip Wood via GitGitGadget
2021-06-14 13:04 ` [PATCH 09/10] diff --color-moved-ws=allow-indentation-change: improve hash lookups Phillip Wood via GitGitGadget
2021-07-09 15:36   ` Elijah Newren
2021-06-14 13:04 ` [PATCH 10/10] diff --color-moved: intern strings Phillip Wood via GitGitGadget
2021-06-16 14:24 ` [PATCH 00/10] diff --color-moved[-ws] speedups Ævar Arnfjörð Bjarmason
2021-06-21 10:03   ` Phillip Wood
2021-07-20 10:36 ` [PATCH v2 00/12] " Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 01/12] diff --color-moved: add perf tests Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 02/12] diff --color-moved=zebra: fix alternate coloring Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 03/12] diff --color-moved: avoid false short line matches and bad zerba coloring Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 04/12] diff: simplify allow-indentation-change delta calculation Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 05/12] diff --color-moved-ws=allow-indentation-change: simplify and optimize Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 06/12] diff --color-moved: call comparison function directly Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 07/12] diff --color-moved: unify moved block growth functions Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 08/12] diff --color-moved: shrink potential moved blocks as we go Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 09/12] diff --color-moved: stop clearing potential moved blocks Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 10/12] diff --color-moved-ws=allow-indentation-change: improve hash lookups Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 11/12] diff: use designated initializers for emitted_diff_symbol Phillip Wood via GitGitGadget
2021-07-20 10:36   ` [PATCH v2 12/12] diff --color-moved: intern strings Phillip Wood via GitGitGadget
2021-07-20 13:38   ` [PATCH v2 00/12] diff --color-moved[-ws] speedups Phillip Wood
2021-10-27 12:04   ` [PATCH v3 00/15] " Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 01/15] diff --color-moved: add perf tests Phillip Wood via GitGitGadget
2021-10-28 21:32       ` Junio C Hamano
2021-10-29 10:24         ` Phillip Wood
2021-10-29 11:06           ` Ævar Arnfjörð Bjarmason
2021-11-10 11:05             ` Phillip Wood
2021-10-27 12:04     ` [PATCH v3 02/15] diff --color-moved: clear all flags on blocks that are too short Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 03/15] diff --color-moved: factor out function Phillip Wood via GitGitGadget
2021-10-28 21:51       ` Junio C Hamano
2021-10-29 10:35         ` Phillip Wood
2021-10-27 12:04     ` [PATCH v3 04/15] diff --color-moved: rewind when discarding pmb Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 05/15] diff --color-moved=zebra: fix alternate coloring Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 06/15] diff --color-moved: avoid false short line matches and bad zerba coloring Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 07/15] diff: simplify allow-indentation-change delta calculation Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 08/15] diff --color-moved-ws=allow-indentation-change: simplify and optimize Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 09/15] diff --color-moved: call comparison function directly Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 10/15] diff --color-moved: unify moved block growth functions Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 11/15] diff --color-moved: shrink potential moved blocks as we go Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 12/15] diff --color-moved: stop clearing potential moved blocks Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 13/15] diff --color-moved-ws=allow-indentation-change: improve hash lookups Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 14/15] diff: use designated initializers for emitted_diff_symbol Phillip Wood via GitGitGadget
2021-10-27 12:04     ` [PATCH v3 15/15] diff --color-moved: intern strings Phillip Wood via GitGitGadget
2021-10-27 13:28     ` [PATCH v3 00/15] diff --color-moved[-ws] speedups Phillip Wood
2021-11-16  9:49     ` [PATCH v4 " Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 01/15] diff --color-moved: add perf tests Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 02/15] diff --color-moved: clear all flags on blocks that are too short Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 03/15] diff --color-moved: factor out function Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 04/15] diff --color-moved: rewind when discarding pmb Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 05/15] diff --color-moved=zebra: fix alternate coloring Phillip Wood via GitGitGadget
2021-11-22 13:34         ` Johannes Schindelin
2021-11-16  9:49       ` [PATCH v4 06/15] diff --color-moved: avoid false short line matches and bad zerba coloring Phillip Wood via GitGitGadget
2021-11-22 14:18         ` Johannes Schindelin
2021-11-22 19:00           ` Phillip Wood
2021-11-22 21:54             ` Johannes Schindelin
2021-11-16  9:49       ` [PATCH v4 07/15] diff: simplify allow-indentation-change delta calculation Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 08/15] diff --color-moved-ws=allow-indentation-change: simplify and optimize Phillip Wood via GitGitGadget
2021-11-23 14:51         ` Johannes Schindelin
2021-11-16  9:49       ` [PATCH v4 09/15] diff --color-moved: call comparison function directly Phillip Wood via GitGitGadget
2021-11-23 15:09         ` Johannes Schindelin
2021-11-16  9:49       ` [PATCH v4 10/15] diff --color-moved: unify moved block growth functions Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 11/15] diff --color-moved: shrink potential moved blocks as we go Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 12/15] diff --color-moved: stop clearing potential moved blocks Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 13/15] diff --color-moved-ws=allow-indentation-change: improve hash lookups Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 14/15] diff: use designated initializers for emitted_diff_symbol Phillip Wood via GitGitGadget
2021-11-16  9:49       ` [PATCH v4 15/15] diff --color-moved: intern strings Phillip Wood via GitGitGadget
2021-12-08 12:30       ` [PATCH v4 00/15] diff --color-moved[-ws] speedups Johannes Schindelin
2021-12-09 10:29       ` [PATCH v5 " Phillip Wood via GitGitGadget
2021-12-09 10:29         ` [PATCH v5 01/15] diff --color-moved: add perf tests Phillip Wood via GitGitGadget
2021-12-09 10:29         ` [PATCH v5 02/15] diff --color-moved: clear all flags on blocks that are too short Phillip Wood via GitGitGadget
2021-12-09 10:29         ` [PATCH v5 03/15] diff --color-moved: factor out function Phillip Wood via GitGitGadget
2021-12-09 10:29         ` [PATCH v5 04/15] diff --color-moved: rewind when discarding pmb Phillip Wood via GitGitGadget
2021-12-09 10:29         ` [PATCH v5 05/15] diff --color-moved=zebra: fix alternate coloring Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 06/15] diff --color-moved: avoid false short line matches and bad zebra coloring Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 07/15] diff: simplify allow-indentation-change delta calculation Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 08/15] diff --color-moved-ws=allow-indentation-change: simplify and optimize Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 09/15] diff --color-moved: call comparison function directly Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 10/15] diff --color-moved: unify moved block growth functions Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 11/15] diff --color-moved: shrink potential moved blocks as we go Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 12/15] diff --color-moved: stop clearing potential moved blocks Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 13/15] diff --color-moved-ws=allow-indentation-change: improve hash lookups Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 14/15] diff: use designated initializers for emitted_diff_symbol Phillip Wood via GitGitGadget
2021-12-09 10:30         ` [PATCH v5 15/15] diff --color-moved: intern strings Phillip Wood via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3d02a0a91a086417f9dec4823255f50644e3aa87.1623675889.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).