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

[PATCH v2 16/21] setup.c: keep track of the .git file location if read

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 14, 2013, 10:55 UTC
Message-ID
<1387018507-21999-17-git-send-email-pclouds@gmail.com>
In-Reply-To
<1387018507-21999-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 cache.h       |  2 +-
 environment.c |  2 +-
 setup.c       | 44 ++++++++++++++++++++++++++++++--------------
 3 files changed, 32 insertions(+), 16 deletions(-)
diff --git a/cache.h b/cache.h
index 4c09223..e036a7d 100644
--- a/cache.h
+++ b/cache.h
@@ -411,7 +411,7 @@ extern const char *get_git_namespace(void);
 extern const char *strip_namespace(const char *namespaced_ref);
 extern const char *get_git_work_tree(void);
 extern const char *read_gitfile(const char *path);
-extern const char *read_gitfile_super(const char *path, char **super);
+extern const char *read_gitfile_super(const char *path, char **super, char **gitfile);
 extern const char *resolve_gitdir(const char *suspect);
 extern void set_git_work_tree(const char *tree);
 
diff --git a/environment.c b/environment.c
index 5cbbe11..61aff23 100644
--- a/environment.c
+++ b/environment.c
@@ -130,7 +130,7 @@ static void setup_git_env(void)
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
 	if (!git_dir)
 		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
-	gitfile = read_gitfile_super(git_dir, &super);
+	gitfile = read_gitfile_super(git_dir, &super, NULL);
 	git_dir = xstrdup(gitfile ? gitfile : git_dir);
 	if (super)
 		git_super_dir = xstrdup(super);
diff --git a/setup.c b/setup.c
index 7ccb1f8..ac7d90c 100644
--- a/setup.c
+++ b/setup.c
@@ -332,7 +332,7 @@ static char *path_from_gitfile(const char *path, const char *buf, int len)
  * to the absolute path of the given path. The next line contains the
  * repo id.
  */
-const char *read_gitfile_super(const char *path, char **super)
+const char *read_gitfile_super(const char *path, char **super, char **gitfile)
 {
 	char *buf, *dir;
 	struct stat st;
@@ -384,6 +384,10 @@ const char *read_gitfile_super(const char *path, char **super)
 
 	if (!is_git_directory_super(dir, super ? *super : NULL))
 		die("Not a git repository: %s", dir);
+
+	if (gitfile)
+		*gitfile = xstrdup(real_path(path));
+
 	path = real_path(dir);
 
 	free(dir);
@@ -393,14 +397,15 @@ const char *read_gitfile_super(const char *path, char **super)
 
 const char *read_gitfile(const char *path)
 {
-	return read_gitfile_super(path, NULL);
+	return read_gitfile_super(path, NULL, NULL);
 }
 
 
 static const char *setup_explicit_git_dir(const char *gitdirenv,
 					  const char *super,
 					  char *cwd, int len,
-					  int *nongit_ok)
+					  int *nongit_ok,
+					  char **gitfile_path)
 {
 	const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
 	const char *worktree;
@@ -413,7 +418,8 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 	if (super)
 		gitfile = (char*)read_gitfile(gitdirenv);
 	else {
-		gitfile = (char*)read_gitfile_super(gitdirenv, &super_new);
+		gitfile = (char*)read_gitfile_super(gitdirenv, &super_new,
+						    gitfile_path);
 		super = super_new;
 	}
 	if (gitfile) {
@@ -500,7 +506,8 @@ done_null:
 static const char *setup_discovered_git_dir(const char *gitdir,
 					    const char *super,
 					    char *cwd, int offset, int len,
-					    int *nongit_ok)
+					    int *nongit_ok,
+					    char **gitfile_path)
 {
 	if (check_repository_format_gently(super ? super : gitdir, nongit_ok))
 		return NULL;
@@ -512,7 +519,8 @@ static const char *setup_discovered_git_dir(const char *gitdir,
 			gitdir = xstrdup(real_path(gitdir));
 		if (chdir(cwd))
 			die_errno("Could not come back to cwd");
-		return setup_explicit_git_dir(gitdir, super, cwd, len, nongit_ok);
+		return setup_explicit_git_dir(gitdir, super, cwd, len,
+					      nongit_ok, gitfile_path);
 	}
 
 	/* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
@@ -540,7 +548,8 @@ static const char *setup_discovered_git_dir(const char *gitdir,
 }
 
 /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
-static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
+static const char *setup_bare_git_dir(char *cwd, int offset, int len,
+				      int *nongit_ok, char **gitfile_path)
 {
 	int root_len;
 
@@ -556,7 +565,8 @@ static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongi
 		gitdir = offset == len ? "." : xmemdupz(cwd, offset);
 		if (chdir(cwd))
 			die_errno("Could not come back to cwd");
-		return setup_explicit_git_dir(gitdir, NULL, cwd, len, nongit_ok);
+		return setup_explicit_git_dir(gitdir, NULL, cwd, len,
+					      nongit_ok, gitfile_path);
 	}
 
 	inside_git_dir = 1;
@@ -630,7 +640,8 @@ static int canonicalize_ceiling_entry(struct string_list_item *item,
  * 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.
  */
-static const char *setup_git_directory_gently_1(int *nongit_ok)
+static const char *setup_git_directory_gently_1(int *nongit_ok,
+						char **gitfile_path)
 {
 	const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
 	struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
@@ -662,7 +673,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 	super = getenv(GIT_SUPER_DIR_ENVIRONMENT);
 	if (gitdirenv)
 		return setup_explicit_git_dir(gitdirenv, super,
-					      cwd, len, nongit_ok);
+					      cwd, len, nongit_ok,
+					      gitfile_path);
 
 	if (env_ceiling_dirs) {
 		int empty_entry_found = 0;
@@ -696,7 +708,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 			gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
 		else {
 			gitfile = (char*)read_gitfile_super(DEFAULT_GIT_DIR_ENVIRONMENT,
-							    &super_new);
+							    &super_new, gitfile_path);
 			super = super_new;
 		}
 		if (gitfile)
@@ -709,7 +721,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 		if (gitdirenv) {
 			ret = setup_discovered_git_dir(gitdirenv, super,
 						       cwd, offset, len,
-						       nongit_ok);
+						       nongit_ok,
+						       gitfile_path);
 			free(gitfile);
 			free(super_new);
 			return ret;
@@ -717,7 +730,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 		free(gitfile);
 
 		if (is_git_directory(".")) {
-			ret = setup_bare_git_dir(cwd, offset, len, nongit_ok);
+			ret = setup_bare_git_dir(cwd, offset, len,
+						 nongit_ok, gitfile_path);
 			free(super_new);
 			return ret;
 		}
@@ -754,8 +768,9 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
 const char *setup_git_directory_gently(int *nongit_ok)
 {
 	const char *prefix;
+	char *gitfile = NULL;
 
-	prefix = setup_git_directory_gently_1(nongit_ok);
+	prefix = setup_git_directory_gently_1(nongit_ok, &gitfile);
 	if (prefix)
 		setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
 	else
@@ -765,6 +780,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 		startup_info->have_repository = !nongit_ok || !*nongit_ok;
 		startup_info->prefix = prefix;
 	}
+	free(gitfile);
 	return prefix;
 }
 
-- 
1.8.5.1.77.g42c48fa
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 43 of 49 in “Support multiple worktrees”
  1. 0/7 Support multiple worktreesNguyễn Thái Ngọc Duy, Dec 11, 2013
  2. 1/7 Make git_path() beware of file relocation in $GIT_DIRNguyễn Thái Ngọc Duy, Dec 11, 2013
  3. Junio C HamanoDec 13, 2013
  4. 2/7 Add new environment variable $GIT_SUPER_DIRNguyễn Thái Ngọc Duy, Dec 11, 2013
  5. Junio C HamanoDec 13, 2013
  6. Duy NguyenDec 14, 2013
  7. Junio C HamanoDec 14, 2013
  8. 3/7 setup.c: add split-repo support to .git filesNguyễn Thái Ngọc Duy, Dec 11, 2013
  9. Junio C HamanoDec 13, 2013
  10. Jonathan NiederDec 13, 2013
  11. Duy NguyenDec 14, 2013
  12. Duy NguyenDec 23, 2013
  13. 4/7 setup.c: add split-repo support to is_git_directory()Nguyễn Thái Ngọc Duy, Dec 11, 2013
  14. 5/7 setup.c: reduce cleanup sites in setup_explicit_git_dir()Nguyễn Thái Ngọc Duy, Dec 11, 2013
  15. 6/7 setup.c: add split-repo support to setup_git_directory*Nguyễn Thái Ngọc Duy, Dec 11, 2013
  16. 7/7 init: add --split-repo with the same functionality as git-new-workdirNguyễn Thái Ngọc Duy, Dec 11, 2013
  17. 00/21 Support multiple worktreesNguyễn Thái Ngọc Duy, Dec 14, 2013
  18. 01/21 path.c: avoid PATH_MAX as buffer size from get_pathname()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  19. Torsten BögershausenDec 15, 2013
  20. Duy NguyenDec 15, 2013
  21. Antoine PelisseDec 15, 2013
  22. 02/21 path.c: rename vsnpath() to git_vsnpath()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  23. Ramsay JonesDec 14, 2013
  24. Duy NguyenDec 15, 2013
  25. Ramsay JonesDec 15, 2013
  26. Duy NguyenDec 16, 2013
  27. Jonathan NiederDec 16, 2013
  28. Junio C HamanoDec 16, 2013
  29. Ramsay JonesDec 16, 2013
  30. 03/21 path.c: move git_path() closer to similar functions git_pathdup()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  31. 04/21 Make git_path() aware of file relocation in $GIT_DIRNguyễn Thái Ngọc Duy, Dec 14, 2013
  32. 05/21 reflog: use avoid constructing .lock path with git_pathNguyễn Thái Ngọc Duy, Dec 14, 2013
  33. 06/21 fast-import: use git_path() for accessing .git dir instead of get_git_dir()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  34. 07/21 Add new environment variable $GIT_SUPER_DIRNguyễn Thái Ngọc Duy, Dec 14, 2013
  35. 08/21 setup.c: refactor path manipulation out of read_gitfile()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  36. 09/21 setup.c: add split-repo support to .git filesNguyễn Thái Ngọc Duy, Dec 14, 2013
  37. 10/21 setup.c: add split-repo support to is_git_directory()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  38. 11/21 setup.c: reduce cleanup sites in setup_explicit_git_dir()Nguyễn Thái Ngọc Duy, Dec 14, 2013
  39. 12/21 environment.c: support super .git file specified by $GIT_DIRNguyễn Thái Ngọc Duy, Dec 14, 2013
  40. 13/21 setup: support $GIT_SUPER_DIR as well as super .git filesNguyễn Thái Ngọc Duy, Dec 14, 2013
  41. 14/21 checkout: support checking out into a new working directoryNguyễn Thái Ngọc Duy, Dec 14, 2013
  42. 15/21 checkout: clean up half-prepared directories in --to modeNguyễn Thái Ngọc Duy, Dec 14, 2013
  43. 16/21 setup.c: keep track of the .git file location if readNguyễn Thái Ngọc Duy, Dec 14, 2013
  44. 17/21 prune: strategies for split repositoriesNguyễn Thái Ngọc Duy, Dec 14, 2013
  45. 18/21 refs: adjust reflog path for repos/<id>/HEADNguyễn Thái Ngọc Duy, Dec 14, 2013
  46. 19/21 refs: detach split repos' HEAD when the linked ref is updated/deletedNguyễn Thái Ngọc Duy, Dec 14, 2013
  47. 20/21 refs.c: refactor do_head_ref(... to do_one_head_ref("HEAD", ...Nguyễn Thái Ngọc Duy, Dec 14, 2013
  48. 21/21 revision: include repos/../HEAD in --allNguyễn Thái Ngọc Duy, Dec 14, 2013
  49. Duy NguyenDec 15, 2013

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.