diff.c: migrate emit_line_checked to use emit_diff_symbol
Add a new flags field to emit_diff_symbol, that will be used by context lines for: * white space rules that are applicable (The first 12 bits) Take a note in cahe.c as well, when this ws rules are extended we have to fix the bits in the flags field. * how the rules are evaluated (actually this double encodes the sign of the line, but the code is easier to keep this way, bits 13,14,15) * if the line a blank line at EOF (bit 16) The check if new lines need to be marked up as extra lines at the end of file, is now done unconditionally. That should be ok, as 'new_blank_line_at_eof' has a quick early return. Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Stefan Beller committed
Jun 29, 2017 at 17:06 UTC
091f8e28b452bf800e7abb8dace49ebd8897faac
3 files changed
+80
-44
cache.h
+2
@@ -1996,6 +1996,8 @@ void shift_tree_by(const struct object_id *, const struct object_id *, struct ob
1996
#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
1997
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
1998
#define WS_TAB_WIDTH_MASK 077
1999
+/* All WS_* -- when extended, adapt diff.c emit_symbol */
2000
+#define WS_RULE_MASK 07777
2001
extern unsigned whitespace_rule_cfg;
2002
extern unsigned whitespace_rule(const char *);
2003
extern unsigned parse_whitespace_rule(const char *);
diff.c
+75
-41
@@ -561,17 +561,54 @@ static void emit_line(struct diff_options *o, const char *set, const char *reset
561
}
562
563
enum diff_symbol {
564
+ DIFF_SYMBOL_CONTEXT,
565
+ DIFF_SYMBOL_PLUS,
566
+ DIFF_SYMBOL_MINUS,
567
DIFF_SYMBOL_NO_LF_EOF,
568
DIFF_SYMBOL_CONTEXT_FRAGINFO,
569
DIFF_SYMBOL_CONTEXT_MARKER,
570
DIFF_SYMBOL_SEPARATOR
571
};
572
+/*
573
+ * Flags for content lines:
574
+ * 0..12 are whitespace rules
575
+ * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
576
+ * 16 is marking if the line is blank at EOF
577
+ */
578
+#define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
579
+#define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
580
+
581
+static void emit_line_ws_markup(struct diff_options *o,
582
+ const char *set, const char *reset,
583
+ const char *line, int len, char sign,
584
+ unsigned ws_rule, int blank_at_eof)
585
+{
586
+ const char *ws = NULL;
587
+
588
+ if (o->ws_error_highlight & ws_rule) {
589
+ ws = diff_get_color_opt(o, DIFF_WHITESPACE);
590
+ if (!*ws)
591
+ ws = NULL;
592
+ }
593
+
594
+ if (!ws)
595
+ emit_line_0(o, set, reset, sign, line, len);
596
+ else if (blank_at_eof)
597
+ /* Blank line at EOF - paint '+' as well */
598
+ emit_line_0(o, ws, reset, sign, line, len);
599
+ else {
600
+ /* Emit just the prefix, then the rest. */
601
+ emit_line_0(o, set, reset, sign, "", 0);
602
+ ws_check_emit(line, len, ws_rule,
603
+ o->file, set, reset, ws);
604
+ }
605
+}
606
607
static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
571
- const char *line, int len)
608
+ const char *line, int len, unsigned flags)
609
{
610
static const char *nneof = " No newline at end of file\n";
574
- const char *context, *reset;
611
+ const char *context, *reset, *set;
612
switch (s) {
613
case DIFF_SYMBOL_NO_LF_EOF:
614
context = diff_get_color_opt(o, DIFF_CONTEXT);
@@ -593,6 +630,25 @@ static void emit_diff_symbol(struct diff_options *o, enum diff_symbol s,
630
diff_line_prefix(o),
631
o->line_termination);
632
break;
633
+ case DIFF_SYMBOL_CONTEXT:
634
+ set = diff_get_color_opt(o, DIFF_CONTEXT);
635
+ reset = diff_get_color_opt(o, DIFF_RESET);
636
+ emit_line_ws_markup(o, set, reset, line, len, ' ',
637
+ flags & (DIFF_SYMBOL_CONTENT_WS_MASK), 0);
638
+ break;
639
+ case DIFF_SYMBOL_PLUS:
640
+ set = diff_get_color_opt(o, DIFF_FILE_NEW);
641
+ reset = diff_get_color_opt(o, DIFF_RESET);
642
+ emit_line_ws_markup(o, set, reset, line, len, '+',
643
+ flags & DIFF_SYMBOL_CONTENT_WS_MASK,
644
+ flags & DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF);
645
+ break;
646
+ case DIFF_SYMBOL_MINUS:
647
+ set = diff_get_color_opt(o, DIFF_FILE_OLD);
648
+ reset = diff_get_color_opt(o, DIFF_RESET);
649
+ emit_line_ws_markup(o, set, reset, line, len, '-',
650
+ flags & DIFF_SYMBOL_CONTENT_WS_MASK, 0);
651
+ break;
652
default:
653
die("BUG: unknown diff symbol");
654
}
@@ -609,57 +665,31 @@ static int new_blank_line_at_eof(struct emit_callback *ecbdata, const char *line
665
return ws_blank_line(line, len, ecbdata->ws_rule);
666
}
667
612
-static void emit_line_checked(const char *reset,
613
- struct emit_callback *ecbdata,
614
- const char *line, int len,
615
- enum color_diff color,
616
- unsigned ws_error_highlight,
617
- char sign)
618
-{
619
- const char *set = diff_get_color(ecbdata->color_diff, color);
620
- const char *ws = NULL;
621
-
622
- if (ecbdata->opt->ws_error_highlight & ws_error_highlight) {
623
- ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
624
- if (!*ws)
625
- ws = NULL;
626
- }
627
-
628
- if (!ws)
629
- emit_line_0(ecbdata->opt, set, reset, sign, line, len);
630
- else if (sign == '+' && new_blank_line_at_eof(ecbdata, line, len))
631
- /* Blank line at EOF - paint '+' as well */
632
- emit_line_0(ecbdata->opt, ws, reset, sign, line, len);
633
- else {
634
- /* Emit just the prefix, then the rest. */
635
- emit_line_0(ecbdata->opt, set, reset, sign, "", 0);
636
- ws_check_emit(line, len, ecbdata->ws_rule,
637
- ecbdata->opt->file, set, reset, ws);
638
- }
639
-}
640
-
668
static void emit_add_line(const char *reset,
669
struct emit_callback *ecbdata,
670
const char *line, int len)
671
{
645
- emit_line_checked(reset, ecbdata, line, len,
646
- DIFF_FILE_NEW, WSEH_NEW, '+');
672
+ unsigned flags = WSEH_NEW | ecbdata->ws_rule;
673
+ if (new_blank_line_at_eof(ecbdata, line, len))
674
+ flags |= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF;
675
+
676
+ emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_PLUS, line, len, flags);
677
}
678
679
static void emit_del_line(const char *reset,
680
struct emit_callback *ecbdata,
681
const char *line, int len)
682
{
653
- emit_line_checked(reset, ecbdata, line, len,
654
- DIFF_FILE_OLD, WSEH_OLD, '-');
683
+ unsigned flags = WSEH_OLD | ecbdata->ws_rule;
684
+ emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_MINUS, line, len, flags);
685
}
686
687
static void emit_context_line(const char *reset,
688
struct emit_callback *ecbdata,
689
const char *line, int len)
690
{
661
- emit_line_checked(reset, ecbdata, line, len,
662
- DIFF_CONTEXT, WSEH_CONTEXT, ' ');
691
+ unsigned flags = WSEH_CONTEXT | ecbdata->ws_rule;
692
+ emit_diff_symbol(ecbdata->opt, DIFF_SYMBOL_CONTEXT, line, len, flags);
693
}
694
695
static void emit_hunk_header(struct emit_callback *ecbdata,
@@ -683,7 +713,7 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
713
memcmp(line, atat, 2) ||
714
!(ep = memmem(line + 2, len - 2, atat, 2))) {
715
emit_diff_symbol(ecbdata->opt,
686
- DIFF_SYMBOL_CONTEXT_MARKER, line, len);
716
+ DIFF_SYMBOL_CONTEXT_MARKER, line, len, 0);
717
return;
718
}
719
ep += 2; /* skip over @@ */
@@ -719,7 +749,7 @@ static void emit_hunk_header(struct emit_callback *ecbdata,
749
strbuf_add(&msgbuf, line + len, org_len - len);
750
strbuf_complete_line(&msgbuf);
751
emit_diff_symbol(ecbdata->opt,
722
- DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len);
752
+ DIFF_SYMBOL_CONTEXT_FRAGINFO, msgbuf.buf, msgbuf.len, 0);
753
strbuf_release(&msgbuf);
754
}
755
@@ -778,7 +808,7 @@ static void emit_rewrite_lines(struct emit_callback *ecb,
808
data += len;
809
}
810
if (!endp)
781
- emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0);
811
+ emit_diff_symbol(ecb->opt, DIFF_SYMBOL_NO_LF_EOF, NULL, 0, 0);
812
}
813
814
static void emit_rewrite_diff(const char *name_a,
@@ -4771,6 +4801,10 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
4801
{
4802
int i;
4803
struct diff_queue_struct *q = &diff_queued_diff;
4804
+
4805
+ if (WSEH_NEW & WS_RULE_MASK)
4806
+ die("BUG: WS rules bit mask overlaps with diff symbol flags");
4807
+
4808
for (i = 0; i < q->nr; i++) {
4809
struct diff_filepair *p = q->queue[i];
4810
if (check_pair_status(p))
@@ -4861,7 +4895,7 @@ void diff_flush(struct diff_options *options)
4895
4896
if (output_format & DIFF_FORMAT_PATCH) {
4897
if (separator) {
4864
- emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0);
4898
+ emit_diff_symbol(options, DIFF_SYMBOL_SEPARATOR, NULL, 0, 0);
4899
if (options->stat_sep) {
4900
/* attach patch instead of inline */
4901
fputs(options->stat_sep, options->file);
diff.h
+3
-3
@@ -148,9 +148,9 @@ struct diff_options {
148
int abbrev;
149
int ita_invisible_in_index;
150
/* white-space error highlighting */
151
-#define WSEH_NEW 1
152
-#define WSEH_CONTEXT 2
153
-#define WSEH_OLD 4
151
+#define WSEH_NEW (1<<12)
152
+#define WSEH_CONTEXT (1<<13)
153
+#define WSEH_OLD (1<<14)
154
unsigned ws_error_highlight;
155
const char *prefix;
156
int prefix_length;