grep: use designated initializers for `grep_defaults`
In 15fabd1bbd ("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., cbc0f81d96 ("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 <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin Ågren committed
Nov 21, 2020 at 19:31 UTC
96313423a75fa8d88b6ecd5a15c21a7fbaf9e9be
6 files changed
+26
-52
Documentation/MyFirstObjectWalk.txt
+1
-9
@@ -388,17 +388,9 @@ Next, let's try to filter the commits we see based on their author. This is
388
equivalent to running `git log --author=<pattern>`. We can add a filter by
389
modifying `rev_info.grep_filter`, which is a `struct grep_opt`.
390
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()`:
392
393
----
395
-static void init_walken_defaults(void)
396
-{
397
- init_grep_defaults();
398
-}
399
-
400
-...
401
-
394
static int git_walken_config(const char *var, const char *value, void *cb)
395
{
396
grep_config(var, value, cb);
builtin/grep.c
-1
@@ -950,7 +950,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
950
OPT_END()
951
};
952
953
- init_grep_defaults();
953
git_config(grep_cmd_config, NULL);
954
grep_init(&opt, the_repository, prefix);
955
builtin/log.c
-1
@@ -131,7 +131,6 @@ static int log_line_range_callback(const struct option *option, const char *arg,
131
132
static void init_log_defaults(void)
133
{
134
- init_grep_defaults();
134
init_diff_ui_defaults();
135
136
decoration_style = auto_decoration_style();
grep.c
+25
-39
@@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs);
14
static int grep_source_is_binary(struct grep_source *gs,
15
struct index_state *istate);
16
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
+};
42
43
#ifdef USE_LIBPCRE2
44
static pcre2_general_context *pcre2_global_context;
@@ -42,49 +66,11 @@ static const char *color_grep_slots[] = {
66
[GREP_COLOR_SEP] = "separator",
67
};
68
45
-static void std_output(struct grep_opt *opt, const void *buf, size_t size)
46
-{
47
- fwrite(buf, size, 1, stdout);
48
-}
49
-
69
static void color_set(char *dst, const char *color_bytes)
70
{
71
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
72
}
73
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
-
74
static int parse_pattern_type_arg(const char *opt, const char *arg)
75
{
76
if (!strcmp(arg, "default"))
grep.h
-1
@@ -170,7 +170,6 @@ struct grep_opt {
170
void *output_priv;
171
};
172
173
-void init_grep_defaults(void);
173
int grep_config(const char *var, const char *value, void *);
174
void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
175
void grep_destroy(void);
revision.c
-1
@@ -1834,7 +1834,6 @@ void repo_init_revisions(struct repository *r,
1834
revs->commit_format = CMIT_FMT_DEFAULT;
1835
revs->expand_tabs_in_log_default = 8;
1836
1837
- init_grep_defaults();
1837
grep_init(&revs->grep_filter, revs->repo, prefix);
1838
revs->grep_filter.status_only = 1;
1839