Refactor git_set_argv0_path() to git_extract_argv0_path()
This commit moves the code that computes the dirname of argv[0] from git.c's main() to git_set_argv0_path() and renames the function to git_extract_argv0_path(). This makes the code in git.c's main less cluttered, and we can use the dirname computation from other main() functions too. [ spr: - split Steve's original commit and wrote new commit message. - Integrated Johannes Schindelin's cca1704897e7fdb182f68d4c48a437c5d7bc5203 while rebasing onto master. ] Signed-off-by: Steve Haslam <shaslam@lastminute.com> Signed-off-by: Steffen Prohaska <prohaska@zib.de> Acked-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Steve Haslam committed
Jan 18, 2009 at 13:00 UTC
4dd47c3b867f51211d0dc4474dab0fee5ca614da
3 files changed
+19
-18
exec_cmd.c
+12
-2
@@ -20,9 +20,19 @@ const char *system_path(const char *path)
20
return path;
21
}
22
23
-void git_set_argv0_path(const char *path)
23
+const char *git_extract_argv0_path(const char *argv0)
24
{
25
- argv0_path = path;
25
+ const char *slash = argv0 + strlen(argv0);
26
+
27
+ while (argv0 <= slash && !is_dir_sep(*slash))
28
+ slash--;
29
+
30
+ if (slash >= argv0) {
31
+ argv0_path = xstrndup(argv0, slash - argv0);
32
+ return slash + 1;
33
+ }
34
+
35
+ return argv0;
36
}
37
38
void git_set_argv_exec_path(const char *exec_path)
exec_cmd.h
+2
-2
@@ -2,8 +2,8 @@
2
#define GIT_EXEC_CMD_H
3
4
extern void git_set_argv_exec_path(const char *exec_path);
5
-extern void git_set_argv0_path(const char *path);
6
-extern const char* git_exec_path(void);
5
+extern const char *git_extract_argv0_path(const char *path);
6
+extern const char *git_exec_path(void);
7
extern void setup_path(void);
8
extern const char **prepare_git_cmd(const char **argv);
9
extern int execv_git_cmd(const char **argv); /* NULL terminated */
git.c
+5
-14
@@ -442,21 +442,12 @@ static int run_argv(int *argcp, const char ***argv)
442
443
int main(int argc, const char **argv)
444
{
445
- const char *cmd = argv[0] && *argv[0] ? argv[0] : "git-help";
446
- char *slash = (char *)cmd + strlen(cmd);
445
+ const char *cmd;
446
448
- /*
449
- * Take the basename of argv[0] as the command
450
- * name, and the dirname as the default exec_path
451
- * if we don't have anything better.
452
- */
453
- while (cmd <= slash && !is_dir_sep(*slash))
454
- slash--;
455
- if (cmd <= slash) {
456
- *slash++ = 0;
457
- git_set_argv0_path(cmd);
458
- cmd = slash;
459
- }
447
+ if (argv[0] && *argv[0])
448
+ cmd = git_extract_argv0_path(argv[0]);
449
+ else
450
+ cmd = "git-help";
451
452
/*
453
* "git-xxxx" is the same as "git xxxx", but we obviously: