@matheusbsbs / matheus-bagliano / commits / 873537fadc
Fork 0

Take --git-dir into consideration during bash completion.

If the user has setup a command line of "git --git-dir=baz" then anything we complete must be performed within the scope of "baz" and not the current working directory. This is useful with commands such as "git --git-dir=git.git log m" to complete out "master" and view the log for the master branch of the git.git repository. As a nice side effect this also works for aliases within the target repository, just as git would honor them. Unfortunately because we still examine arguments by absolute position in most of the more complex commands (e.g. git push) using --git-dir with those commands will probably still cause completion to fail. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Nov 5, 2006 at 06:21 UTC 873537fadc9bdc35726d1c69c46926c7f5c49dd2
1 file changed +70 -53
contrib/completion/git-completion.bash
+70 -53
@@ -19,15 +19,20 @@
19 # source ~/.git-completion.sh
20 #
21
22 +__gitdir ()
23 +{
24 + echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
25 +}
26 +
27 __git_refs ()
28 {
24 - local cmd i is_hash=y
25 - if [ -d "$1" ]; then
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
30 - for i in $($cmd "$1" 2>/dev/null); do
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 ;;
@@ -40,13 +45,13 @@ __git_refs ()
45
46 __git_refs2 ()
47 {
43 - local cmd i is_hash=y
44 - if [ -d "$1" ]; then
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
49 - for i in $($cmd "$1" 2>/dev/null); do
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 ;;
@@ -59,14 +64,14 @@ __git_refs2 ()
64
65 __git_remotes ()
66 {
62 - local i ngoff IFS=$'\n'
67 + local i ngoff IFS=$'\n' d="$(__gitdir)"
68 shopt -q nullglob || ngoff=1
69 shopt -s nullglob
65 - for i in .git/remotes/*; do
66 - echo ${i#.git/remotes/}
70 + for i in "$d/remotes"/*; do
71 + echo ${i#$d/remotes/}
72 done
73 [ "$ngoff" ] && shopt -u nullglob
69 - for i in $(git repo-config --list); do
74 + for i in $(git --git-dir="$d" repo-config --list); do
75 case "$i" in
76 remote.*.url=*)
77 i="${i#remote.}"
@@ -95,7 +100,7 @@ __git_complete_file ()
100 ;;
101 esac
102 COMPREPLY=($(compgen -P "$pfx" \
98 - -W "$(git-ls-tree "$ls" \
103 + -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
104 | sed '/^100... blob /s,^.* ,,
105 /^040000 tree /{
106 s,^.* ,,
@@ -105,7 +110,7 @@ __git_complete_file ()
110 -- "$cur"))
111 ;;
112 *)
108 - COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
113 + COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
114 ;;
115 esac
116 }
@@ -113,7 +118,7 @@ __git_complete_file ()
118 __git_aliases ()
119 {
120 local i IFS=$'\n'
116 - for i in $(git repo-config --list); do
121 + for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
122 case "$i" in
123 alias.*)
124 i="${i#alias.}"
@@ -125,7 +130,8 @@ __git_aliases ()
130
131 __git_aliased_command ()
132 {
128 - local word cmdline=$(git repo-config --get "alias.$1")
133 + local word cmdline=$(git --git-dir="$(__gitdir)" \
134 + repo-config --get "alias.$1")
135 for word in $cmdline; do
136 if [ "${word##-*}" ]; then
137 echo $word
@@ -137,7 +143,7 @@ __git_aliased_command ()
143 _git_branch ()
144 {
145 local cur="${COMP_WORDS[COMP_CWORD]}"
140 - COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs .)" -- "$cur"))
146 + COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
147 }
148
149 _git_cat_file ()
@@ -159,7 +165,7 @@ _git_cat_file ()
165 _git_checkout ()
166 {
167 local cur="${COMP_WORDS[COMP_CWORD]}"
162 - COMPREPLY=($(compgen -W "-l -b $(__git_refs .)" -- "$cur"))
168 + COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
169 }
170
171 _git_diff ()
@@ -170,7 +176,7 @@ _git_diff ()
176 _git_diff_tree ()
177 {
178 local cur="${COMP_WORDS[COMP_CWORD]}"
173 - COMPREPLY=($(compgen -W "-r -p -M $(__git_refs .)" -- "$cur"))
179 + COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
180 }
181
182 _git_fetch ()
@@ -188,7 +194,7 @@ _git_fetch ()
194 case "$cur" in
195 *:*)
196 cur=$(echo "$cur" | sed 's/^.*://')
191 - COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
197 + COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
198 ;;
199 *)
200 local remote
@@ -221,10 +227,10 @@ _git_log ()
227 *..*)
228 local pfx=$(echo "$cur" | sed 's/\.\..*$/../')
229 cur=$(echo "$cur" | sed 's/^.*\.\.//')
224 - COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur"))
230 + COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
231 ;;
232 *)
227 - COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
233 + COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
234 ;;
235 esac
236 }
@@ -232,7 +238,7 @@ _git_log ()
238 _git_merge_base ()
239 {
240 local cur="${COMP_WORDS[COMP_CWORD]}"
235 - COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
241 + COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
242 }
243
244 _git_pull ()
@@ -280,7 +286,7 @@ _git_push ()
286 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
287 ;;
288 *)
283 - COMPREPLY=($(compgen -W "$(__git_refs2 .)" -- "$cur"))
289 + COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
290 ;;
291 esac
292 ;;
@@ -291,56 +297,67 @@ _git_reset ()
297 {
298 local cur="${COMP_WORDS[COMP_CWORD]}"
299 local opt="--mixed --hard --soft"
294 - COMPREPLY=($(compgen -W "$opt $(__git_refs .)" -- "$cur"))
300 + COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
301 }
302
303 _git_show ()
304 {
305 local cur="${COMP_WORDS[COMP_CWORD]}"
300 - COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
306 + COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
307 }
308
309 _git ()
310 {
305 - if [ $COMP_CWORD = 1 ]; then
311 + local i c=1 command __git_dir
312 +
313 + while [ $c -lt $COMP_CWORD ]; do
314 + i="${COMP_WORDS[c]}"
315 + case "$i" in
316 + --git-dir=*) __git_dir="${i#--git-dir=}" ;;
317 + --bare) __git_dir="." ;;
318 + --version|--help|-p|--paginate) ;;
319 + *) command="$i"; break ;;
320 + esac
321 + c=$((++c))
322 + done
323 +
324 + if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
325 COMPREPLY=($(compgen \
307 - -W "--version $(git help -a|egrep '^ ') \
326 + -W "--git-dir= --version \
327 + $(git help -a|egrep '^ ') \
328 $(__git_aliases)" \
329 -- "${COMP_WORDS[COMP_CWORD]}"))
310 - else
311 - local command="${COMP_WORDS[1]}"
312 - local expansion=$(__git_aliased_command "$command")
330 + return;
331 + fi
332
314 - if [ "$expansion" ]; then
315 - command="$expansion"
316 - fi
333 + local expansion=$(__git_aliased_command "$command")
334 + [ "$expansion" ] && command="$expansion"
335
318 - case "$command" in
319 - branch) _git_branch ;;
320 - cat-file) _git_cat_file ;;
321 - checkout) _git_checkout ;;
322 - diff) _git_diff ;;
323 - diff-tree) _git_diff_tree ;;
324 - fetch) _git_fetch ;;
325 - log) _git_log ;;
326 - ls-remote) _git_ls_remote ;;
327 - ls-tree) _git_ls_tree ;;
328 - merge-base) _git_merge_base ;;
329 - pull) _git_pull ;;
330 - push) _git_push ;;
331 - reset) _git_reset ;;
332 - show) _git_show ;;
333 - show-branch) _git_log ;;
334 - whatchanged) _git_log ;;
335 - *) COMPREPLY=() ;;
336 - esac
337 - fi
336 + case "$command" in
337 + branch) _git_branch ;;
338 + cat-file) _git_cat_file ;;
339 + checkout) _git_checkout ;;
340 + diff) _git_diff ;;
341 + diff-tree) _git_diff_tree ;;
342 + fetch) _git_fetch ;;
343 + log) _git_log ;;
344 + ls-remote) _git_ls_remote ;;
345 + ls-tree) _git_ls_tree ;;
346 + merge-base) _git_merge_base ;;
347 + pull) _git_pull ;;
348 + push) _git_push ;;
349 + reset) _git_reset ;;
350 + show) _git_show ;;
351 + show-branch) _git_log ;;
352 + whatchanged) _git_log ;;
353 + *) COMPREPLY=() ;;
354 + esac
355 }
356
357 _gitk ()
358 {
359 local cur="${COMP_WORDS[COMP_CWORD]}"
343 - COMPREPLY=($(compgen -W "--all $(__git_refs .)" -- "$cur"))
360 + COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
361 }
362
363 complete -o default -o nospace -F _git git