common-main: call git_extract_argv0_path()

Every program which links against libgit.a must call this function, or risk hitting an assert() in system_path() that checks whether we have configured argv0_path (though only when RUNTIME_PREFIX is defined, so essentially only on Windows). Looking at the diff, you can see that putting it into the common main() saves us having to do it individually in each of the external commands. But what you can't see are the cases where we _should_ have been doing so, but weren't (e.g., git-credential-store, and all of the t/helper test programs). This has been an accident-waiting-to-happen for a long time, but wasn't triggered until recently because it involves one of those programs actually calling system_path(). That happened with git-credential-store in v2.8.0 with ae5f677 (lazily load core.sharedrepository, 2016-03-11). The program: - takes a lock file, which... - opens a tempfile, which... - calls adjust_shared_perm to fix permissions, which... - lazy-loads the config (as of ae5f677), which... - calls system_path() to find the location of /etc/gitconfig On systems with RUNTIME_PREFIX, this means credential-store reliably hits that assert() and cannot be used. We never noticed in the test suite, because we set GIT_CONFIG_NOSYSTEM there, which skips the system_path() lookup entirely. But if we were to tweak git_config() to find /etc/gitconfig even when we aren't going to open it, then the test suite shows multiple failures (for credential-store, and for some other test helpers). I didn't include that tweak here because it's way too specific to this particular call to be worth carrying around what is essentially dead code. The implementation is fairly straightforward, with one exception: there is exactly one caller (git.c) that actually cares about the result of the function, and not the side-effect of setting up argv0_path. We can accommodate that by simply replacing the value of argv[0] in the array we hand down to cmd_main(). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 1, 2016 at 02:04 UTC 650c449250d7279dcbfe2f7cc23624955d53d339
12 files changed +4 -19
common-main.c
+3
@@ -1,4 +1,5 @@
1 #include "git-compat-util.h"
2 +#include "exec_cmd.h"
3
4 int main(int argc, char **av)
5 {
@@ -8,5 +9,7 @@ int main(int argc, char **av)
9 */
10 const char **argv = (const char **)av;
11
12 + argv[0] = git_extract_argv0_path(argv[0]);
13 +
14 return cmd_main(argc, argv);
15 }
daemon.c
-3
@@ -1,6 +1,5 @@
1 #include "cache.h"
2 #include "pkt-line.h"
3 -#include "exec_cmd.h"
3 #include "run-command.h"
4 #include "strbuf.h"
5 #include "string-list.h"
@@ -1190,8 +1189,6 @@ int cmd_main(int argc, const char **argv)
1189
1190 git_setup_gettext();
1191
1193 - git_extract_argv0_path(argv[0]);
1194 -
1192 for (i = 1; i < argc; i++) {
1193 const char *arg = argv[i];
1194 const char *v;
fast-import.c
-3
@@ -164,7 +164,6 @@ Format of STDIN stream:
164 #include "refs.h"
165 #include "csum-file.h"
166 #include "quote.h"
167 -#include "exec_cmd.h"
167 #include "dir.h"
168
169 #define PACK_ID_BITS 16
@@ -3385,8 +3384,6 @@ int cmd_main(int argc, const char **argv)
3384 {
3385 unsigned int i;
3386
3388 - git_extract_argv0_path(argv[0]);
3389 -
3387 git_setup_gettext();
3388
3389 if (argc == 2 && !strcmp(argv[1], "-h"))
git.c
+1 -1
@@ -635,7 +635,7 @@ int cmd_main(int argc, const char **argv)
635 const char *cmd;
636 int done_help = 0;
637
638 - cmd = git_extract_argv0_path(argv[0]);
638 + cmd = argv[0];
639 if (!cmd)
640 cmd = "git-help";
641
http-backend.c
-1
@@ -642,7 +642,6 @@ int cmd_main(int argc, const char **argv)
642
643 git_setup_gettext();
644
645 - git_extract_argv0_path(argv[0]);
645 set_die_routine(die_webcgi);
646 set_die_is_recursing_routine(die_webcgi_recursing);
647
http-fetch.c
-2
@@ -24,8 +24,6 @@ int cmd_main(int argc, const char **argv)
24
25 git_setup_gettext();
26
27 - git_extract_argv0_path(argv[0]);
28 -
27 while (arg < argc && argv[arg][0] == '-') {
28 if (argv[arg][1] == 't') {
29 get_tree = 1;
http-push.c
-2
@@ -1711,8 +1711,6 @@ int cmd_main(int argc, const char **argv)
1711
1712 git_setup_gettext();
1713
1714 - git_extract_argv0_path(argv[0]);
1715 -
1714 repo = xcalloc(1, sizeof(*repo));
1715
1716 argv++;
imap-send.c
-2
@@ -1500,8 +1500,6 @@ int cmd_main(int argc, const char **argv)
1500 int total;
1501 int nongit_ok;
1502
1503 - git_extract_argv0_path(argv[0]);
1504 -
1503 git_setup_gettext();
1504
1505 setup_git_directory_gently(&nongit_ok);
remote-curl.c
-1
@@ -991,7 +991,6 @@ int cmd_main(int argc, const char **argv)
991
992 git_setup_gettext();
993
994 - git_extract_argv0_path(argv[0]);
994 setup_git_directory_gently(&nongit);
995 if (argc < 2) {
996 error("remote-curl: usage: git remote-curl <remote> [<url>]");
remote-testsvn.c
-1
@@ -292,7 +292,6 @@ int cmd_main(int argc, const char **argv)
292 static struct remote *remote;
293 const char *url_in;
294
295 - git_extract_argv0_path(argv[0]);
295 setup_git_directory();
296 if (argc < 2 || argc > 3) {
297 usage("git-remote-svn <remote-name> [<url>]");
shell.c
-2
@@ -147,8 +147,6 @@ int cmd_main(int argc, const char **argv)
147
148 git_setup_gettext();
149
150 - git_extract_argv0_path(argv[0]);
151 -
150 /*
151 * Always open file descriptors 0/1/2 to avoid clobbering files
152 * in die(). It also avoids messing up when the pipes are dup'ed
upload-pack.c
-1
@@ -826,7 +826,6 @@ int cmd_main(int argc, const char **argv)
826 git_setup_gettext();
827
828 packet_trace_identity("upload-pack");
829 - git_extract_argv0_path(argv[0]);
829 check_replace_refs = 0;
830
831 for (i = 1; i < argc; i++) {