@matheusbsbs / matheus-bagliano / commits / 52d5c3b5b2
Fork 0

bash completion: fix completion issues with fetch, pull, and push

Sverre Rabbelier noticed a completion issue with push: $ git push ori<tab> git push origin $ git push -f ori<tab> git push -f origin/ Markus Heidelberg pointed out that the issue extends to fetch and pull. The reason is that the current code naively assumes that if COMP_CWORD=2, it should complete a remote name, otherwise it should complete a refspec. This assumption fails if there are any --options. This patch fixes that issue by instead scanning COMP_CWORDS to see if the remote has been completed yet (we now assume the first non-dashed argument is the remote). The new logic is factored into a function, shared by fetch, pull, and push. The new function also properly handles '.' as the remote. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Acked-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jay Soffian committed Mar 5, 2009 at 23:39 UTC 52d5c3b5b22b6a672ace19f631768a63bb6a2250
1 file changed +60 -49
contrib/completion/git-completion.bash
+60 -49
@@ -387,6 +387,63 @@ __git_complete_revlist ()
387 esac
388 }
389
390 +__git_complete_remote_or_refspec ()
391 +{
392 + local cmd="${COMP_WORDS[1]}"
393 + local cur="${COMP_WORDS[COMP_CWORD]}"
394 + local i c=2 remote="" pfx="" lhs=1
395 + while [ $c -lt $COMP_CWORD ]; do
396 + i="${COMP_WORDS[c]}"
397 + case "$i" in
398 + -*) ;;
399 + *) remote="$i"; break ;;
400 + esac
401 + c=$((++c))
402 + done
403 + if [ -z "$remote" ]; then
404 + __gitcomp "$(__git_remotes)"
405 + return
406 + fi
407 + [ "$remote" = "." ] && remote=
408 + case "$cur" in
409 + *:*)
410 + case "$COMP_WORDBREAKS" in
411 + *:*) : great ;;
412 + *) pfx="${cur%%:*}:" ;;
413 + esac
414 + cur="${cur#*:}"
415 + lhs=0
416 + ;;
417 + +*)
418 + pfx="+"
419 + cur="${cur#+}"
420 + ;;
421 + esac
422 + case "$cmd" in
423 + fetch)
424 + if [ $lhs = 1 ]; then
425 + __gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
426 + else
427 + __gitcomp "$(__git_refs)" "$pfx" "$cur"
428 + fi
429 + ;;
430 + pull)
431 + if [ $lhs = 1 ]; then
432 + __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
433 + else
434 + __gitcomp "$(__git_refs)" "$pfx" "$cur"
435 + fi
436 + ;;
437 + push)
438 + if [ $lhs = 1 ]; then
439 + __gitcomp "$(__git_refs)" "$pfx" "$cur"
440 + else
441 + __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
442 + fi
443 + ;;
444 + esac
445 +}
446 +
447 __git_all_commands ()
448 {
449 if [ -n "${__git_all_commandlist-}" ]; then
@@ -832,25 +889,7 @@ _git_diff ()
889
890 _git_fetch ()
891 {
835 - local cur="${COMP_WORDS[COMP_CWORD]}"
836 -
837 - if [ "$COMP_CWORD" = 2 ]; then
838 - __gitcomp "$(__git_remotes)"
839 - else
840 - case "$cur" in
841 - *:*)
842 - local pfx=""
843 - case "$COMP_WORDBREAKS" in
844 - *:*) : great ;;
845 - *) pfx="${cur%%:*}:" ;;
846 - esac
847 - __gitcomp "$(__git_refs)" "$pfx" "${cur#*:}"
848 - ;;
849 - *)
850 - __gitcomp "$(__git_refs2 "${COMP_WORDS[2]}")"
851 - ;;
852 - esac
853 - fi
892 + __git_complete_remote_or_refspec
893 }
894
895 _git_format_patch ()
@@ -1120,40 +1159,12 @@ _git_name_rev ()
1159
1160 _git_pull ()
1161 {
1123 - local cur="${COMP_WORDS[COMP_CWORD]}"
1124 -
1125 - if [ "$COMP_CWORD" = 2 ]; then
1126 - __gitcomp "$(__git_remotes)"
1127 - else
1128 - __gitcomp "$(__git_refs "${COMP_WORDS[2]}")"
1129 - fi
1162 + __git_complete_remote_or_refspec
1163 }
1164
1165 _git_push ()
1166 {
1134 - local cur="${COMP_WORDS[COMP_CWORD]}"
1135 -
1136 - if [ "$COMP_CWORD" = 2 ]; then
1137 - __gitcomp "$(__git_remotes)"
1138 - else
1139 - case "$cur" in
1140 - *:*)
1141 - local pfx=""
1142 - case "$COMP_WORDBREAKS" in
1143 - *:*) : great ;;
1144 - *) pfx="${cur%%:*}:" ;;
1145 - esac
1146 -
1147 - __gitcomp "$(__git_refs "${COMP_WORDS[2]}")" "$pfx" "${cur#*:}"
1148 - ;;
1149 - +*)
1150 - __gitcomp "$(__git_refs)" + "${cur#+}"
1151 - ;;
1152 - *)
1153 - __gitcomp "$(__git_refs)"
1154 - ;;
1155 - esac
1156 - fi
1167 + __git_complete_remote_or_refspec
1168 }
1169
1170 _git_rebase ()