#compdef github gh
# ------------------------------------------------------------------------------
# Copyright (c) 2011 Github zsh-users - http://github.com/zsh-users
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the zsh-users nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZSH-USERS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ------------------------------------------------------------------------------
# Description
# -----------
#
#  Completion script for Github gem 0.6.2 (https://github.com/defunkt/github-gem).
#
# ------------------------------------------------------------------------------
# Authors
# -------
#
#  * Julien Nicoulaud (https://github.com/nicoulaj)
#
# ------------------------------------------------------------------------------


_github() {
  typeset -A opt_args
  local context state line curcontext="$curcontext"

  local ret=1

  _arguments -C \
    '1:cmd:->cmds' \
    '*::arg:->args' \
  && ret=0

  case "$state" in
    (cmds)
      local commands; commands=(
        'admin:open this repo'\''s Admin panel a web browser'
        'browse:open this repo in a web browser'
        'clone:clone a repo'
        'config:automatically set configuration info, or pass args to specify'
        'create:create a new, empty GitHub repository'
        'create-from-local:create a new GitHub repository from the current local repository'
        'fetch:fetch from a remote to a local branch'
        'fetch_all:fetch all refs from a user'
        'fork:forks a GitHub repository'
        'home:open this repo'\''s master branch in a web browser'
        'ignore:ignore a SHA'
        'info:info about this project'
        'issues:project issues tools'
        'network:project network tools'
        'open:open the given user/project in a web browser'
        'pull:pull from a remote'
        'pull-request:generate the text for a pull request'
        'search:search GitHub for the given repository name'
        'track:track another user'\''s repository'
      )
      _describe -t commands 'command' commands && ret=0
    ;;
    (args)
      curcontext="${curcontext%:*:*}:github-cmd-$words[1]:"
      case $words[1] in
        (admin|fetch|fetch_all|home|info)
          _message 'no more arguments' && ret=0
        ;;
        (browse)
          _arguments \
            '1: :_github_users' \
            '2: :_github_branches' \
          && ret=0
        ;;
        (clone)
          _arguments \
            '1: :_github_users' \
            '2: :_github_repos' \
            '3: :_files -/' \
            '--search[search for user or repo and clone selected repository]:user or repo' \
            '--ssh[clone using the git@github.com style url]' \
          && ret=0
        ;;
        (config)
          _arguments \
            '1: :_github_users' \
            '2: :_github_repos' \
          && ret=0
        ;;
        (create)
          _arguments \
            '1:repo name' \
            '--markdown[create README.markdown]' \
            '--mdown[create README.mdown]' \
            '--private[create private repository]' \
            '--rdoc[create README.rdoc]' \
            '--rst[create README.rst]' \
            '--textile[create README.textile]' \
          && ret=0
        ;;
        (create-from-local)
          _arguments \
            '--private[create private repository]' \
          && ret=0
        ;;
        (fork)
          _arguments \
            '1: :_github_user_slash_repos' \
          && ret=0
        ;;
        (ignore)
          _arguments \
            '1: :_github_network_commits' \
          && ret=0
        ;;
        (issues)
          _arguments -C \
            '1:issue-status:->issue-statuses' \
            '2:: :_github_users' \
            '--after[only show issues updated after a certain date]:date' \
            '--label[only show issues with a certain label]:label' \
          && ret=0
          case "$state" in
            (issue-statuses)
              local statuses; statuses=(
                'open:show open issues'
                'closed:show closed issues'
              )
              _describe -t statuses 'status' statuses && ret=0
            ;;
          esac
        ;;
        (network)
          _arguments -C \
            '1:network-subcommand:->network-subcommands' \
            '*::network-arg:->network-args' \
            '(--after)--before[only show commits before a certain date]:date' \
            '(--before)--after[only show commits after a certain date]:date' \
            '--applies[filter commits to patches that apply cleanly]' \
            '--author[filter commits on a email address of author]:author' \
            '--cache[use the network data even if it'\''s expired]' \
            '--common[show common branch point]' \
            '--limit[only look through the first X heads - useful for really large projects]:number' \
            '--noapply[filter commits to patches that do not apply cleanly]' \
            '--nocache[do not use the cached network data]' \
            '--project[filter commits on a certain project]:project' \
            '--shas[only show shas]' \
            '--sort[how to sort : date, branch, author]:sort type:((date\:sort\ by\ date\ \(default\) branch\:sort\ by\ branch author\:sort\ by\ author))' \
            '--thisbranch[look at branches that match the current one]' \
          && ret=0
          case "$state" in
            (network-subcommands)
              local subcommands; subcommands=(
                'web:open network in a web browser'
                'list:list network members'
                'fetch:fetch network members commits'
                'commits:list network members commits'
              )
              _describe -t subcommands 'subcommand' subcommands && ret=0
            ;;
            (network-args)
              case $words[1] in
                (web)
                  _arguments \
                    '1: :_github_users' \
                  && ret=0
                ;;
              esac
            ;;
          esac
        ;;
        (open)
          _arguments \
            '1: :_github_user_slash_repos' \
          && ret=0
        ;;
        (pull)
          _arguments \
            '1: :_github_users' \
            '2: :_github_branches' \
            '--merge[automatically merge remote'\''s changes into your master]' \
          && ret=0
        ;;
        (pull-request)
          _arguments \
            '1: :_github_users' \
            '2: :_github_branches' \
          && ret=0
        ;;
        (search)
          _arguments \
            '1:query' \
          && ret=0
        ;;
        (track)
          _arguments \
            '1:: :((remote))' \
            '*: :_github_user_slash_repos' \
            {--private,--ssh}'[use git@github.com: instead of git://github.com/]' \
          && ret=0
        ;;
      esac
    ;;
  esac

  return ret
}

(( $+functions[_github_users] )) ||
_github_users() {
  _alternative \
    'users:user:_users' \
    'github-network-users:github network user:_github_network_users'
}

(( $+functions[_github_network_users] )) ||
_github_network_users() {
  local users; users=(${(f)"$(_call_program users $service network list 2>/dev/null)"})
  _describe -t github-network-users 'github network user' users "$@"
}

(( $+functions[_github_repos] )) ||
_github_repos() {
  _message -e repos 'repo'
}

(( $+functions[_github_branches] )) ||
_github_branches() {
  _message -e branches 'branch'
}

(( $+functions[_github_user_slash_repos] )) ||
_github_user_slash_repos() {
  local ret=1
  if compset -P '*/'; then
    _wanted repos expl 'repo' _github_repos && ret=0
  else
    _wanted users expl 'user' _github_users -qS/ && ret=0
  fi
  return ret
}

(( $+functions[_github_network_commits] )) ||
_github_network_commits() {
  local commits; commits=(${${(f)"$(_call_program commits $service network commits 2>/dev/null)"}%%[[:blank:]]*})
  _describe -t commits 'commit' commits "$@"
}

_github "$@"

# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et
