#compdef git-ls-ignored
#description List files which exist and are ignored by this repository.

# Offer the paths the script would actually list, so the candidates match its
# rules rather than the --exclude-standard set used by zsh's own
# __git_ignored_other_files. _multi_parts descends one component at a time, so
# a directory holding ignored files completes as a way to narrow the listing.
__git-ls-ignored_files() {
  local common_dir expl
  local -a excludes files

  if (( $+opt_args[-g] + $+opt_args[--global] )); then
    excludes=(--exclude-standard)
  else
    common_dir=$(_call_program common-dir git rev-parse --git-common-dir 2>/dev/null)
    [[ -n $common_dir ]] || return 1
    [[ $common_dir == /* ]] || common_dir=$PWD/$common_dir
    excludes=(--exclude-per-directory=.gitignore)
    [[ -f $common_dir/info/exclude ]] &&
      excludes+=(--exclude-from=$common_dir/info/exclude)
  fi

  files=(${(0)"$(_call_program ignored-files git ls-files -z --others --ignored \
    ${(q)excludes} 2>/dev/null)"})
  (( $#files )) || return 1

  _wanted ignored-files expl 'ignored file' _multi_parts -f - / files
}

_git-ls-ignored() {
  _arguments -s -S \
    '(-d --directory)'{-d,--directory}'[list an ignored directory by name instead of its contents]' \
    '(-g --global)'{-g,--global}'[also apply the global core.excludesfile]' \
    '(-z)-z[terminate entries with NUL instead of newline]' \
    '(- *)'{-h,--help}'[show usage information]' \
    '*:pathspec:__git-ls-ignored_files'
}

_git-ls-ignored "$@"

# vim: ft=zsh
