paulirish.git-open/git-open
Nils Winkler 5046786893 Added support for Atlassian Stash.
Stash remote URLs always contain /scm/, I'm using that to detect a repo
hosted on Stash. The web URL for Stash repos is using a different
format, where it inserts "projects" and "repos" to separate the project
and the repo.

I had to move the branch detection to the top, since Stash is using a
different format for selecting the branch.

I've only tried this with HTTPS URLs, not with git: URLs - there's
probably some more code needed for this.
2015-02-05 14:21:11 +01:00

94 lines
1.8 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
# get current branch
if [ -z "$2" ]
then
branch=`git symbolic-ref -q --short HEAD`
else
branch="$2"
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
# Atlassian Stash support - Stash URLs always contain the /scm/ part
elif grep -q "/scm/" <<<$giturl; then
re='(.*)/scm/(.*)/(.*)\.git'
if [[ $giturl =~ $re ]]
then
giturl=${BASH_REMATCH[1]}/projects/${BASH_REMATCH[2]}/repos/${BASH_REMATCH[3]}
providerUrlDifference=browse
branch="?at=refs%2Fheads%2F${branch}"
fi
fi
giturl=${giturl%\.git}
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