1135
done
1136
}
1137
1138
+# Similar to __git_find_on_cmdline, except that it loops backwards and thus
1139
+# prints the *last* word found. Useful for finding which of two options that
1140
+# supersede each other came last, such as "--guess" and "--no-guess".
1141
+#
1142
+# Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
1143
+# --show-idx: Optionally show the index of the found word in the $words array.
1144
+__git_find_last_on_cmdline ()
1145
+{
1146
+ local word c=$cword show_idx
1147
+
1148
+ while test $# -gt 1; do
1149
+ case "$1" in
1150
+ --show-idx) show_idx=y ;;
1151
+ *) return 1 ;;
1152
+ esac
1153
+ shift
1154
+ done
1155
+ local wordlist="$1"
1156
+
1157
+ while [ $c -gt 1 ]; do
1158
+ ((c--))
1159
+ for word in $wordlist; do
1160
+ if [ "$word" = "${words[c]}" ]; then
1161
+ if [ -n "$show_idx" ]; then
1162
+ echo "$c $word"
1163
+ else
1164
+ echo "$word"
1165
+ fi
1166
+ return
1167
+ fi
1168
+ done
1169
+ done
1170
+}
1171
+
1172
# Echo the value of an option set on the command line or config
1173
#
1174
# $1: short option name
1423
esac
1424
}
1425
1426
+# Helper function to decide whether or not we should enable DWIM logic for
1427
+# git-switch and git-checkout.
1428
+#
1429
+# To decide between the following rules in priority order
1430
+# 1) the last provided of "--guess" or "--no-guess" explicitly enable or
1431
+# disable completion of DWIM logic respectively.
1432
+# 2) If the --no-track option is provided, take this as a hint to disable the
1433
+# DWIM completion logic
1434
+# 3) If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
1435
+# logic, as requested by the user.
1436
+# 4) Enable DWIM logic otherwise.
1437
+#
1438
+__git_checkout_default_dwim_mode ()
1439
+{
1440
+ local last_option dwim_opt="--dwim"
1441
+
1442
+ if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ]; then
1443
+ dwim_opt=""
1444
+ fi
1445
+
1446
+ # --no-track disables DWIM, but with lower priority than
1447
+ # --guess/--no-guess
1448
+ if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
1449
+ dwim_opt=""
1450
+ fi
1451
+
1452
+ # Find the last provided --guess or --no-guess
1453
+ last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
1454
+ case "$last_option" in
1455
+ --guess)
1456
+ dwim_opt="--dwim"
1457
+ ;;
1458
+ --no-guess)
1459
+ dwim_opt=""
1460
+ ;;
1461
+ esac
1462
+
1463
+ echo "$dwim_opt"
1464
+}
1465
+
1466
_git_checkout ()
1467
{
1468
__git_has_doubledash && return
1475
__gitcomp_builtin checkout
1476
;;
1477
*)
1404
- # check if --track, --no-track, or --no-guess was specified
1405
- # if so, disable DWIM mode
1406
- local flags="--track --no-track --no-guess" dwim_opt="--dwim"
1407
- if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1408
- [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1409
- dwim_opt=''
1410
- fi
1478
+ local dwim_opt="$(__git_checkout_default_dwim_mode)"
1479
__git_complete_refs $dwim_opt
1480
;;
1481
esac
2324
__gitcomp_builtin switch
2325
;;
2326
*)
2259
- # check if --track, --no-track, or --no-guess was specified
2260
- # if so, disable DWIM mode
2261
- local dwim_opt="--dwim" only_local_ref=n
2262
- if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
2263
- [ -n "$(__git_find_on_cmdline "--track --no-track --no-guess")" ]; then
2264
- dwim_opt=''
2265
- fi
2266
- # explicit --guess enables DWIM mode regardless of
2267
- # $GIT_COMPLETION_CHECKOUT_NO_GUESS
2268
- if [ -n "$(__git_find_on_cmdline "--guess")" ]; then
2269
- dwim_opt='--dwim'
2270
- fi
2327
+ local dwim_opt="$(__git_checkout_default_dwim_mode)" only_local_ref=n
2328
if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then
2329
only_local_ref=y
2330
else