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 <martin.agren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Martin Ågren committed
Nov 29, 2020 at 20:52 UTC
6ba9bb76e0279bce9f614cb7f4ee28d8a601e79e
1 file changed
+10
-23
grep.c
+10
-23
@@ -66,11 +66,6 @@ static const char *color_grep_slots[] = {
66
[GREP_COLOR_SEP] = "separator",
67
};
68
69
-static void color_set(char *dst, const char *color_bytes)
70
-{
71
- xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
72
-}
73
-
69
static int parse_pattern_type_arg(const char *opt, const char *arg)
70
{
71
if (!strcmp(arg, "default"))
@@ -100,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb)
95
if (userdiff_config(var, value) < 0)
96
return -1;
97
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
+
106
if (!strcmp(var, "grep.extendedregexp")) {
107
opt->extended_regexp_option = git_config_bool(var, value);
108
return 0;
@@ -157,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb)
160
*/
161
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
162
{
160
- struct grep_opt *def = &grep_defaults;
161
- int i;
162
-
163
#if defined(USE_LIBPCRE2)
164
if (!pcre2_global_context)
165
pcre2_global_context = pcre2_general_context_create(
@@ -171,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
171
pcre_free = free;
172
#endif
173
174
- memset(opt, 0, sizeof(*opt));
174
+ *opt = grep_defaults;
175
+
176
opt->repo = repo;
177
opt->prefix = prefix;
178
opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
179
opt->pattern_tail = &opt->pattern_list;
180
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]);
181
}
182
183
void grep_destroy(void)