git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] If git is ran with a relative path, try building an absolute exec_path from it
@ 2007-10-20  6:46 Scott R Parish
  2007-10-20  7:21 ` [PATCH] Deduce exec_path also from calls to git with a relative path Johannes Schindelin
  0 siblings, 1 reply; 4+ messages in thread
From: Scott R Parish @ 2007-10-20  6:46 UTC (permalink / raw)
  To: git

 Signed-off-by: Scott R Parish <srp@srparish.net>

---
 git.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)

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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-10-21  2:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-20  6:46 [PATCH] If git is ran with a relative path, try building an absolute exec_path from it Scott R Parish
2007-10-20  7:21 ` [PATCH] Deduce exec_path also from calls to git with a relative path Johannes Schindelin
2007-10-20 12:25   ` David Brown
2007-10-20 21:25     ` Johannes Schindelin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).