Skip to content

Commit 6ba9bb7

Browse files
Martin Ågrengitster
authored andcommitted
grep: copy struct in one fell swoop
We have a `struct grep_opt` with our defaults which we then copy into the caller's struct. Rather than zeroing the target struct and copying each element one by one, just copy everything at once. This leaves the code simpler and more maintainable. We don't have any ownership issues with what we're copying now and can just greedily copy the whole thing. If and when we do need to handle such elements (`char *`?), we must and can handle it appropriately. Make sure to leave a comment to our future selves. Signed-off-by: Martin Ågren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9631342 commit 6ba9bb7

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

grep.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,6 @@ static const char *color_grep_slots[] = {
6666
[GREP_COLOR_SEP] = "separator",
6767
};
6868

69-
static void color_set(char *dst, const char *color_bytes)
70-
{
71-
xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
72-
}
73-
7469
static int parse_pattern_type_arg(const char *opt, const char *arg)
7570
{
7671
if (!strcmp(arg, "default"))
@@ -100,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb)
10095
if (userdiff_config(var, value) < 0)
10196
return -1;
10297

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+
103106
if (!strcmp(var, "grep.extendedregexp")) {
104107
opt->extended_regexp_option = git_config_bool(var, value);
105108
return 0;
@@ -157,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb)
157160
*/
158161
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
159162
{
160-
struct grep_opt *def = &grep_defaults;
161-
int i;
162-
163163
#if defined(USE_LIBPCRE2)
164164
if (!pcre2_global_context)
165165
pcre2_global_context = pcre2_general_context_create(
@@ -171,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
171171
pcre_free = free;
172172
#endif
173173

174-
memset(opt, 0, sizeof(*opt));
174+
*opt = grep_defaults;
175+
175176
opt->repo = repo;
176177
opt->prefix = prefix;
177178
opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
178179
opt->pattern_tail = &opt->pattern_list;
179180
opt->header_tail = &opt->header_list;
180-
181-
opt->only_matching = def->only_matching;
182-
opt->color = def->color;
183-
opt->extended_regexp_option = def->extended_regexp_option;
184-
opt->pattern_type_option = def->pattern_type_option;
185-
opt->linenum = def->linenum;
186-
opt->columnnum = def->columnnum;
187-
opt->max_depth = def->max_depth;
188-
opt->pathname = def->pathname;
189-
opt->relative = def->relative;
190-
opt->output = def->output;
191-
192-
for (i = 0; i < NR_GREP_COLORS; i++)
193-
color_set(opt->colors[i], def->colors[i]);
194181
}
195182

196183
void grep_destroy(void)

0 commit comments

Comments
 (0)