321
done
322
}
323
324
+if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
325
+_get_comp_words_by_ref ()
326
+{
327
+ while [ $# -gt 0 ]; do
328
+ case "$1" in
329
+ cur)
330
+ cur=${COMP_WORDS[COMP_CWORD]}
331
+ ;;
332
+ prev)
333
+ prev=${COMP_WORDS[COMP_CWORD-1]}
334
+ ;;
335
+ words)
336
+ words=("${COMP_WORDS[@]}")
337
+ ;;
338
+ cword)
339
+ cword=$COMP_CWORD
340
+ ;;
341
+ -n)
342
+ # assume COMP_WORDBREAKS is already set sanely
343
+ shift
344
+ ;;
345
+ esac
346
+ shift
347
+ done
348
+}
349
+fi
350
+
351
# __gitcomp accepts 1, 2, 3, or 4 arguments
352
# generates completion reply with compgen
353
__gitcomp ()
354
{
328
- local cur="${COMP_WORDS[COMP_CWORD]}"
355
+ local cur
356
+ _get_comp_words_by_ref -n =: cur
357
if [ $# -gt 2 ]; then
358
cur="$3"
359
fi
412
__git_refs ()
413
{
414
local i is_hash=y dir="$(__gitdir "${1-}")"
387
- local cur="${COMP_WORDS[COMP_CWORD]}" format refs
415
+ local cur format refs
416
+ _get_comp_words_by_ref -n =: cur
417
if [ -d "$dir" ]; then
418
case "$cur" in
419
refs|refs/*)
511
512
__git_complete_file ()
513
{
485
- local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
514
+ local pfx ls ref cur
515
+ _get_comp_words_by_ref -n =: cur
516
case "$cur" in
517
?*:*)
518
ref="${cur%%:*}"
560
561
__git_complete_revlist ()
562
{
533
- local pfx cur="${COMP_WORDS[COMP_CWORD]}"
563
+ local pfx cur
564
+ _get_comp_words_by_ref -n =: cur
565
case "$cur" in
566
*...*)
567
pfx="${cur%...*}..."
581
582
__git_complete_remote_or_refspec ()
583
{
553
- local cmd="${COMP_WORDS[1]}"
554
- local cur="${COMP_WORDS[COMP_CWORD]}"
584
+ local cur words cword
585
+ _get_comp_words_by_ref -n =: cur words cword
586
+ local cmd="${words[1]}"
587
local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
556
- while [ $c -lt $COMP_CWORD ]; do
557
- i="${COMP_WORDS[c]}"
588
+ while [ $c -lt $cword ]; do
589
+ i="${words[c]}"
590
case "$i" in
591
--mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
592
--all)
654
655
__git_complete_strategy ()
656
{
657
+ local cur prev
658
+ _get_comp_words_by_ref -n =: cur prev
659
__git_compute_merge_strategies
626
- case "${COMP_WORDS[COMP_CWORD-1]}" in
660
+ case "$prev" in
661
-s|--strategy)
662
__gitcomp "$__git_merge_strategies"
663
return 0
664
esac
631
- local cur="${COMP_WORDS[COMP_CWORD]}"
665
case "$cur" in
666
--strategy=*)
667
__gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
821
# __git_find_on_cmdline requires 1 argument
822
__git_find_on_cmdline ()
823
{
791
- local word subcommand c=1
792
-
793
- while [ $c -lt $COMP_CWORD ]; do
794
- word="${COMP_WORDS[c]}"
824
+ local word subcommand c=1 words cword
825
+ _get_comp_words_by_ref -n =: words cword
826
+ while [ $c -lt $cword ]; do
827
+ word="${words[c]}"
828
for subcommand in $1; do
829
if [ "$subcommand" = "$word" ]; then
830
echo "$subcommand"
837
838
__git_has_doubledash ()
839
{
807
- local c=1
808
- while [ $c -lt $COMP_CWORD ]; do
809
- if [ "--" = "${COMP_WORDS[c]}" ]; then
840
+ local c=1 words cword
841
+ _get_comp_words_by_ref -n =: words cword
842
+ while [ $c -lt $cword ]; do
843
+ if [ "--" = "${words[c]}" ]; then
844
return 0
845
fi
846
c=$((++c))
852
853
_git_am ()
854
{
821
- local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
855
+ local cur dir="$(__gitdir)"
856
+ _get_comp_words_by_ref -n =: cur
857
if [ -d "$dir"/rebase-apply ]; then
858
__gitcomp "--skip --continue --resolved --abort"
859
return
877
878
_git_apply ()
879
{
845
- local cur="${COMP_WORDS[COMP_CWORD]}"
880
+ local cur
881
+ _get_comp_words_by_ref -n =: cur
882
case "$cur" in
883
--whitespace=*)
884
__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
901
{
902
__git_has_doubledash && return
903
868
- local cur="${COMP_WORDS[COMP_CWORD]}"
904
+ local cur
905
+ _get_comp_words_by_ref -n =: cur
906
case "$cur" in
907
--*)
908
__gitcomp "
916
917
_git_archive ()
918
{
882
- local cur="${COMP_WORDS[COMP_CWORD]}"
919
+ local cur
920
+ _get_comp_words_by_ref -n =: cur
921
case "$cur" in
922
--format=*)
923
__gitcomp "$(git archive --list)" "" "${cur##--format=}"
961
962
_git_branch ()
963
{
926
- local i c=1 only_local_ref="n" has_r="n"
964
+ local i c=1 only_local_ref="n" has_r="n" cur words cword
965
928
- while [ $c -lt $COMP_CWORD ]; do
929
- i="${COMP_WORDS[c]}"
966
+ _get_comp_words_by_ref -n =: cur words cword
967
+ while [ $c -lt $cword ]; do
968
+ i="${words[c]}"
969
case "$i" in
970
-d|-m) only_local_ref="y" ;;
971
-r) has_r="y" ;;
973
c=$((++c))
974
done
975
937
- case "${COMP_WORDS[COMP_CWORD]}" in
976
+ case "$cur" in
977
--*)
978
__gitcomp "
979
--color --no-color --verbose --abbrev= --no-abbrev
993
994
_git_bundle ()
995
{
957
- local cmd="${COMP_WORDS[2]}"
958
- case "$COMP_CWORD" in
996
+ local words cword
997
+ _get_comp_words_by_ref -n =: words cword
998
+ local cmd="${words[2]}"
999
+ case "$cword" in
1000
2)
1001
__gitcomp "create list-heads verify unbundle"
1002
;;
1017
{
1018
__git_has_doubledash && return
1019
979
- local cur="${COMP_WORDS[COMP_CWORD]}"
1020
+ local cur
1021
+ _get_comp_words_by_ref -n =: cur
1022
case "$cur" in
1023
--conflict=*)
1024
__gitcomp "diff3 merge" "" "${cur##--conflict=}"
1042
1043
_git_cherry_pick ()
1044
{
1003
- local cur="${COMP_WORDS[COMP_CWORD]}"
1045
+ local cur
1046
+ _get_comp_words_by_ref -n =: cur
1047
case "$cur" in
1048
--*)
1049
__gitcomp "--edit --no-commit"
1058
{
1059
__git_has_doubledash && return
1060
1018
- local cur="${COMP_WORDS[COMP_CWORD]}"
1061
+ local cur
1062
+ _get_comp_words_by_ref -n =: cur
1063
case "$cur" in
1064
--*)
1065
__gitcomp "--dry-run --quiet"
1071
1072
_git_clone ()
1073
{
1030
- local cur="${COMP_WORDS[COMP_CWORD]}"
1074
+ local cur
1075
+ _get_comp_words_by_ref -n =: cur
1076
case "$cur" in
1077
--*)
1078
__gitcomp "
1099
{
1100
__git_has_doubledash && return
1101
1057
- local cur="${COMP_WORDS[COMP_CWORD]}"
1102
+ local cur
1103
+ _get_comp_words_by_ref -n =: cur
1104
case "$cur" in
1105
--cleanup=*)
1106
__gitcomp "default strip verbatim whitespace
1135
1136
_git_describe ()
1137
{
1092
- local cur="${COMP_WORDS[COMP_CWORD]}"
1138
+ local cur
1139
+ _get_comp_words_by_ref -n =: cur
1140
case "$cur" in
1141
--*)
1142
__gitcomp "
1168
{
1169
__git_has_doubledash && return
1170
1124
- local cur="${COMP_WORDS[COMP_CWORD]}"
1171
+ local cur
1172
+ _get_comp_words_by_ref -n =: cur
1173
case "$cur" in
1174
--*)
1175
__gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1190
{
1191
__git_has_doubledash && return
1192
1145
- local cur="${COMP_WORDS[COMP_CWORD]}"
1193
+ local cur
1194
+ _get_comp_words_by_ref -n =: cur
1195
case "$cur" in
1196
--tool=*)
1197
__gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1216
1217
_git_fetch ()
1218
{
1170
- local cur="${COMP_WORDS[COMP_CWORD]}"
1219
+ local cur
1220
+ _get_comp_words_by_ref -n =: cur
1221
case "$cur" in
1222
--*)
1223
__gitcomp "$__git_fetch_options"
1229
1230
_git_format_patch ()
1231
{
1182
- local cur="${COMP_WORDS[COMP_CWORD]}"
1232
+ local cur
1233
+ _get_comp_words_by_ref -n =: cur
1234
case "$cur" in
1235
--thread=*)
1236
__gitcomp "
1262
1263
_git_fsck ()
1264
{
1214
- local cur="${COMP_WORDS[COMP_CWORD]}"
1265
+ local cur
1266
+ _get_comp_words_by_ref -n =: cur
1267
case "$cur" in
1268
--*)
1269
__gitcomp "
1278
1279
_git_gc ()
1280
{
1229
- local cur="${COMP_WORDS[COMP_CWORD]}"
1281
+ local cur
1282
+ _get_comp_words_by_ref -n =: cur
1283
case "$cur" in
1284
--*)
1285
__gitcomp "--prune --aggressive"
1298
{
1299
__git_has_doubledash && return
1300
1248
- local cur="${COMP_WORDS[COMP_CWORD]}"
1301
+ local cur
1302
+ _get_comp_words_by_ref -n =: cur
1303
case "$cur" in
1304
--*)
1305
__gitcomp "
1322
1323
_git_help ()
1324
{
1271
- local cur="${COMP_WORDS[COMP_CWORD]}"
1325
+ local cur
1326
+ _get_comp_words_by_ref -n =: cur
1327
case "$cur" in
1328
--*)
1329
__gitcomp "--all --info --man --web"
1341
1342
_git_init ()
1343
{
1289
- local cur="${COMP_WORDS[COMP_CWORD]}"
1344
+ local cur
1345
+ _get_comp_words_by_ref -n =: cur
1346
case "$cur" in
1347
--shared=*)
1348
__gitcomp "
1362
{
1363
__git_has_doubledash && return
1364
1309
- local cur="${COMP_WORDS[COMP_CWORD]}"
1365
+ local cur
1366
+ _get_comp_words_by_ref -n =: cur
1367
case "$cur" in
1368
--*)
1369
__gitcomp "--cached --deleted --modified --others --ignored
1417
{
1418
__git_has_doubledash && return
1419
1363
- local cur="${COMP_WORDS[COMP_CWORD]}"
1420
local g="$(git rev-parse --git-dir 2>/dev/null)"
1421
local merge=""
1422
if [ -f "$g/MERGE_HEAD" ]; then
1423
merge="--merge"
1424
fi
1425
+ local cur
1426
+ _get_comp_words_by_ref -n =: cur
1427
case "$cur" in
1428
--pretty=*)
1429
__gitcomp "$__git_log_pretty_formats
1477
{
1478
__git_complete_strategy && return
1479
1422
- local cur="${COMP_WORDS[COMP_CWORD]}"
1480
+ local cur
1481
+ _get_comp_words_by_ref -n =: cur
1482
case "$cur" in
1483
--*)
1484
__gitcomp "$__git_merge_options"
1489
1490
_git_mergetool ()
1491
{
1433
- local cur="${COMP_WORDS[COMP_CWORD]}"
1492
+ local cur
1493
+ _get_comp_words_by_ref -n =: cur
1494
case "$cur" in
1495
--tool=*)
1496
__gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1511
1512
_git_mv ()
1513
{
1454
- local cur="${COMP_WORDS[COMP_CWORD]}"
1514
+ local cur
1515
+ _get_comp_words_by_ref -n =: cur
1516
case "$cur" in
1517
--*)
1518
__gitcomp "--dry-run"
1530
_git_notes ()
1531
{
1532
local subcommands="edit show"
1533
+ local words cword
1534
+ _get_comp_words_by_ref -n =: words cword
1535
if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
1536
__gitcomp "$subcommands"
1537
return
1538
fi
1539
1477
- case "${COMP_WORDS[COMP_CWORD-1]}" in
1540
+ case "${words[cword-1]}" in
1541
-m|-F)
1542
COMPREPLY=()
1543
;;
1551
{
1552
__git_complete_strategy && return
1553
1491
- local cur="${COMP_WORDS[COMP_CWORD]}"
1554
+ local cur
1555
+ _get_comp_words_by_ref -n =: cur
1556
case "$cur" in
1557
--*)
1558
__gitcomp "
1568
1569
_git_push ()
1570
{
1507
- local cur="${COMP_WORDS[COMP_CWORD]}"
1508
- case "${COMP_WORDS[COMP_CWORD-1]}" in
1571
+ local cur prev
1572
+ _get_comp_words_by_ref -n =: cur prev
1573
+ case "$prev" in
1574
--repo)
1575
__gitcomp "$(__git_remotes)"
1576
return
1593
1594
_git_rebase ()
1595
{
1531
- local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1596
+ local dir="$(__gitdir)"
1597
+ local cur
1598
+ _get_comp_words_by_ref -n =: cur
1599
if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1600
__gitcomp "--continue --skip --abort"
1601
return
1625
1626
_git_send_email ()
1627
{
1561
- local cur="${COMP_WORDS[COMP_CWORD]}"
1628
+ local cur
1629
+ _get_comp_words_by_ref -n =: cur
1630
case "$cur" in
1631
--confirm=*)
1632
__gitcomp "
1668
1669
__git_config_get_set_variables ()
1670
{
1603
- local prevword word config_file= c=$COMP_CWORD
1671
+ local words cword
1672
+ _get_comp_words_by_ref -n =: words cword
1673
+ local prevword word config_file= c=$cword
1674
while [ $c -gt 1 ]; do
1605
- word="${COMP_WORDS[c]}"
1675
+ word="${words[c]}"
1676
case "$word" in
1677
--global|--system|--file=*)
1678
config_file="$word"
1700
1701
_git_config ()
1702
{
1633
- local cur="${COMP_WORDS[COMP_CWORD]}"
1634
- local prv="${COMP_WORDS[COMP_CWORD-1]}"
1635
- case "$prv" in
1703
+ local cur prev
1704
+ _get_comp_words_by_ref -n =: cur prev
1705
+ case "$prev" in
1706
branch.*.remote)
1707
__gitcomp "$(__git_remotes)"
1708
return
1712
return
1713
;;
1714
remote.*.fetch)
1645
- local remote="${prv#remote.}"
1715
+ local remote="${prev#remote.}"
1716
remote="${remote%.fetch}"
1717
__gitcomp "$(__git_refs_remotes "$remote")"
1718
return
1719
;;
1720
remote.*.push)
1651
- local remote="${prv#remote.}"
1721
+ local remote="${prev#remote.}"
1722
remote="${remote%.push}"
1723
__gitcomp "$(git --git-dir="$(__gitdir)" \
1724
for-each-ref --format='%(refname):%(refname)' \
2109
{
2110
__git_has_doubledash && return
2111
2042
- local cur="${COMP_WORDS[COMP_CWORD]}"
2112
+ local cur
2113
+ _get_comp_words_by_ref -n =: cur
2114
case "$cur" in
2115
--*)
2116
__gitcomp "--merge --mixed --hard --soft --patch"
2122
2123
_git_revert ()
2124
{
2054
- local cur="${COMP_WORDS[COMP_CWORD]}"
2125
+ local cur
2126
+ _get_comp_words_by_ref -n =: cur
2127
case "$cur" in
2128
--*)
2129
__gitcomp "--edit --mainline --no-edit --no-commit --signoff"
2137
{
2138
__git_has_doubledash && return
2139
2068
- local cur="${COMP_WORDS[COMP_CWORD]}"
2140
+ local cur
2141
+ _get_comp_words_by_ref -n =: cur
2142
case "$cur" in
2143
--*)
2144
__gitcomp "--cached --dry-run --ignore-unmatch --quiet"
2152
{
2153
__git_has_doubledash && return
2154
2082
- local cur="${COMP_WORDS[COMP_CWORD]}"
2155
+ local cur
2156
+ _get_comp_words_by_ref -n =: cur
2157
case "$cur" in
2158
--*)
2159
__gitcomp "
2171
{
2172
__git_has_doubledash && return
2173
2100
- local cur="${COMP_WORDS[COMP_CWORD]}"
2174
+ local cur
2175
+ _get_comp_words_by_ref -n =: cur
2176
case "$cur" in
2177
--pretty=*)
2178
__gitcomp "$__git_log_pretty_formats
2196
2197
_git_show_branch ()
2198
{
2124
- local cur="${COMP_WORDS[COMP_CWORD]}"
2199
+ local cur
2200
+ _get_comp_words_by_ref -n =: cur
2201
case "$cur" in
2202
--*)
2203
__gitcomp "
2214
2215
_git_stash ()
2216
{
2141
- local cur="${COMP_WORDS[COMP_CWORD]}"
2217
+ local cur
2218
+ _get_comp_words_by_ref -n =: cur
2219
local save_opts='--keep-index --no-keep-index --quiet --patch'
2220
local subcommands='save list show apply clear drop pop create branch'
2221
local subcommand="$(__git_find_on_cmdline "$subcommands")"
2260
2261
local subcommands="add status init update summary foreach sync"
2262
if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2186
- local cur="${COMP_WORDS[COMP_CWORD]}"
2263
+ local cur
2264
+ _get_comp_words_by_ref -n =: cur
2265
case "$cur" in
2266
--*)
2267
__gitcomp "--quiet --cached"
2305
--edit --rmdir --find-copies-harder --copy-similarity=
2306
"
2307
2230
- local cur="${COMP_WORDS[COMP_CWORD]}"
2308
+ local cur
2309
+ _get_comp_words_by_ref -n =: cur
2310
case "$subcommand,$cur" in
2311
fetch,--*)
2312
__gitcomp "--revision= --fetch-all $fc_opts"
2378
_git_tag ()
2379
{
2380
local i c=1 f=0
2302
- while [ $c -lt $COMP_CWORD ]; do
2303
- i="${COMP_WORDS[c]}"
2381
+ local words cword prev
2382
+ _get_comp_words_by_ref -n =: words cword prev
2383
+ while [ $c -lt $cword ]; do
2384
+ i="${words[c]}"
2385
case "$i" in
2386
-d|-v)
2387
__gitcomp "$(__git_tags)"
2394
c=$((++c))
2395
done
2396
2316
- case "${COMP_WORDS[COMP_CWORD-1]}" in
2397
+ case "$prev" in
2398
-m|-F)
2399
COMPREPLY=()
2400
;;
2420
{
2421
local i c=1 command __git_dir
2422
2342
- while [ $c -lt $COMP_CWORD ]; do
2343
- i="${COMP_WORDS[c]}"
2423
+ local cur words cword
2424
+ _get_comp_words_by_ref -n =: cur words cword
2425
+ while [ $c -lt $cword ]; do
2426
+ i="${words[c]}"
2427
case "$i" in
2428
--git-dir=*) __git_dir="${i#--git-dir=}" ;;
2429
--bare) __git_dir="." ;;
2435
done
2436
2437
if [ -z "$command" ]; then
2355
- case "${COMP_WORDS[COMP_CWORD]}" in
2438
+ case "$cur" in
2439
--*) __gitcomp "
2440
--paginate
2441
--no-pager
2468
{
2469
__git_has_doubledash && return
2470
2388
- local cur="${COMP_WORDS[COMP_CWORD]}"
2471
+ local cur
2472
local g="$(__gitdir)"
2473
local merge=""
2474
if [ -f "$g/MERGE_HEAD" ]; then
2475
merge="--merge"
2476
fi
2477
+ _get_comp_words_by_ref -n =: cur
2478
case "$cur" in
2479
--*)
2480
__gitcomp "