git-web--browse.shon commit Merge branch 'nd/maint-hide-checkout-index-from-error' (6a79be3)
   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] [--config=conf.var] url/file ...'
  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
  24. git-sh-setup
  25
  26valid_custom_tool()
  27{
  28        browser_cmd="$(git config "browser.$1.cmd")"
  29        test -n "$browser_cmd"
  30}
  31
  32valid_tool() {
  33        case "$1" in
  34                firefox | iceweasel | chrome | google-chrome | chromium | konqueror | w3m | links | lynx | dillo | open | start)
  35                        ;; # happy
  36                *)
  37                        valid_custom_tool "$1" || return 1
  38                        ;;
  39        esac
  40}
  41
  42init_browser_path() {
  43        browser_path=$(git config "browser.$1.path")
  44        test -z "$browser_path" && browser_path="$1"
  45}
  46
  47while test $# != 0
  48do
  49    case "$1" in
  50        -b|--browser*|-t|--tool*)
  51            case "$#,$1" in
  52                *,*=*)
  53                    browser=`expr "z$1" : 'z-[^=]*=\(.*\)'`
  54                    ;;
  55                1,*)
  56                    usage ;;
  57                *)
  58                    browser="$2"
  59                    shift ;;
  60            esac
  61            ;;
  62        -c|--config*)
  63            case "$#,$1" in
  64                *,*=*)
  65                    conf=`expr "z$1" : 'z-[^=]*=\(.*\)'`
  66                    ;;
  67                1,*)
  68                    usage ;;
  69                *)
  70                    conf="$2"
  71                    shift ;;
  72            esac
  73            ;;
  74        --)
  75            break
  76            ;;
  77        -*)
  78            usage
  79            ;;
  80        *)
  81            break
  82            ;;
  83    esac
  84    shift
  85done
  86
  87test $# = 0 && usage
  88
  89if test -z "$browser"
  90then
  91    for opt in "$conf" "web.browser"
  92    do
  93        test -z "$opt" && continue
  94        browser="`git config $opt`"
  95        test -z "$browser" || break
  96    done
  97    if test -n "$browser" && ! valid_tool "$browser"; then
  98        echo >&2 "git config option $opt set to unknown browser: $browser"
  99        echo >&2 "Resetting to default..."
 100        unset browser
 101    fi
 102fi
 103
 104if test -z "$browser" ; then
 105    if test -n "$DISPLAY"; then
 106        browser_candidates="firefox iceweasel google-chrome chrome chromium konqueror w3m links lynx dillo"
 107        if test "$KDE_FULL_SESSION" = "true"; then
 108            browser_candidates="konqueror $browser_candidates"
 109        fi
 110    else
 111        browser_candidates="w3m links lynx"
 112    fi
 113    # SECURITYSESSIONID indicates an OS X GUI login session
 114    if test -n "$SECURITYSESSIONID" \
 115            -o "$TERM_PROGRAM" = "Apple_Terminal" ; then
 116        browser_candidates="open $browser_candidates"
 117    fi
 118    # /bin/start indicates MinGW
 119    if test -x /bin/start; then
 120        browser_candidates="start $browser_candidates"
 121    fi
 122
 123    for i in $browser_candidates; do
 124        init_browser_path $i
 125        if type "$browser_path" > /dev/null 2>&1; then
 126            browser=$i
 127            break
 128        fi
 129    done
 130    test -z "$browser" && die "No known browser available."
 131else
 132    valid_tool "$browser" || die "Unknown browser '$browser'."
 133
 134    init_browser_path "$browser"
 135
 136    if test -z "$browser_cmd" && ! type "$browser_path" > /dev/null 2>&1; then
 137        die "The browser $browser is not available as '$browser_path'."
 138    fi
 139fi
 140
 141case "$browser" in
 142    firefox|iceweasel)
 143        # Check version because firefox < 2.0 does not support "-new-tab".
 144        vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
 145        NEWTAB='-new-tab'
 146        test "$vers" -lt 2 && NEWTAB=''
 147        "$browser_path" $NEWTAB "$@" &
 148        ;;
 149    google-chrome|chrome|chromium)
 150        # Actual command for chromium is chromium-browser.
 151        # No need to specify newTab. It's default in chromium
 152        eval "$browser_path" "$@" &
 153        ;;
 154    konqueror)
 155        case "$(basename "$browser_path")" in
 156            konqueror)
 157                # It's simpler to use kfmclient to open a new tab in konqueror.
 158                browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
 159                type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
 160                eval "$browser_path" newTab "$@"
 161                ;;
 162            kfmclient)
 163                eval "$browser_path" newTab "$@"
 164                ;;
 165            *)
 166                "$browser_path" "$@" &
 167                ;;
 168        esac
 169        ;;
 170    w3m|links|lynx|open)
 171        eval "$browser_path" "$@"
 172        ;;
 173    start)
 174        exec "$browser_path" '"web-browse"' "$@"
 175        ;;
 176    dillo)
 177        "$browser_path" "$@" &
 178        ;;
 179    *)
 180        if test -n "$browser_cmd"; then
 181            ( eval $browser_cmd "$@" )
 182        fi
 183        ;;
 184esac