paulirish.git-open/git-open
2015-02-05 13:06:27 -07:00

100 lines
1.9 KiB
Bash
Executable file

#!/bin/bash
# Opens the github page for a repo/branch in your browser.
# https://github.com/paulirish/git-open/
#
# git open
# git open [remote] [branch]
# are we in a git repo?
git rev-parse --is-inside-work-tree &>/dev/null
if [[ $? != 0 ]]
then
echo "Not a git repository."
exit 1
fi
# assume origin if not provided
# fallback to upstream if neither is present.
remote="origin"
if [ ! -z "$1" ]
then
remote="$1"
fi
remote_url="remote.${remote}.url"
giturl=$(git config --get $remote_url)
if [ -z "$giturl" ]
then
echo "$remote_url not set."
exit 1
fi
# URL normalization
# Github support
if grep -q github <<<$giturl; then
giturl=${giturl/git\@github\.com\:/https://github.com/}
providerUrlDifference=tree
# bitbucket support
elif grep -q bitbucket <<<$giturl; then
giturl=${giturl/git\@bitbucket\.org\:/https://bitbucket.org/}
providerUrlDifference=branch
else
gitlab_domain=$(git config --get gitopen.gitlab.domain)
if [ ! -z "$gitlab_domain" ]; then
if grep -q $gitlab_domain <<<$giturl; then
giturl=${giturl/git\@${gitlab_domain}:/https://${gitlab_domain}/}
gitlab_urldifference=$(git config --get gitopen.gitlab.urldifference)
if [ -z "$gitlab_urldifference" ]; then
providerUrlDifference=tree
else
providerUrlDifference=$gitlab_urldifference
fi
fi
fi
fi
giturl=${giturl%\.git}
# get current branch
if [ -z "$2" ]
then
branch=`git symbolic-ref -q --short HEAD`
else
branch="$2"
fi
if [ ! -z "$branch" ]
then
giturl="${giturl}/${providerUrlDifference}/${branch}"
fi
# simplify URL for master
giturl=${giturl/tree\/master/}
# get current open browser command
if [ $(uname -s) == "Darwin" ]
then
open=open
elif [ "$TERM" == "cygwin" ]
then
if [ "$MSYSTEM" == "MINGW32" ]
then
open=start
else
open=cygstart
fi
else
open=xdg-open
fi
# open it in a browser
$open $giturl &> /dev/null
exit 0