454454
455455# This function is equivalent to
456456#
457- # __gitcomp " $(git xxx --git-completion-helper) ..."
457+ # ___git_resolved_builtins= $(git xxx --git-completion-helper)
458458#
459- # except that the output is cached. Accept 1-3 arguments:
459+ # except that the result of the execution is cached.
460+ #
461+ # Accept 1-3 arguments:
460462# 1: the git command to execute, this is also the cache key
463+ # (use "_" when the command contains spaces, e.g. "remote add"
464+ # becomes "remote_add")
461465# 2: extra options to be added on top (e.g. negative forms)
462466# 3: options to be excluded
463- __gitcomp_builtin ()
467+ __git_resolve_builtins ()
464468{
465- # spaces must be replaced with underscore for multi-word
466- # commands, e.g. "git remote add" becomes remote_add.
467469 local cmd=" $1 "
468470 local incl=" ${2-} "
469471 local excl=" ${3-} "
@@ -489,7 +491,24 @@ __gitcomp_builtin ()
489491 eval " $var =\" $options \" "
490492 fi
491493
492- __gitcomp " $options "
494+ ___git_resolved_builtins=" $options "
495+ }
496+
497+ # This function is equivalent to
498+ #
499+ # __gitcomp "$(git xxx --git-completion-helper) ..."
500+ #
501+ # except that the output is cached. Accept 1-3 arguments:
502+ # 1: the git command to execute, this is also the cache key
503+ # (use "_" when the command contains spaces, e.g. "remote add"
504+ # becomes "remote_add")
505+ # 2: extra options to be added on top (e.g. negative forms)
506+ # 3: options to be excluded
507+ __gitcomp_builtin ()
508+ {
509+ __git_resolve_builtins " $1 " " $2 " " $3 "
510+
511+ __gitcomp " $___git_resolved_builtins "
493512}
494513
495514# Variation of __gitcomp_nl () that appends to the existing list of
@@ -556,6 +575,26 @@ __gitcomp_file ()
556575 true
557576}
558577
578+ # Find the current subcommand for commands that follow the syntax:
579+ #
580+ # git <command> <subcommand>
581+ #
582+ # 1: List of possible subcommands.
583+ # 2: Optional subcommand to return when none is found.
584+ __git_find_subcommand ()
585+ {
586+ local subcommand subcommands=" $1 " default_subcommand=" $2 "
587+
588+ for subcommand in $subcommands ; do
589+ if [ " $subcommand " = " ${words[__git_cmd_idx+1]} " ]; then
590+ echo $subcommand
591+ return
592+ fi
593+ done
594+
595+ echo $default_subcommand
596+ }
597+
559598# Execute 'git ls-files', unless the --committable option is specified, in
560599# which case it runs 'git diff-index' to find out the files that can be
561600# committed. It return paths relative to the directory specified in the first
@@ -2471,13 +2510,30 @@ _git_rebase ()
24712510
24722511_git_reflog ()
24732512{
2474- local subcommands=" show delete expire"
2475- local subcommand=" $( __git_find_on_cmdline " $subcommands " ) "
2513+ local subcommands subcommand
24762514
2477- if [ -z " $subcommand " ]; then
2478- __gitcomp " $subcommands "
2479- else
2480- __git_complete_refs
2515+ __git_resolve_builtins " reflog"
2516+
2517+ subcommands=" $___git_resolved_builtins "
2518+ subcommand=" $( __git_find_subcommand " $subcommands " " show" ) "
2519+
2520+ case " $subcommand ,$cur " in
2521+ show,--* )
2522+ __gitcomp "
2523+ $__git_log_common_options
2524+ "
2525+ return
2526+ ;;
2527+ $subcommand ,--* )
2528+ __gitcomp_builtin " reflog_$subcommand "
2529+ return
2530+ ;;
2531+ esac
2532+
2533+ __git_complete_refs
2534+
2535+ if [ $(( cword - __git_cmd_idx)) -eq 1 ]; then
2536+ __gitcompappend " $subcommands " " " " $cur " " "
24812537 fi
24822538}
24832539
0 commit comments