1#!/bin/sh2#3# This program launch a web browser on the html page4# describing a git command.5#6# Copyright (c) 2007 Christian Couder7# Copyright (c) 2006 Theodore Y. Ts'o8#9# This file is heavily stolen from git-mergetool.sh, by10# Theodore Y. Ts'o (thanks) that is:11#12# Copyright (c) 2006 Theodore Y. Ts'o13#14# This file is licensed under the GPL v2, or a later version15# at the discretion of Junio C Hamano or any other official16# git maintainer.17#1819USAGE='[--browser=browser|--tool=browser] [--config=conf.var] url/file ...'2021# This must be capable of running outside of git directory, so22# the vanilla git-sh-setup should not be used.23NONGIT_OK=Yes24. git-sh-setup2526valid_custom_tool()27{28browser_cmd="$(git config "browser.$1.cmd")"29test -n "$browser_cmd"30}3132valid_tool() {33case "$1" in34firefox | iceweasel | konqueror | w3m | links | lynx | dillo | open | start)35;; # happy36*)37valid_custom_tool "$1" || return 138;;39esac40}4142init_browser_path() {43browser_path=$(git config "browser.$1.path")44test -z "$browser_path" && browser_path="$1"45}4647while test $# != 048do49case "$1" in50-b|--browser*|-t|--tool*)51case "$#,$1" in52*,*=*)53browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`54;;551,*)56usage ;;57*)58browser="$2"59shift ;;60esac61;;62-c|--config*)63case "$#,$1" in64*,*=*)65conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`66;;671,*)68usage ;;69*)70conf="$2"71shift ;;72esac73;;74--)75break76;;77-*)78usage79;;80*)81break82;;83esac84shift85done8687test $# = 0 && usage8889if test -z "$browser"90then91for opt in "$conf" "web.browser"92do93test -z "$opt" && continue94browser="`git config $opt`"95test -z "$browser" || break96done97if test -n "$browser" && ! valid_tool "$browser"; then98echo >&2 "git config option $opt set to unknown browser: $browser"99echo >&2 "Resetting to default..."100unset browser101fi102fi103104if test -z "$browser" ; then105if test -n "$DISPLAY"; then106browser_candidates="firefox iceweasel konqueror w3m links lynx dillo"107if test "$KDE_FULL_SESSION" = "true"; then108browser_candidates="konqueror $browser_candidates"109fi110else111browser_candidates="w3m links lynx"112fi113# SECURITYSESSIONID indicates an OS X GUI login session114if test -n "$SECURITYSESSIONID" \115-o "$TERM_PROGRAM" = "Apple_Terminal" ; then116browser_candidates="open $browser_candidates"117fi118# /bin/start indicates MinGW119if test -x /bin/start; then120browser_candidates="start $browser_candidates"121fi122123for i in $browser_candidates; do124init_browser_path $i125if type "$browser_path" > /dev/null 2>&1; then126browser=$i127break128fi129done130test -z "$browser" && die "No known browser available."131else132valid_tool "$browser" || die "Unknown browser '$browser'."133134init_browser_path "$browser"135136if test -z "$browser_cmd" && ! type "$browser_path" > /dev/null 2>&1; then137die "The browser $browser is not available as '$browser_path'."138fi139fi140141case "$browser" in142firefox|iceweasel)143# Check version because firefox < 2.0 does not support "-new-tab".144vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')145NEWTAB='-new-tab'146test "$vers" -lt 2 && NEWTAB=''147"$browser_path" $NEWTAB "$@" &148;;149konqueror)150case "$(basename "$browser_path")" in151konqueror)152# It's simpler to use kfmclient to open a new tab in konqueror.153browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"154type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."155eval "$browser_path" newTab "$@"156;;157kfmclient)158eval "$browser_path" newTab "$@"159;;160*)161"$browser_path" "$@" &162;;163esac164;;165w3m|links|lynx|open)166eval "$browser_path" "$@"167;;168start)169exec "$browser_path" '"web-browse"' "$@"170;;171dillo)172"$browser_path" "$@" &173;;174*)175if test -n "$browser_cmd"; then176( eval $browser_cmd "$@" )177fi178;;179esac