summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Martin Ågren <martin.agren@gmail.com>2020-11-29 20:52:21 +0100
committerLibravatar Junio C Hamano <gitster@pobox.com>2020-11-30 13:55:54 -0800
commit6ba9bb76e0279bce9f614cb7f4ee28d8a601e79e (patch)
treec7c287563a004035efd7ddfa6298a60d2b3cc5ea
parentgrep: use designated initializers for `grep_defaults` (diff)
downloadtgif-6ba9bb76e0279bce9f614cb7f4ee28d8a601e79e.tar.xz
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>
-rw-r--r--grep.c33
1 files changed, 10 insertions, 23 deletions
diff --git a/grep.c b/grep.c
index 8f2009ec9f..efeb6dc58d 100644
--- a/grep.c
+++ b/grep.c
@@ -66,11 +66,6 @@ static const char *color_grep_slots[] = {
[GREP_COLOR_SEP] = "separator",
};
-static void color_set(char *dst, const char *color_bytes)
-{
- xsnprintf(dst, COLOR_MAXLEN, "%s", color_bytes);
-}
-
static int parse_pattern_type_arg(const char *opt, const char *arg)
{
if (!strcmp(arg, "default"))
@@ -100,6 +95,14 @@ int grep_config(const char *var, const char *value, void *cb)
if (userdiff_config(var, value) < 0)
return -1;
+ /*
+ * The instance of grep_opt that we set up here is copied by
+ * grep_init() to be used by each individual invocation.
+ * When populating a new field of this structure here, be
+ * sure to think about ownership -- e.g., you might need to
+ * override the shallow copy in grep_init() with a deep copy.
+ */
+
if (!strcmp(var, "grep.extendedregexp")) {
opt->extended_regexp_option = git_config_bool(var, value);
return 0;
@@ -157,9 +160,6 @@ int grep_config(const char *var, const char *value, void *cb)
*/
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
{
- struct grep_opt *def = &grep_defaults;
- int i;
-
#if defined(USE_LIBPCRE2)
if (!pcre2_global_context)
pcre2_global_context = pcre2_general_context_create(
@@ -171,26 +171,13 @@ void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix
pcre_free = free;
#endif
- memset(opt, 0, sizeof(*opt));
+ *opt = grep_defaults;
+
opt->repo = repo;
opt->prefix = prefix;
opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
opt->pattern_tail = &opt->pattern_list;
opt->header_tail = &opt->header_list;
-
- opt->only_matching = def->only_matching;
- opt->color = def->color;
- opt->extended_regexp_option = def->extended_regexp_option;
- opt->pattern_type_option = def->pattern_type_option;
- opt->linenum = def->linenum;
- opt->columnnum = def->columnnum;
- opt->max_depth = def->max_depth;
- opt->pathname = def->pathname;
- opt->relative = def->relative;
- opt->output = def->output;
-
- for (i = 0; i < NR_GREP_COLORS; i++)
- color_set(opt->colors[i], def->colors[i]);
}
void grep_destroy(void)