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

[WIP PATCH 02/26] setup_git_directory*: Explicitly set git dir

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 16, 2010, 16:04 UTC
Message-ID
<1266336317-607-3-git-send-email-pclouds@gmail.com>
In-Reply-To
<1266336317-607-1-git-send-email-pclouds@gmail.com>

If setup_git_directory_gently() and its callers do not set gitdir now, it will be eventually set by setup_git_env(), through obscure call chain. One of the indirect call site is git_config().

My goal is to remove gitdir auto detection in setup_git_env(). So the first step is not depend on that behavior.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 setup.c |   34 +++++++++++++++++++++++-----------
 1 files changed, 23 insertions(+), 11 deletions(-)
diff --git a/setup.c b/setup.c
index b38cbee..183bcf6 100644
--- a/setup.c
+++ b/setup.c
@@ -313,8 +313,12 @@ const char *read_gitfile_gently(const char *path)
 /*
  * We cannot decide in this function whether we are in the work tree or
  * not, since the config can only be read _after_ this function was called.
+ *
+ * After this function is finished, set_git_dir() must be called if a
+ * a repository is found. Repo format will be checked later by
+ * setup_git_directory_gently()
  */
-const char *setup_git_directory_gently(int *nongit_ok)
+static const char *setup_git_directory_gently_1(int *nongit_ok)
 {
 	const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
 	const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
@@ -345,18 +349,17 @@ const char *setup_git_directory_gently(int *nongit_ok)
 			const char *retval;
 
 			if (!work_tree_env) {
+				/* core.worktree may override worktree */
 				retval = set_work_tree(gitdirenv);
-				/* config may override worktree */
-				if (check_repository_format_gently(nongit_ok))
-					return NULL;
+				set_git_dir(gitdirenv);
 				return retval;
 			}
-			if (check_repository_format_gently(nongit_ok))
-				return NULL;
 			retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
 					get_git_work_tree());
-			if (!retval || !*retval)
+			if (!retval || !*retval) {
+				set_git_dir(gitdirenv);
 				return NULL;
+			}
 			set_git_dir(make_absolute_path(gitdirenv));
 			if (chdir(work_tree_env) < 0)
 				die_errno ("Could not chdir to '%s'", work_tree_env);
@@ -396,8 +399,10 @@ const char *setup_git_directory_gently(int *nongit_ok)
 				die("Repository setup failed");
 			break;
 		}
-		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
+		if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
+			set_git_dir(DEFAULT_GIT_DIR_ENVIRONMENT);
 			break;
+		}
 		if (is_git_directory(".")) {
 			inside_git_dir = 1;
 			if (!work_tree_env)
@@ -407,7 +412,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
 				set_git_dir(cwd);
 			} else
 				set_git_dir(".");
-			check_repository_format_gently(nongit_ok);
 			return NULL;
 		}
 		while (--offset > ceil_offset && cwd[offset] != '/');
@@ -428,8 +432,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	if (!work_tree_env)
 		inside_work_tree = 1;
 	git_work_tree_cfg = xstrndup(cwd, offset);
-	if (check_repository_format_gently(nongit_ok))
-		return NULL;
 	if (offset == len)
 		return NULL;
 
@@ -440,6 +442,16 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	return cwd + offset;
 }
 
+const char *setup_git_directory_gently(int *nongit_ok)
+{
+	const char *prefix;
+
+	prefix = setup_git_directory_gently_1(nongit_ok);
+	if (!nongit_ok || (!*nongit_ok && check_repository_format_gently(nongit_ok)))
+		prefix = NULL;
+	return prefix;
+}
+
 int git_config_perm(const char *var, const char *value)
 {
 	int i;
-- 
1.7.0.195.g637a2
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 3 of 27 in “Git setup cleanup series”
  1. 00/26 Git setup cleanup seriesNguyễn Thái Ngọc Duy, Feb 16, 2010
  2. 01/26 rev-parse --git-dir: print relative gitdir correctlyNguyễn Thái Ngọc Duy, Feb 16, 2010
  3. 02/26 setup_git_directory*: Explicitly set git dirNguyễn Thái Ngọc Duy, Feb 16, 2010
  4. 03/26 Save setup_git_dir* info globally for later useNguyễn Thái Ngọc Duy, Feb 16, 2010
  5. 04/26 Add GIT_HARDENED_SETUP to detect gitdir/worktree related mis-setup errorsNguyễn Thái Ngọc Duy, Feb 16, 2010
  6. 05/26 enter_repo(): use setup_git_directory_gently internallyNguyễn Thái Ngọc Duy, Feb 16, 2010
  7. 06/26 Tweak init/clone to work properly with GIT_HARDENED_SETUP=1Nguyễn Thái Ngọc Duy, Feb 16, 2010
  8. 07/26 Support running setup_git_dir_gently() from the beginning for builtin commandsNguyễn Thái Ngọc Duy, Feb 16, 2010
  9. 08/26 config: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  10. 09/26 hash-object: move gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  11. 10/26 shortlog: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  12. 11/26 Do not look for .git/info/exclude when gitdir is not set upNguyễn Thái Ngọc Duy, Feb 16, 2010
  13. 12/26 grep: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  14. 13/26 USE_PAGER should not be used without RUN_SETUP*Nguyễn Thái Ngọc Duy, Feb 16, 2010
  15. 14/26 Do not try to read $GIT_DIR/info/attributes if there is no repositoryNguyễn Thái Ngọc Duy, Feb 16, 2010
  16. 15/26 archive: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  17. 16/26 mailinfo: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  18. 17/26 check-ref-format: setup gitdir gentlyNguyễn Thái Ngọc Duy, Feb 16, 2010
  19. 18/26 verify-pack: set up gitdir gentlyNguyễn Thái Ngọc Duy, Feb 16, 2010
  20. 19/26 apply: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  21. 20/26 bundle: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  22. 21/26 diff: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  23. 22/26 help: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  24. 23/26 ls-remote: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  25. 24/26 var: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  26. 25/26 merge-file: move up gitdir setup to run_builtin()Nguyễn Thái Ngọc Duy, Feb 16, 2010
  27. 26/26 Turn on GIT_HARDENED_SETUP for the whole test suiteNguyễn Thái Ngọc Duy, Feb 16, 2010

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.