-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ignore-completion.bash
More file actions
124 lines (106 loc) · 4.38 KB
/
git-ignore-completion.bash
File metadata and controls
124 lines (106 loc) · 4.38 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
#!/usr/bin/env bash
# Bash completion for git-ignore command
# Installation: source this file in your .bashrc or place in /etc/bash_completion.d/
_git_ignore() {
local cur prev words cword
_init_completion -n : || return
local commands="add remove rm list ls check help"
local git_root
# Try to get git root, return if not in a git repository
git_root=$(git rev-parse --show-toplevel 2>/dev/null) || return
# Get the command (if any)
local command=""
local i
for ((i=1; i < cword; i++)); do
if [[ " $commands " == *" ${words[i]} "* ]]; then
command="${words[i]}"
break
fi
done
# If no command yet, complete with commands or files
if [[ -z "$command" ]]; then
if [[ "$cur" == -* ]]; then
COMPREPLY=($(compgen -W "--help -h" -- "$cur"))
else
# Offer both commands and files/directories for the default 'add' action
local cmd_completions=($(compgen -W "$commands" -- "$cur"))
local file_completions=()
# Get files and directories, excluding those already in .gitignore
if [[ -f "$git_root/.gitignore" ]]; then
# Get all files not ignored
local files=$(git ls-files --others --exclude-standard 2>/dev/null | head -50)
file_completions=($(compgen -W "$files" -- "$cur"))
# Add directories
local dirs=$(find . -maxdepth 2 -type d -not -path '*/\.*' 2>/dev/null | sed 's|^\./||' | grep -v '^\.$')
file_completions+=($(compgen -W "$dirs" -- "$cur"))
else
# If no .gitignore, show all files and directories
file_completions=($(compgen -f -- "$cur"))
fi
COMPREPLY=("${cmd_completions[@]}" "${file_completions[@]}")
fi
return
fi
# Handle command-specific completions
case "$command" in
add)
# Complete with untracked files and directories
local untracked=$(git ls-files --others --exclude-standard 2>/dev/null | head -50)
local dirs=$(find . -maxdepth 2 -type d -not -path '*/\.*' 2>/dev/null | sed 's|^\./||' | grep -v '^\.$')
COMPREPLY=($(compgen -W "$untracked $dirs" -- "$cur"))
# Add common patterns as suggestions if no current word
if [[ -z "$cur" ]]; then
local common_patterns="*.log *.tmp *.swp .DS_Store node_modules/ build/ dist/ target/ *.pyc __pycache__/ .env .vscode/ .idea/"
COMPREPLY+=($(compgen -W "$common_patterns" -- "$cur"))
fi
;;
remove|rm)
# Complete with patterns from .gitignore
if [[ -f "$git_root/.gitignore" ]]; then
local patterns=$(grep -v '^#' "$git_root/.gitignore" 2>/dev/null | grep -v '^[[:space:]]*$')
COMPREPLY=($(compgen -W "$patterns" -- "$cur"))
fi
;;
check)
# Complete with all files
COMPREPLY=($(compgen -f -- "$cur"))
;;
list|ls|help)
# No completion needed
;;
esac
}
# Register completion for git-ignore when called directly
complete -F _git_ignore git-ignore
# Register completion for 'git ignore' subcommand
# This hooks into git's completion system
_git_ignore_integrated() {
_git_ignore
}
# Try to register with git's completion system if available
if declare -F __git_complete >/dev/null 2>&1; then
__git_complete git-ignore _git_ignore_integrated
fi
# Also handle 'git ignore' format by extending git's completion
if declare -F _git >/dev/null 2>&1; then
# Save the original git completion function
_git_orig=$(declare -f _git)
# Override git completion to handle 'ignore' subcommand
_git_extended() {
local cur words cword prev
_get_comp_words_by_ref -n : cur words cword prev
# Check if we're completing 'git ignore'
if [[ ${words[1]} == "ignore" ]] && [[ $cword -ge 2 ]]; then
# Shift the words array to remove 'git'
words=("${words[@]:1}")
((cword--))
_git_ignore
else
# Call original git completion
eval "$_git_orig"
_git
fi
}
# Replace git completion
complete -F _git_extended git
fi