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_pull ()
330 {
331 local cur="${COMP_WORDS[COMP_CWORD]}"
333 case "${COMP_WORDS[0]},$COMP_CWORD" in
334 git-pull*,1)
335 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
336 ;;
337 git,2)
338 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
339 ;;
340 *)
341 local remote
342 case "${COMP_WORDS[0]}" in
343 git-pull) remote="${COMP_WORDS[1]}" ;;
344 git) remote="${COMP_WORDS[2]}" ;;
345 esac
346 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
347 ;;
348 esac
349 }
351 _git_push ()
352 {
353 local cur="${COMP_WORDS[COMP_CWORD]}"
355 case "${COMP_WORDS[0]},$COMP_CWORD" in
356 git-push*,1)
357 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
358 ;;
359 git,2)
360 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
361 ;;
362 *)
363 case "$cur" in
364 *:*)
365 local remote
366 case "${COMP_WORDS[0]}" in
367 git-push) remote="${COMP_WORDS[1]}" ;;
368 git) remote="${COMP_WORDS[2]}" ;;
369 esac
370 cur="${cur#*:}"
371 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
372 ;;
373 *)
374 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
375 ;;
376 esac
377 ;;
378 esac
379 }
381 _git_reset ()
382 {
383 local cur="${COMP_WORDS[COMP_CWORD]}"
384 local opt="--mixed --hard --soft"
385 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
386 }
388 _git_show ()
389 {
390 local cur="${COMP_WORDS[COMP_CWORD]}"
391 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
392 }
394 _git ()
395 {
396 local i c=1 command __git_dir
398 while [ $c -lt $COMP_CWORD ]; do
399 i="${COMP_WORDS[c]}"
400 case "$i" in
401 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
402 --bare) __git_dir="." ;;
403 --version|--help|-p|--paginate) ;;
404 *) command="$i"; break ;;
405 esac
406 c=$((++c))
407 done
409 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
410 COMPREPLY=($(compgen -W "
411 --git-dir= --version --exec-path
412 $(__git_commands)
413 $(__git_aliases)
414 " -- "${COMP_WORDS[COMP_CWORD]}"))
415 return;
416 fi
418 local expansion=$(__git_aliased_command "$command")
419 [ "$expansion" ] && command="$expansion"
421 case "$command" in
422 branch) _git_branch ;;
423 cat-file) _git_cat_file ;;
424 checkout) _git_checkout ;;
425 diff) _git_diff ;;
426 diff-tree) _git_diff_tree ;;
427 fetch) _git_fetch ;;
428 log) _git_log ;;
429 ls-remote) _git_ls_remote ;;
430 ls-tree) _git_ls_tree ;;
431 merge) _git_merge;;
432 merge-base) _git_merge_base ;;
433 pull) _git_pull ;;
434 push) _git_push ;;
435 reset) _git_reset ;;
436 show) _git_show ;;
437 show-branch) _git_log ;;
438 whatchanged) _git_log ;;
439 *) COMPREPLY=() ;;
440 esac
441 }
443 _gitk ()
444 {
445 local cur="${COMP_WORDS[COMP_CWORD]}"
446 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
447 }
449 complete -o default -o nospace -F _git git
450 complete -o default -F _gitk gitk
451 complete -o default -F _git_branch git-branch
452 complete -o default -o nospace -F _git_cat_file git-cat-file
453 complete -o default -F _git_checkout git-checkout
454 complete -o default -o nospace -F _git_diff git-diff
455 complete -o default -F _git_diff_tree git-diff-tree
456 complete -o default -o nospace -F _git_fetch git-fetch
457 complete -o default -o nospace -F _git_log git-log
458 complete -o default -F _git_ls_remote git-ls-remote
459 complete -o default -o nospace -F _git_ls_tree git-ls-tree
460 complete -o default -F _git_merge git-merge
461 complete -o default -F _git_merge_base git-merge-base
462 complete -o default -o nospace -F _git_pull git-pull
463 complete -o default -o nospace -F _git_push git-push
464 complete -o default -F _git_reset git-reset
465 complete -o default -F _git_show git-show
466 complete -o default -o nospace -F _git_log git-show-branch
467 complete -o default -o nospace -F _git_log git-whatchanged
469 # The following are necessary only for Cygwin, and only are needed
470 # when the user has tab-completed the executable name and consequently
471 # included the '.exe' suffix.
472 #
473 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
474 complete -o default -o nospace -F _git git.exe
475 complete -o default -F _git_branch git-branch.exe
476 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
477 complete -o default -o nospace -F _git_diff git-diff.exe
478 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
479 complete -o default -o nospace -F _git_log git-log.exe
480 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
481 complete -o default -F _git_merge_base git-merge-base.exe
482 complete -o default -o nospace -F _git_push git-push.exe
483 complete -o default -o nospace -F _git_log git-show-branch.exe
484 complete -o default -o nospace -F _git_log git-whatchanged.exe
485 fi