help: make 'git-help--browse' usable outside 'git-help'.
[git/git.git] / git-help--browse.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
482cce82 19USAGE='[--browser=browser|--tool=browser] url/file ...'
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
5d6491c7
CC
26valid_tool() {
27 case "$1" in
28 firefox | iceweasel | konqueror | w3m | links | lynx | dillo)
29 ;; # happy
30 *)
31 return 1
32 ;;
33 esac
34}
35
36init_browser_path() {
70087cdb 37 browser_path=`git config browser.$1.path`
5d6491c7
CC
38 test -z "$browser_path" && browser_path=$1
39}
40
41while test $# != 0
42do
43 case "$1" in
44 -b|--browser*|-t|--tool*)
45 case "$#,$1" in
46 *,*=*)
47 browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
48 ;;
49 1,*)
50 usage ;;
51 *)
52 browser="$2"
53 shift ;;
54 esac
55 ;;
56 --)
57 break
58 ;;
59 -*)
60 usage
61 ;;
62 *)
63 break
64 ;;
65 esac
66 shift
67done
68
482cce82
CC
69test $# = 0 && usage
70
70087cdb 71if test -z "$browser"
22c90717 72then
d3a866bc
CC
73 for opt in "help.browser" "web.browser"
74 do
75 browser="`git config $opt`"
76 test -z "$browser" || break
77 done
5d6491c7 78 if test -n "$browser" && ! valid_tool "$browser"; then
d3a866bc 79 echo >&2 "git config option $opt set to unknown browser: $browser"
5d6491c7
CC
80 echo >&2 "Resetting to default..."
81 unset browser
82 fi
83fi
84
85if test -z "$browser" ; then
86 if test -n "$DISPLAY"; then
87 browser_candidates="firefox iceweasel konqueror w3m links lynx dillo"
88 if test "$KDE_FULL_SESSION" = "true"; then
89 browser_candidates="konqueror $browser_candidates"
90 fi
91 else
92 browser_candidates="w3m links lynx"
93 fi
22c90717 94
5d6491c7
CC
95 for i in $browser_candidates; do
96 init_browser_path $i
97 if type "$browser_path" > /dev/null 2>&1; then
98 browser=$i
99 break
100 fi
101 done
102 test -z "$browser" && die "No known browser available."
103else
104 valid_tool "$browser" || die "Unknown browser '$browser'."
105
106 init_browser_path "$browser"
107
108 if ! type "$browser_path" > /dev/null 2>&1; then
109 die "The browser $browser is not available as '$browser_path'."
110 fi
111fi
112
5d6491c7
CC
113case "$browser" in
114 firefox|iceweasel)
115 # Check version because firefox < 2.0 does not support "-new-tab".
116 vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
117 NEWTAB='-new-tab'
118 test "$vers" -lt 2 && NEWTAB=''
482cce82 119 nohup "$browser_path" $NEWTAB "$@" &
5d6491c7
CC
120 ;;
121 konqueror)
122 case "$(basename "$browser_path")" in
123 konqueror)
124 # It's simpler to use kfmclient to open a new tab in konqueror.
125 browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
126 type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
482cce82 127 eval "$browser_path" newTab "$@"
5d6491c7
CC
128 ;;
129 kfmclient)
482cce82 130 eval "$browser_path" newTab "$@"
5d6491c7
CC
131 ;;
132 *)
482cce82 133 nohup "$browser_path" "$@" &
5d6491c7
CC
134 ;;
135 esac
136 ;;
137 w3m|links|lynx)
482cce82 138 eval "$browser_path" "$@"
5d6491c7
CC
139 ;;
140 dillo)
482cce82 141 nohup "$browser_path" "$@" &
5d6491c7
CC
142 ;;
143esac