This repository has been archived on 2022-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
prezto/modules/git/functions/git-hub-browse

58 lines
1.2 KiB
Plaintext
Raw Normal View History

2012-01-31 23:37:51 -05:00
#
# Opens a GitHub repository in the default browser.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
2012-09-24 17:57:48 -04:00
local remotes remote references reference file url
remote="${1:-origin}"
2012-09-24 17:57:48 -04:00
remotes=($(
git config --local --get-regexp 'remote.*.url' \
| awk 'BEGIN {FS="."} ; {print $2}'
))
if (( $remotes[(i)$remote] == $#remotes + 1 )); then
print "$0: remote not found: $remote" >&2
return 1
fi
url=$(
2012-09-24 01:46:24 -04:00
git config --local --get "remote.${remote}.url" \
| sed -En "s/(git|https?)(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\4\/\5/p"
2011-10-11 23:13:58 -04:00
)
2012-09-24 17:57:48 -04:00
reference="${${2:-$(git-branch-current)}:-HEAD}"
references=(
HEAD
${$(git ls-remote --heads --tags "$remote" | awk '{print $2}')##refs/(heads|tags)/}
)
if (( $references[(i)$reference] == $#references + 1 )); then
print "$0: branch or tag not found: $reference" >&2
return 1
fi
2011-10-11 23:13:58 -04:00
2012-09-24 17:57:48 -04:00
if [[ "$reference" == 'HEAD' ]]; then
reference="$(git rev-parse HEAD 2>/dev/null)"
fi
file="$3"
2011-10-11 23:13:58 -04:00
if [[ -n "$url" ]]; then
2012-09-24 17:57:48 -04:00
url="${url}/tree/${reference}/${file}"
2011-10-11 23:13:58 -04:00
if (( $+commands[$BROWSER] )); then
"$BROWSER" "$url"
return 0
else
2012-01-19 02:28:01 -05:00
print "$0: browser not set or set to a non-existent browser" >&2
2011-10-11 23:13:58 -04:00
return 1
fi
else
2012-09-24 17:57:48 -04:00
print "$0: not a Git repository or remote not set" >&2
2011-10-11 23:13:58 -04:00
return 1
fi