Skip to content

Commit 54443bb

Browse files
rscharfegitster
authored andcommitted
userdiff: add and use struct external_diff
Wrap the string specifying the external diff command in a new struct to simplify adding attributes, which the next patch will do. Make sure external_diff() still returns NULL if neither the environment variable GIT_EXTERNAL_DIFF nor the configuration option diff.external is set, to continue allowing its use in a boolean context. Use a designated initializer for the default builtin userdiff driver to adjust to the type change of the second struct member. Spelling out only the non-zero members improves readability as a nice side-effect. No functional change intended. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33be6cf commit 54443bb

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

diff.c

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static int diff_color_moved_ws_default;
5757
static int diff_context_default = 3;
5858
static int diff_interhunk_context_default;
5959
static char *diff_word_regex_cfg;
60-
static char *external_diff_cmd_cfg;
60+
static struct external_diff external_diff_cfg;
6161
static char *diff_order_file_cfg;
6262
int diff_auto_refresh_index = 1;
6363
static int diff_mnemonic_prefix;
@@ -431,7 +431,7 @@ int git_diff_ui_config(const char *var, const char *value,
431431
return 0;
432432
}
433433
if (!strcmp(var, "diff.external"))
434-
return git_config_string(&external_diff_cmd_cfg, var, value);
434+
return git_config_string(&external_diff_cfg.cmd, var, value);
435435
if (!strcmp(var, "diff.wordregex"))
436436
return git_config_string(&diff_word_regex_cfg, var, value);
437437
if (!strcmp(var, "diff.orderfile"))
@@ -548,18 +548,20 @@ static char *quote_two(const char *one, const char *two)
548548
return strbuf_detach(&res, NULL);
549549
}
550550

551-
static const char *external_diff(void)
551+
static const struct external_diff *external_diff(void)
552552
{
553-
static const char *external_diff_cmd = NULL;
553+
static struct external_diff external_diff_env, *external_diff_ptr;
554554
static int done_preparing = 0;
555555

556556
if (done_preparing)
557-
return external_diff_cmd;
558-
external_diff_cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
559-
if (!external_diff_cmd)
560-
external_diff_cmd = external_diff_cmd_cfg;
557+
return external_diff_ptr;
558+
external_diff_env.cmd = xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
559+
if (external_diff_env.cmd)
560+
external_diff_ptr = &external_diff_env;
561+
else if (external_diff_cfg.cmd)
562+
external_diff_ptr = &external_diff_cfg;
561563
done_preparing = 1;
562-
return external_diff_cmd;
564+
return external_diff_ptr;
563565
}
564566

565567
/*
@@ -4375,7 +4377,7 @@ static void add_external_diff_name(struct repository *r,
43754377
* infile2 infile2-sha1 infile2-mode [ rename-to ]
43764378
*
43774379
*/
4378-
static void run_external_diff(const char *pgm,
4380+
static void run_external_diff(const struct external_diff *pgm,
43794381
const char *name,
43804382
const char *other,
43814383
struct diff_filespec *one,
@@ -4386,7 +4388,7 @@ static void run_external_diff(const char *pgm,
43864388
struct child_process cmd = CHILD_PROCESS_INIT;
43874389
struct diff_queue_struct *q = &diff_queued_diff;
43884390

4389-
strvec_push(&cmd.args, pgm);
4391+
strvec_push(&cmd.args, pgm->cmd);
43904392
strvec_push(&cmd.args, name);
43914393

43924394
if (one && two) {
@@ -4512,7 +4514,7 @@ static void fill_metainfo(struct strbuf *msg,
45124514
}
45134515
}
45144516

4515-
static void run_diff_cmd(const char *pgm,
4517+
static void run_diff_cmd(const struct external_diff *pgm,
45164518
const char *name,
45174519
const char *other,
45184520
const char *attr_path,
@@ -4530,8 +4532,8 @@ static void run_diff_cmd(const char *pgm,
45304532
if (o->flags.allow_external || !o->ignore_driver_algorithm)
45314533
drv = userdiff_find_by_path(o->repo->index, attr_path);
45324534

4533-
if (o->flags.allow_external && drv && drv->external)
4534-
pgm = drv->external;
4535+
if (o->flags.allow_external && drv && drv->external.cmd)
4536+
pgm = &drv->external;
45354537

45364538
if (msg) {
45374539
/*
@@ -4597,7 +4599,7 @@ static void strip_prefix(int prefix_length, const char **namep, const char **oth
45974599

45984600
static void run_diff(struct diff_filepair *p, struct diff_options *o)
45994601
{
4600-
const char *pgm = external_diff();
4602+
const struct external_diff *pgm = external_diff();
46014603
struct strbuf msg;
46024604
struct diff_filespec *one = p->one;
46034605
struct diff_filespec *two = p->two;

userdiff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ PATTERNS("scheme",
333333
"|([^][)(}{[ \t])+"),
334334
PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
335335
"\\\\[a-zA-Z@]+|\\\\.|([a-zA-Z0-9]|[^\x01-\x7f])+"),
336-
{ "default", NULL, NULL, -1, { NULL, 0 } },
336+
{ .name = "default", .binary = -1 },
337337
};
338338
#undef PATTERNS
339339
#undef IPATTERN
@@ -445,7 +445,7 @@ int userdiff_config(const char *k, const char *v)
445445
if (!strcmp(type, "binary"))
446446
return parse_tristate(&drv->binary, k, v);
447447
if (!strcmp(type, "command"))
448-
return git_config_string(&drv->external, k, v);
448+
return git_config_string(&drv->external.cmd, k, v);
449449
if (!strcmp(type, "textconv"))
450450
return git_config_string(&drv->textconv, k, v);
451451
if (!strcmp(type, "cachetextconv"))

userdiff.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ struct userdiff_funcname {
1111
int cflags;
1212
};
1313

14+
struct external_diff {
15+
char *cmd;
16+
};
17+
1418
struct userdiff_driver {
1519
const char *name;
16-
char *external;
20+
struct external_diff external;
1721
char *algorithm;
1822
int binary;
1923
struct userdiff_funcname funcname;

0 commit comments

Comments
 (0)