1
0
mirror of https://github.com/dcarrillo/prezto.git synced 2024-12-22 23:08:00 +00:00
prezto/modules/git/functions/git-hub-browse
Indrajit Raychaudhuri a3643f124e git: Optimize scripts for git helper functions
Optimize git internal calls and adjustment git helper functions to use
more idiomatic Zsh conventions avoiding external calls.
2021-05-21 13:40:49 -05:00

63 lines
1.3 KiB
Plaintext

#
# Opens a GitHub repository in the default browser.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
# function git-hub-browse {
if ! is-true "$(command git rev-parse --is-inside-work-tree 2> /dev/null)"; then
print "$0: not a repository work tree: $PWD" >&2
return 1
fi
local remotes remote references reference file url
remote="${1:-origin}"
remotes=($(command git remote show))
if (( $remotes[(i)$remote] == $#remotes + 1 )); then
print "$0: remote not found: $remote" >&2
return 1
fi
url=$(
command git remote get-url "$remote" \
| sed -En "s#(git@|https?://)(github.com)(:|/)(.+)/(.+)\.git#https://\2/\4/\5#p"
)
reference="${${2:-$(git-branch-current)}:-HEAD}"
references=(
HEAD
${${(f)"$(command git ls-remote --heads --tags "$remote")"}##*refs/(heads|tags)/}
)
if (( $references[(i)$reference] == $#references + 1 )); then
print "$0: branch or tag not found: $reference" >&2
return 1
fi
if [[ "$reference" == 'HEAD' ]]; then
reference="$(command git rev-parse HEAD 2> /dev/null)"
fi
file="$3"
if [[ -n "$url" ]]; then
url="$url/tree/$reference/$file"
if [[ -z "$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 not set" >&2
return 1
fi
# }