From 66c4aae59e223c030c6176950b80c7db8b7ffbca Mon Sep 17 00:00:00 2001 From: Sorin Ionescu Date: Thu, 19 Jan 2012 17:32:27 -0500 Subject: [PATCH] Support the browsing of other Git remotes/branches. --- plugins/git/alias.zsh | 2 ++ plugins/git/completions/_git-hub | 29 +++++++++++++++++++++++++++++ plugins/git/functions/git-hub | 26 ++++++++++++++++++++++---- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 plugins/git/completions/_git-hub diff --git a/plugins/git/alias.zsh b/plugins/git/alias.zsh index 67d6000..e266a39 100644 --- a/plugins/git/alias.zsh +++ b/plugins/git/alias.zsh @@ -182,6 +182,8 @@ alias ghc='git remote prune' compdef _git ghc=git-remote alias ghs='git remote show' compdef _git ghs=git-remote +alias ghb='git-hub' +compdef _git-hub ghb=git-hub # Stash (t) alias gta='git stash apply' diff --git a/plugins/git/completions/_git-hub b/plugins/git/completions/_git-hub new file mode 100644 index 0000000..3a9c6e5 --- /dev/null +++ b/plugins/git/completions/_git-hub @@ -0,0 +1,29 @@ +#compdef git-hub +#autoload + +local state remotes remote branches ret=1 + +_arguments -C -s -S \ + '1::args:->remote' \ + '2::args:->branch' && ret=0 + +case "$state" in + (remote) + remotes=($( + git config -l \ + | grep 'remote\.[^.]*\.url' \ + | cut -d'.' -f2)) + _describe -t branch 'remotes' remotes && ret=0 + ;; + (branch) + remote="$words[(($CURRENT - 1))]" + branches=($( + git branch -a \ + | grep "remotes/${remote}" \ + | sed -e 's/[ \*]*//g' -e "s:remotes/${remote}/::g" + )) + _describe -t branch 'branches' branches && ret=0 + ;; +esac + +return $ret diff --git a/plugins/git/functions/git-hub b/plugins/git/functions/git-hub index 6bfbb9a..e9d18ae 100644 --- a/plugins/git/functions/git-hub +++ b/plugins/git/functions/git-hub @@ -1,12 +1,30 @@ # Open the GitHub repository in the browser. -local url=$( +local remote branches branch current_branch url + +remote="${1:-origin}" +url=$( git config -l \ - | grep "remote.origin.url" \ - | sed -En "s/remote.origin.url=(git|https?)(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\4\/\5/p" + | grep "remote.${remote}.url" \ + | sed -En "s/remote.${remote}.url=(git|https?)(@|:\/\/)github.com(:|\/)(.+)\/(.+).git/https:\/\/github.com\/\4\/\5/p" ) +branches=($( + git branch -a \ + | grep "remotes/${remote}" \ + | sed -e 's/[ \*]*//g' -e "s:remotes/${remote}/::g" +)) +current_branch="$(git-branch)" +branch="${2:-master}" + +if [[ -z "$2" ]]; then + if (( $branches[(I)$current_branch] != 0 )); then + branch="$current_branch" + else + branch='master' + fi +fi if [[ -n "$url" ]]; then - url="${url}/tree/${$(git-branch):-master}" + url="${url}/tree/${branch}" if (( $+commands[$BROWSER] )); then "$BROWSER" "$url"