Parent directory

vcs_info.zsh

2677 bytes
 1# Don't skip this file until a Zsh release does the necessary quoting.
 2# This is because even though 5.8.1 undid recursive prompt_subst inside
 3# prompt sequences, % characters in relevant fields will still be rendered
 4# incorrectly in vcs_info, on all Zsh releases up to writing this.
 5#
 6# There is no release yet that does this right, since it requires changing
 7# how what vcs_info hooks expect to receive. Even so, I'd rather be correct
 8# and break custom vcs_info hooks than have a broken prompt.
 9
10# Quote necessary $hook_com[<field>] items just before they are used
11# in the line "VCS_INFO_hook 'post-backend'" of the VCS_INFO_formats
12# function, where <field> is:
13#
14#   base:       the full path of the repository's root directory.
15#   base-name:  the name of the repository's root directory.
16#   branch:     the name of the currently checked out branch.
17#   misc:       a string that may contain anything the vcs_info backend wants.
18#   revision:   an identifier of the currently checked out revision.
19#   subdir:     the path of the current directory relative to the
20#               repository's root directory.
21#
22# This patch %-quotes these fields previous to their use in vcs_info hooks and
23# the zformat call and, eventually, when they get expanded in the prompt.
24# It's important to quote these here, and not later after hooks have modified the
25# fields, because then we could be quoting % characters from valid prompt sequences,
26# like %F{color}, %B, etc.
27#
28#  32   │ hook_com[subdir]="$(VCS_INFO_reposub ${hook_com[base]})"
29#  33   │ hook_com[subdir_orig]="${hook_com[subdir]}"
30#  34   │
31#  35 + │ for tmp in base base-name branch misc revision subdir; do
32#  36 + │     hook_com[$tmp]="${hook_com[$tmp]//\%/%%}"
33#  37 + │ done
34#  38 + │
35#  39   │ VCS_INFO_hook 'post-backend'
36#
37# This is especially important so that no command substitution is performed
38# due to malicious input as a consequence of CVE-2021-45444, which affects
39# zsh versions from 5.0.3 to 5.8.
40#
41autoload -Uz +X regexp-replace VCS_INFO_formats 2>/dev/null || return 0
42
43# We use $tmp here because it's already a local variable in VCS_INFO_formats
44typeset PATCH='for tmp (base base-name branch misc revision subdir) hook_com[$tmp]="${hook_com[$tmp]//\%/%%}"'
45# Unique string to avoid reapplying the patch if this code gets called twice
46typeset PATCH_ID=vcs_info-patch-9b9840f2-91e5-4471-af84-9e9a0dc68c1b
47# Only patch the VCS_INFO_formats function if not already patched
48if [[ "$functions[VCS_INFO_formats]" != *$PATCH_ID* ]]; then
49  regexp-replace 'functions[VCS_INFO_formats]' \
50    "VCS_INFO_hook 'post-backend'" \
51    ': ${PATCH_ID}; ${PATCH}; ${MATCH}'
52fi
53unset PATCH PATCH_ID