git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / Atom feed
From: Stefan Beller <sbeller@google.com>
To: sbeller@google.com
Cc: git@vger.kernel.org, gitster@pobox.com, jonathantanmy@google.com
Subject: [PATCH 7/8] diff.c: add --color-moved-ignore-space-delta option
Date: Thu, 17 May 2018 12:46:52 -0700
Message-ID: <20180517194653.48928-8-sbeller@google.com> (raw)
In-Reply-To: <20180517194653.48928-1-sbeller@google.com>

This marks moved code still as blocks when their indentation level
changes uniformly.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 Documentation/diff-options.txt |  4 ++
 diff.c                         | 83 +++++++++++++++++++++++++++++++---
 diff.h                         |  2 +
 t/t4015-diff-whitespace.sh     | 54 ++++++++++++++++++++++
 4 files changed, 137 insertions(+), 6 deletions(-)

diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index 7b2527b9a19..facdbc8f95f 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -304,6 +304,10 @@ dimmed_zebra::
 	Ignore whitespace when comparing lines when performing the move
 	detection for --color-moved.  This ignores differences even if
 	one line has whitespace where the other line has none.
+--color-moved-[no-]ignore-space-prefix-delta::
+	Ignores whitespace when comparing lines when performing the move
+	detection for --color-moved. This ignores uniform differences
+	of white space at the beginning lines in moved blocks.
 
 --word-diff[=<mode>]::
 	Show a word diff, using the <mode> to delimit changed words.
diff --git a/diff.c b/diff.c
index b5819dd538f..1227a4d2a83 100644
--- a/diff.c
+++ b/diff.c
@@ -709,6 +709,31 @@ struct moved_entry {
 	struct moved_entry *next_line;
 };
 
+struct ws_delta {
+	char *string; /* The prefix delta, which is the same in the block */
+	int direction; /* adding or removing the line? */
+	int missmatch; /* in the remainder */
+};
+#define WS_DELTA_INIT { NULL, 0, 0 }
+
+static void compute_ws_delta(const struct emitted_diff_symbol *a,
+			     const struct emitted_diff_symbol *b,
+			     struct ws_delta *out)
+{
+	const struct emitted_diff_symbol *longer =  a->len > b->len ? a : b;
+	const struct emitted_diff_symbol *shorter = a->len > b->len ? b : a;
+	int d = longer->len - shorter->len;
+
+	out->missmatch = !memcmp(longer->line + d, shorter->line, shorter->len);
+	out->string = xmemdupz(longer->line, d);
+	out->direction = (a == longer);
+}
+
+static int compare_ws_delta(const struct ws_delta *a, const struct ws_delta *b)
+{
+	return a->direction == b->direction && !strcmp(a->string, b->string);
+}
+
 static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
 			   const void *entry,
 			   const void *entry_or_key,
@@ -720,6 +745,15 @@ static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
 	unsigned flags = diffopt->color_moved_ws_handling
 			 & XDF_WHITESPACE_FLAGS;
 
+	if (diffopt->color_moved_ws_handling & COLOR_MOVED_DELTA_WHITESPACES)
+		/*
+		 * As there is not specific white space config given,
+		 * we'd need to check for a new block, so ignore all
+		 * white space. The setup of the white space
+		 * configuration for the next block is done else where
+		 */
+		flags |= XDF_IGNORE_WHITESPACE;
+
 	return !xdiff_compare_lines(a->es->line, a->es->len,
 				    b->es->line, b->es->len,
 				    flags);
@@ -772,7 +806,8 @@ static void add_lines_to_move_detection(struct diff_options *o,
 }
 
 static int shrink_potential_moved_blocks(struct moved_entry **pmb,
-					 int pmb_nr)
+					 int pmb_nr,
+					 struct ws_delta **wsd)
 {
 	int lp, rp;
 
@@ -788,6 +823,10 @@ static int shrink_potential_moved_blocks(struct moved_entry **pmb,
 
 		if (lp < pmb_nr && rp > -1 && lp < rp) {
 			pmb[lp] = pmb[rp];
+			if (*wsd) {
+				free((*wsd)[lp].string);
+				(*wsd)[lp] = (*wsd)[rp];
+			}
 			pmb[rp] = NULL;
 			rp--;
 			lp++;
@@ -837,8 +876,11 @@ static void mark_color_as_moved(struct diff_options *o,
 {
 	struct moved_entry **pmb = NULL; /* potentially moved blocks */
 	int pmb_nr = 0, pmb_alloc = 0;
-	int n, flipped_block = 1, block_length = 0;
 
+	struct ws_delta *wsd = NULL; /* white space deltas between pmb */
+	int wsd_alloc = 0;
+
+	int n, flipped_block = 1, block_length = 0;
 
 	for (n = 0; n < o->emitted_symbols->nr; n++) {
 		struct hashmap *hm = NULL;
@@ -881,14 +923,31 @@ static void mark_color_as_moved(struct diff_options *o,
 			struct moved_entry *p = pmb[i];
 			struct moved_entry *pnext = (p && p->next_line) ?
 					p->next_line : NULL;
-			if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
-				pmb[i] = p->next_line;
+
+			if (o->color_moved_ws_handling & COLOR_MOVED_DELTA_WHITESPACES) {
+				struct ws_delta out = WS_DELTA_INIT;
+
+				if (pnext)
+					compute_ws_delta(l, pnext->es, &out);
+				if (pnext &&
+				    !hm->cmpfn(o, pnext, match, NULL) &&
+				    compare_ws_delta(&out, &wsd[i])) {
+					pmb[i] = p->next_line;
+					/* wsd[i] is the same */
+				} else {
+					pmb[i] = NULL;
+				}
+				free(out.string);
 			} else {
-				pmb[i] = NULL;
+				if (pnext && !hm->cmpfn(o, pnext, match, NULL)) {
+					pmb[i] = p->next_line;
+				} else {
+					pmb[i] = NULL;
+				}
 			}
 		}
 
-		pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr);
+		pmb_nr = shrink_potential_moved_blocks(pmb, pmb_nr, &wsd);
 
 		if (pmb_nr == 0) {
 			/*
@@ -897,6 +956,10 @@ static void mark_color_as_moved(struct diff_options *o,
 			 */
 			for (; match; match = hashmap_get_next(hm, match)) {
 				ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
+				if (o->color_moved_ws_handling & COLOR_MOVED_DELTA_WHITESPACES) {
+					ALLOC_GROW(wsd, pmb_nr + 1, wsd_alloc);
+					compute_ws_delta(l, match->es, &wsd[pmb_nr]);
+				}
 				pmb[pmb_nr++] = match;
 			}
 
@@ -914,6 +977,7 @@ static void mark_color_as_moved(struct diff_options *o,
 	adjust_last_block(o, n, block_length);
 
 	free(pmb);
+	free(wsd);
 }
 
 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
@@ -4647,12 +4711,16 @@ int diff_opt_parse(struct diff_options *options,
 		options->color_moved_ws_handling &= ~XDF_IGNORE_WHITESPACE_CHANGE;
 	else if (!strcmp(arg, "--color-moved-no-ignore-space-at-eol"))
 		options->color_moved_ws_handling &= ~XDF_IGNORE_WHITESPACE_AT_EOL;
+	else if (!strcmp(arg, "--color-moved-no-ignore-space-prefix-delta"))
+		options->color_moved_ws_handling &= ~COLOR_MOVED_DELTA_WHITESPACES;
 	else if (!strcmp(arg, "--color-moved-ignore-all-space"))
 		options->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
 	else if (!strcmp(arg, "--color-moved-ignore-space-change"))
 		options->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE_CHANGE;
 	else if (!strcmp(arg, "--color-moved-ignore-space-at-eol"))
 		options->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE_AT_EOL;
+	else if (!strcmp(arg, "--color-moved-ignore-space-prefix-delta"))
+		options->color_moved_ws_handling |= COLOR_MOVED_DELTA_WHITESPACES;
 	else if (!strcmp(arg, "--indent-heuristic"))
 		DIFF_XDL_SET(options, INDENT_HEURISTIC);
 	else if (!strcmp(arg, "--no-indent-heuristic"))
@@ -5558,6 +5626,9 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
 			hashmap_init(&del_lines, moved_entry_cmp, o, 0);
 			hashmap_init(&add_lines, moved_entry_cmp, o, 0);
 
+			if (o->color_moved_ws_handling & COLOR_MOVED_DELTA_WHITESPACES)
+				o->color_moved_ws_handling |= XDF_IGNORE_WHITESPACE;
+
 			add_lines_to_move_detection(o, &add_lines, &del_lines);
 			mark_color_as_moved(o, &add_lines, &del_lines);
 			if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
diff --git a/diff.h b/diff.h
index de5dc680051..b00ea76c083 100644
--- a/diff.h
+++ b/diff.h
@@ -214,6 +214,8 @@ struct diff_options {
 	} color_moved;
 	#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA
 	#define COLOR_MOVED_MIN_ALNUM_COUNT 20
+	/* XDF_WHITESPACE_FLAGS regarding block detection are set at 2, 3, 4 */
+	#define COLOR_MOVED_DELTA_WHITESPACES	(1 << 22)
 	int color_moved_ws_handling;
 };
 
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
index 751fc478dde..37ff528822f 100755
--- a/t/t4015-diff-whitespace.sh
+++ b/t/t4015-diff-whitespace.sh
@@ -1847,4 +1847,58 @@ test_expect_success 'only move detection ignores white spaces' '
 	test_cmp expected actual
 '
 
+test_expect_success 'compare whitespace delta across moved blocks' '
+
+	git reset --hard &&
+	q_to_tab <<-\EOF >text.txt &&
+	QIndented
+	QText across
+	Qthree lines
+	QBut! <- this stands out
+	Qthis one
+	QQline did
+	Qnot adjust
+	EOF
+
+	git add text.txt &&
+	git commit -m "add text.txt" &&
+
+	q_to_tab <<-\EOF >text.txt &&
+	QQIndented
+	QQText across
+	QQthree lines
+	QQQBut! <- this stands out
+	this one
+	Qline did
+	not adjust
+	EOF
+
+	git diff --color --color-moved --color-moved-ignore-space-prefix-delta |
+		grep -v "index" |
+		test_decode_color >actual &&
+
+	q_to_tab <<-\EOF >expected &&
+		<BOLD>diff --git a/text.txt b/text.txt<RESET>
+		<BOLD>--- a/text.txt<RESET>
+		<BOLD>+++ b/text.txt<RESET>
+		<CYAN>@@ -1,7 +1,7 @@<RESET>
+		<BOLD;MAGENTA>-QIndented<RESET>
+		<BOLD;MAGENTA>-QText across<RESET>
+		<BOLD;MAGENTA>-Qthree lines<RESET>
+		<RED>-QBut! <- this stands out<RESET>
+		<BOLD;MAGENTA>-Qthis one<RESET>
+		<BOLD;MAGENTA>-QQline did<RESET>
+		<BOLD;MAGENTA>-Qnot adjust<RESET>
+		<BOLD;YELLOW>+<RESET>QQ<BOLD;YELLOW>Indented<RESET>
+		<BOLD;YELLOW>+<RESET>QQ<BOLD;YELLOW>Text across<RESET>
+		<BOLD;YELLOW>+<RESET>QQ<BOLD;YELLOW>three lines<RESET>
+		<GREEN>+<RESET>QQQ<GREEN>But! <- this stands out<RESET>
+		<BOLD;YELLOW>+<RESET><BOLD;YELLOW>this one<RESET>
+		<BOLD;YELLOW>+<RESET>Q<BOLD;YELLOW>line did<RESET>
+		<BOLD;YELLOW>+<RESET><BOLD;YELLOW>not adjust<RESET>
+	EOF
+
+	test_cmp expected actual
+'
+
 test_done
-- 
2.17.0.582.gccdcbd54c44.dirty


  parent reply index

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-17  6:01 What's cooking in git.git (May 2018, #02; Thu, 17) Junio C Hamano
2018-05-17  6:39 ` jk/branch-l-0-deprecation (was Re: What's cooking in git.git (May 2018, #02; Thu, 17)) Kaartic Sivaraam
2018-05-17  9:48   ` Ævar Arnfjörð Bjarmason
2018-05-17 11:00     ` Kaartic Sivaraam
2018-05-17 12:02       ` Ævar Arnfjörð Bjarmason
2018-05-17 13:36     ` Jeff King
2018-05-24 15:10       ` Kaartic Sivaraam
2018-05-24 19:22         ` Jeff King
2018-05-24 19:31           ` [PATCH] branch: issue "-l" deprecation warning after pager starts Jeff King
2018-05-25  1:55             ` Junio C Hamano
2018-05-25  2:40               ` Jeff King
2018-05-25  8:56                 ` Junio C Hamano
2018-05-25  9:14                   ` Junio C Hamano
2018-05-25 17:10                     ` Jeff King
2018-05-26  2:37                       ` Junio C Hamano
2018-05-25 21:00                     ` [RFC PATCH 0/3] usage: prefix all lines in `vreportf()`, not just the first Martin Ågren
2018-05-25 21:00                       ` [RFC PATCH 1/3] usage: extract `prefix_suffix_lines()` from `advise()` Martin Ågren
2018-05-28  8:27                         ` Junio C Hamano
2018-05-28 18:40                         ` Duy Nguyen
2018-05-29 21:33                           ` Jeff King
2018-05-29 21:39                         ` Jeff King
2018-05-30  1:42                           ` Junio C Hamano
2018-05-30  6:00                             ` Junio C Hamano
2018-05-30 10:26                               ` Martin Ågren
2018-05-31  6:07                                 ` Jeff King
2018-05-25 21:00                       ` [RFC PATCH 2/3] usage: prefix all lines in `vreportf()`, not just the first Martin Ågren
2018-05-28  9:25                         ` Junio C Hamano
2018-05-28 18:45                           ` Duy Nguyen
2018-05-28 21:45                             ` Junio C Hamano
2018-05-29  4:49                               ` Martin Ågren
2018-05-29  5:50                                 ` Junio C Hamano
2018-05-29 10:30                                   ` Martin Ågren
2018-05-29 12:08                                     ` Junio C Hamano
2018-05-29 15:50                                 ` Duy Nguyen
2018-05-30 10:19                                   ` Martin Ågren
2018-05-29 21:32                           ` Jeff King
2018-05-30 10:20                             ` Martin Ågren
2018-05-25 21:00                       ` [RFC PATCH 3/3] usage: translate the "error: "-prefix and others Martin Ågren
2018-05-26  2:32                   ` [PATCH] branch: issue "-l" deprecation warning after pager starts Junio C Hamano
2018-05-26  2:33                     ` Junio C Hamano
2018-05-29 21:20                     ` Jeff King
2018-05-29 21:21                       ` Jeff King
2018-05-30  2:48                         ` Junio C Hamano
2018-05-31  5:44                           ` Jeff King
2018-05-26 19:39                 ` Kaartic Sivaraam
2018-06-02  4:46                 ` Duy Nguyen
2018-06-02  8:10                   ` Jeff King
2018-05-26 18:45             ` Kaartic Sivaraam
2018-05-29 21:15               ` Jeff King
2018-05-30  2:52                 ` Junio C Hamano
2018-05-31  5:51                   ` Jeff King
2018-06-01  1:35                     ` Junio C Hamano
2018-05-31  5:52                   ` Kaartic.Sivaraam
2018-05-17 13:22 ` What's cooking in git.git (May 2018, #02; Thu, 17) Derrick Stolee
2018-05-17 18:20 ` Stefan Beller
2018-05-17 18:29   ` [PATCH 0/2] Reroll 2 last commits of sb/object-store-replace Stefan Beller
2018-05-17 18:29     ` [PATCH 1/2] object.c: free replace map in raw_object_store_clear Stefan Beller
2018-05-17 18:29     ` [PATCH 2/2] replace-object.c: remove the_repository from prepare_replace_object Stefan Beller
2018-05-17 18:40   ` [PATCH] merge-recursive: give notice when submodule commit gets fast-forwarded Stefan Beller
2018-05-18 19:43     ` [PATCH v2 0/1] rebased: inform about auto submodule ff Leif Middelschulte
2018-05-18 19:48     ` [PATCH v3 " Leif Middelschulte
2018-05-18 19:48       ` [PATCH 1/1] Inform about fast-forwarding of submodules during merge Leif Middelschulte
2018-05-18 21:25         ` Elijah Newren
2018-05-21  4:12           ` Junio C Hamano
2018-05-17 19:46   ` [PATCH 0/8] Reroll of sb/diff-color-move-more Stefan Beller
2018-05-17 19:46     ` [PATCH 1/8] xdiff/xdiff.h: remove unused flags Stefan Beller
2018-05-17 19:46     ` [PATCH 2/8] xdiff/xdiffi.c: remove unneeded function declarations Stefan Beller
2018-05-17 19:46     ` [PATCH 3/8] diff.c: do not pass diff options as keydata to hashmap Stefan Beller
2018-05-17 19:46     ` [PATCH 4/8] diff.c: adjust hash function signature to match hashmap expectation Stefan Beller
2018-05-17 19:46     ` [PATCH 5/8] diff.c: add a blocks mode for moved code detection Stefan Beller
2018-05-17 19:46     ` [PATCH 6/8] diff.c: decouple white space treatment from move detection algorithm Stefan Beller
2018-05-18  4:00       ` Simon Ruderich
2018-05-18 19:25         ` Stefan Beller
2018-05-17 19:46     ` Stefan Beller [this message]
2018-05-17 19:46     ` [PATCH 8/8] diff: color-moved white space handling options imply color-moved Stefan Beller
2018-05-17 22:53     ` [PATCH 0/8] Reroll of sb/diff-color-move-more Jonathan Tan
2018-06-07 23:54     ` Jacob Keller
2018-05-17 22:36   ` What's cooking in git.git (May 2018, #02; Thu, 17) Junio C Hamano
2018-05-17 22:39     ` Stefan Beller
2018-05-17 22:56     ` Junio C Hamano
2018-05-17 22:58       ` Stefan Beller
2018-05-21  1:57 ` brian m. carlson
2018-05-21 17:36   ` Stefan Beller
2018-05-25 12:28 ` sb/submodule-move-nested breaks t7411 under GIT_FSMONITOR_TEST Ævar Arnfjörð Bjarmason
2018-05-25 17:27   ` Stefan Beller
2018-05-25 19:49   ` Stefan Beller
2018-09-06 12:31     ` Ævar Arnfjörð Bjarmason
2018-09-06 16:57       ` Stefan Beller
2018-09-06 19:03         ` Ben Peart
2018-09-06 20:14           ` Stefan Beller
2018-09-06 20:34             ` [PATCH] git-mv: allow submodules and fsmonitor to work together Stefan Beller
2018-09-10 15:58               ` Ben Peart
2018-09-10 16:29                 ` [PATCH v1] " Ben Peart
2018-09-10 17:07                   ` Stefan Beller
2018-09-10 19:38                     ` Ben Peart

Reply instructions:

You may reply publically 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=20180517194653.48928-8-sbeller@google.com \
    --to=sbeller@google.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jonathantanmy@google.com \
    /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

git@vger.kernel.org list mirror (unofficial, one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.org/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox