summaryrefslogtreecommitdiff
path: root/lib/git.zsh
diff options
context:
space:
mode:
authorMarc Cornellà <marc@mcornella.com>2024-03-09 18:22:35 +0100
committerMarc Cornellà <marc@mcornella.com>2024-03-09 18:29:53 +0100
commit06753e8146332aa787857fc5cc41caa2b5f753f0 (patch)
treec1ae52519dfa402614d4d15bca7941ed5dd6e31e /lib/git.zsh
parent32d4389aa6e896b27d9786d142a5c44163104056 (diff)
downloadzsh-06753e8146332aa787857fc5cc41caa2b5f753f0.tar.gz
zsh-06753e8146332aa787857fc5cc41caa2b5f753f0.tar.bz2
zsh-06753e8146332aa787857fc5cc41caa2b5f753f0.zip
fix(async): register the git prompt async handler correctly (#12267)
This fix conditionally registers the git prompt async handler only if `git_prompt_info` is used anywhere in the prompt variables. This is done in the proper order, so that the async request is processed once the handler has been registered. This fix also passes the return value of the previous command to each of the async handlers, in case they are needed.
Diffstat (limited to 'lib/git.zsh')
-rw-r--r--lib/git.zsh20
1 files changed, 19 insertions, 1 deletions
diff --git a/lib/git.zsh b/lib/git.zsh
index 8fe999095..96df5589d 100644
--- a/lib/git.zsh
+++ b/lib/git.zsh
@@ -40,11 +40,29 @@ function _omz_git_prompt_status() {
# Enable async prompt by default unless the setting is at false / no
if zstyle -t ':omz:alpha:lib:git' async-prompt; then
function git_prompt_info() {
- _omz_register_handler _omz_git_prompt_status
if [[ -n "$_OMZ_ASYNC_OUTPUT[_omz_git_prompt_status]" ]]; then
echo -n "$_OMZ_ASYNC_OUTPUT[_omz_git_prompt_status]"
fi
}
+
+ # Conditionally register the async handler, only if it's needed in $PROMPT
+ # or any of the other prompt variables
+ function _defer_async_git_register() {
+ # Check if git_prompt_info is used in a prompt variable
+ case "${PS1}:${PS2}:${PS3}:${PS4}:${RPS1}:${RPS2}:${RPS3}:${RPS4}" in
+ *(\$\(git_prompt_info\)|\`git_prompt_info\`)*)
+ _omz_register_handler _omz_git_prompt_status
+ return
+ ;;
+ esac
+
+ add-zsh-hook -d precmd _defer_async_git_register
+ unset -f _defer_async_git_register
+ }
+
+ # Register the async handler first. This needs to be done before
+ # the async request prompt is run
+ precmd_functions=(_defer_async_git_register $precmd_functions)
else
function git_prompt_info() {
_omz_git_prompt_status