Skip to content

Commit 4804aab

Browse files
sprohaskagitster
authored andcommitted
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 <prohaska@zib.de> Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 868da8d commit 4804aab

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

compat/mingw.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,3 +1017,25 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler)
10171017
timer_fn = handler;
10181018
return old;
10191019
}
1020+
1021+
static const char *make_backslash_path(const char *path)
1022+
{
1023+
static char buf[PATH_MAX + 1];
1024+
char *c;
1025+
1026+
if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
1027+
die("Too long path: %.*s", 60, path);
1028+
1029+
for (c = buf; *c; c++) {
1030+
if (*c == '/')
1031+
*c = '\\';
1032+
}
1033+
return buf;
1034+
}
1035+
1036+
void mingw_open_html(const char *unixpath)
1037+
{
1038+
const char *htmlpath = make_backslash_path(unixpath);
1039+
printf("Launching default browser to display HTML ...\n");
1040+
ShellExecute(NULL, "open", htmlpath, NULL, "\\", 0);
1041+
}

compat/mingw.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ sig_handler_t mingw_signal(int sig, sig_handler_t handler);
202202
#define PATH_SEP ';'
203203
#define PRIuMAX "I64u"
204204

205+
void mingw_open_html(const char *path);
206+
#define open_html mingw_open_html
207+
205208
/*
206209
* helpers
207210
*/

help.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,14 +644,26 @@ static void get_html_page_path(struct strbuf *page_path, const char *page)
644644
strbuf_addf(page_path, "%s/%s.html", html_path, page);
645645
}
646646

647+
/*
648+
* If open_html is not defined in a platform-specific way (see for
649+
* example compat/mingw.h), we use the script web--browse to display
650+
* HTML.
651+
*/
652+
#ifndef open_html
653+
void open_html(const char *path)
654+
{
655+
execl_git_cmd("web--browse", "-c", "help.browser", path, NULL);
656+
}
657+
#endif
658+
647659
static void show_html_page(const char *git_cmd)
648660
{
649661
const char *page = cmd_to_page(git_cmd);
650662
struct strbuf page_path; /* it leaks but we exec bellow */
651663

652664
get_html_page_path(&page_path, page);
653665

654-
execl_git_cmd("web--browse", "-c", "help.browser", page_path.buf, NULL);
666+
open_html(page_path.buf);
655667
}
656668

657669
void help_unknown_cmd(const char *cmd)

0 commit comments

Comments
 (0)