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

Reply via email to