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

Re: [RFC PATCH] repo-settings: set defaults even when not in a repo

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 30, 2022, 20:14 UTC
Message-ID
<xmqqwngb6vum.fsf@gitster.g>
In-Reply-To
<220330.86ilrvnxb6.gmgdl@evledraar.gmail.com>
Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:
Show 5 quoted lines
> Or just:
>
>     git mv {repo,global}-settings.c
>
> Since that's what it seems to want to be anyway.
Hmph, care to elaborate a bit more on "seems to"?
Here is my take
 - The code makes extensive use of repo_cfg_bool(), which is a thin
   wrapper around repo_config_get_bool(); despite its name, it is
   not about reading from the configuration file of that repository
   and nowhere else.  It can be affected by global configuration.
 - Other uses of repo_config_get_*() it uses is the same way.

So, it wants to grab a set of configuration that would +apply+ to this specific instance of "struct repository".

But that is quite different from "give us settings that would apply in general, I do not have a specific repository in mind", which is what "global-settings.c" would imply at least to me.

And in order for the "this specific instance" to make sense, the caller should have made sure that it is indeed a repository. Lifting that BUG() from the code path not only smells sloppy way to work around some corner case code that does not prepare the repository properly, but does not make much sense, at least to me. In exchange for scrapping the safety to help a caller that forgets to prepare repository before it is ready to call this function, what are we gaining?

I went back to the thread-starter message and re-read its justification. It talks about:

Show 10 quoted lines
> Concerns:
>
> Are any callers strictly dependent on having a BUG() here? I suspect
> that the worst that would happen is that rather than this BUG(), the
> caller would later hit its own BUG() or die(), so I do not think this is
> a blocker. Additionally, every builtin that directly calls
> prepare_repo_settings is either marked as RUN_SETUP, which means we
> would die() prior to calling it anyway, or checks on its own before
> calling it (builtin/diff.c). There are several callers in library code,
> though, and I have not tracked down how all of those are used.

Asking for existing callers being dependent on having a BUG() is a pure nonsense. The existing callers are there in shipped versions of Git exactly because they do things correctly not to hit the BUG(), so BY DEFINITION, they do not care if the BUG() is there or not.

So that is not "a blocker", but is a non-argument to ask if existing code paths care if the BUG() is gone.

What BUG() is protecting us against is a careless developer who writes a new code or alters an existing code path that ends up making the control flow in such a way that a proper set-up of the repository structure is bypassed by mistake before calling this function. The function is call-once by r->settings.initialized guarding it, calling it and then doing a set-up will result in an unexplainable bug even if the caller tries to compensate by calling it twice, as r->settings that is set incorrectly will be sticky.

Having said all that, I can be pursuaded to consider an approach to allow callers to explicitly ask for running outside repository, just like the more strict setup_git_directory() for majority of callers has looser setup_git_directory_gently() counterpart. The current callers should retain the "you must have discovered gitdir" there, but a special purpose code that is not even Git (like fuzzer) can say

    prepare_repo_settings_gently(r, &nongit_ok);
instead.
diff --git c/repo-settings.c w/repo-settings.c
index b4fbd16cdc..c492bc7671 100644
--- c/repo-settings.c
+++ w/repo-settings.c
@@ -10,15 +10,24 @@ static void repo_cfg_bool(struct repository *r, const char *key, int *dest,
 		*dest = def;
 }
 
-void prepare_repo_settings(struct repository *r)
+void prepare_repo_settings_gently(struct repository *r, int *nongit)
 {
 	int experimental;
 	int value;
 	char *strval;
 	int manyfiles;
 
-	if (!r->gitdir)
-		BUG("Cannot add settings for uninitialized repository");
+	if (!r->gitdir) {
+		/*
+		 * The caller can pass nongit (out paremeter) to ask if r is already
+		 * initialized (and act on it after this function returns).
+		 */
+		if (!nongit)
+			BUG("Cannot add settings for uninitialized repository");
+		*nongit = 1;
+	} else if (nongit) {
+		*nongit = 0;
+	}
 
 	if (r->settings.initialized++)
 		return;
diff --git c/repository.h w/repository.h
index e29f361703..98f6ec12cc 100644
--- c/repository.h
+++ w/repository.h
@@ -222,7 +222,8 @@ int repo_read_index_unmerged(struct repository *);
  */
 void repo_update_index_if_able(struct repository *, struct lock_file *);
 
-void prepare_repo_settings(struct repository *r);
+#define prepare_repo_settings(r) prepare_repo_settings_gently((r), NULL)
+void prepare_repo_settings_gently(struct repository *r, int *nongit);
 
 /*
  * Return 1 if upgrade repository format to target_version succeeded,
Previous: Ævar Arnfjörð BjarmasonNext: Josh Steadmon
Message 14 of 27 in “repo-settings: set defaults even when not in a repo”
  1. repo-settings: set defaults even when not in a repoJosh Steadmon, Mar 23, 2022
  2. Derrick StoleeMar 23, 2022
  3. Taylor BlauMar 23, 2022
  4. Josh SteadmonMar 28, 2022
  5. Taylor BlauMar 29, 2022
  6. Josh SteadmonMar 28, 2022
  7. Taylor BlauMar 29, 2022
  8. Ævar Arnfjörð BjarmasonMar 29, 2022
  9. Taylor BlauMar 30, 2022
  10. Josh SteadmonApr 9, 2022
  11. Ævar Arnfjörð BjarmasonMar 29, 2022
  12. Taylor BlauMar 30, 2022
  13. Ævar Arnfjörð BjarmasonMar 30, 2022
  14. Junio C HamanoMar 30, 2022
  15. commit-graph: refactor to avoid prepare_repo_settingsJosh Steadmon, Apr 9, 2022
  16. Jonathan TanJun 7, 2022
  17. Josh SteadmonJun 14, 2022
  18. commit-graph: refactor to avoid prepare_repo_settingsJosh Steadmon, Jun 14, 2022
  19. Taylor BlauJun 14, 2022
  20. Junio C HamanoJun 23, 2022
  21. Josh SteadmonJul 14, 2022
  22. commit-graph: pass repo_settings instead of repositoryJosh Steadmon, Jul 14, 2022
  23. Junio C HamanoJul 14, 2022
  24. Victoria DyeMar 23, 2022
  25. Junio C HamanoMar 23, 2022
  26. Victoria DyeMar 23, 2022
  27. Junio C HamanoMar 23, 2022

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.