mirror of
https://github.com/paulirish/git-open.git
synced 2026-09-10 15:36:16 -04:00
79 lines
1.9 KiB
Bash
Executable file
79 lines
1.9 KiB
Bash
Executable file
#!/usr/bin/env 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?
|
||
if ! git rev-parse --is-inside-work-tree &>/dev/null; then
|
||
echo "Not a git repository." 1>&2
|
||
exit 1
|
||
fi
|
||
|
||
# assume remote origin if not provided
|
||
remote=${1:-"origin"}
|
||
|
||
# @TODO ls-remote will also expand "insteadOf" items `giturl=$(git ls-remote --get-url $remote)``
|
||
giturl=$(git config --get "remote.${remote}.url")
|
||
|
||
if [[ -z "$giturl" ]]; then
|
||
echo "Git remote is not set for $remote" 1>&2
|
||
exit 1
|
||
fi
|
||
|
||
# Initial case examples: 'git@example.com:user/project', 'https://example.com:8080/scm/user/project.git/'
|
||
# Trim "/" and ".git" from the end of the url
|
||
giturl=${giturl%/} giturl=${giturl%.git}
|
||
|
||
# Trim before last '@' and protocol (*://) from beginning
|
||
uri=${giturl##*@} uri=${uri##*://}
|
||
|
||
# Trims before first ':' or '/' to get path
|
||
path=${uri#*[/:]}
|
||
|
||
# Trims after first ':' or '/' to remove path
|
||
server=${uri%%[/:]*}
|
||
|
||
# Get current branch
|
||
branch=${2:-$(git symbolic-ref -q --short HEAD)}
|
||
|
||
providerUrlDifference="tree"
|
||
case "$server" in
|
||
"bitbucket.org") providerUrlDifference="src" ;;
|
||
esac
|
||
|
||
# @TODO: support non-https?
|
||
openurl="https://$server/$path"
|
||
|
||
# simplify URL for master
|
||
if [[ $branch != "master" ]]; then
|
||
# Make # and % characters url friendly
|
||
# github.com/paulirish/git-open/pull/24
|
||
branch=${branch//%/%25} branch=${branch//#/%23}
|
||
openurl="$openurl/$providerUrlDifference/$branch"
|
||
fi
|
||
|
||
# get current open browser command
|
||
case $( uname -s ) in
|
||
Darwin) open=open;;
|
||
MINGW*) open=start;;
|
||
CYGWIN*) open=cygstart;;
|
||
MSYS*) open="powershell.exe –NoProfile Start";;
|
||
*) open=${BROWSER:-xdg-open};;
|
||
esac
|
||
|
||
# if testing, log results to stdout. otherwise, mute the output
|
||
if [ "$BATS_CWD" ]; then
|
||
open=echo
|
||
else
|
||
exec &>/dev/null
|
||
fi;
|
||
|
||
# open it in a browser
|
||
$open "$openurl"
|
||
|
||
exit $?
|