Repository navigation
Expand file tree
/
Copy pathgit-ignore-completion.zsh
More file actions
161 lines (140 loc) · 5.85 KB
/
Copy pathgit-ignore-completion.zsh
File metadata and controls
161 lines (140 loc) · 5.85 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#compdef git-ignore
# Zsh completion for git-ignore command
# Installation: Place in a directory in your $fpath (e.g., ~/.zsh/completions/)
# and ensure compinit is called in your .zshrc
_git-ignore() {
local context state line
local -A opt_args
local git_root
# Get git root directory
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -z "$git_root" ]]; then
return 1
fi
# Define common ignore patterns for suggestions
local -a common_patterns
common_patterns=(
'*.log:Log files'
'*.tmp:Temporary files'
'*.swp:Vim swap files'
'.DS_Store:macOS system files'
'node_modules/:Node.js dependencies'
'build/:Build directory'
'dist/:Distribution directory'
'target/:Target directory'
'*.pyc:Python compiled files'
'__pycache__/:Python cache directory'
'.env:Environment variables file'
'.vscode/:VS Code directory'
'.idea/:IntelliJ IDEA directory'
'*.o:Object files'
'*.so:Shared libraries'
'*.exe:Executable files'
'venv/:Python virtual environment'
'.pytest_cache/:Pytest cache'
'coverage/:Coverage reports'
)
# Main argument handling
_arguments -C \
'1:command:->command' \
'*:argument:->argument'
case $state in
command)
local -a subcommands
subcommands=(
'add:Add pattern(s) to .gitignore'
'remove:Remove pattern(s) from .gitignore'
'rm:Remove pattern(s) from .gitignore'
'list:List all patterns in .gitignore'
'ls:List all patterns in .gitignore'
'check:Check if file(s) are ignored'
'help:Show help information'
)
# Also offer files for direct add (default action)
if [[ -z "$words[2]" ]] || [[ "$words[2]" == "${PREFIX}"* ]]; then
local -a files_and_commands
files_and_commands=()
# Add subcommands
files_and_commands+=($subcommands)
# Add untracked files
local untracked
untracked=(${(f)"$(git ls-files --others --exclude-standard 2>/dev/null | head -50)"})
for file in $untracked; do
files_and_commands+=("$file:Untracked file")
done
# Add directories
local dirs
dirs=(${(f)"$(find . -maxdepth 2 -type d -not -path '*/\.*' 2>/dev/null | sed 's|^\./||' | grep -v '^\.$')"})
for dir in $dirs; do
files_and_commands+=("$dir:Directory")
done
_describe -t commands 'git-ignore commands' files_and_commands
else
_describe -t commands 'git-ignore commands' subcommands
fi
;;
argument)
case $words[2] in
add)
local -a suggestions
suggestions=()
# Add untracked files
local untracked
untracked=(${(f)"$(git ls-files --others --exclude-standard 2>/dev/null | head -50)"})
for file in $untracked; do
suggestions+=("$file:Untracked file")
done
# Add directories
local dirs
dirs=(${(f)"$(find . -maxdepth 2 -type d -not -path '*/\.*' 2>/dev/null | sed 's|^\./||' | grep -v '^\.$')"})
for dir in $dirs; do
suggestions+=("$dir:Directory")
done
# Add common patterns
suggestions+=($common_patterns)
_describe -t patterns 'patterns to ignore' suggestions
;;
remove|rm)
# Complete with existing patterns from .gitignore
local -a patterns
if [[ -f "$git_root/.gitignore" ]]; then
patterns=(${(f)"$(grep -v '^#' "$git_root/.gitignore" 2>/dev/null | grep -v '^[[:space:]]*$')"})
_describe -t patterns 'patterns in .gitignore' patterns
fi
;;
check)
# Complete with all files
_files
;;
*)
# For unrecognised commands or when adding directly, complete with files
local -a untracked_files
untracked_files=(${(f)"$(git ls-files --others --exclude-standard 2>/dev/null | head -50)"})
if [[ ${#untracked_files} -gt 0 ]]; then
_describe -t files 'untracked files' untracked_files
fi
# Also offer common patterns
_describe -t patterns 'common patterns' common_patterns
;;
esac
;;
esac
}
# Register completion for git-ignore command
compdef _git-ignore git-ignore
# Also register for 'git ignore' if git completion is available
if (( $+functions[_git] )); then
# Create a wrapper function for git-ignore when called as 'git ignore'
_git_ignore() {
# Remove 'git' from the words array and adjust CURRENT
words=("git-ignore" "${words[@]:2}")
CURRENT=$((CURRENT - 1))
_git-ignore
}
# Register as a git subcommand
if (( $+functions[_git_commands] )); then
_git_commands+=('ignore:manage .gitignore file')
fi
# Register the completion function
compdef _git_ignore git-ignore
fi