[PATCH] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack.

Subsystems: the rest

STALE3706d

5 messages, 2 authors, 2016-06-15 · open the first message on its own page

[PATCH] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack.

From: Steve Haslam <hidden>
Date: 2016-06-15 22:45:04

If the user specified the full path to git-upload-pack as the -u option to
"git clone" when cloning a remote repository, and git was not on the default
PATH on the remote machine, git-upload-pack was failing to exec
git-pack-objects.

By making the argv[0] path (if any) available to setup_path(), this will
allow finding the "git" executable in the same directory as
"git-upload-pack". The default built in to exec_cmd.c is to look for "git"
in the ".../libexec/git-core" directory, but it is not installed there (any
longer).

Much the same applies to invoking git-receive-pack from a non-PATH location
using the "--exec" argument to "git push". Since both commands require the
same treatment, a single function has been added in exec_cmd.c to serve both.

Signed-off-by: Steve Haslam <redacted>
---
 exec_cmd.c     |   15 +++++++++++++--
 exec_cmd.h     |    2 +-
 git.c          |   20 +++++---------------
 receive-pack.c |    3 +++
 upload-pack.c  |    3 +++
 5 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/exec_cmd.c b/exec_cmd.c
index ce6741e..a2a6a0d 100644
--- a/exec_cmd.c
+++ b/exec_cmd.c
@@ -17,9 +17,20 @@ const char *system_path(const char *path)
 	return path;
 }
 
-void git_set_argv0_path(const char *path)
+const char *git_extract_argv0_path(const char *argv0)
 {
-	argv0_path = path;
+	const char *slash = argv0 + strlen(argv0);
+
+	do
+		--slash;
+	while (slash >= argv0 && !is_dir_sep(*slash));
+
+	if (slash >= argv0) {
+		argv0_path = xstrndup(argv0, slash - argv0);
+		return slash + 1;
+	}
+
+	return argv0;
 }
 
 void git_set_argv_exec_path(const char *exec_path)
diff --git a/exec_cmd.h b/exec_cmd.h
index 594f961..392e903 100644
--- a/exec_cmd.h
+++ b/exec_cmd.h
@@ -2,7 +2,7 @@
 #define GIT_EXEC_CMD_H
 
 extern void git_set_argv_exec_path(const char *exec_path);
-extern void git_set_argv0_path(const char *path);
+extern const char* git_extract_argv0_path(const char *path);
 extern const char* git_exec_path(void);
 extern void setup_path(void);
 extern const char **prepare_git_cmd(const char **argv);
diff --git a/git.c b/git.c
index 37b1d76..b1eff4a 100644
--- a/git.c
+++ b/git.c
@@ -416,23 +416,13 @@ static void execv_dashed_external(const char **argv)
 
 int main(int argc, const char **argv)
 {
-	const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
-	char *slash = (char *)cmd + strlen(cmd);
+	const char *cmd;
 	int done_alias = 0;
 
-	/*
-	 * Take the basename of argv[0] as the command
-	 * name, and the dirname as the default exec_path
-	 * if we don't have anything better.
-	 */
-	do
-		--slash;
-	while (cmd <= slash && !is_dir_sep(*slash));
-	if (cmd <= slash) {
-		*slash++ = 0;
-		git_set_argv0_path(cmd);
-		cmd = slash;
-	}
+	if (argv[0] && *argv[0])
+		cmd = git_extract_argv0_path(argv[0]);
+	else
+		cmd = "git-help";
 
 	/*
 	 * "git-xxxx" is the same as "git xxxx", but we obviously:
diff --git a/receive-pack.c b/receive-pack.c
index d44c19e..3699b16 100644
--- a/receive-pack.c
+++ b/receive-pack.c
@@ -467,6 +467,9 @@ int main(int argc, char **argv)
 	int i;
 	char *dir = NULL;
 
+	if (argv[0] && *argv[0])
+		git_extract_argv0_path(argv[0]);
+
 	argv++;
 	for (i = 1; i < argc; i++) {
 		char *arg = *argv++;
diff --git a/upload-pack.c b/upload-pack.c
index c911e70..086eff6 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -616,6 +616,9 @@ int main(int argc, char **argv)
 	int i;
 	int strict = 0;
 
+	if (argv[0] && *argv[0])
+		git_extract_argv0_path(argv[0]);
+
 	for (i = 1; i < argc; i++) {
 		char *arg = argv[i];
 
-- 
1.5.6.3

Re: [PATCH] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack.

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:45:04

Hi,
quoted hunk
diff --git a/upload-pack.c b/upload-pack.c
index c911e70..086eff6 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -616,6 +616,9 @@ int main(int argc, char **argv)
 	int i;
 	int strict = 0;
 
+	if (argv[0] && *argv[0])
+		git_extract_argv0_path(argv[0]);
+
This is ugly.  The called function should already do it itself.

Further, why not go the full nine yards and avoid the calculation 
altogether, until it is necessary?  Then the change to add 
lookup_program_in_path() would be nice and non-intrusive.

IOW why not leave the function name as-is, and just enhance system_path() 
to have a static variable "initialized", which does the whole calculation? 
I.e. move the calculation from git.c to exec_cmd.c, but at the same time 
do it only when needed.

And your change to set argv0_path from receive-pack and upload-pack would 
be a second patch.

And then the patch to add support to "glean" (did not know that word) the 
path from the PATH (lookup_program_in_path()) could come as a third patch.

Ciao,
Dscho

Re: [PATCH] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack.

From: Steve Haslam <hidden>
Date: 2016-06-15 22:45:05

[sorry, was intended to cc Johannes Sixt when I mailed the patch to the 
list... not doing well with the recipient etiquette]

[and then Icedove decided it was going to send mail as HTML. This is so embarassing.]

Johannes Schindelin wrote:
quoted
diff --git a/upload-pack.c b/upload-pack.c
index c911e70..086eff6 100644
--- a/upload-pack.c
+++ b/upload-pack.c
@@ -616,6 +616,9 @@ int main(int argc, char **argv)
 	int i;
 	int strict = 0;
 
+	if (argv[0] && *argv[0])
+		git_extract_argv0_path(argv[0]);
+
    
This is ugly.  The called function should already do it itself.
  
Fair enough.
Further, why not go the full nine yards and avoid the calculation 
altogether, until it is necessary?  Then the change to add 
lookup_program_in_path() would be nice and non-intrusive.
git.c will always need to do the calculation, to determine which command 
it is being invoked as, so is there much value in delaying until necessary?

If the code in git.c is left alone, then it needs to be eventually 
duplicated in upload-pack.c and receive-pack.c, or in exec_cmd.c. [I 
botched when sending out the patch originally and only sent it to 
Johannes and not the list, so it's my fault that the history of how this 
has evolved is unclear I'm afraid]
IOW why not leave the function name as-is, and just enhance system_path() 
to have a static variable "initialized", which does the whole calculation? 
I.e. move the calculation from git.c to exec_cmd.c, but at the same time 
do it only when needed.
Hmm, system_path and setup_path both use argv0_path; git.c would need to 
call an additional function in exec_cmd.c to get the "leafname" result 
of the calculation, though.
And your change to set argv0_path from receive-pack and upload-pack would 
be a second patch.
OK.
And then the patch to add support to "glean" (did not know that word) the 
path from the PATH (lookup_program_in_path()) could come as a third patch.
I think that once git-upload-pack.c et al get the argv[0] path over to 
setup_path() then there's nothing more to do; setup_path() already uses 
argv0_path in its list of paths to try. I'm confused to the reference to 
PATH, though: we're avoiding the PATH environment variable completely.

SRH

Re: [PATCH] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack.

From: Johannes Schindelin <hidden>
Date: 2016-06-15 22:45:05

Hi,

On Wed, 30 Jul 2008, Steve Haslam wrote:
I think that once git-upload-pack.c et al get the argv[0] path over to 
setup_path() then there's nothing more to do; setup_path() already uses 
argv0_path in its list of paths to try. I'm confused to the reference to 
PATH, though: we're avoiding the PATH environment variable completely.
There is an issue in setting up the argv0_path when Git is called as "git" 
and resolved via the PATH: argv[0] then is literally "git".

And we need argv0_path for other things than just calling programs: 
relative etc/gitconfig and libexecdir.

Ciao,
Dscho

Re: [PATCH] Glean libexec path from argv[0] for git-upload-pack and git-receive-pack.

From: Steve Haslam <hidden>
Date: 2016-06-15 22:45:05

Johannes Schindelin wrote:
Hi,

On Wed, 30 Jul 2008, Steve Haslam wrote:
  
quoted
I think that once git-upload-pack.c et al get the argv[0] path over to 
setup_path() then there's nothing more to do; setup_path() already uses 
argv0_path in its list of paths to try. I'm confused to the reference to 
PATH, though: we're avoiding the PATH environment variable completely.
    
There is an issue in setting up the argv0_path when Git is called as "git" 
and resolved via the PATH: argv[0] then is literally "git".

And we need argv0_path for other things than just calling programs: 
relative etc/gitconfig and libexecdir.
  
Ah, I see what you meant. And all setup_path() actually does is add to 
the end of PATH. I should have understood before.

OK, I will split the patch up and tidy it.

SRH
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help