1 #
2 # bash completion support for core Git.
3 #
4 # Copyright (C) 2006 Shawn Pearce
5 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
6 #
7 # The contained completion routines provide support for completing:
8 #
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) tree paths within 'ref:path/to/file' expressions
14 #
15 # To use these routines:
16 #
17 # 1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
18 # 2) Added the following line to your .bashrc:
19 # source ~/.git-completion.sh
20 #
22 __gitdir ()
23 {
24 echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
25 }
27 __git_refs ()
28 {
29 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
30 if [ -d "$dir" ]; then
31 cmd=git-peek-remote
32 else
33 cmd=git-ls-remote
34 fi
35 for i in $($cmd "$dir" 2>/dev/null); do
36 case "$is_hash,$i" in
37 y,*) is_hash=n ;;
38 n,*^{}) is_hash=y ;;
39 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
40 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
41 n,*) is_hash=y; echo "$i" ;;
42 esac
43 done
44 }
46 __git_refs2 ()
47 {
48 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
49 if [ -d "$dir" ]; then
50 cmd=git-peek-remote
51 else
52 cmd=git-ls-remote
53 fi
54 for i in $($cmd "$dir" 2>/dev/null); do
55 case "$is_hash,$i" in
56 y,*) is_hash=n ;;
57 n,*^{}) is_hash=y ;;
58 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
59 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
60 n,*) is_hash=y; echo "$i:$i" ;;
61 esac
62 done
63 }
65 __git_remotes ()
66 {
67 local i ngoff IFS=$'\n' d="$(__gitdir)"
68 shopt -q nullglob || ngoff=1
69 shopt -s nullglob
70 for i in "$d/remotes"/*; do
71 echo ${i#$d/remotes/}
72 done
73 [ "$ngoff" ] && shopt -u nullglob
74 for i in $(git --git-dir="$d" repo-config --list); do
75 case "$i" in
76 remote.*.url=*)
77 i="${i#remote.}"
78 echo "${i/.url=*/}"
79 ;;
80 esac
81 done
82 }
84 __git_merge_strategies ()
85 {
86 sed -n "/^all_strategies='/{
87 s/^all_strategies='//
88 s/'//
89 p
90 q
91 }" "$(git --exec-path)/git-merge"
92 }
94 __git_complete_file ()
95 {
96 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
97 case "$cur" in
98 ?*:*)
99 ref="${cur%%:*}"
100 cur="${cur#*:}"
101 case "$cur" in
102 ?*/*)
103 pfx="${cur%/*}"
104 cur="${cur##*/}"
105 ls="$ref:$pfx"
106 pfx="$pfx/"
107 ;;
108 *)
109 ls="$ref"
110 ;;
111 esac
112 COMPREPLY=($(compgen -P "$pfx" \
113 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
114 | sed '/^100... blob /s,^.* ,,
115 /^040000 tree /{
116 s,^.* ,,
117 s,$,/,
118 }
119 s/^.* //')" \
120 -- "$cur"))
121 ;;
122 *)
123 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
124 ;;
125 esac
126 }
128 __git_commands ()
129 {
130 local i IFS=" "$'\n'
131 for i in $(git help -a|egrep '^ ')
132 do
133 case $i in
134 check-ref-format) : plumbing;;
135 commit-tree) : plumbing;;
136 convert-objects) : plumbing;;
137 cvsserver) : daemon;;
138 daemon) : daemon;;
139 fetch-pack) : plumbing;;
140 hash-object) : plumbing;;
141 http-*) : transport;;
142 index-pack) : plumbing;;
143 local-fetch) : plumbing;;
144 mailinfo) : plumbing;;
145 mailsplit) : plumbing;;
146 merge-*) : plumbing;;
147 mktree) : plumbing;;
148 mktag) : plumbing;;
149 pack-objects) : plumbing;;
150 pack-redundant) : plumbing;;
151 pack-refs) : plumbing;;
152 parse-remote) : plumbing;;
153 patch-id) : plumbing;;
154 peek-remote) : plumbing;;
155 read-tree) : plumbing;;
156 receive-pack) : plumbing;;
157 rerere) : plumbing;;
158 rev-list) : plumbing;;
159 rev-parse) : plumbing;;
160 runstatus) : plumbing;;
161 sh-setup) : internal;;
162 shell) : daemon;;
163 send-pack) : plumbing;;
164 show-index) : plumbing;;
165 ssh-*) : transport;;
166 stripspace) : plumbing;;
167 symbolic-ref) : plumbing;;
168 unpack-file) : plumbing;;
169 unpack-objects) : plumbing;;
170 update-ref) : plumbing;;
171 update-server-info) : daemon;;
172 upload-archive) : plumbing;;
173 upload-pack) : plumbing;;
174 write-tree) : plumbing;;
175 *) echo $i;;
176 esac
177 done
178 }
180 __git_aliases ()
181 {
182 local i IFS=$'\n'
183 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
184 case "$i" in
185 alias.*)
186 i="${i#alias.}"
187 echo "${i/=*/}"
188 ;;
189 esac
190 done
191 }
193 __git_aliased_command ()
194 {
195 local word cmdline=$(git --git-dir="$(__gitdir)" \
196 repo-config --get "alias.$1")
197 for word in $cmdline; do
198 if [ "${word##-*}" ]; then
199 echo $word
200 return
201 fi
202 done
203 }
205 _git_branch ()
206 {
207 local cur="${COMP_WORDS[COMP_CWORD]}"
208 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
209 }
211 _git_cat_file ()
212 {
213 local cur="${COMP_WORDS[COMP_CWORD]}"
214 case "${COMP_WORDS[0]},$COMP_CWORD" in
215 git-cat-file*,1)
216 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
217 ;;
218 git,2)
219 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
220 ;;
221 *)
222 __git_complete_file
223 ;;
224 esac
225 }
227 _git_checkout ()
228 {
229 local cur="${COMP_WORDS[COMP_CWORD]}"
230 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
231 }
233 _git_diff ()
234 {
235 __git_complete_file
236 }
238 _git_diff_tree ()
239 {
240 local cur="${COMP_WORDS[COMP_CWORD]}"
241 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
242 }
244 _git_fetch ()
245 {
246 local cur="${COMP_WORDS[COMP_CWORD]}"
248 case "${COMP_WORDS[0]},$COMP_CWORD" in
249 git-fetch*,1)
250 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
251 ;;
252 git,2)
253 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
254 ;;
255 *)
256 case "$cur" in
257 *:*)
258 cur="${cur#*:}"
259 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
260 ;;
261 *)
262 local remote
263 case "${COMP_WORDS[0]}" in
264 git-fetch) remote="${COMP_WORDS[1]}" ;;
265 git) remote="${COMP_WORDS[2]}" ;;
266 esac
267 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
268 ;;
269 esac
270 ;;
271 esac
272 }
274 _git_ls_remote ()
275 {
276 local cur="${COMP_WORDS[COMP_CWORD]}"
277 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
278 }
280 _git_ls_tree ()
281 {
282 __git_complete_file
283 }
285 _git_log ()
286 {
287 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
288 case "$cur" in
289 *...*)
290 pfx="${cur%...*}..."
291 cur="${cur#*...}"
292 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
293 ;;
294 *..*)
295 pfx="${cur%..*}.."
296 cur="${cur#*..}"
297 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
298 ;;
299 *)
300 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
301 ;;
302 esac
303 }
305 _git_merge ()
306 {
307 local cur="${COMP_WORDS[COMP_CWORD]}"
308 case "$cur" in
309 --*)
310 COMPREPLY=($(compgen -W "
311 --no-commit --no-summary --squash
312 " -- "$cur"))
313 return
314 esac
315 if [ $COMP_CWORD -gt 1 -a X-s = "X${COMP_WORDS[COMP_CWORD-1]}" ]
316 then
317 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
318 else
319 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
320 fi
321 }
323 _git_merge_base ()
324 {
325 local cur="${COMP_WORDS[COMP_CWORD]}"
326 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
327 }
329 _git_name_rev ()
330 {
331 local cur="${COMP_WORDS[COMP_CWORD]}"
332 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
333 }
335 _git_pull ()
336 {
337 local cur="${COMP_WORDS[COMP_CWORD]}"
339 case "${COMP_WORDS[0]},$COMP_CWORD" in
340 git-pull*,1)
341 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
342 ;;
343 git,2)
344 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
345 ;;
346 *)
347 local remote
348 case "${COMP_WORDS[0]}" in
349 git-pull) remote="${COMP_WORDS[1]}" ;;
350 git) remote="${COMP_WORDS[2]}" ;;
351 esac
352 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
353 ;;
354 esac
355 }
357 _git_push ()
358 {
359 local cur="${COMP_WORDS[COMP_CWORD]}"
361 case "${COMP_WORDS[0]},$COMP_CWORD" in
362 git-push*,1)
363 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
364 ;;
365 git,2)
366 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
367 ;;
368 *)
369 case "$cur" in
370 *:*)
371 local remote
372 case "${COMP_WORDS[0]}" in
373 git-push) remote="${COMP_WORDS[1]}" ;;
374 git) remote="${COMP_WORDS[2]}" ;;
375 esac
376 cur="${cur#*:}"
377 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
378 ;;
379 *)
380 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
381 ;;
382 esac
383 ;;
384 esac
385 }
387 _git_reset ()
388 {
389 local cur="${COMP_WORDS[COMP_CWORD]}"
390 local opt="--mixed --hard --soft"
391 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
392 }
394 _git_show ()
395 {
396 local cur="${COMP_WORDS[COMP_CWORD]}"
397 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
398 }
400 _git ()
401 {
402 local i c=1 command __git_dir
404 while [ $c -lt $COMP_CWORD ]; do
405 i="${COMP_WORDS[c]}"
406 case "$i" in
407 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
408 --bare) __git_dir="." ;;
409 --version|--help|-p|--paginate) ;;
410 *) command="$i"; break ;;
411 esac
412 c=$((++c))
413 done
415 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
416 COMPREPLY=($(compgen -W "
417 --git-dir= --version --exec-path
418 $(__git_commands)
419 $(__git_aliases)
420 " -- "${COMP_WORDS[COMP_CWORD]}"))
421 return;
422 fi
424 local expansion=$(__git_aliased_command "$command")
425 [ "$expansion" ] && command="$expansion"
427 case "$command" in
428 branch) _git_branch ;;
429 cat-file) _git_cat_file ;;
430 checkout) _git_checkout ;;
431 diff) _git_diff ;;
432 diff-tree) _git_diff_tree ;;
433 fetch) _git_fetch ;;
434 log) _git_log ;;
435 ls-remote) _git_ls_remote ;;
436 ls-tree) _git_ls_tree ;;
437 merge) _git_merge;;
438 merge-base) _git_merge_base ;;
439 name-rev) _git_name_rev ;;
440 pull) _git_pull ;;
441 push) _git_push ;;
442 reset) _git_reset ;;
443 show) _git_show ;;
444 show-branch) _git_log ;;
445 whatchanged) _git_log ;;
446 *) COMPREPLY=() ;;
447 esac
448 }
450 _gitk ()
451 {
452 local cur="${COMP_WORDS[COMP_CWORD]}"
453 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
454 }
456 complete -o default -o nospace -F _git git
457 complete -o default -F _gitk gitk
458 complete -o default -F _git_branch git-branch
459 complete -o default -o nospace -F _git_cat_file git-cat-file
460 complete -o default -F _git_checkout git-checkout
461 complete -o default -o nospace -F _git_diff git-diff
462 complete -o default -F _git_diff_tree git-diff-tree
463 complete -o default -o nospace -F _git_fetch git-fetch
464 complete -o default -o nospace -F _git_log git-log
465 complete -o default -F _git_ls_remote git-ls-remote
466 complete -o default -o nospace -F _git_ls_tree git-ls-tree
467 complete -o default -F _git_merge git-merge
468 complete -o default -F _git_merge_base git-merge-base
469 complete -o default -F _git_name_rev git-name-rev
470 complete -o default -o nospace -F _git_pull git-pull
471 complete -o default -o nospace -F _git_push git-push
472 complete -o default -F _git_reset git-reset
473 complete -o default -F _git_show git-show
474 complete -o default -o nospace -F _git_log git-show-branch
475 complete -o default -o nospace -F _git_log git-whatchanged
477 # The following are necessary only for Cygwin, and only are needed
478 # when the user has tab-completed the executable name and consequently
479 # included the '.exe' suffix.
480 #
481 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
482 complete -o default -o nospace -F _git git.exe
483 complete -o default -F _git_branch git-branch.exe
484 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
485 complete -o default -o nospace -F _git_diff git-diff.exe
486 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
487 complete -o default -o nospace -F _git_log git-log.exe
488 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
489 complete -o default -F _git_merge_base git-merge-base.exe
490 complete -o default -F _git_name_rev git-name-rev.exe
491 complete -o default -o nospace -F _git_push git-push.exe
492 complete -o default -o nospace -F _git_log git-show-branch.exe
493 complete -o default -o nospace -F _git_log git-whatchanged.exe
494 fi