Skip to content

Instantly share code, notes, and snippets.

@glidenote
Created February 24, 2012 06:34
Show Gist options
  • Save glidenote/1898410 to your computer and use it in GitHub Desktop.
Save glidenote/1898410 to your computer and use it in GitHub Desktop.
#compdef hub
typeset -A opt_args
local context state line
_hub() {
_arguments -s -S \
--version \
"*::hub commands:_hub_command"
}
(( $+functions[_hub_command] )) ||
_hub_command() {
local cmd ret=1
(( $+hub_cmds )) || _hub_cmds=(
"init:Create an empty git repository or reinitialize an existing one" \
"add:Add new or modified files to the staging area" \
"rm:Remove files from the working directory and staging area" \
"mv:Move or rename a file, a directory, or a symlink" \
"status:Show the status of the working directory and staging area" \
"commit:Record changes to the repository" \
"log:Show the commit history log" \
"diff:Show changes between commits, commit and working tree, etc" \
"show:Show information about commits, tags or files" \
"branch:List, create, or delete branches" \
"checkout:Switch the active branch to another branch" \
"merge:Join two or more development histories (branches) together" \
"tag:Create, list, delete, sign or verify a tag object" \
"clone:Clone a remote repository into a new directory" \
"fetch:Download data, tags and branches from a remote repository" \
"pull:Fetch from and merge with another repository or a local branch" \
"push:Upload data, tags and branches to a remote repository" \
"remote:View and manage a set of remote repositories" \
"reset:Reset your staging area or working directory to another point" \
"rebase:Re-apply a series of patches in one branch onto another" \
"bisect:Find by binary search the change that introduced a bug" \
"grep:Print files with lines matching a pattern in your codebase" \
"create:repo created on GitHub"
"cherry-pick:apply changes introduced by some existing commits"
"am:apply patches from a mailbox"
"apply:apply patches from a mailbox"
"fork:repo forked on GitHub"
"pull-request:Opens a pull request on GitHub for the project that the 'origin' remote points to."
"browse:Open repository's GitHub page in the system's default web browser"
"compare:Open a GitHub compare view page in the system's default web browser."
"submodule:initialize, update, or inspect submodules"
"help:display help information about hub"
)
if (( CURRENT == 1 )); then
_describe -t commands 'hub subcommand' _hub_cmds \
|| compadd "$@" - ${(s.:.)${(j.:.)_hub_syns}}
else
local curcontext="$curcontext"
cmd="${${_hub_cmds[(r)$words[1]:*]%%:*}:-${(k)_hub_syns[(r)(*:|)$words[1](:*|)]}}"
if (( $#cmd )); then
curcontext="${curcontext%:*:*}:hub-${cmd}:"
_call_function ret _hub_$cmd || _message 'no more arguments'
else
_message "unknown hub subcommand: $words[1]"
fi
return ret
fi
}
# get hub branch list
(( $+functions[_get_hub_branchname] )) ||
_get_hub_branchname() {
local cache_policy
zstyle -s ":completion:${curcontext}:" cache-policy cache_policy
if [[ -z "$cache_policy" ]]; then
zstyle ":completion:${curcontext}:" cache-policy _hub_branchname_caching_policy
fi
if ( [[ ${+_hub_branchname} -eq 0 ]] || _cache_invalid hub_branchname ) \
&& ! _retrieve_cache hub_branchname; then
_hub_branchname=(${${(f)"$(echo -n `git branch` | sed -e 's/\*//')"}})
_store_cache hub_branchname _hub_branchname
fi
local expl
_wanted hub_branchname expl 'branch-name' compadd -a _hub_branchname
}
_hub_branchname_caching_policy() {
local -a oldp
oldp=( "$1"(Nmw+1) )
(( $#oldp ))
}
(( $+functions[_hub_commit] )) ||
_hub_commit() {
_arguments -s \
'*:file: _files'
}
(( $+functions[_hub_remote] )) ||
_hub_remote() {
if (( CURRENT == 2 )); then
compadd add
fi
}
(( $+functions[_hub_submodule] )) ||
_hub_submodule() {
if (( CURRENT == 2 )); then
compadd add
fi
}
(( $+functions[_hub_checkout] )) ||
_hub_checkout() {
_arguments -s \
'*::hub branch-name:_get_hub_branchname'
}
_hub "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment