From e43f2b265972429339b02220ae573b4c9d9297c7 Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Mon, 24 Sep 2012 17:57:48 -0400 Subject: [PATCH] Add support for browsing GitHub tags --- modules/git/functions/_git-hub | 17 ++++++++----- modules/git/functions/git-hub | 44 +++++++++++++++++++++------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/modules/git/functions/_git-hub b/modules/git/functions/_git-hub index abefd03..a4d9473 100644 --- a/modules/git/functions/_git-hub +++ b/modules/git/functions/_git-hub @@ -8,11 +8,11 @@ # Sorin Ionescu # -local state remotes remote branches files ret=1 +local state remotes remote branches_or_tags branches tags files ret=1 _arguments -C -s -S \ '1::args:->remote' \ - '2::args:->branch' \ + '2::args:->branch-or-tag' \ '3::args:->file' && ret=0 case "$state" in @@ -23,13 +23,18 @@ case "$state" in )) _describe -t branch 'remotes' remotes && ret=0 ;; - (branch) + (branch-or-tag) remote="$words[(($CURRENT - 1))]" - branches=($( - git ls-remote --heads "$remote" \ - | awk '{gsub(/refs\/heads\//, "", $2)} ; {print $2}' + + branches_or_tags=($( + git ls-remote --heads --tags "$remote" | awk '{print $2}' )) + + branches=(HEAD ${${(M)branches_or_tags[@]##refs/heads/?##}##refs/heads/}) + tags=(${${(M)branches_or_tags[@]##refs/tags/?##}##refs/tags/}) + _describe -t branch 'branches' branches && ret=0 + _describe -t tag 'tags' tags && ret=0 ;; (file) files=(${(0)"$(_call_program files git ls-files -z --exclude-standard 2>/dev/null)"}) diff --git a/modules/git/functions/git-hub b/modules/git/functions/git-hub index 95a7d52..bc2a070 100644 --- a/modules/git/functions/git-hub +++ b/modules/git/functions/git-hub @@ -5,31 +5,43 @@ # Sorin Ionescu # -local remote branches branch current_branch file url +local remotes remote references reference file url remote="${1:-origin}" +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=$( git config --local --get "remote.${remote}.url" \ | sed -En "s/(git|https?)(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\4\/\5/p" ) -branches=($( - 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 +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 +if [[ "$reference" == 'HEAD' ]]; then + reference="$(git rev-parse HEAD 2>/dev/null)" +fi + +file="$3" + if [[ -n "$url" ]]; then - url="${url}/tree/${branch}/${file}" + url="${url}/tree/${reference}/${file}" if (( $+commands[$BROWSER] )); then "$BROWSER" "$url" @@ -39,7 +51,7 @@ if [[ -n "$url" ]]; then return 1 fi else - print "$0: not a Git repository or remote origin not set" >&2 + print "$0: not a Git repository or remote not set" >&2 return 1 fi