From e01f16df3ff8189dc685a2d60f80e476758280aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johann=20Kl=C3=A4hn?= Date: Mon, 4 Apr 2011 19:24:55 +0200 Subject: [PATCH] rewrite zsh completion plugin --- git-flow-completion.plugin.zsh | 282 ++++++++++++++++++++++++++++ git-flow-completion.zsh | 332 --------------------------------- 2 files changed, 282 insertions(+), 332 deletions(-) create mode 100644 git-flow-completion.plugin.zsh delete mode 100644 git-flow-completion.zsh diff --git a/git-flow-completion.plugin.zsh b/git-flow-completion.plugin.zsh new file mode 100644 index 0000000..a370e67 --- /dev/null +++ b/git-flow-completion.plugin.zsh @@ -0,0 +1,282 @@ +# zsh completion module for git flow +# • use an up-to-date git-completion module, f.ex. from here: +# http://zsh.git.sourceforge.net/git/gitweb.cgi?p=zsh/zsh;a=blob_plain;f=Completion/Unix/Command/_git;hb=HEAD +# • source this file somewhere in your .zshrc (eg. source ~/git-flow-completion.plugin.zsh) +# • or use this file as a oh-my-zsh plugin + +# ------------------------------------------------------------------------------ + +# Tip: Use indentation based folding when browsing this file. + +# Note: I will not include the 'help' subcommands because all information is +# already provided by the completion. Nevertheless this would be as simple as +# adding '(-):::(help)' to _arguments. + +# ------------------------------------------------------------------------------ + +# make sub-command known to zsh's builtin git completion +zstyle ':completion:*:*:git:*' user-commands flow:'git-flow branching model' + +_git-flow () +{ + local context state line + typeset -A opt_args + + _arguments \ + ':command:->command' \ + '*::options:->options' + case $state in + (command) + local -a commands + + commands=( + 'init:initialize repository for use with git-flow' + 'feature:develop new features for the upcoming or a distant future release' + 'release:prepare a new production release' + 'hotfix:act immediately upon an undesired state of a live production version' + 'support:long-term support branches of historic versions' + 'help' + 'version' + ) + _describe -t commands 'git flow command' commands + ;; + (options) + case $line[1] in + (help) ;& + (version) + ;; + (init) + _arguments \ + {-f,--force}'[force setting of gitflow branches, even if already configured]' \ + {-d,--default}'[use default branch naming conventions]' + ;; + *) # subcommand-specific completion + if (( $+functions[__git-flow-$words[1]] )); then + _call_function ret __git-flow-$words[1] + return ret + fi + ;; + esac + ;; + esac + + # ---------------------------------------------------------------------------- + + __git-flow-feature () { + local prefix=$(__git_flow_prefix "feature") + local origin="$(git config gitflow.origin 2> /dev/null || echo "origin")" + + local context state line + typeset -A opt_args + + _arguments \ + ':command:->command' \ + '*::options:->options' + case $state in + (command) + local -a commands + + commands=('list' 'start' 'finish' 'publish' 'track' 'diff' 'rebase' 'checkout' 'pull') + _describe -t commands 'git flow feature command' commands + ;; + (options) + case $line[1] in + (list) + _arguments {-v,--verbose}'[verbose (more) output]' + ;; + (start) + _arguments \ + {-F,--fetch}'[fetch from origin before performing local operation]' \ + ':name:__git_guard_branch-name' \ + '::base:__git_revisions' + ;; + (finish) + _arguments \ + {-F,--fetch}'[fetch from origin before performing finish]' \ + {-r,--rebase}'[rebase instead of merge]' \ + {-k,--keep}'[keep branch after performing finish]' \ + ":name:__git_flow_branch_names --prefix $prefix" + ;; + (publish) + _arguments \ + ":name:__git_flow_branch_names --prefix $prefix" + ;; + (track) + _arguments \ + ":name:__git_flow_branch_names --type remotes --prefix '$origin/$prefix'" + ;; + (diff) + _arguments \ + ":name:__git_flow_branch_names --prefix $prefix" + ;; + (rebase) + _arguments \ + {-i,--interactive}'[do an interactive rebase]' \ + ":name:__git_flow_branch_names --prefix $prefix" + ;; + (checkout|co) + _arguments \ + ":name:__git_flow_branch_names --prefix $prefix" + ;; + (pull) + _arguments \ + ':remote:__git_any_repositories' \ + ":name:__git_flow_branch_names --prefix $prefix" + ;; + esac + ;; + esac + } + + __git-flow-release () { + local prefix=$(__git_flow_prefix "release") + local origin="$(git config gitflow.origin 2> /dev/null || echo "origin")" + + local context state line + typeset -A opt_args + + _arguments \ + ':command:->command' \ + '*::options:->options' + case $state in + (command) + local -a commands + + commands=('list' 'start' 'finish' 'publish' 'track') + _describe -t commands 'git flow release command' commands + ;; + (options) + case $line[1] in + (list) + _arguments {-v,--verbose}'[verbose (more) output]' + ;; + (start) + _arguments \ + {-F,--fetch}'[fetch from origin before performing local operation]' \ + ':version:__git_guard_branch-name' + ;; + (finish) + _arguments \ + {-F,--fetch}'[fetch from origin before performing finish]' \ + {-s,--sign}'[sign the release tag cryptographically]' \ + {-u,--signingkey}'[use the given GPG-key for the digital signature (implies -s)]' \ + {-m,--message}'[use the given tag message]' \ + {-p,--push}'[push to origin after performing finish]' \ + {-k,--keep}'[keep branch after performing finish]' \ + {-n,--notag}"[don't tag this release]" \ + ":version:__git_flow_branch_names --prefix $prefix" + ;; + (publish) + _arguments \ + ":version:__git_flow_branch_names --prefix $prefix" + ;; + (track) + _arguments \ + ":version:__git_flow_branch_names --type remotes --prefix $prefix" + ;; + esac + ;; + esac + } + + __git-flow-hotfix () { + local prefix=$(__git_flow_prefix "hotfix") + + local context state line + typeset -A opt_args + + _arguments \ + ':command:->command' \ + '*::options:->options' + case $state in + (command) + local -a commands + + commands=('list' 'start' 'finish') + _describe -t commands 'git flow hotfix command' commands + ;; + (options) + case $line[1] in + (list) + _arguments {-v,--verbose}'[verbose (more) output]' + ;; + (start) + _arguments \ + {-F,--fetch}'[fetch from origin before performing local operation]' \ + ':version:__git_guard_branch-name' \ + '::base:__git_revisions' + ;; + (finish) + _arguments \ + {-F,--fetch}'[fetch from origin before performing finish]' \ + {-s,--sign}'[sign the release tag cryptographically]' \ + {-u,--signingkey}'[use the given GPG-key for the digital signature (implies -s)]' \ + {-m,--message}'[use the given tag message]' \ + {-p,--push}'[push to origin after performing finish]' \ + {-k,--keep}'[keep branch after performing finish]' \ + {-n,--notag}"[don't tag this release]" \ + ":version:__git_flow_branch_names --prefix $prefix" + ;; + esac + ;; + esac + } + + __git-flow-support () { + local prefix=$(__git_flow_prefix "support") + + local context state line + typeset -A opt_args + + _arguments \ + ':command:->command' \ + '*::options:->options' + case $state in + (command) + local -a commands + + commands=('list' 'start') + _describe -t commands 'git flow support command' commands + ;; + (options) + case $line[1] in + (list) + _arguments {-v,--verbose}'[verbose (more) output]' + ;; + (start) + _arguments \ + {-F,--fetch}'[fetch from origin before performing local operation]' \ + ':version:__git_guard_branch-name' \ + '::base:__git_revisions' + ;; + esac + ;; + esac + } + + # ---------------------------------------------------------------------------- + + __git_flow_prefix () { + case $1 in + (feature|release|hotfix|support) + git config "gitflow.prefix.$1" 2> /dev/null || echo "$1/" + ;; + esac + } + + # taken and modified from zsh's git completion module + #(( $+functions[__git_flow_branches] )) || + __git_flow_branch_names () { + typeset -A opts + opts[--type]="heads" + zparseopts -A opts -E -D -K -- -type: -prefix: + + local expl + typeset -a branch_names + + branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/${opts[--type]}/${opts[--prefix]} 2>/dev/null)"}#refs/${opts[--type]}/${opts[--prefix]}}) + __git_command_successful || return + + _wanted branch-names expl branch-name compadd $* - $branch_names + } +} diff --git a/git-flow-completion.zsh b/git-flow-completion.zsh deleted file mode 100644 index 270bcbe..0000000 --- a/git-flow-completion.zsh +++ /dev/null @@ -1,332 +0,0 @@ -#!zsh -# -# Installation -# ------------ -# -# To achieve git-flow completion nirvana: -# -# 0. Update your zsh's git-completion module to the newest verion. -# From here. http://zsh.git.sourceforge.net/git/gitweb.cgi?p=zsh/zsh;a=blob_plain;f=Completion/Unix/Command/_git;hb=HEAD -# -# 1. Install this file. Either: -# -# a. Place it in your .zshrc: -# -# b. Or, copy it somewhere (e.g. ~/.git-flow-completion.zsh) and put the following line in -# your .zshrc: -# -# source ~/.git-flow-completion.zsh -# -# c. Or, use this file as a oh-my-zsh plugin. -# - -_git-flow () -{ - local curcontext="$curcontext" state line - typeset -A opt_args - - _arguments -C \ - ':command:->command' \ - '*::options:->options' - - case $state in - (command) - - local -a subcommands - subcommands=( - 'init:Initialize a new git repo with support for the branching model.' - 'feature:Manage your feature branches.' - 'release:Manage your release branches.' - 'hotfix:Manage your hotfix branches.' - 'support:Manage your support branches.' - 'version:Shows version information.' - ) - _describe -t commands 'git flow' subcommands - ;; - - (options) - case $line[1] in - - (init) - _arguments \ - -f'[Force setting of gitflow branches, even if already configured]' - ;; - - (version) - ;; - - (hotfix) - __git-flow-hotfix - ;; - - (release) - __git-flow-release - ;; - - (feature) - __git-flow-feature - ;; - esac - ;; - esac -} - -__git-flow-release () -{ - local curcontext="$curcontext" state line - typeset -A opt_args - - _arguments -C \ - ':command:->command' \ - '*::options:->options' - - case $state in - (command) - - local -a subcommands - subcommands=( - 'start:Start a new release branch.' - 'finish:Finish a release branch.' - 'list:List all your release branches. (Alias to `git flow release`)' - ) - _describe -t commands 'git flow release' subcommands - _arguments \ - -v'[Verbose (more) output]' - ;; - - (options) - case $line[1] in - - (start) - _arguments \ - -F'[Fetch from origin before performing finish]'\ - ':version:__git_flow_version_list' - ;; - - (finish) - _arguments \ - -F'[Fetch from origin before performing finish]' \ - -s'[Sign the release tag cryptographically]'\ - -u'[Use the given GPG-key for the digital signature (implies -s)]'\ - -m'[Use the given tag message]'\ - -p'[Push to $ORIGIN after performing finish]'\ - ':version:__git_flow_version_list' - ;; - - *) - _arguments \ - -v'[Verbose (more) output]' - ;; - esac - ;; - esac -} - -__git-flow-hotfix () -{ - local curcontext="$curcontext" state line - typeset -A opt_args - - _arguments -C \ - ':command:->command' \ - '*::options:->options' - - case $state in - (command) - - local -a subcommands - subcommands=( - 'start:Start a new hotfix branch.' - 'finish:Finish a hotfix branch.' - 'list:List all your hotfix branches. (Alias to `git flow hotfix`)' - ) - _describe -t commands 'git flow hotfix' subcommands - _arguments \ - -v'[Verbose (more) output]' - ;; - - (options) - case $line[1] in - - (start) - _arguments \ - -F'[Fetch from origin before performing finish]'\ - ':hotfix:__git_flow_version_list'\ - ':branch-name:__git_branch_names' - ;; - - (finish) - _arguments \ - -F'[Fetch from origin before performing finish]' \ - -s'[Sign the release tag cryptographically]'\ - -u'[Use the given GPG-key for the digital signature (implies -s)]'\ - -m'[Use the given tag message]'\ - -p'[Push to $ORIGIN after performing finish]'\ - ':hotfix:__git_flow_hotfix_list' - ;; - - *) - _arguments \ - -v'[Verbose (more) output]' - ;; - esac - ;; - esac -} - -__git-flow-feature () -{ - local curcontext="$curcontext" state line - typeset -A opt_args - - _arguments -C \ - ':command:->command' \ - '*::options:->options' - - case $state in - (command) - - local -a subcommands - subcommands=( - 'start:Start a new feature branch.' - 'finish:Finish a feature branch.' - 'list:List all your feature branches. (Alias to `git flow feature`)' - 'publish: public' - 'track: track' - 'diff: diff' - 'rebase: rebase' - 'checkout: checkout' - 'pull: pull' - ) - _describe -t commands 'git flow feature' subcommands - _arguments \ - -v'[Verbose (more) output]' - ;; - - (options) - case $line[1] in - - (start) - _arguments \ - -F'[Fetch from origin before performing finish]'\ - ':feature:__git_flow_feature_list'\ - ':branch-name:__git_branch_names' - ;; - - (finish) - _arguments \ - -F'[Fetch from origin before performing finish]' \ - -r'[Rebase instead of merge]'\ - ':feature:__git_flow_feature_list' - ;; - - (publish) - _arguments \ - ':feature:__git_flow_feature_list'\ - ;; - - (track) - _arguments \ - ':feature:__git_flow_feature_list'\ - ;; - - (diff) - _arguments \ - ':branch:__git_branch_names'\ - ;; - - (rebase) - _arguments \ - -i'[Do an interactive rebase]' \ - ':branch:__git_branch_names' - ;; - - (checkout) - _arguments \ - ':branch:__git_flow_feature_list'\ - ;; - - (pull) - _arguments \ - ':remote:__git_remotes'\ - ':branch:__git_branch_names' - ;; - - *) - _arguments \ - -v'[Verbose (more) output]' - ;; - esac - ;; - esac -} - -__git_flow_version_list () -{ - local expl - declare -a versions - - versions=(${${(f)"$(_call_program versions git flow release list 2> /dev/null | tr -d ' |*')"}}) - __git_command_successful || return - - _wanted versions expl 'version' compadd $versions -} - -__git_flow_feature_list () -{ - local expl - declare -a features - - features=(${${(f)"$(_call_program features git flow feature list 2> /dev/null | tr -d ' |*')"}}) - __git_command_successful || return - - _wanted features expl 'feature' compadd $features -} - -__git_remotes () { - local expl gitdir remotes - - gitdir=$(_call_program gitdir git rev-parse --git-dir 2>/dev/null) - __git_command_successful || return - - remotes=(${${(f)"$(_call_program remotes git config --get-regexp '"^remote\..*\.url$"')"}//#(#b)remote.(*).url */$match[1]}) - __git_command_successful || return - - # TODO: Should combine the two instead of either or. - if (( $#remotes > 0 )); then - _wanted remotes expl remote compadd $* - $remotes - else - _wanted remotes expl remote _files $* - -W "($gitdir/remotes)" -g "$gitdir/remotes/*" - fi -} - -__git_flow_hotfix_list () -{ - local expl - declare -a hotfixes - - hotfixes=(${${(f)"$(_call_program hotfixes git flow hotfix list 2> /dev/null | tr -d ' |*')"}}) - __git_command_successful || return - - _wanted hotfixes expl 'hotfix' compadd $hotfixes -} - -__git_branch_names () { - local expl - declare -a branch_names - - branch_names=(${${(f)"$(_call_program branchrefs git for-each-ref --format='"%(refname)"' refs/heads 2>/dev/null)"}#refs/heads/}) - __git_command_successful || return - - _wanted branch-names expl branch-name compadd $* - $branch_names -} - -__git_command_successful () { - if (( ${#pipestatus:#0} > 0 )); then - _message 'not a git repository' - return 1 - fi - return 0 -} - -zstyle ':completion:*:*:git:*' user-commands flow:'description for foo' \ No newline at end of file