2012-02-01 05:37:51 +01:00
|
|
|
#
|
|
|
|
# Opens a GitHub repository in the default browser.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Sorin Ionescu <sorin.ionescu@gmail.com>
|
|
|
|
#
|
|
|
|
|
2012-02-03 00:25:21 +01:00
|
|
|
local remote branches branch current_branch file url
|
2012-01-19 23:32:27 +01:00
|
|
|
|
|
|
|
remote="${1:-origin}"
|
|
|
|
url=$(
|
2012-09-24 07:46:24 +02:00
|
|
|
git config --local --get "remote.${remote}.url" \
|
|
|
|
| sed -En "s/(git|https?)(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\4\/\5/p"
|
2011-10-12 05:13:58 +02:00
|
|
|
)
|
2012-01-19 23:32:27 +01:00
|
|
|
branches=($(
|
2012-09-24 07:46:24 +02:00
|
|
|
git ls-remote --heads "$remote" \
|
|
|
|
| awk '{gsub(/refs\/heads\//, "", $2)} ; {print $2}'
|
2012-01-19 23:32:27 +01:00
|
|
|
))
|
2012-05-21 22:11:32 +02:00
|
|
|
current_branch="$(git-branch-current)"
|
2012-01-19 23:32:27 +01:00
|
|
|
branch="${2:-master}"
|
2012-02-03 00:25:21 +01:00
|
|
|
file="$3"
|
2012-01-19 23:32:27 +01:00
|
|
|
|
|
|
|
if [[ -z "$2" ]]; then
|
|
|
|
if (( $branches[(I)$current_branch] != 0 )); then
|
|
|
|
branch="$current_branch"
|
|
|
|
else
|
|
|
|
branch='master'
|
|
|
|
fi
|
|
|
|
fi
|
2011-10-12 05:13:58 +02:00
|
|
|
|
|
|
|
if [[ -n "$url" ]]; then
|
2012-02-03 00:25:21 +01:00
|
|
|
url="${url}/tree/${branch}/${file}"
|
2011-10-12 05:13:58 +02:00
|
|
|
|
|
|
|
if (( $+commands[$BROWSER] )); then
|
|
|
|
"$BROWSER" "$url"
|
|
|
|
return 0
|
|
|
|
else
|
2012-01-19 08:28:01 +01:00
|
|
|
print "$0: browser not set or set to a non-existent browser" >&2
|
2011-10-12 05:13:58 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
else
|
2012-01-19 08:28:01 +01:00
|
|
|
print "$0: not a Git repository or remote origin not set" >&2
|
2011-10-12 05:13:58 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|