You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
988 B

#
# Opens a GitHub repository in the default browser.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
local remote branches branch current_branch file url
remote="${1:-origin}"
url=$(
git config --local --get "remote.${remote}.url" \
| sed -En "s/(git|https?)(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\4\/\5/p"
)
branches=($(
git ls-remote --heads "$remote" \
| awk '{gsub(/refs\/heads\//, "", $2)} ; {print $2}'
))
current_branch="$(git-branch-current)"
branch="${2:-master}"
file="$3"
if [[ -z "$2" ]]; then
if (( $branches[(I)$current_branch] != 0 )); then
branch="$current_branch"
else
branch='master'
fi
fi
if [[ -n "$url" ]]; then
url="${url}/tree/${branch}/${file}"
if (( $+commands[$BROWSER] )); then
"$BROWSER" "$url"
return 0
else
print "$0: browser not set or set to a non-existent browser" >&2
return 1
fi
else
print "$0: not a Git repository or remote origin not set" >&2
return 1
fi