Skip to content

Commit 9631342

Browse files
Martin Ågrengitster
authored andcommitted
grep: use designated initializers for grep_defaults
In 15fabd1 ("builtin/grep.c: make configuration callback more reusable", 2012-10-09), we learned to fill a `static struct grep_opt grep_defaults` which we can use as a blueprint for other such structs. At the time, we didn't consider designated initializers to be widely useable, but these days, we do. (See, e.g., cbc0f81 ("strbuf: use designated initializers in STRBUF_INIT", 2017-07-10).) Use designated initializers to let the compiler set up the struct and so that we don't need to remember to call `init_grep_defaults()`. Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1d38787 commit 9631342

File tree

6 files changed

+26
-52
lines changed

6 files changed

+26
-52
lines changed

Documentation/MyFirstObjectWalk.txt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -388,17 +388,9 @@ Next, let's try to filter the commits we see based on their author. This is
388388
equivalent to running `git log --author=<pattern>`. We can add a filter by
389389
modifying `rev_info.grep_filter`, which is a `struct grep_opt`.
390390

391-
First some setup. Add `init_grep_defaults()` to `init_walken_defaults()` and add
392-
`grep_config()` to `git_walken_config()`:
391+
First some setup. Add `grep_config()` to `git_walken_config()`:
393392

394393
----
395-
static void init_walken_defaults(void)
396-
{
397-
init_grep_defaults();
398-
}
399-
400-
...
401-
402394
static int git_walken_config(const char *var, const char *value, void *cb)
403395
{
404396
grep_config(var, value, cb);

builtin/grep.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
950950
OPT_END()
951951
};
952952

953-
init_grep_defaults();
954953
git_config(grep_cmd_config, NULL);
955954
grep_init(&opt, the_repository, prefix);
956955

builtin/log.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ static int log_line_range_callback(const struct option *option, const char *arg,
131131

132132
static void init_log_defaults(void)
133133
{
134-
init_grep_defaults();
135134
init_diff_ui_defaults();
136135

137136
decoration_style = auto_decoration_style();

grep.c

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs);
1414
static int grep_source_is_binary(struct grep_source *gs,
1515
struct index_state *istate);
1616

17-
static struct grep_opt grep_defaults;
17+
static void std_output(struct grep_opt *opt, const void *buf, size_t size)
18+
{
19+
fwrite(buf, size, 1, stdout);
20+
}
21+
22+
static struct grep_opt grep_defaults = {
23+
.relative = 1,
24+
.pathname = 1,
25+
.max_depth = -1,
26+
.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED,
27+
.colors = {
28+
[GREP_COLOR_CONTEXT] = "",
29+
[GREP_COLOR_FILENAME] = "",
30+
[GREP_COLOR_FUNCTION] = "",
31+
[GREP_COLOR_LINENO] = "",
32+
[GREP_COLOR_COLUMNNO] = "",
33+
[GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED,
34+
[GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED,
35+
[GREP_COLOR_SELECTED] = "",
36+
[GREP_COLOR_SEP] = GIT_COLOR_CYAN,
37+
},
38+
.only_matching = 0,
39+
.color = -1,
40+
.output = std_output,
41+
};
1842

1943
#ifdef USE_LIBPCRE2
2044
static pcre2_general_context *pcre2_global_context;
@@ -42,49 +66,11 @@ static const char *color_grep_slots[] = {
4266
[GREP_COLOR_SEP] = "separator",
4367
};
4468

45-
static void std_output(struct grep_opt *opt, const void *buf, size_t size)
46-
{
47-
fwrite(buf, size, 1, stdout);
48-
}
49-
5069
static void color_set(char *dst, const char *color_bytes)
5170
{
5271
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
5372
}
5473

55-
/*
56-
* Initialize the grep_defaults template with hardcoded defaults.
57-
* We could let the compiler do this, but without C99 initializers
58-
* the code gets unwieldy and unreadable, so...
59-
*/
60-
void init_grep_defaults(void)
61-
{
62-
struct grep_opt *opt = &grep_defaults;
63-
static int run_once;
64-
65-
if (run_once)
66-
return;
67-
run_once++;
68-
69-
memset(opt, 0, sizeof(*opt));
70-
opt->relative = 1;
71-
opt->pathname = 1;
72-
opt->max_depth = -1;
73-
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
74-
color_set(opt->colors[GREP_COLOR_CONTEXT], "");
75-
color_set(opt->colors[GREP_COLOR_FILENAME], "");
76-
color_set(opt->colors[GREP_COLOR_FUNCTION], "");
77-
color_set(opt->colors[GREP_COLOR_LINENO], "");
78-
color_set(opt->colors[GREP_COLOR_COLUMNNO], "");
79-
color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED);
80-
color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED);
81-
color_set(opt->colors[GREP_COLOR_SELECTED], "");
82-
color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN);
83-
opt->only_matching = 0;
84-
opt->color = -1;
85-
opt->output = std_output;
86-
}
87-
8874
static int parse_pattern_type_arg(const char *opt, const char *arg)
8975
{
9076
if (!strcmp(arg, "default"))

grep.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ struct grep_opt {
170170
void *output_priv;
171171
};
172172

173-
void init_grep_defaults(void);
174173
int grep_config(const char *var, const char *value, void *);
175174
void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
176175
void grep_destroy(void);

revision.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1834,7 +1834,6 @@ void repo_init_revisions(struct repository *r,
18341834
revs->commit_format = CMIT_FMT_DEFAULT;
18351835
revs->expand_tabs_in_log_default = 8;
18361836

1837-
init_grep_defaults();
18381837
grep_init(&revs->grep_filter, revs->repo, prefix);
18391838
revs->grep_filter.status_only = 1;
18401839

0 commit comments

Comments
 (0)