Skip to content

Commit 5dfb976

Browse files
committed
Merge branch 'ma/grep-init-default'
Code clean-up. * ma/grep-init-default: MyFirstObjectWalk: drop `init_walken_defaults()` grep: copy struct in one fell swoop grep: use designated initializers for `grep_defaults` grep: don't set up a "default" repo for grep
2 parents 01b8886 + 3bf97e1 commit 5dfb976

File tree

6 files changed

+36
-100
lines changed

6 files changed

+36
-100
lines changed

Documentation/MyFirstObjectWalk.txt

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,6 @@ its `init_log_defaults()` sets its own state (`decoration_style`) and asks
182182
`grep` and `diff` to initialize themselves by calling each of their
183183
initialization functions.
184184

185-
For our first example within `git walken`, we don't intend to use any other
186-
components within Git, and we don't have any configuration to do. However, we
187-
may want to add some later, so for now, we can add an empty placeholder. Create
188-
a new function in `builtin/walken.c`:
189-
190-
----
191-
static void init_walken_defaults(void)
192-
{
193-
/*
194-
* We don't actually need the same components `git log` does; leave this
195-
* empty for now.
196-
*/
197-
}
198-
----
199-
200-
Make sure to add a line invoking it inside of `cmd_walken()`.
201-
202-
----
203-
int cmd_walken(int argc, const char **argv, const char *prefix)
204-
{
205-
init_walken_defaults();
206-
}
207-
----
208-
209185
==== Configuring From `.gitconfig`
210186

211187
Next, we should have a look at any relevant configuration settings (i.e.,
@@ -388,17 +364,9 @@ Next, let's try to filter the commits we see based on their author. This is
388364
equivalent to running `git log --author=<pattern>`. We can add a filter by
389365
modifying `rev_info.grep_filter`, which is a `struct grep_opt`.
390366

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

394369
----
395-
static void init_walken_defaults(void)
396-
{
397-
init_grep_defaults(the_repository);
398-
}
399-
400-
...
401-
402370
static int git_walken_config(const char *var, const char *value, void *cb)
403371
{
404372
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(the_repository);
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
@@ -133,7 +133,6 @@ static int log_line_range_callback(const struct option *option, const char *arg,
133133

134134
static void init_log_defaults(void)
135135
{
136-
init_grep_defaults(the_repository);
137136
init_diff_ui_defaults();
138137

139138
decoration_style = auto_decoration_style();

grep.c

Lines changed: 35 additions & 63 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,50 +66,6 @@ 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-
50-
static void color_set(char *dst, const char *color_bytes)
51-
{
52-
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
53-
}
54-
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(struct repository *repo)
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->repo = repo;
71-
opt->relative = 1;
72-
opt->pathname = 1;
73-
opt->max_depth = -1;
74-
opt->pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED;
75-
color_set(opt->colors[GREP_COLOR_CONTEXT], "");
76-
color_set(opt->colors[GREP_COLOR_FILENAME], "");
77-
color_set(opt->colors[GREP_COLOR_FUNCTION], "");
78-
color_set(opt->colors[GREP_COLOR_LINENO], "");
79-
color_set(opt->colors[GREP_COLOR_COLUMNNO], "");
80-
color_set(opt->colors[GREP_COLOR_MATCH_CONTEXT], GIT_COLOR_BOLD_RED);
81-
color_set(opt->colors[GREP_COLOR_MATCH_SELECTED], GIT_COLOR_BOLD_RED);
82-
color_set(opt->colors[GREP_COLOR_SELECTED], "");
83-
color_set(opt->colors[GREP_COLOR_SEP], GIT_COLOR_CYAN);
84-
opt->only_matching = 0;
85-
opt->color = -1;
86-
opt->output = std_output;
87-
}
88-
8969
static int parse_pattern_type_arg(const char *opt, const char *arg)
9070
{
9171
if (!strcmp(arg, "default"))
@@ -115,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb)
11595
if (userdiff_config(var, value) < 0)
11696
return -1;
11797

98+
/*
99+
* The instance of grep_opt that we set up here is copied by
100+
* grep_init() to be used by each individual invocation.
101+
* When populating a new field of this structure here, be
102+
* sure to think about ownership -- e.g., you might need to
103+
* override the shallow copy in grep_init() with a deep copy.
104+
*/
105+
118106
if (!strcmp(var, "grep.extendedregexp")) {
119107
opt->extended_regexp_option = git_config_bool(var, value);
120108
return 0;
@@ -172,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb)
172160
*/
173161
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
174162
{
175-
struct grep_opt *def = &grep_defaults;
176-
int i;
177-
178163
#if defined(USE_LIBPCRE2)
179164
if (!pcre2_global_context)
180165
pcre2_global_context = pcre2_general_context_create(
@@ -186,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
186171
pcre_free = free;
187172
#endif
188173

189-
memset(opt, 0, sizeof(*opt));
174+
*opt = grep_defaults;
175+
190176
opt->repo = repo;
191177
opt->prefix = prefix;
192178
opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
193179
opt->pattern_tail = &opt->pattern_list;
194180
opt->header_tail = &opt->header_list;
195-
196-
opt->only_matching = def->only_matching;
197-
opt->color = def->color;
198-
opt->extended_regexp_option = def->extended_regexp_option;
199-
opt->pattern_type_option = def->pattern_type_option;
200-
opt->linenum = def->linenum;
201-
opt->columnnum = def->columnnum;
202-
opt->max_depth = def->max_depth;
203-
opt->pathname = def->pathname;
204-
opt->relative = def->relative;
205-
opt->output = def->output;
206-
207-
for (i = 0; i < NR_GREP_COLORS; i++)
208-
color_set(opt->colors[i], def->colors[i]);
209181
}
210182

211183
void grep_destroy(void)

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(struct repository *);
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
@@ -1827,7 +1827,6 @@ void repo_init_revisions(struct repository *r,
18271827
revs->commit_format = CMIT_FMT_DEFAULT;
18281828
revs->expand_tabs_in_log_default = 8;
18291829

1830-
init_grep_defaults(revs->repo);
18311830
grep_init(&revs->grep_filter, revs->repo, prefix);
18321831
revs->grep_filter.status_only = 1;
18331832

0 commit comments

Comments
 (0)