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

[PATCH] always interpret GIT_WORK_DIR relative to $GIT_DIR

From
MLMatthias Lederhofer <matled@gmx.net>
Date
Mar 12, 2007, 19:18 UTC
Message-ID
<20070312191812.GA29327@moooo.ath.cx>
In-Reply-To
<20070312180837.GA22701@moooo.ath.cx>
Signed-off-by: Matthias Lederhofer <matled@gmx.net>
---
 setup.c |   39 +++++++++++++++++++++++----------------
 1 files changed, 23 insertions(+), 16 deletions(-)
diff --git a/setup.c b/setup.c
index a8b9fae..f8c020e 100644
--- a/setup.c
+++ b/setup.c
@@ -192,16 +192,36 @@ int is_inside_git_dir(void)
 	return inside_git_dir;
 }
 
+static int stat_relative(const char *base, const char *path, struct stat *st)
+{
+	char cwd[PATH_MAX];
+	int ret;
+
+	if (path[0] != '/') {
+		if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
+			die("Unable to read current working directory");
+		if (chdir(base))
+			die("Cannot change directory to '%s'", base);
+	}
+
+	ret = stat(path, st);
+
+	if (path[0] != '/' && chdir(cwd))
+		die("Cannot come back to cwd");
+
+	return ret;
+}
+
 static int stat_git_work_dir(struct stat *st)
 {
-	char workdir[PATH_MAX], cwd[PATH_MAX];
+	char workdir[PATH_MAX];
 	const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 	const char *gitwd = getenv(GIT_WORKING_DIR_ENVIRONMENT);
 	int offset;
 	FILE *fp;
 
 	if (gitwd) {
-		if (!stat(gitwd, st))
+		if (!stat_relative(gitdir, gitwd, st))
 			return 1;
 		die("Unable to stat git working directory '%s'", gitwd);
 	}
@@ -231,21 +251,8 @@ static int stat_git_work_dir(struct stat *st)
 		--offset;
 	workdir[offset+1] = '\0';
 
-	/* relative path: change to gitdir for stat */
-	if (workdir[0] != '/') {
-		if (!getcwd(cwd, sizeof(cwd)) || cwd[0] != '/')
-			die("Unable to read current working directory");
-		if (chdir(gitdir))
-			die("Cannot change directory to '%s'", gitdir);
-	}
-
-	if (stat(workdir, st))
+	if (stat_relative(gitdir, workdir, st))
 		die("Unable to stat directory from GIT_DIR/workdir");
-	if (!(st->st_mode & S_IFDIR))
-		die("GIT_DIR/workdir does not point to a directory");
-
-	if (workdir[0] != '/' && chdir(cwd))
-		die("Cannot come back to cwd");
 
 	return 1;
 }
-- 
1.5.0.3.1007.g7ff7
Previous: Matthias LederhoferNext: Matthias Lederhofer
Message 35 of 39 in “[RFC] introduce GIT_WORK_DIR environment variable”
  1. Matthias LederhoferMar 11, 2007
  2. Junio C HamanoMar 11, 2007
  3. Andy ParkinsMar 11, 2007
  4. Matthias LederhoferMar 11, 2007
  5. Junio C HamanoMar 11, 2007
  6. Johannes SchindelinMar 11, 2007
  7. Linus TorvaldsMar 11, 2007
  8. Junio C HamanoMar 11, 2007
  9. Linus TorvaldsMar 11, 2007
  10. A.J. RossiniMar 12, 2007
  11. Andy ParkinsApr 1, 2007
  12. Nguyen Thai Ngoc DuyMar 11, 2007
  13. Matthias LederhoferMar 11, 2007
  14. Nguyen Thai Ngoc DuyMar 11, 2007
  15. Matthias LederhoferMar 11, 2007
  16. Nguyen Thai Ngoc DuyMar 11, 2007
  17. introduce GIT_WORK_DIR environment variableMatthias Lederhofer, Mar 11, 2007
  18. Nguyen Thai Ngoc DuyMar 11, 2007
  19. use $GIT_DIR/workdir as working directory with $GIT_DIRMatthias Lederhofer, Mar 11, 2007
  20. core.workdir config variableMatthias Lederhofer, Mar 13, 2007
  21. core.workdir config variableMatthias Lederhofer, Mar 13, 2007
  22. Shawn O. PearceMar 14, 2007
  23. Junio C HamanoMar 14, 2007
  24. Shawn O. PearceMar 14, 2007
  25. Nguyen Thai Ngoc DuyMar 11, 2007
  26. rev-parse: --is-bare-repository optionMatthias Lederhofer, Mar 11, 2007
  27. git-init: set up GIT_DIR/workdir if GIT_WORK_DIR is setMatthias Lederhofer, Mar 12, 2007
  28. Joshua N PritikinMar 12, 2007
  29. Matthias LederhoferMar 12, 2007
  30. Matthias LederhoferMar 12, 2007
  31. Nguyen Thai Ngoc DuyMar 12, 2007
  32. Matthias LederhoferMar 12, 2007
  33. Junio C HamanoMar 12, 2007
  34. Matthias LederhoferMar 12, 2007
  35. always interpret GIT_WORK_DIR relative to $GIT_DIRMatthias Lederhofer, Mar 12, 2007
  36. GIT_WORK_DIR: documentation for relative pathMatthias Lederhofer, Mar 12, 2007
  37. Junio C HamanoMar 12, 2007
  38. Matthias LederhoferMar 12, 2007
  39. git-init: set up GIT_DIR/workdir if GIT_WORK_DIR is setMatthias Lederhofer, Mar 12, 2007

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.