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

[RFC PATCH 7/7] diff/am: enhance diff format to use */~ for moved lines

From
Stefan Beller <sbeller@google.com>
Date
Aug 4, 2018, 01:53 UTC
Message-ID
<20180804015317.182683-8-sbeller@google.com>
In-Reply-To
<20180804015317.182683-1-sbeller@google.com>
Try it out via
    ./git-format-patch --mark-moved 15ef69314d^..15ef69314d
to see if you like it.

This separates the coloring decision from the detection of moved lines. When giving --mark-moved, move detection is still performed and the output markers are adjusted to */~ for new and old code.

git-apply and git-am will also accept these patches by rewriting those signs back to +/-.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 apply.c | 12 ++++++++++++
 diff.c  | 21 +++++++++++++++++++++
 diff.h  |  5 ++++-
 3 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/apply.c b/apply.c
index 23a0f25ded8..cc42a4fa02a 100644
--- a/apply.c
+++ b/apply.c
@@ -2900,6 +2900,12 @@ static int apply_one_fragment(struct apply_state *state,
 			    ws_blank_line(patch + 1, plen, ws_rule))
 				is_blank_context = 1;
 			/* fallthrough */
+		case '~':
+			/*
+			 * For now ignore moved line indicators and apply
+			 * as a regular old line
+			 */
+			/* fallthrough */
 		case '-':
 			memcpy(old, patch + 1, plen);
 			add_line_info(&preimage, old, plen,
@@ -2908,6 +2914,12 @@ static int apply_one_fragment(struct apply_state *state,
 			if (first == '-')
 				break;
 			/* fallthrough */
+		case '*':
+			/*
+			 * For now ignore moved line indicators and apply
+			 * as a regular new line
+			 */
+			/* fallthrough */
 		case '+':
 			/* --no-add does not add new lines */
 			if (first == '+' && state->no_add)
diff --git a/diff.c b/diff.c
index 56bab011df7..8e39e77229f 100644
--- a/diff.c
+++ b/diff.c
@@ -1043,6 +1043,9 @@ static const char *determine_line_color(struct diff_options *o,
 	const int off = (eds->s == DIFF_SYMBOL_PLUS) ?
 		DIFF_FILE_NEW_MOVED - DIFF_FILE_OLD_MOVED : 0;
 
+	if (!o->color_moved)
+		goto default_color;
+
 	switch (flags & (DIFF_SYMBOL_MOVED_LINE |
 			 DIFF_SYMBOL_MOVED_LINE_ALT |
 			 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
@@ -1063,6 +1066,7 @@ static const char *determine_line_color(struct diff_options *o,
 		set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED + off);
 		break;
 	default:
+default_color:
 		set = (eds->s == DIFF_SYMBOL_PLUS) ?
 			diff_get_color_opt(o, DIFF_FILE_NEW):
 			diff_get_color_opt(o, DIFF_FILE_OLD);
@@ -1152,6 +1156,9 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
 
 		first = o->output_indicators[OI_NEW] ?
 			o->output_indicators[OI_NEW] : "+";
+		if (o->output_indicators[OI_MOVED_NEW] &&
+		   (flags & DIFF_SYMBOL_MOVED_LINE))
+			first = "*";
 		emit_line_ws_markup(o, set_sign, set, reset, first, line, len,
 				    flags & DIFF_SYMBOL_CONTENT_WS_MASK,
 				    flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
@@ -1176,6 +1183,9 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
 		}
 		first = o->output_indicators[OI_OLD] ?
 			o->output_indicators[OI_OLD] : "-";
+		if (o->output_indicators[OI_MOVED_NEW] &&
+		   (flags & DIFF_SYMBOL_MOVED_LINE))
+			first = "~";
 		emit_line_ws_markup(o, set_sign, set, reset, first, line, len,
 				    flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
 		break;
@@ -4795,6 +4805,7 @@ int diff_opt_parse(struct diff_options *options,
 	else if (!strcmp(arg, "--no-color"))
 		options->use_color = 0;
 	else if (!strcmp(arg, "--color-moved")) {
+		options->color_moved = 1;
 		if (diff_color_moved_default)
 			options->markup_moved = diff_color_moved_default;
 		if (options->markup_moved == COLOR_MOVED_NO)
@@ -4806,6 +4817,16 @@ int diff_opt_parse(struct diff_options *options,
 		if (cm < 0)
 			die("bad --color-moved argument: %s", arg);
 		options->markup_moved = cm;
+		options->color_moved = 1;
+	} else if (skip_prefix(arg, "--mark-moved", &arg)) {
+		/*
+		 * NEEDSWORK:
+		 * Once merged with 51da15eb230 (diff.c: add a blocks mode for
+		 * moved code detection, 2018-07-16), make it COLOR_MOVED_BLOCKS
+		 */
+		options->markup_moved = COLOR_MOVED_PLAIN;
+		options->output_indicators[OI_MOVED_NEW] = "*";
+		options->output_indicators[OI_MOVED_OLD] = "~";
 	} else if (skip_to_optional_arg_default(arg, "--color-words", &options->word_regex, NULL)) {
 		options->use_color = 1;
 		options->word_diff = DIFF_WORDS_COLOR;
diff --git a/diff.h b/diff.h
index 0dd1651dda4..0058602c849 100644
--- a/diff.h
+++ b/diff.h
@@ -197,7 +197,9 @@ struct diff_options {
 #define OI_NEW 0
 #define OI_OLD 1
 #define OI_CONTEXT 2
-	const char *output_indicators[3];
+#define OI_MOVED_NEW 3
+#define OI_MOVED_OLD 4
+	const char *output_indicators[5];
 
 	struct pathspec pathspec;
 	pathchange_fn_t pathchange;
@@ -211,6 +213,7 @@ struct diff_options {
 
 	int diff_path_counter;
 
+	unsigned color_moved : 1;
 	struct emitted_diff_symbols *emitted_symbols;
 	enum {
 		COLOR_MOVED_NO = 0,
-- 
2.18.0.597.ga71716f1ad-goog
Previous: Stefan BellerNext: Junio C Hamano
Message 8 of 13 in “improve range-diffs coloring and [RFC] move detection”
  1. 0/7 improve range-diffs coloring and [RFC] move detectionStefan Beller, Aug 4, 2018
  2. 1/7 diff.c: emit_line_0 to take string instead of first signStefan Beller, Aug 4, 2018
  3. 2/7 diff.c: add --output-indicator-{new, old, context}Stefan Beller, Aug 4, 2018
  4. 3/7 range-diff: make use of different output indicatorsStefan Beller, Aug 4, 2018
  5. 4/7 range-diff: indent special lines as contextStefan Beller, Aug 4, 2018
  6. 5/7 diff.c: rename color_moved to markup_movedStefan Beller, Aug 4, 2018
  7. 6/7 diff.c: factor determine_line_color out of emit_diff_symbol_from_structStefan Beller, Aug 4, 2018
  8. 7/7 diff/am: enhance diff format to use */~ for moved linesStefan Beller, Aug 4, 2018
  9. Junio C HamanoAug 4, 2018
  10. Stefan BellerAug 6, 2018
  11. Junio C HamanoAug 4, 2018
  12. Stefan BellerAug 6, 2018
  13. Junio C HamanoAug 6, 2018

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.