Skip to content

Commit 840383b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a941d5e commit 840383b

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

diff.c

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ static char diff_colors[][COLOR_MAXLEN] = {
4343
};
4444

4545
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);
4748

4849
static int parse_diff_color_slot(const char *var, int ofs)
4950
{
@@ -477,7 +478,7 @@ static void emit_rewrite_diff(const char *name_a,
477478
const char *reset = diff_get_color(color_diff, DIFF_RESET);
478479
static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
479480
const char *a_prefix, *b_prefix;
480-
const char *data_one, *data_two;
481+
char *data_one, *data_two;
481482
size_t size_one, size_two;
482483
struct emit_callback ecbdata;
483484

@@ -499,26 +500,8 @@ static void emit_rewrite_diff(const char *name_a,
499500
quote_two_c_style(&a_name, a_prefix, name_a, 0);
500501
quote_two_c_style(&b_name, b_prefix, name_b, 0);
501502

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);
522505

523506
memset(&ecbdata, 0, sizeof(ecbdata));
524507
ecbdata.color_diff = color_diff;
@@ -1717,20 +1700,8 @@ static void builtin_diff(const char *name_a,
17171700
strbuf_reset(&header);
17181701
}
17191702

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);
17341705

17351706
pe = diff_funcname_pattern(one);
17361707
if (!pe)
@@ -3916,3 +3887,26 @@ static char *run_textconv(const char *pgm, struct diff_filespec *spec,
39163887

39173888
return strbuf_detach(&buf, outsize);
39183889
}
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+
}

0 commit comments

Comments
 (0)