572
buffer->text.ptr[buffer->text.size] = '\0';
573
}
574
575
+struct diff_words_style_elem
576
+{
577
+ const char *prefix;
578
+ const char *suffix;
579
+ const char *color; /* NULL; filled in by the setup code if
580
+ * color is enabled */
581
+};
582
+
583
+struct diff_words_style
584
+{
585
+ enum diff_words_type type;
586
+ struct diff_words_style_elem new, old, ctx;
587
+ const char *newline;
588
+};
589
+
590
+struct diff_words_style diff_words_styles[] = {
591
+ { DIFF_WORDS_PORCELAIN, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
592
+ { DIFF_WORDS_PLAIN, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
593
+ { DIFF_WORDS_COLOR, {"", ""}, {"", ""}, {"", ""}, "\n" }
594
+};
595
+
596
struct diff_words_data {
597
struct diff_words_buffer minus, plus;
598
const char *current_plus;
599
FILE *file;
600
regex_t *word_regex;
601
+ enum diff_words_type type;
602
+ struct diff_words_style *style;
603
};
604
605
+static int fn_out_diff_words_write_helper(FILE *fp,
606
+ struct diff_words_style_elem *st_el,
607
+ const char *newline,
608
+ size_t count, const char *buf)
609
+{
610
+ while (count) {
611
+ char *p = memchr(buf, '\n', count);
612
+ if (p != buf) {
613
+ if (st_el->color && fputs(st_el->color, fp) < 0)
614
+ return -1;
615
+ if (fputs(st_el->prefix, fp) < 0 ||
616
+ fwrite(buf, p ? p - buf : count, 1, fp) != 1 ||
617
+ fputs(st_el->suffix, fp) < 0)
618
+ return -1;
619
+ if (st_el->color && *st_el->color
620
+ && fputs(GIT_COLOR_RESET, fp) < 0)
621
+ return -1;
622
+ }
623
+ if (!p)
624
+ return 0;
625
+ if (fputs(newline, fp) < 0)
626
+ return -1;
627
+ count -= p + 1 - buf;
628
+ buf = p + 1;
629
+ }
630
+ return 0;
631
+}
632
+
633
static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
634
{
635
struct diff_words_data *diff_words = priv;
636
+ struct diff_words_style *style = diff_words->style;
637
int minus_first, minus_len, plus_first, plus_len;
638
const char *minus_begin, *minus_end, *plus_begin, *plus_end;
639
657
plus_begin = plus_end = diff_words->plus.orig[plus_first].end;
658
659
if (diff_words->current_plus != plus_begin)
608
- fwrite(diff_words->current_plus,
609
- plus_begin - diff_words->current_plus, 1,
610
- diff_words->file);
660
+ fn_out_diff_words_write_helper(diff_words->file,
661
+ &style->ctx, style->newline,
662
+ plus_begin - diff_words->current_plus,
663
+ diff_words->current_plus);
664
if (minus_begin != minus_end)
612
- color_fwrite_lines(diff_words->file,
613
- diff_get_color(1, DIFF_FILE_OLD),
665
+ fn_out_diff_words_write_helper(diff_words->file,
666
+ &style->old, style->newline,
667
minus_end - minus_begin, minus_begin);
668
if (plus_begin != plus_end)
616
- color_fwrite_lines(diff_words->file,
617
- diff_get_color(1, DIFF_FILE_NEW),
669
+ fn_out_diff_words_write_helper(diff_words->file,
670
+ &style->new, style->newline,
671
plus_end - plus_begin, plus_begin);
672
673
diff_words->current_plus = plus_end;
750
xdemitconf_t xecfg;
751
xdemitcb_t ecb;
752
mmfile_t minus, plus;
753
+ struct diff_words_style *style = diff_words->style;
754
755
/* special case: only removal */
756
if (!diff_words->plus.text.size) {
703
- color_fwrite_lines(diff_words->file,
704
- diff_get_color(1, DIFF_FILE_OLD),
757
+ fn_out_diff_words_write_helper(diff_words->file,
758
+ &style->old, style->newline,
759
diff_words->minus.text.size, diff_words->minus.text.ptr);
760
diff_words->minus.text.size = 0;
761
return;
776
free(plus.ptr);
777
if (diff_words->current_plus != diff_words->plus.text.ptr +
778
diff_words->plus.text.size)
725
- fwrite(diff_words->current_plus,
779
+ fn_out_diff_words_write_helper(diff_words->file,
780
+ &style->ctx, style->newline,
781
diff_words->plus.text.ptr + diff_words->plus.text.size
727
- - diff_words->current_plus, 1,
728
- diff_words->file);
782
+ - diff_words->current_plus, diff_words->current_plus);
783
diff_words->minus.text.size = diff_words->plus.text.size = 0;
784
}
785
891
892
if (len < 1) {
893
emit_line(ecbdata->file, reset, reset, line, len);
894
+ if (ecbdata->diff_words
895
+ && ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN)
896
+ fputs("~\n", ecbdata->file);
897
return;
898
}
899
908
return;
909
}
910
diff_words_flush(ecbdata);
854
- line++;
855
- len--;
856
- emit_line(ecbdata->file, plain, reset, line, len);
911
+ if (ecbdata->diff_words->type == DIFF_WORDS_PORCELAIN) {
912
+ emit_line(ecbdata->file, plain, reset, line, len);
913
+ fputs("~\n", ecbdata->file);
914
+ } else {
915
+ /* don't print the prefix character */
916
+ emit_line(ecbdata->file, plain, reset, line+1, len-1);
917
+ }
918
return;
919
}
920
1816
xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1817
else if (!prefixcmp(diffopts, "-u"))
1818
xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1758
- if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1819
+ if (o->word_diff) {
1820
+ int i;
1821
+
1822
ecbdata.diff_words =
1823
xcalloc(1, sizeof(struct diff_words_data));
1824
ecbdata.diff_words->file = o->file;
1825
+ ecbdata.diff_words->type = o->word_diff;
1826
if (!o->word_regex)
1827
o->word_regex = userdiff_word_regex(one);
1828
if (!o->word_regex)
1838
die ("Invalid regular expression: %s",
1839
o->word_regex);
1840
}
1841
+ for (i = 0; i < ARRAY_SIZE(diff_words_styles); i++) {
1842
+ if (o->word_diff == diff_words_styles[i].type) {
1843
+ ecbdata.diff_words->style =
1844
+ &diff_words_styles[i];
1845
+ break;
1846
+ }
1847
+ }
1848
+ if (DIFF_OPT_TST(o, COLOR_DIFF)) {
1849
+ struct diff_words_style *st = ecbdata.diff_words->style;
1850
+ st->old.color = diff_get_color_opt(o, DIFF_FILE_OLD);
1851
+ st->new.color = diff_get_color_opt(o, DIFF_FILE_NEW);
1852
+ st->ctx.color = diff_get_color_opt(o, DIFF_PLAIN);
1853
+ }
1854
}
1855
xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
1856
&xpp, &xecfg, &ecb);
1780
- if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1857
+ if (o->word_diff)
1858
free_diff_words_data(&ecbdata);
1859
if (textconv_one)
1860
free(mf1.ptr);
2922
DIFF_OPT_CLR(options, COLOR_DIFF);
2923
else if (!strcmp(arg, "--color-words")) {
2924
DIFF_OPT_SET(options, COLOR_DIFF);
2848
- DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
2925
+ options->word_diff = DIFF_WORDS_COLOR;
2926
}
2927
else if (!prefixcmp(arg, "--color-words=")) {
2928
DIFF_OPT_SET(options, COLOR_DIFF);
2852
- DIFF_OPT_SET(options, COLOR_DIFF_WORDS);
2929
+ options->word_diff = DIFF_WORDS_COLOR;
2930
options->word_regex = arg + 14;
2931
}
2932
+ else if (!strcmp(arg, "--word-diff")) {
2933
+ if (options->word_diff == DIFF_WORDS_NONE)
2934
+ options->word_diff = DIFF_WORDS_PLAIN;
2935
+ }
2936
+ else if (!prefixcmp(arg, "--word-diff=")) {
2937
+ const char *type = arg + 12;
2938
+ if (!strcmp(type, "plain"))
2939
+ options->word_diff = DIFF_WORDS_PLAIN;
2940
+ else if (!strcmp(type, "color")) {
2941
+ DIFF_OPT_SET(options, COLOR_DIFF);
2942
+ options->word_diff = DIFF_WORDS_COLOR;
2943
+ }
2944
+ else if (!strcmp(type, "porcelain"))
2945
+ options->word_diff = DIFF_WORDS_PORCELAIN;
2946
+ else if (!strcmp(type, "none"))
2947
+ options->word_diff = DIFF_WORDS_NONE;
2948
+ else
2949
+ die("bad --word-diff argument: %s", type);
2950
+ }
2951
+ else if (!prefixcmp(arg, "--word-diff-regex=")) {
2952
+ if (options->word_diff == DIFF_WORDS_NONE)
2953
+ options->word_diff = DIFF_WORDS_PLAIN;
2954
+ options->word_regex = arg + 18;
2955
+ }
2956
else if (!strcmp(arg, "--exit-code"))
2957
DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2958
else if (!strcmp(arg, "--quiet"))