Skip to content

Commit 6854a8f

Browse files
peffgitster
authored andcommitted
common-main: stop munging argv[0] path
Since 650c449 (common-main: call git_extract_argv0_path(), 2016-07-01), the argv[0] that is seen in cmd_main() of individual programs is always the basename of the executable, as common-main strips off the full path. This can produce confusing results for git-daemon, which wants to re-exec itself. For instance, if the program was originally run as "/usr/lib/git/git-daemon", it will try just re-execing "git-daemon", which will find the first instance in $PATH. If git's exec-path has not been prepended to $PATH, we may find the git-daemon from a different version (or no git-daemon at all). Normally this isn't a problem. Git commands are run as "git daemon", the git wrapper puts the exec-path at the front of $PATH, and argv[0] is already "daemon" anyway. But running git-daemon via its full exec-path, while not really a recommended method, did work prior to 650c449. Let's make it work again. The real goal of 650c449 was not to munge argv[0], but to reliably set the argv0_path global. The only reason it munges at all is that one caller, the git.c wrapper, piggy-backed on that computation to find the command basename. Instead, let's leave argv[0] untouched in common-main, and have git.c do its own basename computation. While we're at it, let's drop the return value from git_extract_argv0_path(). It was only ever used in this one callsite, and its dual purposes is what led to this confusion in the first place. Note that by changing the interface, the compiler can confirm for us that there are no other callers storing the return value. But the compiler can't tell us whether any of the cmd_main() functions (besides git.c) were relying on the basename munging. However, we can observe that prior to 650c449, no other cmd_main() functions did that munging, and no new cmd_main() functions have been introduced since then. So we can't be regressing any of those cases. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5c238e2 commit 6854a8f

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

common-main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main(int argc, const char **argv)
3333

3434
git_setup_gettext();
3535

36-
argv[0] = git_extract_argv0_path(argv[0]);
36+
git_extract_argv0_path(argv[0]);
3737

3838
restore_sigpipe_to_default();
3939

exec_cmd.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,17 @@ char *system_path(const char *path)
3838
return strbuf_detach(&d, NULL);
3939
}
4040

41-
const char *git_extract_argv0_path(const char *argv0)
41+
void git_extract_argv0_path(const char *argv0)
4242
{
4343
const char *slash;
4444

4545
if (!argv0 || !*argv0)
46-
return NULL;
46+
return;
4747

4848
slash = find_last_dir_sep(argv0);
4949

50-
if (slash) {
50+
if (slash)
5151
argv0_path = xstrndup(argv0, slash - argv0);
52-
return slash + 1;
53-
}
54-
55-
return argv0;
5652
}
5753

5854
void git_set_argv_exec_path(const char *exec_path)

exec_cmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
struct argv_array;
55

66
extern void git_set_argv_exec_path(const char *exec_path);
7-
extern const char *git_extract_argv0_path(const char *path);
7+
extern void git_extract_argv0_path(const char *path);
88
extern const char *git_exec_path(void);
99
extern void setup_path(void);
1010
extern const char **prepare_git_cmd(struct argv_array *out, const char **argv);

git.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,11 @@ int cmd_main(int argc, const char **argv)
617617
cmd = argv[0];
618618
if (!cmd)
619619
cmd = "git-help";
620+
else {
621+
const char *slash = find_last_dir_sep(cmd);
622+
if (slash)
623+
cmd = slash + 1;
624+
}
620625

621626
trace_command_performance(argv);
622627

0 commit comments

Comments
 (0)