threads / patch / 10388

patchIf git is ran with a relative path, try building an absolute exec_path from it

Subject: [PATCH] If git is ran with a relative path, try building an absolute exec_path from it

## tl;dr

4 messages between Oct 20, 2007 and Oct 20, 2007. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

Scott R Parish· Oct 20, 2007, 06:46 UTC · lore
 Signed-off-by: Scott R Parish <srp@srparish.net>
---
 git.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)
Show changes to git.c +33 −2
diff --git a/git.c b/git.c
index 9eaca1d..d129ecc 100644
--- a/git.c
+++ b/git.c
@@ -28,6 +28,35 @@ static void prepend_to_path(const char *dir, int len)
 	free(path);
 }
 
+static char *rel_to_abs_exec_path(const char *cmd) {
+	int len, rc;
+	char *exec_path = xmalloc(PATH_MAX + 1);
+
+	if (!getcwd(exec_path, PATH_MAX)) {
+		fprintf(stderr, "git: cannot determine current directory: %s\n",
+			strerror(errno));
+		free(exec_path);
+		return NULL;
+	}
+	len = strlen(exec_path);
+
+	/* Trivial cleanup */
+	while (!prefixcmp(cmd, "./")) {
+		cmd += 2;
+		while (*cmd == '/')
+			cmd++;
+	}
+
+	rc = snprintf(exec_path + len, PATH_MAX - len, "/%s", cmd);
+	if (rc < 0 || rc >= PATH_MAX - len) {
+		fprintf(stderr, "git: command name given is too long.\n");
+		free(exec_path);
+		return NULL;
+	}
+
+	return exec_path;
+}
+
 static int handle_options(const char*** argv, int* argc, int* envchanged)
 {
 	int handled = 0;
@@ -409,13 +438,15 @@ int main(int argc, const char **argv)
 	/*
 	 * Take the basename of argv[0] as the command
 	 * name, and the dirname as the default exec_path
-	 * if it's an absolute path and we don't have
-	 * anything better.
+	 * if we don't have anything better.
 	 */
 	if (slash) {
 		*slash++ = 0;
 		if (*cmd == '/')
 			exec_path = cmd;
+		else
+			exec_path = rel_to_abs_exec_path(cmd);
+
 		cmd = slash;
 	}
 
-- 
1.5.3.GIT
Johannes Schindelin· Oct 20, 2007, 07:21 UTC · re: Scott R Parish · lore

[PATCH] Deduce exec_path also from calls to git with a relative path

There is already logic in the git wrapper to deduce the exec_path from argv[0], when the git wrapper was called with an absolute path. Extend that logic to handle relative paths as well.

For example, when you call "../../hello/world/git", it will not turn "../../hello/world" into an absolute path, and use that.

Initial implementation by Scott R Parish.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	On Fri, 19 Oct 2007, Scott R Parish wrote:
	>  Signed-off-by: Scott R Parish <srp@srparish.net>
	That is a little short for a commit message ;-)
	>  git.c |   35 +++++++++++++++++++++++++++++++++--
	>  1 files changed, 33 insertions(+), 2 deletions(-)
	I had commented on this before.  Probably I did a very bad job 
	at explaining things, so hopefully this is better:
 git.c |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
Show changes to git.c +3 −2
diff --git a/git.c b/git.c
index d7c6bca..1dad764 100644
--- a/git.c
+++ b/git.c
@@ -414,13 +414,14 @@ int main(int argc, const char **argv)
 	/*
 	 * Take the basename of argv[0] as the command
 	 * name, and the dirname as the default exec_path
-	 * if it's an absolute path and we don't have
-	 * anything better.
+	 * if we don't have anything better.
 	 */
 	if (slash) {
 		*slash++ = 0;
 		if (*cmd == '/')
 			exec_path = cmd;
+		else
+			exec_path = xstrdup(make_absolute_path(cmd));
 		cmd = slash;
 	}
 
-- 
1.5.3.4.1287.g8b31e
David Brown· Oct 20, 2007, 12:25 UTC · re: Johannes Schindelin · lore

Re: [PATCH] Deduce exec_path also from calls to git with a relative path

On Sat, Oct 20, 2007 at 08:21:34AM +0100, Johannes Schindelin wrote:
>For example, when you call "../../hello/world/git", it will not turn
>"../../hello/world" into an absolute path, and use that.
Did you mean "it will turn..."?
David
Johannes Schindelin· Oct 20, 2007, 21:25 UTC · re: David Brown · lore

Re: [PATCH] Deduce exec_path also from calls to git with a relative path

Hi,
On Sat, 20 Oct 2007, David Brown wrote:
Show 6 quoted lines
> On Sat, Oct 20, 2007 at 08:21:34AM +0100, Johannes Schindelin wrote:
> 
> > For example, when you call "../../hello/world/git", it will not turn 
> > "../../hello/world" into an absolute path, and use that.
> 
> Did you mean "it will turn..."?

Yes, I meant that. I was in a hurry, since a car was waiting outside the door, taking me to the highlands.

Ciao, Dscho

← back to recent threads