summaryrefslogtreecommitdiff
path: root/etc/completions/__git_files
blob: 808fe8ec507bc7363104d625bd33074d6a0dd2b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#autoload +X
local expl files ls_opts opts gitdir dirdiff i j prefix item self

zparseopts -D -E -a opts -- -cached -deleted -modified -others -ignored -unmerged -killed

gitdir=$(_call_program gitdir git rev-parse --git-dir 2>/dev/null)
__git_command_successful || return

ls_opts=("--exclude-per-directory=.gitignore")
[[ -f "$gitdir/info/exclude" ]] && ls_opts+="--exclude-from=$gitdir/info/exclude"

files=(${(ps:\0:)"$(cd $gitdir/..; _call_program files git ls-files -z $ls_opts $opts 2>/dev/null)"})
__git_command_successful || return

if [[ -z $gitdir ]] {
	echo "wtf: $gitdir"
	return
}

if [[ ! -d .git ]] {
	dirdiff=${PWD#${gitdir%/.git}}
	for ((i = 1; i <= $#files; i++)) {
		prefix=''
		item=(${(s:/:)files[$i]})
		self=(${(s:/:)dirdiff})
		for ((j = 1; j <= ${#item} && j <= ${#self}; j++)) {
			if [[ $item[$j] == $self[$j] ]] {
				item[$j]=''
			} else {
				prefix+='../'
			}
		}
		files[$i]=${prefix}${(j:/:)item}
	}
}
_wanted files expl 'index file' _multi_parts $@ - / files