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

46 lines
988 B
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>
#
local remote branches branch current_branch file url
remote="${1:-origin}"
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
)
branches=($(
2012-09-24 01:46:24 -04:00
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
2011-10-11 23:13:58 -04:00
if [[ -n "$url" ]]; then
url="${url}/tree/${branch}/${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-01-19 02:28:01 -05:00
print "$0: not a Git repository or remote origin not set" >&2
2011-10-11 23:13:58 -04:00
return 1
fi