2012-02-01 04:37:51 +00:00
|
|
|
#
|
|
|
|
# Opens a GitHub repository in the default browser.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
|
|
|
|
2017-07-06 23:01:26 +00:00
|
|
|
# function git-hub-browse {
|
|
|
|
|
2018-03-05 08:56:25 +00:00
|
|
|
if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then
|
2012-09-30 19:30:18 +00:00
|
|
|
print "$0: not a repository work tree: $PWD" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2012-09-24 21:57:48 +00:00
|
|
|
local remotes remote references reference file url
|
2012-01-19 22:32:27 +00:00
|
|
|
|
|
|
|
remote="${1:-origin}"
|
2021-05-20 00:54:57 +00:00
|
|
|
remotes=($(command git remote show))
|
2012-09-24 21:57:48 +00:00
|
|
|
|
|
|
|
if (( $remotes[(i)$remote] == $#remotes + 1 )); then
|
|
|
|
print "$0: remote not found: $remote" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2012-01-19 22:32:27 +00:00
|
|
|
url=$(
|
2021-05-20 00:54:57 +00:00
|
|
|
command git remote get-url "$remote" \
|
|
|
|
| sed -En "s#(git@|https?://)(github.com)(:|/)(.+)/(.+)\.git#https://\2/\4/\5#p"
|
2011-10-12 03:13:58 +00:00
|
|
|
)
|
2012-01-19 22:32:27 +00:00
|
|
|
|
2012-09-24 21:57:48 +00:00
|
|
|
reference="${${2:-$(git-branch-current)}:-HEAD}"
|
|
|
|
references=(
|
|
|
|
HEAD
|
2021-05-20 00:54:57 +00:00
|
|
|
${${(f)"$(command git ls-remote --heads --tags "$remote")"}##*refs/(heads|tags)/}
|
2012-09-24 21:57:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if (( $references[(i)$reference] == $#references + 1 )); then
|
|
|
|
print "$0: branch or tag not found: $reference" >&2
|
|
|
|
return 1
|
2012-01-19 22:32:27 +00:00
|
|
|
fi
|
2011-10-12 03:13:58 +00:00
|
|
|
|
2012-09-24 21:57:48 +00:00
|
|
|
if [[ "$reference" == 'HEAD' ]]; then
|
2018-03-05 08:56:25 +00:00
|
|
|
reference="$(command git rev-parse HEAD 2> /dev/null)"
|
2012-09-24 21:57:48 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
file="$3"
|
|
|
|
|
2011-10-12 03:13:58 +00:00
|
|
|
if [[ -n "$url" ]]; then
|
2021-05-20 00:54:57 +00:00
|
|
|
url="$url/tree/$reference/$file"
|
2011-10-12 03:13:58 +00:00
|
|
|
|
2021-11-16 17:49:33 +00:00
|
|
|
if [[ -n "$BROWSER" ]]; then
|
2011-10-12 03:13:58 +00:00
|
|
|
"$BROWSER" "$url"
|
|
|
|
return 0
|
|
|
|
else
|
2012-01-19 07:28:01 +00:00
|
|
|
print "$0: browser not set or set to a non-existent browser" >&2
|
2011-10-12 03:13:58 +00:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
2012-09-24 21:57:48 +00:00
|
|
|
print "$0: not a Git repository or remote not set" >&2
|
2011-10-12 03:13:58 +00:00
|
|
|
return 1
|
|
|
|
fi
|
2017-07-06 23:01:26 +00:00
|
|
|
|
|
|
|
# }
|