Merge branch 'wc/diff'
[git/git.git] / git-browse-help.sh
CommitLineData
5d6491c7
CC
1#!/bin/sh
2#
3# This program launch a web browser on the html page
4# describing a git command.
5#
6# Copyright (c) 2007 Christian Couder
7# Copyright (c) 2006 Theodore Y. Ts'o
8#
9# This file is heavily stolen from git-mergetool.sh, by
10# Theodore Y. Ts'o (thanks) that is:
11#
12# Copyright (c) 2006 Theodore Y. Ts'o
13#
14# This file is licensed under the GPL v2, or a later version
15# at the discretion of Junio C Hamano or any other official
16# git maintainer.
17#
18
19USAGE='[--browser=browser|--tool=browser] [cmd to display] ...'
22c90717
JH
20
21# This must be capable of running outside of git directory, so
22# the vanilla git-sh-setup should not be used.
23NONGIT_OK=Yes
5d6491c7
CC
24. git-sh-setup
25
26# Install data.
27html_dir="@@HTMLDIR@@"
28
29test -f "$html_dir/git.html" || die "No documentation directory found."
30
31valid_tool() {
32 case "$1" in
33 firefox | iceweasel | konqueror | w3m | links | lynx | dillo)
34 ;; # happy
35 *)
36 return 1
37 ;;
38 esac
39}
40
41init_browser_path() {
22c90717 42 test -z "$GIT_DIR" || browser_path=`git config browser.$1.path`
5d6491c7
CC
43 test -z "$browser_path" && browser_path=$1
44}
45
46while test $# != 0
47do
48 case "$1" in
49 -b|--browser*|-t|--tool*)
50 case "$#,$1" in
51 *,*=*)
52 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
53 ;;
54 1,*)
55 usage ;;
56 *)
57 browser="$2"
58 shift ;;
59 esac
60 ;;
61 --)
62 break
63 ;;
64 -*)
65 usage
66 ;;
67 *)
68 break
69 ;;
70 esac
71 shift
72done
73
22c90717
JH
74if test -z "$browser" && test -n "$GIT_DIR"
75then
d3a866bc
CC
76 for opt in "help.browser" "web.browser"
77 do
78 browser="`git config $opt`"
79 test -z "$browser" || break
80 done
5d6491c7 81 if test -n "$browser" && ! valid_tool "$browser"; then
d3a866bc 82 echo >&2 "git config option $opt set to unknown browser: $browser"
5d6491c7
CC
83 echo >&2 "Resetting to default..."
84 unset browser
85 fi
86fi
87
88if test -z "$browser" ; then
89 if test -n "$DISPLAY"; then
90 browser_candidates="firefox iceweasel konqueror w3m links lynx dillo"
91 if test "$KDE_FULL_SESSION" = "true"; then
92 browser_candidates="konqueror $browser_candidates"
93 fi
94 else
95 browser_candidates="w3m links lynx"
96 fi
22c90717 97
5d6491c7
CC
98 for i in $browser_candidates; do
99 init_browser_path $i
100 if type "$browser_path" > /dev/null 2>&1; then
101 browser=$i
102 break
103 fi
104 done
105 test -z "$browser" && die "No known browser available."
106else
107 valid_tool "$browser" || die "Unknown browser '$browser'."
108
109 init_browser_path "$browser"
110
111 if ! type "$browser_path" > /dev/null 2>&1; then
112 die "The browser $browser is not available as '$browser_path'."
113 fi
114fi
115
116pages=$(for p in "$@"; do echo "$html_dir/$p.html" ; done)
117test -z "$pages" && pages="$html_dir/git.html"
118
119case "$browser" in
120 firefox|iceweasel)
121 # Check version because firefox < 2.0 does not support "-new-tab".
122 vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
123 NEWTAB='-new-tab'
124 test "$vers" -lt 2 && NEWTAB=''
125 nohup "$browser_path" $NEWTAB $pages &
126 ;;
127 konqueror)
128 case "$(basename "$browser_path")" in
129 konqueror)
130 # It's simpler to use kfmclient to open a new tab in konqueror.
131 browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
132 type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
133 eval "$browser_path" newTab $pages
134 ;;
135 kfmclient)
136 eval "$browser_path" newTab $pages
137 ;;
138 *)
139 nohup "$browser_path" $pages &
140 ;;
141 esac
142 ;;
143 w3m|links|lynx)
144 eval "$browser_path" $pages
145 ;;
146 dillo)
147 nohup "$browser_path" $pages &
148 ;;
149esac