From 2de9de5e4ae1353f1552f61cf8cf532e3f1dc5f6 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Jul 2008 22:31:18 +0200 Subject: Move code interpreting path relative to exec-dir to new function system_path() Expanding system paths relative to git_exec_path can be used for creating an installation that can be moved to a different directory without re-compiling. We use this approach for template_dir and the system wide gitconfig. The Windows installer (msysgit) is an example for such a setup. This commit moves common code to a new function system_path(). System paths that are to be interpreted relative to git_exec_path are passed to system_path() and the return value is used instead of the original path. system_path() prefixes a relative path with git_exec_path and leaves absolute paths unmodified. For example, we now write template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); [j6t: moved from path.c to exec_cmd.c] Signed-off-by: Steffen Prohaska Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- builtin-init-db.c | 14 ++------------ config.c | 11 ++--------- exec_cmd.c | 10 ++++++++++ exec_cmd.h | 2 +- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/builtin-init-db.c b/builtin-init-db.c index e23b8438c7..5ba213a595 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -115,18 +115,8 @@ static void copy_templates(const char *template_dir) if (!template_dir) template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); - if (!template_dir) { - /* - * if the hard-coded template is relative, it is - * interpreted relative to the exec_dir - */ - template_dir = DEFAULT_GIT_TEMPLATE_DIR; - if (!is_absolute_path(template_dir)) { - struct strbuf d = STRBUF_INIT; - strbuf_addf(&d, "%s/%s", git_exec_path(), template_dir); - template_dir = strbuf_detach(&d, NULL); - } - } + if (!template_dir) + template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); strcpy(template_path, template_dir); template_len = strlen(template_path); if (template_path[template_len-1] != '/') { diff --git a/config.c b/config.c index 2862cc45cb..1e066c71e0 100644 --- a/config.c +++ b/config.c @@ -581,15 +581,8 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data) const char *git_etc_gitconfig(void) { static const char *system_wide; - if (!system_wide) { - system_wide = ETC_GITCONFIG; - if (!is_absolute_path(system_wide)) { - /* interpret path relative to exec-dir */ - struct strbuf d = STRBUF_INIT; - strbuf_addf(&d, "%s/%s", git_exec_path(), system_wide); - system_wide = strbuf_detach(&d, NULL); - } - } + if (!system_wide) + system_wide = system_path(ETC_GITCONFIG); return system_wide; } diff --git a/exec_cmd.c b/exec_cmd.c index da04efe951..8899e31b3b 100644 --- a/exec_cmd.c +++ b/exec_cmd.c @@ -40,6 +40,16 @@ static const char *builtin_exec_path(void) #endif } +const char *system_path(const char *path) +{ + if (!is_absolute_path(path)) { + struct strbuf d = STRBUF_INIT; + strbuf_addf(&d, "%s/%s", git_exec_path(), path); + path = strbuf_detach(&d, NULL); + } + return path; +} + void git_set_argv_exec_path(const char *exec_path) { argv_exec_path = exec_path; diff --git a/exec_cmd.h b/exec_cmd.h index a892355c82..7eb94e5e11 100644 --- a/exec_cmd.h +++ b/exec_cmd.h @@ -6,6 +6,6 @@ extern const char* git_exec_path(void); extern void setup_path(const char *); extern int execv_git_cmd(const char **argv); /* NULL terminated */ extern int execl_git_cmd(const char *cmd, ...); - +extern const char *system_path(const char *path); #endif /* GIT_EXEC_CMD_H */ -- cgit v1.2.3 From 868da8d5e329c951f0d0cd049a8f9fecda64d388 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Jul 2008 22:31:19 +0200 Subject: help.c: Add support for htmldir relative to git_exec_path() If htmldir (in the Makefile) is a relative path, this path will now be interpreted relative to git_exec_path. This can be used to create an installation that can be moved to a different directory without re-compiling. The Windows installer (msysgit) is an example for such a setup. Note that the Makefile maps htmldir to the define GIT_HTML_PATH. Signed-off-by: Steffen Prohaska Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- help.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/help.c b/help.c index ca9632b6c5..0f055bf9aa 100644 --- a/help.c +++ b/help.c @@ -633,13 +633,15 @@ static void show_info_page(const char *git_cmd) static void get_html_page_path(struct strbuf *page_path, const char *page) { struct stat st; + const char *html_path = system_path(GIT_HTML_PATH); /* Check that we have a git documentation directory. */ - if (stat(GIT_HTML_PATH "/git.html", &st) || !S_ISREG(st.st_mode)) - die("'%s': not a documentation directory.", GIT_HTML_PATH); + if (stat(mkpath("%s/git.html", html_path), &st) + || !S_ISREG(st.st_mode)) + die("'%s': not a documentation directory.", html_path); strbuf_init(page_path, 0); - strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page); + strbuf_addf(page_path, "%s/%s.html", html_path, page); } static void show_html_page(const char *git_cmd) -- cgit v1.2.3 From 4804aabcdbffb41dba96825ca2693ea45830a108 Mon Sep 17 00:00:00 2001 From: Steffen Prohaska Date: Sun, 13 Jul 2008 22:31:20 +0200 Subject: help (Windows): Display HTML in default browser using Windows' shell API The system's default browser for displaying HTML help pages is now used directly on Windows, instead of launching git-web--browser, which requires a Unix shell. Avoiding MSYS' bash when possible is good because it avoids potential path translation issues. In this case it is not too hard to avoid launching a shell, so let's avoid it. The Windows-specific code is implemented in compat/mingw.c to avoid platform-specific code in the main code base. On Windows, open_html is provided as a define. If open_html is not defined, git-web--browse is used. This approach avoids platform-specific ifdefs by using per-function ifdefs. The "ifndef open_html" together with the introductory comment should sufficiently warn developers, so that they hopefully will not break this mechanism. Signed-off-by: Steffen Prohaska Signed-off-by: Johannes Sixt Signed-off-by: Junio C Hamano --- compat/mingw.c | 22 ++++++++++++++++++++++ compat/mingw.h | 3 +++ help.c | 14 +++++++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/compat/mingw.c b/compat/mingw.c index 3a05fe7da6..c0bc849e45 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -1017,3 +1017,25 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler) timer_fn = handler; return old; } + +static const char *make_backslash_path(const char *path) +{ + static char buf[PATH_MAX + 1]; + char *c; + + if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX) + die("Too long path: %.*s", 60, path); + + for (c = buf; *c; c++) { + if (*c == '/') + *c = '\\'; + } + return buf; +} + +void mingw_open_html(const char *unixpath) +{ + const char *htmlpath = make_backslash_path(unixpath); + printf("Launching default browser to display HTML ...\n"); + ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0); +} diff --git a/compat/mingw.h b/compat/mingw.h index 6bc049ad99..5a3bcee29b 100644 --- a/compat/mingw.h +++ b/compat/mingw.h @@ -202,6 +202,9 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler); #define PATH_SEP ';' #define PRIuMAX "I64u" +void mingw_open_html(const char *path); +#define open_html mingw_open_html + /* * helpers */ diff --git a/help.c b/help.c index 0f055bf9aa..52d39b88a3 100644 --- a/help.c +++ b/help.c @@ -644,6 +644,18 @@ static void get_html_page_path(struct strbuf *page_path, const char *page) strbuf_addf(page_path, "%s/%s.html", html_path, page); } +/* + * If open_html is not defined in a platform-specific way (see for + * example compat/mingw.h), we use the script web--browse to display + * HTML. + */ +#ifndef open_html +void open_html(const char *path) +{ + execl_git_cmd("web--browse", "-c", "help.browser", path, NULL); +} +#endif + static void show_html_page(const char *git_cmd) { const char *page = cmd_to_page(git_cmd); @@ -651,7 +663,7 @@ static void show_html_page(const char *git_cmd) get_html_page_path(&page_path, page); - execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL); + open_html(page_path.buf); } void help_unknown_cmd(const char *cmd) -- cgit v1.2.3