diff.c: add dimming to moved line detection
Any lines inside a moved block of code are not interesting. Boundaries of blocks are only interesting if they are next to another block of moved code. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Jun 30, 2017 at 13:53 UTC
86b452e2769e80b220327b1301c7487ad4afa4f4
4 files changed
+254
-13
color.h
+2
@@ -42,6 +42,8 @@ struct strbuf;
42
#define GIT_COLOR_BG_BLUE "\033[44m"
43
#define GIT_COLOR_BG_MAGENTA "\033[45m"
44
#define GIT_COLOR_BG_CYAN "\033[46m"
45
+#define GIT_COLOR_FAINT "\033[2m"
46
+#define GIT_COLOR_FAINT_ITALIC "\033[2;3m"
47
48
/* A special value meaning "no color selected" */
49
#define GIT_COLOR_NIL "NIL"
diff.c
+121
-11
@@ -58,10 +58,14 @@ static char diff_colors[][COLOR_MAXLEN] = {
58
GIT_COLOR_YELLOW, /* COMMIT */
59
GIT_COLOR_BG_RED, /* WHITESPACE */
60
GIT_COLOR_NORMAL, /* FUNCINFO */
61
- GIT_COLOR_MAGENTA, /* OLD_MOVED */
62
- GIT_COLOR_BLUE, /* OLD_MOVED ALTERNATIVE */
63
- GIT_COLOR_CYAN, /* NEW_MOVED */
64
- GIT_COLOR_YELLOW, /* NEW_MOVED ALTERNATIVE */
61
+ GIT_COLOR_BOLD_MAGENTA, /* OLD_MOVED */
62
+ GIT_COLOR_BOLD_BLUE, /* OLD_MOVED ALTERNATIVE */
63
+ GIT_COLOR_FAINT, /* OLD_MOVED_DIM */
64
+ GIT_COLOR_FAINT_ITALIC, /* OLD_MOVED_ALTERNATIVE_DIM */
65
+ GIT_COLOR_BOLD_CYAN, /* NEW_MOVED */
66
+ GIT_COLOR_BOLD_YELLOW, /* NEW_MOVED ALTERNATIVE */
67
+ GIT_COLOR_FAINT, /* NEW_MOVED_DIM */
68
+ GIT_COLOR_FAINT_ITALIC, /* NEW_MOVED_ALTERNATIVE_DIM */
69
};
70
71
static NORETURN void die_want_option(const char *option_name)
@@ -91,10 +95,18 @@ static int parse_diff_color_slot(const char *var)
95
return DIFF_FILE_OLD_MOVED;
96
if (!strcasecmp(var, "oldmovedalternative"))
97
return DIFF_FILE_OLD_MOVED_ALT;
98
+ if (!strcasecmp(var, "oldmoveddimmed"))
99
+ return DIFF_FILE_OLD_MOVED_DIM;
100
+ if (!strcasecmp(var, "oldmovedalternativedimmed"))
101
+ return DIFF_FILE_OLD_MOVED_ALT_DIM;
102
if (!strcasecmp(var, "newmoved"))
103
return DIFF_FILE_NEW_MOVED;
104
if (!strcasecmp(var, "newmovedalternative"))
105
return DIFF_FILE_NEW_MOVED_ALT;
106
+ if (!strcasecmp(var, "newmoveddimmed"))
107
+ return DIFF_FILE_NEW_MOVED_DIM;
108
+ if (!strcasecmp(var, "newmovedalternativedimmed"))
109
+ return DIFF_FILE_NEW_MOVED_ALT_DIM;
110
return -1;
111
}
112
@@ -262,8 +274,10 @@ static int parse_color_moved(const char *arg)
274
return COLOR_MOVED_ZEBRA;
275
else if (!strcmp(arg, "default"))
276
return COLOR_MOVED_DEFAULT;
277
+ else if (!strcmp(arg, "dimmed_zebra"))
278
+ return COLOR_MOVED_ZEBRA_DIM;
279
else
266
- return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'plain'"));
280
+ return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
281
}
282
283
int git_diff_ui_config(const char *var, const char *value, void *cb)
@@ -649,6 +663,7 @@ enum diff_symbol {
663
#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
664
#define DIFF_SYMBOL_MOVED_LINE (1<<17)
665
#define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
666
+#define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
667
#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
668
669
/*
@@ -933,6 +948,67 @@ static void mark_color_as_moved(struct diff_options *o,
948
free(pmb);
949
}
950
951
+#define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
952
+ (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
953
+static void dim_moved_lines(struct diff_options *o)
954
+{
955
+ int n;
956
+ for (n = 0; n < o->emitted_symbols->nr; n++) {
957
+ struct emitted_diff_symbol *prev = (n != 0) ?
958
+ &o->emitted_symbols->buf[n - 1] : NULL;
959
+ struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
960
+ struct emitted_diff_symbol *next =
961
+ (n < o->emitted_symbols->nr - 1) ?
962
+ &o->emitted_symbols->buf[n + 1] : NULL;
963
+
964
+ /* Not a plus or minus line? */
965
+ if (l->s != DIFF_SYMBOL_PLUS && l->s != DIFF_SYMBOL_MINUS)
966
+ continue;
967
+
968
+ /* Not a moved line? */
969
+ if (!(l->flags & DIFF_SYMBOL_MOVED_LINE))
970
+ continue;
971
+
972
+ /*
973
+ * If prev or next are not a plus or minus line,
974
+ * pretend they don't exist
975
+ */
976
+ if (prev && prev->s != DIFF_SYMBOL_PLUS &&
977
+ prev->s != DIFF_SYMBOL_MINUS)
978
+ prev = NULL;
979
+ if (next && next->s != DIFF_SYMBOL_PLUS &&
980
+ next->s != DIFF_SYMBOL_MINUS)
981
+ next = NULL;
982
+
983
+ /* Inside a block? */
984
+ if ((prev &&
985
+ (prev->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
986
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK)) &&
987
+ (next &&
988
+ (next->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK) ==
989
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK))) {
990
+ l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
991
+ continue;
992
+ }
993
+
994
+ /* Check if we are at an interesting bound: */
995
+ if (prev && (prev->flags & DIFF_SYMBOL_MOVED_LINE) &&
996
+ (prev->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
997
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
998
+ continue;
999
+ if (next && (next->flags & DIFF_SYMBOL_MOVED_LINE) &&
1000
+ (next->flags & DIFF_SYMBOL_MOVED_LINE_ALT) !=
1001
+ (l->flags & DIFF_SYMBOL_MOVED_LINE_ALT))
1002
+ continue;
1003
+
1004
+ /*
1005
+ * The boundary to prev and next are not interesting,
1006
+ * so this line is not interesting as a whole
1007
+ */
1008
+ l->flags |= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING;
1009
+ }
1010
+}
1011
+
1012
static void emit_line_ws_markup(struct diff_options *o,
1013
const char *set, const char *reset,
1014
const char *line, int len, char sign,
@@ -1007,24 +1083,56 @@ static void emit_diff_symbol_from_struct(struct diff_options *o,
1083
flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
1084
break;
1085
case DIFF_SYMBOL_PLUS:
1010
- if (flags & DIFF_SYMBOL_MOVED_LINE_ALT)
1086
+ switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1087
+ DIFF_SYMBOL_MOVED_LINE_ALT |
1088
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1089
+ case DIFF_SYMBOL_MOVED_LINE |
1090
+ DIFF_SYMBOL_MOVED_LINE_ALT |
1091
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1092
+ set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT_DIM);
1093
+ break;
1094
+ case DIFF_SYMBOL_MOVED_LINE |
1095
+ DIFF_SYMBOL_MOVED_LINE_ALT:
1096
set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_ALT);
1012
- else if (flags & DIFF_SYMBOL_MOVED_LINE)
1097
+ break;
1098
+ case DIFF_SYMBOL_MOVED_LINE |
1099
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1100
+ set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED_DIM);
1101
+ break;
1102
+ case DIFF_SYMBOL_MOVED_LINE:
1103
set = diff_get_color_opt(o, DIFF_FILE_NEW_MOVED);
1014
- else
1104
+ break;
1105
+ default:
1106
set = diff_get_color_opt(o, DIFF_FILE_NEW);
1107
+ }
1108
reset = diff_get_color_opt(o, DIFF_RESET);
1109
emit_line_ws_markup(o, set, reset, line, len, '+',
1110
flags & DIFF_SYMBOL_CONTENT_WS_MASK,
1111
flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
1112
break;
1113
case DIFF_SYMBOL_MINUS:
1022
- if (flags & DIFF_SYMBOL_MOVED_LINE_ALT)
1114
+ switch (flags & (DIFF_SYMBOL_MOVED_LINE |
1115
+ DIFF_SYMBOL_MOVED_LINE_ALT |
1116
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING)) {
1117
+ case DIFF_SYMBOL_MOVED_LINE |
1118
+ DIFF_SYMBOL_MOVED_LINE_ALT |
1119
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1120
+ set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT_DIM);
1121
+ break;
1122
+ case DIFF_SYMBOL_MOVED_LINE |
1123
+ DIFF_SYMBOL_MOVED_LINE_ALT:
1124
set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_ALT);
1024
- else if (flags & DIFF_SYMBOL_MOVED_LINE)
1125
+ break;
1126
+ case DIFF_SYMBOL_MOVED_LINE |
1127
+ DIFF_SYMBOL_MOVED_LINE_UNINTERESTING:
1128
+ set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED_DIM);
1129
+ break;
1130
+ case DIFF_SYMBOL_MOVED_LINE:
1131
set = diff_get_color_opt(o, DIFF_FILE_OLD_MOVED);
1026
- else
1132
+ break;
1133
+ default:
1134
set = diff_get_color_opt(o, DIFF_FILE_OLD);
1135
+ }
1136
reset = diff_get_color_opt(o, DIFF_RESET);
1137
emit_line_ws_markup(o, set, reset, line, len, '-',
1138
flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
@@ -5420,6 +5528,8 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
5528
5529
add_lines_to_move_detection(o, &add_lines, &del_lines);
5530
mark_color_as_moved(o, &add_lines, &del_lines);
5531
+ if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
5532
+ dim_moved_lines(o);
5533
5534
hashmap_free(&add_lines, 0);
5535
hashmap_free(&del_lines, 0);
diff.h
+7
-2
@@ -192,6 +192,7 @@ struct diff_options {
192
COLOR_MOVED_NO = 0,
193
COLOR_MOVED_PLAIN = 1,
194
COLOR_MOVED_ZEBRA = 2,
195
+ COLOR_MOVED_ZEBRA_DIM = 3,
196
} color_moved;
197
#define COLOR_MOVED_DEFAULT COLOR_MOVED_ZEBRA
198
#define COLOR_MOVED_MIN_BLOCK_LENGTH 3
@@ -218,8 +219,12 @@ enum color_diff {
219
DIFF_FUNCINFO = 8,
220
DIFF_FILE_OLD_MOVED = 9,
221
DIFF_FILE_OLD_MOVED_ALT = 10,
221
- DIFF_FILE_NEW_MOVED = 11,
222
- DIFF_FILE_NEW_MOVED_ALT = 12
222
+ DIFF_FILE_OLD_MOVED_DIM = 11,
223
+ DIFF_FILE_OLD_MOVED_ALT_DIM = 12,
224
+ DIFF_FILE_NEW_MOVED = 13,
225
+ DIFF_FILE_NEW_MOVED_ALT = 14,
226
+ DIFF_FILE_NEW_MOVED_DIM = 15,
227
+ DIFF_FILE_NEW_MOVED_ALT_DIM = 16
228
};
229
const char *diff_get_color(int diff_use_color, enum color_diff ix);
230
#define diff_get_color_opt(o, ix) \
t/t4015-diff-whitespace.sh
+124
@@ -1179,6 +1179,130 @@ test_expect_success 'plain moved code, inside file' '
1179
test_cmp expected actual
1180
'
1181
1182
+test_expect_success 'detect permutations inside moved code -- dimmed_zebra' '
1183
+ git reset --hard &&
1184
+ cat <<-\EOF >lines.txt &&
1185
+ line 1
1186
+ line 2
1187
+ line 3
1188
+ line 4
1189
+ line 5
1190
+ line 6
1191
+ line 7
1192
+ line 8
1193
+ line 9
1194
+ line 10
1195
+ line 11
1196
+ line 12
1197
+ line 13
1198
+ line 14
1199
+ line 15
1200
+ line 16
1201
+ EOF
1202
+ git add lines.txt &&
1203
+ git commit -m "add poetry" &&
1204
+ cat <<-\EOF >lines.txt &&
1205
+ line 4
1206
+ line 5
1207
+ line 6
1208
+ line 7
1209
+ line 8
1210
+ line 9
1211
+ line 1
1212
+ line 2
1213
+ line 3
1214
+ line 14
1215
+ line 15
1216
+ line 16
1217
+ line 10
1218
+ line 11
1219
+ line 12
1220
+ line 13
1221
+ EOF
1222
+ test_config color.diff.oldMoved "magenta" &&
1223
+ test_config color.diff.newMoved "cyan" &&
1224
+ test_config color.diff.oldMovedAlternative "blue" &&
1225
+ test_config color.diff.newMovedAlternative "yellow" &&
1226
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
1227
+ test_config color.diff.newMovedDimmed "normal cyan" &&
1228
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
1229
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
1230
+ git diff HEAD --no-renames --color-moved=dimmed_zebra| test_decode_color >actual &&
1231
+ cat <<-\EOF >expected &&
1232
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
1233
+ <BOLD>index 47ea9c3..ba96a38 100644<RESET>
1234
+ <BOLD>--- a/lines.txt<RESET>
1235
+ <BOLD>+++ b/lines.txt<RESET>
1236
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
1237
+ <BMAGENTA>-line 1<RESET>
1238
+ <BMAGENTA>-line 2<RESET>
1239
+ <BMAGENTA>-line 3<RESET>
1240
+ line 4<RESET>
1241
+ line 5<RESET>
1242
+ line 6<RESET>
1243
+ line 7<RESET>
1244
+ line 8<RESET>
1245
+ line 9<RESET>
1246
+ <BCYAN>+<RESET><BCYAN>line 1<RESET>
1247
+ <BCYAN>+<RESET><BCYAN>line 2<RESET>
1248
+ <CYAN>+<RESET><CYAN>line 3<RESET>
1249
+ <YELLOW>+<RESET><YELLOW>line 14<RESET>
1250
+ <BYELLOW>+<RESET><BYELLOW>line 15<RESET>
1251
+ <BYELLOW>+<RESET><BYELLOW>line 16<RESET>
1252
+ line 10<RESET>
1253
+ line 11<RESET>
1254
+ line 12<RESET>
1255
+ line 13<RESET>
1256
+ <BMAGENTA>-line 14<RESET>
1257
+ <BMAGENTA>-line 15<RESET>
1258
+ <BMAGENTA>-line 16<RESET>
1259
+ EOF
1260
+ test_cmp expected actual
1261
+'
1262
+
1263
+test_expect_success 'cmd option assumes configured colored-moved' '
1264
+ test_config color.diff.oldMoved "magenta" &&
1265
+ test_config color.diff.newMoved "cyan" &&
1266
+ test_config color.diff.oldMovedAlternative "blue" &&
1267
+ test_config color.diff.newMovedAlternative "yellow" &&
1268
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
1269
+ test_config color.diff.newMovedDimmed "normal cyan" &&
1270
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
1271
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
1272
+ test_config diff.colorMoved zebra &&
1273
+ git diff HEAD --no-renames --color-moved| test_decode_color >actual &&
1274
+ cat <<-\EOF >expected &&
1275
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
1276
+ <BOLD>index 47ea9c3..ba96a38 100644<RESET>
1277
+ <BOLD>--- a/lines.txt<RESET>
1278
+ <BOLD>+++ b/lines.txt<RESET>
1279
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
1280
+ <MAGENTA>-line 1<RESET>
1281
+ <MAGENTA>-line 2<RESET>
1282
+ <MAGENTA>-line 3<RESET>
1283
+ line 4<RESET>
1284
+ line 5<RESET>
1285
+ line 6<RESET>
1286
+ line 7<RESET>
1287
+ line 8<RESET>
1288
+ line 9<RESET>
1289
+ <CYAN>+<RESET><CYAN>line 1<RESET>
1290
+ <CYAN>+<RESET><CYAN>line 2<RESET>
1291
+ <CYAN>+<RESET><CYAN>line 3<RESET>
1292
+ <YELLOW>+<RESET><YELLOW>line 14<RESET>
1293
+ <YELLOW>+<RESET><YELLOW>line 15<RESET>
1294
+ <YELLOW>+<RESET><YELLOW>line 16<RESET>
1295
+ line 10<RESET>
1296
+ line 11<RESET>
1297
+ line 12<RESET>
1298
+ line 13<RESET>
1299
+ <MAGENTA>-line 14<RESET>
1300
+ <MAGENTA>-line 15<RESET>
1301
+ <MAGENTA>-line 16<RESET>
1302
+ EOF
1303
+ test_cmp expected actual
1304
+'
1305
+
1306
test_expect_success 'no effect from --color-moved with --word-diff' '
1307
cat <<-\EOF >text.txt &&
1308
Lorem Ipsum is simply dummy text of the printing and typesetting industry.