Skip to contentSkip to conversation

help: make 'git-help--browse' usable outside 'git-help'.

PATCH In review Christian Couder 2 participants 3 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 help: make 'git-help--browse' usable outside 'git-help'.

By moving some help specific stuff from 'git-help--browse.sh' into 'help.c', we make it possible to use 'git-help--browse' outside 'git-help'.

---
 Makefile            |    2 +-
 git-help--browse.sh |   24 +++++++++---------------
 help.c              |   18 +++++++++++++++++-
 3 files changed, 27 insertions(+), 17 deletions(-)
Makefile +1 -1
index 92341c4..a03fd2e 100644
@@ -819,6 +819,7 @@ git$X: git.o $(BUILTIN_OBJS) $(GITLIBS)
 
 help.o: help.c common-cmds.h GIT-CFLAGS
 	$(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) \
+		'-DGIT_HTML_PATH="$(htmldir_SQ)"' \
 		'-DGIT_MAN_PATH="$(mandir_SQ)"' \
 		'-DGIT_INFO_PATH="$(infodir_SQ)"' $<
 
@@ -839,7 +840,6 @@ $(patsubst %.sh,%,$(SCRIPT_SH)) : % : %.sh
 	    -e 's|@@PERL@@|$(PERL_PATH_SQ)|g' \
 	    -e 's/@@GIT_VERSION@@/$(GIT_VERSION)/g' \
 	    -e 's/@@NO_CURL@@/$(NO_CURL)/g' \
-	    -e 's|@@HTMLDIR@@|$(htmldir_SQ)|g' \
 	    [email protected] >$@+ && \
 	chmod +x $@+ && \
 	mv $@+ $@
git-help--browse.sh +9 -15
index 10b0a36..8189c08 100755
@@ -16,18 +16,13 @@
 # git maintainer.
 #
 
-USAGE='[--browser=browser|--tool=browser] [cmd to display] ...'
+USAGE='[--browser=browser|--tool=browser] url/file ...'
 
 # This must be capable of running outside of git directory, so
 # the vanilla git-sh-setup should not be used.
 NONGIT_OK=Yes
 . git-sh-setup
 
-# Install data.
-html_dir="@@HTMLDIR@@"
-
-test -f "$html_dir/git.html" || die "No documentation directory found."
-
 valid_tool() {
 	case "$1" in
 		firefox | iceweasel | konqueror | w3m | links | lynx | dillo)
@@ -71,6 +66,8 @@ do
     shift
 done
 
+test $# = 0 && usage
+
 if test -z "$browser"
 then
     for opt in "help.browser" "web.browser"
@@ -113,16 +110,13 @@ else
     fi
 fi
 
-pages=$(for p in "$@"; do echo "$html_dir/$p.html" ; done)
-test -z "$pages" && pages="$html_dir/git.html"
-
 case "$browser" in
     firefox|iceweasel)
 	# Check version because firefox < 2.0 does not support "-new-tab".
 	vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
 	NEWTAB='-new-tab'
 	test "$vers" -lt 2 && NEWTAB=''
-	nohup "$browser_path" $NEWTAB $pages &
+	nohup "$browser_path" $NEWTAB "$@" &
 	;;
     konqueror)
 	case "$(basename "$browser_path")" in
@@ -130,20 +124,20 @@ case "$browser" in
 		# It's simpler to use kfmclient to open a new tab in konqueror.
 		browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
 		type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
-		eval "$browser_path" newTab $pages
+		eval "$browser_path" newTab "$@"
 		;;
 	    kfmclient)
-		eval "$browser_path" newTab $pages
+		eval "$browser_path" newTab "$@"
 		;;
 	    *)
-	        nohup "$browser_path" $pages &
+	        nohup "$browser_path" "$@" &
 		;;
 	esac
 	;;
     w3m|links|lynx)
-	eval "$browser_path" $pages
+	eval "$browser_path" "$@"
 	;;
     dillo)
-	nohup "$browser_path" $pages &
+	nohup "$browser_path" "$@" &
 	;;
 esac
help.c +17 -1
index 1302a61..b929899 100644
@@ -328,10 +328,26 @@ static void show_info_page(const char *git_cmd)
 	execlp("info", "info", "gitman", page, NULL);
 }
 
+static void get_html_page_path(struct strbuf *page_path, const char *page)
+{
+	struct stat st;
+
+	/* 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);
+
+	strbuf_init(page_path, 0);
+	strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
+}
+
 static void show_html_page(const char *git_cmd)
 {
 	const char *page = cmd_to_page(git_cmd);
-	execl_git_cmd("help--browse", page, NULL);
+	struct strbuf page_path; /* it leaks but we exec bellow */
+
+	get_html_page_path(&page_path, page);
+
+	execl_git_cmd("help--browse", page_path.buf, NULL);
 }
 
 void help_unknown_cmd(const char *cmd)
Signature · 1 line
1.5.4.rc5.25.g7a831-dirty
#1 lore raw
Quoted 3 lines · Christian Couder

By moving some help specific stuff from 'git-help--browse.sh' into 'help.c', we make it possible to use 'git-help--browse' outside 'git-help'.

I was initially puzzled why Eric was CC'ed while reading this first patch in the 4-patch series. It would have been nicer to start the series by mentioning the ultimate goal of the series upfront. You are not making it usable just by anybody, but have a specific goal of sharing the mechanism to launch user's web browser for both help and instaweb.

Quoted 10 lines
git-help--browse.sh +2 -0
@@ -71,6 +66,8 @@ do
     shift
 done
 
+test $# = 0 && usage
+
 if test -z "$browser"
 then
     for opt in "help.browser" "web.browser"

git-help--browse.sh else · line 118

Quoted 6 lines
git-help--browse.sh +0 -3
@@ -113,16 +110,13 @@ else
     fi
 fi
 
-pages=$(for p in "$@"; do echo "$html_dir/$p.html" ; done)
-test -z "$pages" && pages="$html_dir/git.html"
-

When the helper was run without specifying any page, it used to show the toplevel "git" documentation. If your eventual goal is to allow general browsing, obviously you do not want such a logic here, and it needs to migrate to the caller. So far, it makes perfect sense. Let's read on.

Quoted 9 lines
help.c +1 -0
index 1302a61..b929899 100644
@@ -328,10 +328,26 @@ static void show_info_page(const char *git_cmd)
 	execlp("info", "info", "gitman", page, NULL);
 }
 
+static void get_html_page_path(struct strbuf *page_path, const char *page)

help.c static void show_info_page(const char *git_cmd) · line 341

Quoted 10 lines
help.c +10 -0
+{
+	struct stat st;
+
+	/* 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);
+
+	strbuf_init(page_path, 0);
+	strbuf_addf(page_path, GIT_HTML_PATH "/%s.html", page);
+}

Gets a "page", makes page_path by concatenating the manual page location. Looks Ok.

help.c static void show_info_page(const char *git_cmd) · line 334

Quoted 4 lines
help.c +0 -1
 static void show_html_page(const char *git_cmd)
 {
 	const char *page = cmd_to_page(git_cmd);
-	execl_git_cmd("help--browse", page, NULL);

So this function used to give whatever "page" it got back from cmd_to_page(). Maybe it could have been NULL but that would have been handled by the browser helper just fine.

A reviewer would be left wondering if this means that you lost the fallback to the git top page.

help.c static void show_info_page(const char *git_cmd) · line 351

Quoted 6 lines
help.c +5 -0
+	struct strbuf page_path; /* it leaks but we exec bellow */
+
+	get_html_page_path(&page_path, page);
+
+	execl_git_cmd("help--browse", page_path.buf, NULL);
 }

And this part makes the reviewer even more worried. If page could be NULL, then get_html_page_path() would be fed a NULL pointer, which is given to strbuf_addf()! Ugh.

Then the reviewer would find out that cmd_to_page() would never return NULL, as it has its own NULL-to-"git" fallback logic.

I think the code is good, but the proposed commit log message has some room for improvements.

Something like...

[PATCH 1/4] help: make 'git-help--browse' usable outside 'git-help'

"git-help--browse" helper is to launch a browser of the user's choice to view the HTML version of git documentation for a given command. It used to take the name of a command, convert it to the path of the documentation by prefixing the directory name and appending the ".html" suffix, and start the browser on the path.

This updates the division of labor between the caller in help.c and git-help--browser helper. The helper is now responsible for launching a browser of the user's choice on given URLs, and it is the caller's responsibility to tell it the paths to documentation files.

This is in preparation to reuse the logic to choose user's preferred browser in instaweb.

The helper had a provision for running it without any command name, in which case it showed the toplevel "git(7)" documentation, but the caller in help.c never makes such a call. The helper now exits with a usage message when no path is given.

Signed-off-by: ...
---

* Eric is CC'ed because the ultimate goal of this series is to get rid of the duplicated logic between help--browse and instaweb.

Makefile | 2 +-
git-help--browse.sh | 24 +++++++++---------------
...

I have given only a cursory look at the remainder of the series (I'll hopefully be in a mini vacation mode after the release), but I think overall the series makes sense.

Thanks.

#2 lore raw
Quoted 12 lines · Junio C Hamano

help.c static void show_info_page(const char *git_cmd) · line 351

Quoted 6 lines · Christian Couder
help.c +5 -0
+	struct strbuf page_path; /* it leaks but we exec bellow */
+
+	get_html_page_path(&page_path, page);
+
+	execl_git_cmd("help--browse", page_path.buf, NULL);
 }

And this part makes the reviewer even more worried. If page could be NULL, then get_html_page_path() would be fed a NULL pointer, which is given to strbuf_addf()! Ugh.

Quoted 5 lines

Then the reviewer would find out that cmd_to_page() would never return NULL, as it has its own NULL-to-"git" fallback logic.

I think the code is good, but the proposed commit log message has some room for improvements.

Yeah, I could have explained some parts of the patch more. I will try to do better.

Quoted 17 lines

Something like...

[PATCH 1/4] help: make 'git-help--browse' usable outside 'git-help'

"git-help--browse" helper is to launch a browser of the user's choice to view the HTML version of git documentation for a given command. It used to take the name of a command, convert it to the path of the documentation by prefixing the directory name and appending the ".html" suffix, and start the browser on the path.

This updates the division of labor between the caller in help.c and git-help--browser helper. The helper is now responsible for launching a browser of the user's choice on given URLs, and it is the caller's responsibility to tell it the paths to documentation files.

Quoted 8 lines

This is in preparation to reuse the logic to choose user's preferred browser in instaweb.

The helper had a provision for running it without any command name, in which case it showed the toplevel "git(7)" documentation, but the caller in help.c never makes such a call. The helper now exits with a usage message when no path is given.

Great commit message indeed! Though I fear that such long messages (for a not very long patch) could take precious screen real estate when using "git log" or otherwise bother some other readers.

Anyway do you want me to resend the patch or the series with improved commit messages ?

Quoted 7 lines

Signed-off-by: ...
---

* Eric is CC'ed because the ultimate goal of this series is to get rid of the duplicated logic between help--browse and instaweb.

Quoted 6 lines

Makefile | 2 +-
git-help--browse.sh | 24 +++++++++---------------
...

I have given only a cursory look at the remainder of the series (I'll hopefully be in a mini vacation mode after the release),

You definitely deserve it. Thanks for your great release and maintainer work.

Quoted 1 line

but I think overall the series makes sense.

Thanks,
Christian.