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

[PATCH 6/9] Allow add_path() to add non-existent directories to the path

From
Johannes Sixt <johannes.sixt@telecom.at>
Date
Jul 21, 2008, 19:19 UTC
Message-ID
<1216667998-8879-7-git-send-email-johannes.sixt@telecom.at>
In-Reply-To
<1216667998-8879-6-git-send-email-johannes.sixt@telecom.at>

This function had used make_absolute_path(); but this function dies if the directory that contains the entry whose relative path was supplied in the argument does not exist. This is a problem if the argument is, for example, "../libexec/git-core", and that "../libexec" does not exist.

Since the resolution of symbolic links is not required for elements in PATH, we can fall back to using make_nonrelative_path(), which simply prepends $PWD to the path.

We have to move make_nonrelative_path() alongside make_absolute_path() in abspath.c so that git-shell can be linked. See 5b8e6f85f.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
---
 abspath.c  |   36 ++++++++++++++++++++++++++++++++++++
 exec_cmd.c |    2 +-
 path.c     |   36 ------------------------------------
 3 files changed, 37 insertions(+), 37 deletions(-)
diff --git a/abspath.c b/abspath.c
index 4f95a95..0d56124 100644
--- a/abspath.c
+++ b/abspath.c
@@ -66,3 +66,39 @@ const char *make_absolute_path(const char *path)
 
 	return buf;
 }
+
+static const char *get_pwd_cwd(void)
+{
+	static char cwd[PATH_MAX + 1];
+	char *pwd;
+	struct stat cwd_stat, pwd_stat;
+	if (getcwd(cwd, PATH_MAX) == NULL)
+		return NULL;
+	pwd = getenv("PWD");
+	if (pwd && strcmp(pwd, cwd)) {
+		stat(cwd, &cwd_stat);
+		if (!stat(pwd, &pwd_stat) &&
+		    pwd_stat.st_dev == cwd_stat.st_dev &&
+		    pwd_stat.st_ino == cwd_stat.st_ino) {
+			strlcpy(cwd, pwd, PATH_MAX);
+		}
+	}
+	return cwd;
+}
+
+const char *make_nonrelative_path(const char *path)
+{
+	static char buf[PATH_MAX + 1];
+
+	if (is_absolute_path(path)) {
+		if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
+			die("Too long path: %.*s", 60, path);
+	} else {
+		const char *cwd = get_pwd_cwd();
+		if (!cwd)
+			die("Cannot determine the current working directory");
+		if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
+			die("Too long path: %.*s", 60, path);
+	}
+	return buf;
+}
diff --git a/exec_cmd.c b/exec_cmd.c
index c236034..0ed768d 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -50,7 +50,7 @@ static void add_path(struct strbuf *out, const char *path)
 		if (is_absolute_path(path))
 			strbuf_addstr(out, path);
 		else
-			strbuf_addstr(out, make_absolute_path(path));
+			strbuf_addstr(out, make_nonrelative_path(path));
 
 		strbuf_addch(out, PATH_SEP);
 	}
diff --git a/path.c b/path.c
index 504eae0..9df447b 100644
--- a/path.c
+++ b/path.c
@@ -291,42 +291,6 @@ int adjust_shared_perm(const char *path)
 	return 0;
 }
 
-static const char *get_pwd_cwd(void)
-{
-	static char cwd[PATH_MAX + 1];
-	char *pwd;
-	struct stat cwd_stat, pwd_stat;
-	if (getcwd(cwd, PATH_MAX) == NULL)
-		return NULL;
-	pwd = getenv("PWD");
-	if (pwd && strcmp(pwd, cwd)) {
-		stat(cwd, &cwd_stat);
-		if (!stat(pwd, &pwd_stat) &&
-		    pwd_stat.st_dev == cwd_stat.st_dev &&
-		    pwd_stat.st_ino == cwd_stat.st_ino) {
-			strlcpy(cwd, pwd, PATH_MAX);
-		}
-	}
-	return cwd;
-}
-
-const char *make_nonrelative_path(const char *path)
-{
-	static char buf[PATH_MAX + 1];
-
-	if (is_absolute_path(path)) {
-		if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
-			die ("Too long path: %.*s", 60, path);
-	} else {
-		const char *cwd = get_pwd_cwd();
-		if (!cwd)
-			die("Cannot determine the current working directory");
-		if (snprintf(buf, PATH_MAX, "%s/%s", cwd, path) >= PATH_MAX)
-			die ("Too long path: %.*s", 60, path);
-	}
-	return buf;
-}
-
 const char *make_relative_path(const char *abs, const char *base)
 {
 	static char buf[PATH_MAX + 1];
-- 
1.6.0.rc0.18.g6aef2
Previous: Johannes SixtNext: Johannes Sixt
Message 7 of 32 in “Make gitexecdir relative to $(bindir) on Windows”
  1. 0/9 Make gitexecdir relative to $(bindir) on WindowsJohannes Sixt, Jul 21, 2008
  2. 1/9 Makefile: Do not install a copy of 'git' in $(gitexecdir)Johannes Sixt, Jul 21, 2008
  3. 2/9 Makefile: Normalize $(bindir) and $(gitexecdir) before comparingJohannes Sixt, Jul 21, 2008
  4. 3/9 Record the command invocation path earlyJohannes Sixt, Jul 21, 2008
  5. 4/9 Fix relative built-in paths to be relative to the command invocationJohannes Sixt, Jul 21, 2008
  6. 5/9 Allow the built-in exec path to be relative to the command invocation pathJohannes Sixt, Jul 21, 2008
  7. 6/9 Allow add_path() to add non-existent directories to the pathJohannes Sixt, Jul 21, 2008
  8. 7/9 Windows: Make $(gitexecdir) relativeJohannes Sixt, Jul 21, 2008
  9. 8/9 Windows: Make sure argv[0] has a pathJohannes Sixt, Jul 21, 2008
  10. 9/9 Windows: Do not compile git-shellJohannes Sixt, Jul 21, 2008
  11. 10/9 git-gui: git.git now uses $(gitexec_instdir) to point to the exec-pathJohannes Sixt, Jul 21, 2008
  12. Steffen ProhaskaJul 25, 2008
  13. Junio C HamanoJul 23, 2008
  14. 5/9 Allow the built-in exec path to be relative to the command invocation pathJohannes Sixt, Jul 23, 2008
  15. Junio C HamanoJul 24, 2008
  16. Johannes SixtJul 24, 2008
  17. Junio C HamanoJul 25, 2008
  18. Johannes SixtJul 25, 2008
  19. Johannes SixtJul 25, 2008
  20. Junio C HamanoJul 28, 2008
  21. Johannes SchindelinJul 21, 2008
  22. Johannes SixtJul 22, 2008
  23. A Large Angry SCMJul 28, 2008
  24. Junio C HamanoJul 28, 2008
  25. Junio C HamanoJul 28, 2008
  26. A Large Angry SCMJul 28, 2008
  27. A Large Angry SCMJul 28, 2008
  28. A Large Angry SCMJul 28, 2008
  29. Johannes SchindelinJul 21, 2008
  30. Johannes SixtJul 22, 2008
  31. Junio C HamanoJul 23, 2008
  32. Johannes SixtJul 23, 2008

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.