completion: factor out __git_resolve_builtins

We're going to use the result of "git xxx --git-completion-helper" not only for feeding COMPREPLY. Therefore, factor out the execution and the caching of its results in __gitcomp_builtin, to a new function __git_resolve_builtins. While we're here, move an important comment we have in the function to its header, so it gains visibility. Signed-off-by: Rubén Justo <rjusto@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Rubén Justo committed Mar 2, 2024 at 16:52 UTC 476a236e72d7ad0d2a5237faeaa439b1054e80a5
1 file changed +25 -6
contrib/completion/git-completion.bash
+25 -6
@@ -452,16 +452,18 @@ fi
452
453 # This function is equivalent to
454 #
455 -# __gitcomp "$(git xxx --git-completion-helper) ..."
455 +# ___git_resolved_builtins=$(git xxx --git-completion-helper)
456 #
457 -# except that the output is cached. Accept 1-3 arguments:
457 +# except that the result of the execution is cached.
458 +#
459 +# Accept 1-3 arguments:
460 # 1: the git command to execute, this is also the cache key
461 +# (use "_" when the command contains spaces, e.g. "remote add"
462 +# becomes "remote_add")
463 # 2: extra options to be added on top (e.g. negative forms)
464 # 3: options to be excluded
461 -__gitcomp_builtin ()
465 +__git_resolve_builtins ()
466 {
463 - # spaces must be replaced with underscore for multi-word
464 - # commands, e.g. "git remote add" becomes remote_add.
467 local cmd="$1"
468 local incl="${2-}"
469 local excl="${3-}"
@@ -487,7 +489,24 @@ __gitcomp_builtin ()
489 eval "$var=\"$options\""
490 fi
491
490 - __gitcomp "$options"
492 + ___git_resolved_builtins="$options"
493 +}
494 +
495 +# This function is equivalent to
496 +#
497 +# __gitcomp "$(git xxx --git-completion-helper) ..."
498 +#
499 +# except that the output is cached. Accept 1-3 arguments:
500 +# 1: the git command to execute, this is also the cache key
501 +# (use "_" when the command contains spaces, e.g. "remote add"
502 +# becomes "remote_add")
503 +# 2: extra options to be added on top (e.g. negative forms)
504 +# 3: options to be excluded
505 +__gitcomp_builtin ()
506 +{
507 + __git_resolve_builtins "$1" "$2" "$3"
508 +
509 + __gitcomp "$___git_resolved_builtins"
510 }
511
512 # Variation of __gitcomp_nl () that appends to the existing list of