]> git.ipfire.org Git - thirdparty/git.git/commitdiff
grep: use designated initializers for `grep_defaults`
authorMartin Ågren <martin.agren@gmail.com>
Sat, 21 Nov 2020 18:31:08 +0000 (19:31 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 21 Nov 2020 22:50:33 +0000 (14:50 -0800)
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>
Documentation/MyFirstObjectWalk.txt
builtin/grep.c
builtin/log.c
grep.c
grep.h
revision.c

index 85434d193867684263bc60ceb8bdb8bb019a175b..7f4bffc4dd8e1a422231a6fc08ac85cb5c1bc0f4 100644 (file)
@@ -388,17 +388,9 @@ Next, let's try to filter the commits we see based on their author. This is
 equivalent to running `git log --author=<pattern>`. We can add a filter by
 modifying `rev_info.grep_filter`, which is a `struct grep_opt`.
 
-First some setup. Add `init_grep_defaults()` to `init_walken_defaults()` and add
-`grep_config()` to `git_walken_config()`:
+First some setup. Add `grep_config()` to `git_walken_config()`:
 
 ----
-static void init_walken_defaults(void)
-{
-       init_grep_defaults();
-}
-
-...
-
 static int git_walken_config(const char *var, const char *value, void *cb)
 {
        grep_config(var, value, cb);
index 2b96efa8c243dede73dbfca25eacbd9235654a58..ca259af44163184d409ddfaa6f5b9a2d4567d8f8 100644 (file)
@@ -950,7 +950,6 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
                OPT_END()
        };
 
-       init_grep_defaults();
        git_config(grep_cmd_config, NULL);
        grep_init(&opt, the_repository, prefix);
 
index eee4beca4d7e73f4357663e5ff3c6a7aa6f23c1c..cf41714fb0cd91280d5047aea73259f16c2ebd45 100644 (file)
@@ -131,7 +131,6 @@ static int log_line_range_callback(const struct option *option, const char *arg,
 
 static void init_log_defaults(void)
 {
-       init_grep_defaults();
        init_diff_ui_defaults();
 
        decoration_style = auto_decoration_style();
diff --git a/grep.c b/grep.c
index b351449f7ff8958ddec5420acc1fc1a32af155d4..8f2009ec9ff32eb7f85f9af6b79664d5569627ec 100644 (file)
--- a/grep.c
+++ b/grep.c
@@ -14,7 +14,31 @@ static int grep_source_load(struct grep_source *gs);
 static int grep_source_is_binary(struct grep_source *gs,
                                 struct index_state *istate);
 
-static struct grep_opt grep_defaults;
+static void std_output(struct grep_opt *opt, const void *buf, size_t size)
+{
+       fwrite(buf, size, 1, stdout);
+}
+
+static struct grep_opt grep_defaults = {
+       .relative = 1,
+       .pathname = 1,
+       .max_depth = -1,
+       .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED,
+       .colors = {
+               [GREP_COLOR_CONTEXT] = "",
+               [GREP_COLOR_FILENAME] = "",
+               [GREP_COLOR_FUNCTION] = "",
+               [GREP_COLOR_LINENO] = "",
+               [GREP_COLOR_COLUMNNO] = "",
+               [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED,
+               [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED,
+               [GREP_COLOR_SELECTED] = "",
+               [GREP_COLOR_SEP] = GIT_COLOR_CYAN,
+       },
+       .only_matching = 0,
+       .color = -1,
+       .output = std_output,
+};
 
 #ifdef USE_LIBPCRE2
 static pcre2_general_context *pcre2_global_context;
@@ -42,49 +66,11 @@ static const char *color_grep_slots[] = {
        [GREP_COLOR_SEP]            = "separator",
 };
 
-static void std_output(struct grep_opt *opt, const void *buf, size_t size)
-{
-       fwrite(buf, size, 1, stdout);
-}
-
 static void color_set(char *dst, const char *color_bytes)
 {
        xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
 }
 
-/*
- * Initialize the grep_defaults template with hardcoded defaults.
- * We could let the compiler do this, but without C99 initializers
- * the code gets unwieldy and unreadable, so...
- */
-void init_grep_defaults(void)
-{
-       struct grep_opt *opt = &grep_defaults;
-       static int run_once;
-
-       if (run_once)
-               return;
-       run_once++;
-
-       memset(opt, 0, sizeof(*opt));
-       opt->relative = 1;
-       opt->pathname = 1;
-       opt->max_depth = -1;
-       opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
-       color_set(opt->colors[GREP_COLOR_CONTEXT], "");
-       color_set(opt->colors[GREP_COLOR_FILENAME], "");
-       color_set(opt->colors[GREP_COLOR_FUNCTION], "");
-       color_set(opt->colors[GREP_COLOR_LINENO], "");
-       color_set(opt->colors[GREP_COLOR_COLUMNNO], "");
-       color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED);
-       color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED);
-       color_set(opt->colors[GREP_COLOR_SELECTED], "");
-       color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN);
-       opt->only_matching = 0;
-       opt->color = -1;
-       opt->output = std_output;
-}
-
 static int parse_pattern_type_arg(const char *opt, const char *arg)
 {
        if (!strcmp(arg, "default"))
diff --git a/grep.h b/grep.h
index 1c5478f381e07397e001013ab1ab280dc5a39a43..b5c4e223a8f337fb476b5e375685045189389e82 100644 (file)
--- a/grep.h
+++ b/grep.h
@@ -170,7 +170,6 @@ struct grep_opt {
        void *output_priv;
 };
 
-void init_grep_defaults(void);
 int grep_config(const char *var, const char *value, void *);
 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
 void grep_destroy(void);
index f35ea1db110f5d4dbc342dd8456580930ec2890a..963868f699d18aad2d0382384c430ef4931d4e6b 100644 (file)
@@ -1834,7 +1834,6 @@ void repo_init_revisions(struct repository *r,
        revs->commit_format = CMIT_FMT_DEFAULT;
        revs->expand_tabs_in_log_default = 8;
 
-       init_grep_defaults();
        grep_init(&revs->grep_filter, revs->repo, prefix);
        revs->grep_filter.status_only = 1;