Blame git-web--browse.sh

Packit 4511e4
#!/bin/sh
Packit 4511e4
#
Packit 4511e4
# This program launch a web browser on the html page
Packit 4511e4
# describing a git command.
Packit 4511e4
#
Packit 4511e4
# Copyright (c) 2007 Christian Couder
Packit 4511e4
# Copyright (c) 2006 Theodore Y. Ts'o
Packit 4511e4
#
Packit 4511e4
# This file is heavily stolen from git-mergetool.sh, by
Packit 4511e4
# Theodore Y. Ts'o (thanks) that is:
Packit 4511e4
#
Packit 4511e4
# Copyright (c) 2006 Theodore Y. Ts'o
Packit 4511e4
#
Packit 4511e4
# This file is licensed under the GPL v2, or a later version
Packit 4511e4
# at the discretion of Junio C Hamano or any other official
Packit 4511e4
# git maintainer.
Packit 4511e4
#
Packit 4511e4
Packit 4511e4
USAGE='[--browser=browser|--tool=browser] [--config=conf.var] url/file ...'
Packit 4511e4
Packit 4511e4
# This must be capable of running outside of git directory, so
Packit 4511e4
# the vanilla git-sh-setup should not be used.
Packit 4511e4
NONGIT_OK=Yes
Packit 4511e4
. git-sh-setup
Packit 4511e4
Packit 4511e4
valid_custom_tool()
Packit 4511e4
{
Packit 4511e4
	browser_cmd="$(git config "browser.$1.cmd")"
Packit 4511e4
	test -n "$browser_cmd"
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
valid_tool() {
Packit 4511e4
	case "$1" in
Packit 4511e4
	firefox | iceweasel | seamonkey | iceape | \
Packit 4511e4
	chrome | google-chrome | chromium | chromium-browser | \
Packit 4511e4
	konqueror | opera | w3m | elinks | links | lynx | dillo | open | \
Packit 4511e4
	start | cygstart | xdg-open)
Packit 4511e4
		;; # happy
Packit 4511e4
	*)
Packit 4511e4
		valid_custom_tool "$1" || return 1
Packit 4511e4
		;;
Packit 4511e4
	esac
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
init_browser_path() {
Packit 4511e4
	browser_path=$(git config "browser.$1.path")
Packit 4511e4
	if test -z "$browser_path" &&
Packit 4511e4
	   test "$1" = chromium &&
Packit 4511e4
	   type chromium-browser >/dev/null 2>&1
Packit 4511e4
	then
Packit 4511e4
		browser_path=chromium-browser
Packit 4511e4
	fi
Packit 4511e4
	: ${browser_path:="$1"}
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
while test $# != 0
Packit 4511e4
do
Packit 4511e4
	case "$1" in
Packit 4511e4
	-b|--browser*|-t|--tool*)
Packit 4511e4
		case "$#,$1" in
Packit 4511e4
		*,*=*)
Packit 4511e4
			browser=$(expr "z$1" : 'z-[^=]*=\(.*\)')
Packit 4511e4
			;;
Packit 4511e4
		1,*)
Packit 4511e4
			usage ;;
Packit 4511e4
		*)
Packit 4511e4
			browser="$2"
Packit 4511e4
			shift ;;
Packit 4511e4
		esac
Packit 4511e4
		;;
Packit 4511e4
	-c|--config*)
Packit 4511e4
		case "$#,$1" in
Packit 4511e4
		*,*=*)
Packit 4511e4
			conf=$(expr "z$1" : 'z-[^=]*=\(.*\)')
Packit 4511e4
			;;
Packit 4511e4
		1,*)
Packit 4511e4
			usage ;;
Packit 4511e4
		*)
Packit 4511e4
			conf="$2"
Packit 4511e4
			shift ;;
Packit 4511e4
		esac
Packit 4511e4
		;;
Packit 4511e4
	--)
Packit 4511e4
		break
Packit 4511e4
		;;
Packit 4511e4
	-*)
Packit 4511e4
		usage
Packit 4511e4
		;;
Packit 4511e4
	*)
Packit 4511e4
		break
Packit 4511e4
		;;
Packit 4511e4
	esac
Packit 4511e4
	shift
Packit 4511e4
done
Packit 4511e4
Packit 4511e4
test $# = 0 && usage
Packit 4511e4
Packit 4511e4
if test -z "$browser"
Packit 4511e4
then
Packit 4511e4
	for opt in "$conf" "web.browser"
Packit 4511e4
	do
Packit 4511e4
		test -z "$opt" && continue
Packit 4511e4
		browser="$(git config $opt)"
Packit 4511e4
		test -z "$browser" || break
Packit 4511e4
	done
Packit 4511e4
	if test -n "$browser" && ! valid_tool "$browser"; then
Packit 4511e4
		echo >&2 "git config option $opt set to unknown browser: $browser"
Packit 4511e4
		echo >&2 "Resetting to default..."
Packit 4511e4
		unset browser
Packit 4511e4
	fi
Packit 4511e4
fi
Packit 4511e4
Packit 4511e4
if test -z "$browser" ; then
Packit 4511e4
	if test -n "$DISPLAY"; then
Packit 4511e4
		browser_candidates="firefox iceweasel google-chrome chrome chromium chromium-browser konqueror opera seamonkey iceape w3m elinks links lynx dillo xdg-open"
Packit 4511e4
		if test "$KDE_FULL_SESSION" = "true"; then
Packit 4511e4
			browser_candidates="konqueror $browser_candidates"
Packit 4511e4
		fi
Packit 4511e4
	else
Packit 4511e4
		browser_candidates="w3m elinks links lynx"
Packit 4511e4
	fi
Packit 4511e4
	# SECURITYSESSIONID indicates an OS X GUI login session
Packit 4511e4
	if test -n "$SECURITYSESSIONID" || test -n "$TERM_PROGRAM"
Packit 4511e4
	then
Packit 4511e4
		browser_candidates="open $browser_candidates"
Packit 4511e4
	fi
Packit 4511e4
	# /bin/start indicates MinGW
Packit 4511e4
	if test -x /bin/start; then
Packit 4511e4
		browser_candidates="start $browser_candidates"
Packit 4511e4
	fi
Packit 4511e4
	# /usr/bin/cygstart indicates Cygwin
Packit 4511e4
	if test -x /usr/bin/cygstart; then
Packit 4511e4
		browser_candidates="cygstart $browser_candidates"
Packit 4511e4
	fi
Packit 4511e4
Packit 4511e4
	for i in $browser_candidates; do
Packit 4511e4
		init_browser_path $i
Packit 4511e4
		if type "$browser_path" > /dev/null 2>&1; then
Packit 4511e4
			browser=$i
Packit 4511e4
			break
Packit 4511e4
		fi
Packit 4511e4
	done
Packit 4511e4
	test -z "$browser" && die "No known browser available."
Packit 4511e4
else
Packit 4511e4
	valid_tool "$browser" || die "Unknown browser '$browser'."
Packit 4511e4
Packit 4511e4
	init_browser_path "$browser"
Packit 4511e4
Packit 4511e4
	if test -z "$browser_cmd" && ! type "$browser_path" > /dev/null 2>&1; then
Packit 4511e4
		die "The browser $browser is not available as '$browser_path'."
Packit 4511e4
	fi
Packit 4511e4
fi
Packit 4511e4
Packit 4511e4
case "$browser" in
Packit 4511e4
firefox|iceweasel|seamonkey|iceape)
Packit 4511e4
	# Check version because firefox < 2.0 does not support "-new-tab".
Packit 4511e4
	vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
Packit 4511e4
	NEWTAB='-new-tab'
Packit 4511e4
	test "$vers" -lt 2 && NEWTAB=''
Packit 4511e4
	"$browser_path" $NEWTAB "$@" &
Packit 4511e4
	;;
Packit 4511e4
google-chrome|chrome|chromium|chromium-browser)
Packit 4511e4
	# No need to specify newTab. It's default in chromium
Packit 4511e4
	"$browser_path" "$@" &
Packit 4511e4
	;;
Packit 4511e4
konqueror)
Packit 4511e4
	case "$(basename "$browser_path")" in
Packit 4511e4
	konqueror)
Packit 4511e4
		# It's simpler to use kfmclient to open a new tab in konqueror.
Packit 4511e4
		browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
Packit 4511e4
		type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
Packit 4511e4
		"$browser_path" newTab "$@" &
Packit 4511e4
		;;
Packit 4511e4
	kfmclient)
Packit 4511e4
		"$browser_path" newTab "$@" &
Packit 4511e4
		;;
Packit 4511e4
	*)
Packit 4511e4
		"$browser_path" "$@" &
Packit 4511e4
		;;
Packit 4511e4
	esac
Packit 4511e4
	;;
Packit 4511e4
w3m|elinks|links|lynx|open|cygstart|xdg-open)
Packit 4511e4
	"$browser_path" "$@"
Packit 4511e4
	;;
Packit 4511e4
start)
Packit 4511e4
	exec "$browser_path" '"web-browse"' "$@"
Packit 4511e4
	;;
Packit 4511e4
opera|dillo)
Packit 4511e4
	"$browser_path" "$@" &
Packit 4511e4
	;;
Packit 4511e4
*)
Packit 4511e4
	if test -n "$browser_cmd"; then
Packit 4511e4
		( eval "$browser_cmd \"\$@\"" )
Packit 4511e4
	fi
Packit 4511e4
	;;
Packit 4511e4
esac