path: root/setup.c
diff options
authorBrandon Williams <>2017-06-20 19:19:32 (GMT)
committerJunio C Hamano <>2017-06-24 01:24:34 (GMT)
commit73f192c991016bf88a9416cdf0e949f8b946f7e2 (patch)
treef86acaeb5a732acfd642a9b60ca5e76d2728c292 /setup.c
parent25bf951381a4880c43a3d1c65e6dce651e61148f (diff)
setup: don't perform lazy initialization of repository state
Under some circumstances (bogus GIT_DIR value or the discovered gitdir is '.git') 'setup_git_directory()' won't initialize key repository state. This leads to inconsistent state after running the setup code. To account for this inconsistent state, lazy initialization is done once a caller asks for the repository's gitdir or some other piece of repository state. This is confusing and can be error prone. Instead let's tighten the expected outcome of 'setup_git_directory()' and ensure that it initializes repository state in all cases that would have been handled by lazy initialization. This also lets us drop the requirement to have 'have_git_dir()' check if the environment variable GIT_DIR was set as that will be handled by the end of the setup code. Signed-off-by: Brandon Williams <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'setup.c')
1 files changed, 14 insertions, 0 deletions
diff --git a/setup.c b/setup.c
index 358fbc2..24a738b 100644
--- a/setup.c
+++ b/setup.c
@@ -1091,6 +1091,20 @@ const char *setup_git_directory_gently(int *nongit_ok)
startup_info->have_repository = !nongit_ok || !*nongit_ok;
startup_info->prefix = prefix;
+ /*
+ * Not all paths through the setup code will call 'set_git_dir()' (which
+ * directly sets up the environment) so in order to guarantee that the
+ * environment is in a consistent state after setup, explicitly setup
+ * the environment if we have a repository.
+ *
+ * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
+ * code paths so we also need to explicitly setup the environment if
+ * the user has set GIT_DIR. It may be beneficial to disallow bogus
+ * GIT_DIR values at some point in the future.
+ */
+ if (startup_info->have_repository || getenv(GIT_DIR_ENVIRONMENT))
+ setup_git_env();