git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] grep: don't set up a "default" repo for grep

From
MÅMartin Ågren <martin.agren@gmail.com>
Date
Nov 24, 2020, 21:04 UTC
Message-ID
<56c00645c55f0e2cb3d21b19e397f449968b869d.1606251357.git.martin.agren@gmail.com>
In-Reply-To
<cover.1606251357.git.martin.agren@gmail.com>

`init_grep_defaults()` fills a `static struct grep_opt grep_defaults`. This struct is then used by `grep_init()` as a blueprint for other such structs. Notably, `grep_init()` takes a `struct repo *` and assigns it into the target struct.

As a result, it is unnecessary for us to take a `struct repo *` in `init_grep_defaults()` as well. We assign it into the default struct and never look at it again. And in light of how we return early if we have already set up the default struct, it's not just unnecessary, but is also a bit confusing: If we are called twice and with different repos, is it a bug or a feature that we ignore the second repo?

Drop the repo parameter for `init_grep_defaults()`.
Signed-off-by: Martin Ågren <martin.agren@gmail.com>
---
 Documentation/MyFirstObjectWalk.txt | 2 +-
 grep.h                              | 2 +-
 builtin/grep.c                      | 2 +-
 builtin/log.c                       | 2 +-
 grep.c                              | 3 +--
 revision.c                          | 2 +-
 6 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/Documentation/MyFirstObjectWalk.txt b/Documentation/MyFirstObjectWalk.txt
index c3f2d1a831..85434d1938 100644
--- a/Documentation/MyFirstObjectWalk.txt
+++ b/Documentation/MyFirstObjectWalk.txt
@@ -394,7 +394,7 @@ First some setup. Add `init_grep_defaults()` to `init_walken_defaults()` and add
 ----
 static void init_walken_defaults(void)
 {
-	init_grep_defaults(the_repository);
+	init_grep_defaults();
 }
 
 ...
diff --git a/grep.h b/grep.h
index 9115db8515..1c5478f381 100644
--- a/grep.h
+++ b/grep.h
@@ -170,7 +170,7 @@ struct grep_opt {
 	void *output_priv;
 };
 
-void init_grep_defaults(struct repository *);
+void init_grep_defaults(void);
 int grep_config(const char *var, const char *value, void *);
 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
 void grep_destroy(void);
diff --git a/builtin/grep.c b/builtin/grep.c
index e58e57504c..2b96efa8c2 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -950,7 +950,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	init_grep_defaults(the_repository);
+	init_grep_defaults();
 	git_config(grep_cmd_config, NULL);
 	grep_init(&opt, the_repository, prefix);
 
diff --git a/builtin/log.c b/builtin/log.c
index 49eb8f6431..eee4beca4d 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -131,7 +131,7 @@ static int log_line_range_callback(const struct option *option, const char *arg,
 
 static void init_log_defaults(void)
 {
-	init_grep_defaults(the_repository);
+	init_grep_defaults();
 	init_diff_ui_defaults();
 
 	decoration_style = auto_decoration_style();
diff --git a/grep.c b/grep.c
index 54af9f813e..b351449f7f 100644
--- a/grep.c
+++ b/grep.c
@@ -57,7 +57,7 @@ static void color_set(char *dst, const char *color_bytes)
  * We could let the compiler do this, but without C99 initializers
  * the code gets unwieldy and unreadable, so...
  */
-void init_grep_defaults(struct repository *repo)
+void init_grep_defaults(void)
 {
 	struct grep_opt *opt = &grep_defaults;
 	static int run_once;
@@ -67,7 +67,6 @@ void init_grep_defaults(struct repository *repo)
 	run_once++;
 
 	memset(opt, 0, sizeof(*opt));
-	opt->repo = repo;
 	opt->relative = 1;
 	opt->pathname = 1;
 	opt->max_depth = -1;
diff --git a/revision.c b/revision.c
index aa62212040..f35ea1db11 100644
--- a/revision.c
+++ b/revision.c
@@ -1834,7 +1834,7 @@ void repo_init_revisions(struct repository *r,
 	revs->commit_format = CMIT_FMT_DEFAULT;
 	revs->expand_tabs_in_log_default = 8;
 
-	init_grep_defaults(revs->repo);
+	init_grep_defaults();
 	grep_init(&revs->grep_filter, revs->repo, prefix);
 	revs->grep_filter.status_only = 1;
 
-- 
2.29.2.454.gaff20da3a2
Previous: Martin ÅgrenNext: Martin Ågren
Message 12 of 26 in “grep: retire `init_grep_defaults()`”
  1. 0/4 grep: retire `init_grep_defaults()`Martin Ågren, Nov 21, 2020
  2. 1/4 grep: don't set up a "default" repo for grepMartin Ågren, Nov 21, 2020
  3. 2/4 grep: use designated initializers for `grep_defaults`Martin Ågren, Nov 21, 2020
  4. 3/4 grep: simplify color setupMartin Ågren, Nov 21, 2020
  5. Jeff KingNov 21, 2020
  6. Martin ÅgrenNov 21, 2020
  7. Junio C HamanoNov 21, 2020
  8. Jeff KingNov 24, 2020
  9. 4/4 MyFirstObjectWalk: drop `init_walken_defaults()`Martin Ågren, Nov 21, 2020
  10. Johannes SchindelinNov 23, 2020
  11. 0/4 grep: simplify "grep defaults" handlingMartin Ågren, Nov 24, 2020
  12. 1/4 grep: don't set up a "default" repo for grepMartin Ågren, Nov 24, 2020
  13. 2/4 grep: use designated initializers for `grep_defaults`Martin Ågren, Nov 24, 2020
  14. 3/4 grep: copy struct in one fell swoopMartin Ågren, Nov 24, 2020
  15. Junio C HamanoNov 24, 2020
  16. Martin ÅgrenNov 25, 2020
  17. Junio C HamanoNov 25, 2020
  18. Martin ÅgrenNov 26, 2020
  19. 4/4 MyFirstObjectWalk: drop `init_walken_defaults()`Martin Ågren, Nov 24, 2020
  20. Ævar Arnfjörð BjarmasonNov 25, 2020
  21. 0/4 grep: simplify "grep defaults" handlingMartin Ågren, Nov 29, 2020
  22. 1/4 grep: don't set up a "default" repo for grepMartin Ågren, Nov 29, 2020
  23. 2/4 grep: use designated initializers for `grep_defaults`Martin Ågren, Nov 29, 2020
  24. 3/4 grep: copy struct in one fell swoopMartin Ågren, Nov 29, 2020
  25. 4/4 MyFirstObjectWalk: drop `init_walken_defaults()`Martin Ågren, Nov 29, 2020
  26. Junio C HamanoDec 1, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.