Skip to content

Commit 72365bb

Browse files
avargitster
authored andcommitted
grep API: call grep_config() after grep_init()
The grep_init() function used the odd pattern of initializing the passed-in "struct grep_opt" with a statically defined "grep_defaults" struct, which would be modified in-place when we invoked grep_config(). So we effectively (b) initialized config, (a) then defaults, (c) followed by user options. Usually those are ordered as "a", "b" and "c" instead. As the comments being removed here show the previous behavior needed to be carefully explained as we'd potentially share the populated configuration among different instances of grep_init(). In practice we didn't do that, but now that it can't be a concern anymore let's remove those comments. This does not change the behavior of any of the configuration variables or options. That would have been the case if we didn't move around the grep_config() call in "builtin/log.c". But now that we call "grep_config" after "git_log_config" and "git_format_config" we'll need to pass in the already initialized "struct grep_opt *". See 6ba9bb7 (grep: copy struct in one fell swoop, 2020-11-29) and 7687a05 (grep: move the configuration parsing logic to grep.[ch], 2012-10-09) for the commits that added the comments. The memcpy() pattern here will be optimized away and follows the convention of other *_init() functions. See 5726a6b (*.c *_init(): define in terms of corresponding *_INIT macro, 2021-07-01). Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b8db6ed commit 72365bb

File tree

4 files changed

+37
-40
lines changed

4 files changed

+37
-40
lines changed

builtin/grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static int wait_all(void)
285285

286286
static int grep_cmd_config(const char *var, const char *value, void *cb)
287287
{
288-
int st = grep_config(var, value, NULL);
288+
int st = grep_config(var, value, cb);
289289
if (git_color_default_config(var, value, NULL) < 0)
290290
st = -1;
291291

@@ -966,8 +966,8 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
966966
};
967967
grep_prefix = prefix;
968968

969-
git_config(grep_cmd_config, NULL);
970969
grep_init(&opt, the_repository);
970+
git_config(grep_cmd_config, &opt);
971971

972972
/*
973973
* If there is no -- then the paths must exist in the working

builtin/log.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,6 @@ static int git_log_config(const char *var, const char *value, void *cb)
520520
return 0;
521521
}
522522

523-
if (grep_config(var, value, cb) < 0)
524-
return -1;
525523
if (git_gpg_config(var, value, cb) < 0)
526524
return -1;
527525
return git_diff_ui_config(var, value, cb);
@@ -536,6 +534,8 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
536534
git_config(git_log_config, NULL);
537535

538536
repo_init_revisions(the_repository, &rev, prefix);
537+
git_config(grep_config, &rev.grep_filter);
538+
539539
rev.diff = 1;
540540
rev.simplify_history = 0;
541541
memset(&opt, 0, sizeof(opt));
@@ -650,6 +650,8 @@ int cmd_show(int argc, const char **argv, const char *prefix)
650650

651651
memset(&match_all, 0, sizeof(match_all));
652652
repo_init_revisions(the_repository, &rev, prefix);
653+
git_config(grep_config, &rev.grep_filter);
654+
653655
rev.diff = 1;
654656
rev.always_show_header = 1;
655657
rev.no_walk = 1;
@@ -733,6 +735,8 @@ int cmd_log_reflog(int argc, const char **argv, const char *prefix)
733735

734736
repo_init_revisions(the_repository, &rev, prefix);
735737
init_reflog_walk(&rev.reflog_info);
738+
git_config(grep_config, &rev.grep_filter);
739+
736740
rev.verbose_header = 1;
737741
memset(&opt, 0, sizeof(opt));
738742
opt.def = "HEAD";
@@ -766,6 +770,8 @@ int cmd_log(int argc, const char **argv, const char *prefix)
766770
git_config(git_log_config, NULL);
767771

768772
repo_init_revisions(the_repository, &rev, prefix);
773+
git_config(grep_config, &rev.grep_filter);
774+
769775
rev.always_show_header = 1;
770776
memset(&opt, 0, sizeof(opt));
771777
opt.def = "HEAD";
@@ -1848,10 +1854,13 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18481854
extra_hdr.strdup_strings = 1;
18491855
extra_to.strdup_strings = 1;
18501856
extra_cc.strdup_strings = 1;
1857+
18511858
init_log_defaults();
18521859
init_display_notes(&notes_opt);
18531860
git_config(git_format_config, NULL);
18541861
repo_init_revisions(the_repository, &rev, prefix);
1862+
git_config(grep_config, &rev.grep_filter);
1863+
18551864
rev.show_notes = show_notes;
18561865
memcpy(&rev.notes_opt, &notes_opt, sizeof(notes_opt));
18571866
rev.commit_format = CMIT_FMT_EMAIL;

grep.c

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,6 @@ static void std_output(struct grep_opt *opt, const void *buf, size_t size)
1919
fwrite(buf, size, 1, stdout);
2020
}
2121

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] = GIT_COLOR_MAGENTA,
30-
[GREP_COLOR_FUNCTION] = "",
31-
[GREP_COLOR_LINENO] = GIT_COLOR_GREEN,
32-
[GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN,
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-
4322
static const char *color_grep_slots[] = {
4423
[GREP_COLOR_CONTEXT] = "context",
4524
[GREP_COLOR_FILENAME] = "filename",
@@ -75,20 +54,12 @@ define_list_config_array_extra(color_grep_slots, {"match"});
7554
*/
7655
int grep_config(const char *var, const char *value, void *cb)
7756
{
78-
struct grep_opt *opt = &grep_defaults;
57+
struct grep_opt *opt = cb;
7958
const char *slot;
8059

8160
if (userdiff_config(var, value) < 0)
8261
return -1;
8362

84-
/*
85-
* The instance of grep_opt that we set up here is copied by
86-
* grep_init() to be used by each individual invocation.
87-
* When populating a new field of this structure here, be
88-
* sure to think about ownership -- e.g., you might need to
89-
* override the shallow copy in grep_init() with a deep copy.
90-
*/
91-
9263
if (!strcmp(var, "grep.extendedregexp")) {
9364
opt->extended_regexp_option = git_config_bool(var, value);
9465
return 0;
@@ -134,14 +105,10 @@ int grep_config(const char *var, const char *value, void *cb)
134105
return 0;
135106
}
136107

137-
/*
138-
* Initialize one instance of grep_opt and copy the
139-
* default values from the template we read the configuration
140-
* information in an earlier call to git_config(grep_config).
141-
*/
142108
void grep_init(struct grep_opt *opt, struct repository *repo)
143109
{
144-
*opt = grep_defaults;
110+
struct grep_opt blank = GREP_OPT_INIT;
111+
memcpy(opt, &blank, sizeof(*opt));
145112

146113
opt->repo = repo;
147114
opt->pattern_tail = &opt->pattern_list;

grep.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ struct grep_opt {
179179
void *output_priv;
180180
};
181181

182+
#define GREP_OPT_INIT { \
183+
.relative = 1, \
184+
.pathname = 1, \
185+
.max_depth = -1, \
186+
.pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, \
187+
.colors = { \
188+
[GREP_COLOR_CONTEXT] = "", \
189+
[GREP_COLOR_FILENAME] = GIT_COLOR_MAGENTA, \
190+
[GREP_COLOR_FUNCTION] = "", \
191+
[GREP_COLOR_LINENO] = GIT_COLOR_GREEN, \
192+
[GREP_COLOR_COLUMNNO] = GIT_COLOR_GREEN, \
193+
[GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, \
194+
[GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, \
195+
[GREP_COLOR_SELECTED] = "", \
196+
[GREP_COLOR_SEP] = GIT_COLOR_CYAN, \
197+
}, \
198+
.only_matching = 0, \
199+
.color = -1, \
200+
.output = std_output, \
201+
}
202+
182203
int grep_config(const char *var, const char *value, void *);
183204
void grep_init(struct grep_opt *, struct repository *repo);
184205
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);

0 commit comments

Comments
 (0)