@matheusbsbs / matheus-bagliano / commits / da4902a730
Fork 0

completion: remove unnecessary _get_comp_words_by_ref() invocations

In v1.7.4-rc0~11^2~2 (bash: get --pretty=m<tab> completion to work with bash v4, 2010-12-02) we started to use _get_comp_words_by_ref() to access completion-related variables. That was large change, and to make it easily reviewable, we invoked _get_comp_words_by_ref() in each completion function and systematically replaced every occurance of bash's completion-related variables ($COMP_WORDS and $COMP_CWORD) with variables set by _get_comp_words_by_ref(). This has the downside that _get_comp_words_by_ref() is invoked several times during a single completion. The worst offender is perhaps 'git log mas<TAB>': during the completion of 'master' _get_comp_words_by_ref() is invoked no less than six times. However, the variables $prev, $cword, and $words provided by _get_comp_words_by_ref() are not modified in any of the completion functions, and the previous commit ensures that the $cur variable is not modified as well. This makes it possible to invoke _get_comp_words_by_ref() to get those variables only once in our toplevel completion functions _git() and _gitk(), and all other completion functions will inherit them. Signed-off-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Apr 28, 2011 at 18:01 UTC da4902a73017ad82b9926d03101ec69a2802d1e7
1 file changed +11 -105
contrib/completion/git-completion.bash
+11 -105
@@ -489,8 +489,6 @@ fi
489 # generates completion reply with compgen
490 __gitcomp ()
491 {
492 - local cur
493 - _get_comp_words_by_ref -n =: cur
492 local cur_="$cur"
493
494 if [ $# -gt 2 ]; then
@@ -553,8 +551,7 @@ __git_tags ()
551 __git_refs ()
552 {
553 local i is_hash=y dir="$(__gitdir "${1-}")" track="${2-}"
556 - local cur format refs
557 - _get_comp_words_by_ref -n =: cur
554 + local format refs
555 if [ -d "$dir" ]; then
556 case "$cur" in
557 refs|refs/*)
@@ -668,9 +665,7 @@ __git_compute_merge_strategies ()
665
666 __git_complete_revlist_file ()
667 {
671 - local pfx ls ref cur
672 - _get_comp_words_by_ref -n =: cur
673 - local cur_="$cur"
668 + local pfx ls ref cur_="$cur"
669 case "$cur_" in
670 *..?*:*)
671 return
@@ -742,8 +737,6 @@ __git_complete_revlist ()
737
738 __git_complete_remote_or_refspec ()
739 {
745 - local cur words cword
746 - _get_comp_words_by_ref -n =: cur words cword
740 local cur_="$cur" cmd="${words[1]}"
741 local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
742 while [ $c -lt $cword ]; do
@@ -815,8 +808,6 @@ __git_complete_remote_or_refspec ()
808
809 __git_complete_strategy ()
810 {
818 - local cur prev
819 - _get_comp_words_by_ref -n =: cur prev
811 __git_compute_merge_strategies
812 case "$prev" in
813 -s|--strategy)
@@ -994,8 +985,7 @@ __git_aliased_command ()
985 # __git_find_on_cmdline requires 1 argument
986 __git_find_on_cmdline ()
987 {
997 - local word subcommand c=1 words cword
998 - _get_comp_words_by_ref -n =: words cword
988 + local word subcommand c=1
989 while [ $c -lt $cword ]; do
990 word="${words[c]}"
991 for subcommand in $1; do
@@ -1010,8 +1000,7 @@ __git_find_on_cmdline ()
1000
1001 __git_has_doubledash ()
1002 {
1013 - local c=1 words cword
1014 - _get_comp_words_by_ref -n =: words cword
1003 + local c=1
1004 while [ $c -lt $cword ]; do
1005 if [ "--" = "${words[c]}" ]; then
1006 return 0
@@ -1025,8 +1014,7 @@ __git_whitespacelist="nowarn warn error error-all fix"
1014
1015 _git_am ()
1016 {
1028 - local cur dir="$(__gitdir)"
1029 - _get_comp_words_by_ref -n =: cur
1017 + local dir="$(__gitdir)"
1018 if [ -d "$dir"/rebase-apply ]; then
1019 __gitcomp "--skip --continue --resolved --abort"
1020 return
@@ -1050,8 +1038,6 @@ _git_am ()
1038
1039 _git_apply ()
1040 {
1053 - local cur
1054 - _get_comp_words_by_ref -n =: cur
1041 case "$cur" in
1042 --whitespace=*)
1043 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
@@ -1074,8 +1060,6 @@ _git_add ()
1060 {
1061 __git_has_doubledash && return
1062
1077 - local cur
1078 - _get_comp_words_by_ref -n =: cur
1063 case "$cur" in
1064 --*)
1065 __gitcomp "
@@ -1089,8 +1073,6 @@ _git_add ()
1073
1074 _git_archive ()
1075 {
1092 - local cur
1093 - _get_comp_words_by_ref -n =: cur
1076 case "$cur" in
1077 --format=*)
1078 __gitcomp "$(git archive --list)" "" "${cur##--format=}"
@@ -1138,9 +1120,8 @@ _git_bisect ()
1120
1121 _git_branch ()
1122 {
1141 - local i c=1 only_local_ref="n" has_r="n" cur words cword
1123 + local i c=1 only_local_ref="n" has_r="n"
1124
1143 - _get_comp_words_by_ref -n =: cur words cword
1125 while [ $c -lt $cword ]; do
1126 i="${words[c]}"
1127 case "$i" in
@@ -1170,8 +1151,6 @@ _git_branch ()
1151
1152 _git_bundle ()
1153 {
1173 - local words cword
1174 - _get_comp_words_by_ref -n =: words cword
1154 local cmd="${words[2]}"
1155 case "$cword" in
1156 2)
@@ -1194,8 +1173,6 @@ _git_checkout ()
1173 {
1174 __git_has_doubledash && return
1175
1197 - local cur
1198 - _get_comp_words_by_ref -n =: cur
1176 case "$cur" in
1177 --conflict=*)
1178 __gitcomp "diff3 merge" "" "${cur##--conflict=}"
@@ -1225,8 +1202,6 @@ _git_cherry ()
1202
1203 _git_cherry_pick ()
1204 {
1228 - local cur
1229 - _get_comp_words_by_ref -n =: cur
1205 case "$cur" in
1206 --*)
1207 __gitcomp "--edit --no-commit"
@@ -1241,8 +1216,6 @@ _git_clean ()
1216 {
1217 __git_has_doubledash && return
1218
1244 - local cur
1245 - _get_comp_words_by_ref -n =: cur
1219 case "$cur" in
1220 --*)
1221 __gitcomp "--dry-run --quiet"
@@ -1254,8 +1227,6 @@ _git_clean ()
1227
1228 _git_clone ()
1229 {
1257 - local cur
1258 - _get_comp_words_by_ref -n =: cur
1230 case "$cur" in
1231 --*)
1232 __gitcomp "
@@ -1282,8 +1253,6 @@ _git_commit ()
1253 {
1254 __git_has_doubledash && return
1255
1285 - local cur
1286 - _get_comp_words_by_ref -n =: cur
1256 case "$cur" in
1257 --cleanup=*)
1258 __gitcomp "default strip verbatim whitespace
@@ -1318,8 +1287,6 @@ _git_commit ()
1287
1288 _git_describe ()
1289 {
1321 - local cur
1322 - _get_comp_words_by_ref -n =: cur
1290 case "$cur" in
1291 --*)
1292 __gitcomp "
@@ -1351,8 +1318,6 @@ _git_diff ()
1318 {
1319 __git_has_doubledash && return
1320
1354 - local cur
1355 - _get_comp_words_by_ref -n =: cur
1321 case "$cur" in
1322 --*)
1323 __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
@@ -1373,8 +1338,6 @@ _git_difftool ()
1338 {
1339 __git_has_doubledash && return
1340
1376 - local cur
1377 - _get_comp_words_by_ref -n =: cur
1341 case "$cur" in
1342 --tool=*)
1343 __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
@@ -1399,8 +1362,6 @@ __git_fetch_options="
1362
1363 _git_fetch ()
1364 {
1402 - local cur
1403 - _get_comp_words_by_ref -n =: cur
1365 case "$cur" in
1366 --*)
1367 __gitcomp "$__git_fetch_options"
@@ -1412,8 +1373,6 @@ _git_fetch ()
1373
1374 _git_format_patch ()
1375 {
1415 - local cur
1416 - _get_comp_words_by_ref -n =: cur
1376 case "$cur" in
1377 --thread=*)
1378 __gitcomp "
@@ -1445,8 +1404,6 @@ _git_format_patch ()
1404
1405 _git_fsck ()
1406 {
1448 - local cur
1449 - _get_comp_words_by_ref -n =: cur
1407 case "$cur" in
1408 --*)
1409 __gitcomp "
@@ -1461,8 +1418,6 @@ _git_fsck ()
1418
1419 _git_gc ()
1420 {
1464 - local cur
1465 - _get_comp_words_by_ref -n =: cur
1421 case "$cur" in
1422 --*)
1423 __gitcomp "--prune --aggressive"
@@ -1481,8 +1436,6 @@ _git_grep ()
1436 {
1437 __git_has_doubledash && return
1438
1484 - local cur
1485 - _get_comp_words_by_ref -n =: cur
1439 case "$cur" in
1440 --*)
1441 __gitcomp "
@@ -1505,8 +1458,6 @@ _git_grep ()
1458
1459 _git_help ()
1460 {
1508 - local cur
1509 - _get_comp_words_by_ref -n =: cur
1461 case "$cur" in
1462 --*)
1463 __gitcomp "--all --info --man --web"
@@ -1524,8 +1475,6 @@ _git_help ()
1475
1476 _git_init ()
1477 {
1527 - local cur
1528 - _get_comp_words_by_ref -n =: cur
1478 case "$cur" in
1479 --shared=*)
1480 __gitcomp "
@@ -1545,8 +1494,6 @@ _git_ls_files ()
1494 {
1495 __git_has_doubledash && return
1496
1548 - local cur
1549 - _get_comp_words_by_ref -n =: cur
1497 case "$cur" in
1498 --*)
1499 __gitcomp "--cached --deleted --modified --others --ignored
@@ -1607,8 +1554,6 @@ _git_log ()
1554 if [ -f "$g/MERGE_HEAD" ]; then
1555 merge="--merge"
1556 fi
1610 - local cur
1611 - _get_comp_words_by_ref -n =: cur
1557 case "$cur" in
1558 --pretty=*)
1559 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
@@ -1662,8 +1607,6 @@ _git_merge ()
1607 {
1608 __git_complete_strategy && return
1609
1665 - local cur
1666 - _get_comp_words_by_ref -n =: cur
1610 case "$cur" in
1611 --*)
1612 __gitcomp "$__git_merge_options"
@@ -1674,8 +1617,6 @@ _git_merge ()
1617
1618 _git_mergetool ()
1619 {
1677 - local cur
1678 - _get_comp_words_by_ref -n =: cur
1620 case "$cur" in
1621 --tool=*)
1622 __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
@@ -1696,8 +1637,6 @@ _git_merge_base ()
1637
1638 _git_mv ()
1639 {
1699 - local cur
1700 - _get_comp_words_by_ref -n =: cur
1640 case "$cur" in
1641 --*)
1642 __gitcomp "--dry-run"
@@ -1716,8 +1655,6 @@ _git_notes ()
1655 {
1656 local subcommands='add append copy edit list prune remove show'
1657 local subcommand="$(__git_find_on_cmdline "$subcommands")"
1719 - local cur words cword
1720 - _get_comp_words_by_ref -n =: cur words cword
1658
1659 case "$subcommand,$cur" in
1660 ,--*)
@@ -1767,8 +1704,6 @@ _git_pull ()
1704 {
1705 __git_complete_strategy && return
1706
1770 - local cur
1771 - _get_comp_words_by_ref -n =: cur
1707 case "$cur" in
1708 --*)
1709 __gitcomp "
@@ -1784,8 +1719,6 @@ _git_pull ()
1719
1720 _git_push ()
1721 {
1787 - local cur prev
1788 - _get_comp_words_by_ref -n =: cur prev
1722 case "$prev" in
1723 --repo)
1724 __gitcomp "$(__git_remotes)"
@@ -1810,8 +1743,6 @@ _git_push ()
1743 _git_rebase ()
1744 {
1745 local dir="$(__gitdir)"
1813 - local cur
1814 - _get_comp_words_by_ref -n =: cur
1746 if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1747 __gitcomp "--continue --skip --abort"
1748 return
@@ -1853,8 +1784,6 @@ __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1784
1785 _git_send_email ()
1786 {
1856 - local cur
1857 - _get_comp_words_by_ref -n =: cur
1787 case "$cur" in
1788 --confirm=*)
1789 __gitcomp "
@@ -1896,8 +1825,6 @@ _git_stage ()
1825
1826 __git_config_get_set_variables ()
1827 {
1899 - local words cword
1900 - _get_comp_words_by_ref -n =: words cword
1828 local prevword word config_file= c=$cword
1829 while [ $c -gt 1 ]; do
1830 word="${words[c]}"
@@ -1928,8 +1855,6 @@ __git_config_get_set_variables ()
1855
1856 _git_config ()
1857 {
1931 - local cur prev
1932 - _get_comp_words_by_ref -n =: cur prev
1858 case "$prev" in
1859 branch.*.remote)
1860 __gitcomp "$(__git_remotes)"
@@ -2389,8 +2314,6 @@ _git_reset ()
2314 {
2315 __git_has_doubledash && return
2316
2392 - local cur
2393 - _get_comp_words_by_ref -n =: cur
2317 case "$cur" in
2318 --*)
2319 __gitcomp "--merge --mixed --hard --soft --patch"
@@ -2402,8 +2325,6 @@ _git_reset ()
2325
2326 _git_revert ()
2327 {
2405 - local cur
2406 - _get_comp_words_by_ref -n =: cur
2328 case "$cur" in
2329 --*)
2330 __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
@@ -2417,8 +2338,6 @@ _git_rm ()
2338 {
2339 __git_has_doubledash && return
2340
2420 - local cur
2421 - _get_comp_words_by_ref -n =: cur
2341 case "$cur" in
2342 --*)
2343 __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
@@ -2432,8 +2351,6 @@ _git_shortlog ()
2351 {
2352 __git_has_doubledash && return
2353
2435 - local cur
2436 - _get_comp_words_by_ref -n =: cur
2354 case "$cur" in
2355 --*)
2356 __gitcomp "
@@ -2451,8 +2368,6 @@ _git_show ()
2368 {
2369 __git_has_doubledash && return
2370
2454 - local cur
2455 - _get_comp_words_by_ref -n =: cur
2371 case "$cur" in
2372 --pretty=*)
2373 __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
@@ -2476,8 +2391,6 @@ _git_show ()
2391
2392 _git_show_branch ()
2393 {
2479 - local cur
2480 - _get_comp_words_by_ref -n =: cur
2394 case "$cur" in
2395 --*)
2396 __gitcomp "
@@ -2494,8 +2407,6 @@ _git_show_branch ()
2407
2408 _git_stash ()
2409 {
2497 - local cur
2498 - _get_comp_words_by_ref -n =: cur
2410 local save_opts='--keep-index --no-keep-index --quiet --patch'
2411 local subcommands='save list show apply clear drop pop create branch'
2412 local subcommand="$(__git_find_on_cmdline "$subcommands")"
@@ -2540,8 +2451,6 @@ _git_submodule ()
2451
2452 local subcommands="add status init update summary foreach sync"
2453 if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2543 - local cur
2544 - _get_comp_words_by_ref -n =: cur
2454 case "$cur" in
2455 --*)
2456 __gitcomp "--quiet --cached"
@@ -2585,8 +2494,6 @@ _git_svn ()
2494 --edit --rmdir --find-copies-harder --copy-similarity=
2495 "
2496
2588 - local cur
2589 - _get_comp_words_by_ref -n =: cur
2497 case "$subcommand,$cur" in
2498 fetch,--*)
2499 __gitcomp "--revision= --fetch-all $fc_opts"
@@ -2658,8 +2565,6 @@ _git_svn ()
2565 _git_tag ()
2566 {
2567 local i c=1 f=0
2661 - local words cword prev
2662 - _get_comp_words_by_ref -n =: words cword prev
2568 while [ $c -lt $cword ]; do
2569 i="${words[c]}"
2570 case "$i" in
@@ -2705,8 +2610,8 @@ _git ()
2610 setopt KSH_TYPESET
2611 fi
2612
2708 - local cur words cword
2709 - _get_comp_words_by_ref -n =: cur words cword
2613 + local cur words cword prev
2614 + _get_comp_words_by_ref -n =: cur words cword prev
2615 while [ $c -lt $cword ]; do
2616 i="${words[c]}"
2617 case "$i" in
@@ -2756,15 +2661,16 @@ _gitk ()
2661 setopt KSH_TYPESET
2662 fi
2663
2664 + local cur words cword prev
2665 + _get_comp_words_by_ref -n =: cur words cword prev
2666 +
2667 __git_has_doubledash && return
2668
2761 - local cur
2669 local g="$(__gitdir)"
2670 local merge=""
2671 if [ -f "$g/MERGE_HEAD" ]; then
2672 merge="--merge"
2673 fi
2767 - _get_comp_words_by_ref -n =: cur
2674 case "$cur" in
2675 --*)
2676 __gitcomp "