@matheusbsbs / matheus-bagliano / commits / 840383b2c2
Fork 0

textconv: refactor calls to run_textconv

This patch adds a fill_textconv wrapper, which centralizes some minor logic like error checking and handling the case of no-textconv. In addition to dropping the number of lines, this will make it easier in future patches to handle multiple types of textconv. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 1, 2010 at 20:09 UTC 840383b2c2bd7179604f5c2595bf95e22a4e0c84
1 file changed +30 -36
diff.c
+30 -36
@@ -43,7 +43,8 @@ static char diff_colors[][COLOR_MAXLEN] = {
43 };
44
45 static void diff_filespec_load_driver(struct diff_filespec *one);
46 -static char *run_textconv(const char *, struct diff_filespec *, size_t *);
46 +static size_t fill_textconv(const char *cmd,
47 + struct diff_filespec *df, char **outbuf);
48
49 static int parse_diff_color_slot(const char *var, int ofs)
50 {
@@ -477,7 +478,7 @@ static void emit_rewrite_diff(const char *name_a,
478 const char *reset = diff_get_color(color_diff, DIFF_RESET);
479 static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
480 const char *a_prefix, *b_prefix;
480 - const char *data_one, *data_two;
481 + char *data_one, *data_two;
482 size_t size_one, size_two;
483 struct emit_callback ecbdata;
484
@@ -499,26 +500,8 @@ static void emit_rewrite_diff(const char *name_a,
500 quote_two_c_style(&a_name, a_prefix, name_a, 0);
501 quote_two_c_style(&b_name, b_prefix, name_b, 0);
502
502 - diff_populate_filespec(one, 0);
503 - diff_populate_filespec(two, 0);
504 - if (textconv_one) {
505 - data_one = run_textconv(textconv_one, one, &size_one);
506 - if (!data_one)
507 - die("unable to read files to diff");
508 - }
509 - else {
510 - data_one = one->data;
511 - size_one = one->size;
512 - }
513 - if (textconv_two) {
514 - data_two = run_textconv(textconv_two, two, &size_two);
515 - if (!data_two)
516 - die("unable to read files to diff");
517 - }
518 - else {
519 - data_two = two->data;
520 - size_two = two->size;
521 - }
503 + size_one = fill_textconv(textconv_one, one, &data_one);
504 + size_two = fill_textconv(textconv_two, two, &data_two);
505
506 memset(&ecbdata, 0, sizeof(ecbdata));
507 ecbdata.color_diff = color_diff;
@@ -1717,20 +1700,8 @@ static void builtin_diff(const char *name_a,
1700 strbuf_reset(&header);
1701 }
1702
1720 - if (textconv_one) {
1721 - size_t size;
1722 - mf1.ptr = run_textconv(textconv_one, one, &size);
1723 - if (!mf1.ptr)
1724 - die("unable to read files to diff");
1725 - mf1.size = size;
1726 - }
1727 - if (textconv_two) {
1728 - size_t size;
1729 - mf2.ptr = run_textconv(textconv_two, two, &size);
1730 - if (!mf2.ptr)
1731 - die("unable to read files to diff");
1732 - mf2.size = size;
1733 - }
1703 + mf1.size = fill_textconv(textconv_one, one, &mf1.ptr);
1704 + mf2.size = fill_textconv(textconv_two, two, &mf2.ptr);
1705
1706 pe = diff_funcname_pattern(one);
1707 if (!pe)
@@ -3916,3 +3887,26 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
3887
3888 return strbuf_detach(&buf, outsize);
3889 }
3890 +
3891 +static size_t fill_textconv(const char *cmd,
3892 + struct diff_filespec *df,
3893 + char **outbuf)
3894 +{
3895 + size_t size;
3896 +
3897 + if (!cmd) {
3898 + if (!DIFF_FILE_VALID(df)) {
3899 + *outbuf = "";
3900 + return 0;
3901 + }
3902 + if (diff_populate_filespec(df, 0))
3903 + die("unable to read files to diff");
3904 + *outbuf = df->data;
3905 + return df->size;
3906 + }
3907 +
3908 + *outbuf = run_textconv(cmd, df, &size);
3909 + if (!*outbuf)
3910 + die("unable to read files to diff");
3911 + return size;
3912 +}