zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: zsh-workers@zsh.org
Subject: [PATCH] vcs_info: Escape '%' signs in payloads
Date: Sun,  5 Feb 2017 08:28:13 +0000	[thread overview]
Message-ID: <1486283293-2586-1-git-send-email-danielsh@fujitsu.shahaf.local2> (raw)
In-Reply-To: <871svt8v4i.fsf@ft.bewatermyfriend.org>

Test case: a patch whose subject is '%Sfoo%sbar'.  ('S' and 's' are
expandos both in prompts and in the 'formats' style.)
---
 Doc/Zsh/contrib.yo                               | 17 +++++++++++++++++
 Etc/BUGS                                         |  7 -------
 Functions/VCS_Info/Backends/VCS_INFO_get_data_hg |  1 +
 Functions/VCS_Info/VCS_INFO_set-patch-format     | 14 ++++++++++++++
 README                                           |  9 +++++++++
 5 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index a454f60..5d0696d 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -1344,6 +1344,12 @@ tt(branchformat), use tt(%%%%b). Sorry for this inconvenience, but it
 cannot be easily avoided. Luckily we do not clash with a lot of prompt
 expansions and this only needs to be done for those.
 
+When one of the tt(gen-applied-string), tt(gen-unapplied-string), and
+tt(set-patch-format) hooks is defined,
+applying tt(%)-escaping (`tt(foo=${foo//'%'/%%})') to the interpolated values
+for use in the prompt is the responsibility of those hooks (jointly);
+when neither of those hooks is defined, tt(vcs_info) handles escaping by itself.
+We regret this coupling, but it was required for backwards compatibility.
 
 subsect(Quilt Support)
 
@@ -1616,6 +1622,9 @@ top-most patch and so forth.
 When setting tt(ret) to non-zero, the string in
 tt(${hook_com[applied-string]}) will be
 available as tt(%p) in the tt(patch-format) and tt(nopatch-format) styles.
+This hook is, in concert with tt(set-patch-format), responsible for
+tt(%)-escaping that value for use in the prompt.
+(See the `Oddities' section.)
 )
 item(tt(gen-unapplied-string))(
 Called in the tt(git) (with tt(stgit) or during rebase), and tt(hg) (with
@@ -1629,6 +1638,9 @@ the patch next-in-line to be applied and so forth.
 When setting tt(ret) to non-zero, the string in
 tt(${hook_com[unapplied-string]}) will be available as tt(%u) in the
 tt(patch-format) and tt(nopatch-format) styles.
+This hook is, in concert with tt(set-patch-format), responsible for
+tt(%)-escaping that value for use in the prompt.
+(See the `Oddities' section.)
 )
 item(tt(gen-mqguards-string))(
 Called in the tt(hg) backend when tt(guards-string) is generated; the
@@ -1706,6 +1718,11 @@ controllable in addition to that.
 If tt(ret) is set to non-zero, the string in tt(${hook_com[patch-replace]})
 will be used unchanged instead of an expanded format from tt(patch-format) or
 tt(nopatch-format).
+
+This hook is, in concert with the tt(gen-applied-string) or
+tt(gen-unapplied-string) hooks if they are defined, responsible for
+tt(%)-escaping the final tt(patch-format) value for use in the prompt.
+(See the `Oddities' section.)
 )
 item(tt(set-message))(
 Called each time before a `tt(vcs_info_msg_)var(N)tt(_)' message is set.
diff --git a/Etc/BUGS b/Etc/BUGS
index a351464..008e337 100644
--- a/Etc/BUGS
+++ b/Etc/BUGS
@@ -15,13 +15,6 @@ It is currently impossible to time builtins.
 40106: The comp* completion-related builtins (compadd, compset, etc) are
 run with $_comp_options in effect, rather than the user's options.
 ------------------------------------------------------------------------
-40240: vcs_info: percent escapes in payloads are interpreted
-
-Example: hg branch names and quilt patch subjects that contain the literal
-string '%F{blue}', cause $vcs_info_msg_N_ to be rendered in blue.
-
-40240 has a patch, but 40241 explains why that patch is incomplete.
-------------------------------------------------------------------------
 users/20807: vcs_info quilt 'addon' mode: hook lookup context specifies
 the underlying VCS but not whether quilt is used.
 
diff --git a/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg b/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
index 143eb42..d403012 100644
--- a/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
+++ b/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
@@ -202,6 +202,7 @@ if zstyle -T ":vcs_info:${vcs}:${usercontext}:${rrn}" get-mq \
 
     if VCS_INFO_hook 'gen-mqguards-string' "${mqguards[@]}"; then
         guards_string=${(j:,:)mqguards}
+        # TODO: %-escape extra_zformats[g:...] value
     else
         guards_string=${hook_com[guards-string]}
     fi
diff --git a/Functions/VCS_Info/VCS_INFO_set-patch-format b/Functions/VCS_Info/VCS_INFO_set-patch-format
index c5da368..cdf2d30 100644
--- a/Functions/VCS_Info/VCS_INFO_set-patch-format
+++ b/Functions/VCS_Info/VCS_INFO_set-patch-format
@@ -18,12 +18,15 @@
 # - $hook_com is overwritten and the keys 'applied', 'applied-n',
 #   'unapplied', 'unapplied-n', 'all-n' are set.
 {
+    local applied_needs_escaping='unknown'
+    local unapplied_needs_escaping='unknown'
     if VCS_INFO_hook 'gen-applied-string' "${(@P)1}"; then
         if (( ${(P)#1} )); then
             REPLY=${(P)1[1]}
         else
             REPLY=""
         fi
+        applied_needs_escaping='yes'
     else
         REPLY=${hook_com[applied-string]}
     fi
@@ -32,6 +35,7 @@
 
     if VCS_INFO_hook 'gen-unapplied-string' "${(@P)3}"; then
         REPLY=${(P)#3}
+        unapplied_needs_escaping='yes'
     else
         REPLY=${hook_com[unapplied-string]}
     fi
@@ -54,12 +58,22 @@
     hook_com[all-n]=$(( ${hook_com[applied-n]} + ${hook_com[unapplied-n]} ))
     hook_com+=( ${7:+"${(@kvP)7}"} )
     if VCS_INFO_hook 'set-patch-format' "${(P)6}"; then
+        # Escape the value for use in $PS1
+        if [[ $applied_needs_escaping == 'yes' ]]; then
+          hook_com[applied]=${hook_com[applied]//'%'/%%}
+        fi
+        if [[ $unapplied_needs_escaping == 'yes' ]]; then
+          hook_com[unapplied]=${hook_com[unapplied]//'%'/%%}
+        fi
+
         zformat -f REPLY "${(P)6}" "p:${hook_com[applied]}" "u:${hook_com[unapplied]}" \
                                         "n:${hook_com[applied-n]}" "c:${hook_com[unapplied-n]}" \
                                         "a:${hook_com[all-n]}" \
                                         ${8:+"${(@P)8}"}
     else
+        unset applied_needs_escaping unapplied_needs_escaping # the hook deals with escaping
         REPLY=${hook_com[patch-replace]}
     fi
     hook_com=()
+
 }
diff --git a/README b/README
index 26eb245..594b6f1 100644
--- a/README
+++ b/README
@@ -62,6 +62,15 @@ Note that functions including a non-leading / behave as before,
 e.g. if `dir/name' is found anywhere under a directory in $fpath it is
 loaded as a function named `dir/name'.
 
+3) vcs_info: When neither a set-patch-format nor a gen-applied-string
+(resp. gen-unapplied-string) hook is set, vcs_info now '%'-escapes the
+applied-string (resp. unapplied-string) before interpolating it into the
+patch-format string, to prevent literal `%' signs in the interpolated
+value from being interpreted as prompt escape sequences.  If you use
+${vcs_info_msg_0_} in a context other than the shell prompt, you may need
+to undo the escaping with:
+    print -v vcs_info_msg_0_ -Pr -- "${vcs_info_msg_0_}"
+
 Incompatibilities between 5.0.8 and 5.3
 ----------------------------------------
 


  reply	other threads:[~2017-02-05  8:32 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-27 15:05 vcs_info: '%' in payloads not escaped Daniel Shahaf
2016-12-27 15:13 ` Daniel Shahaf
2017-01-05 16:07 ` Daniel Shahaf
2017-01-05 16:27   ` Frank Terbeck
2017-01-06  2:21     ` Daniel Shahaf
2017-01-06 10:41       ` Frank Terbeck
2017-01-06 16:40         ` Daniel Shahaf
2017-01-06 17:27           ` Bart Schaefer
2017-01-23 11:04             ` Daniel Shahaf
2017-01-23 18:54               ` Frank Terbeck
2017-02-05  8:28                 ` Daniel Shahaf [this message]
2017-02-07  8:57                   ` [PATCH] vcs_info: Escape '%' signs in payloads Daniel Shahaf
2017-01-06 15:55   ` vcs_info: '%' in payloads not escaped Bart Schaefer
2017-01-06 16:49     ` Daniel Shahaf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1486283293-2586-1-git-send-email-danielsh@fujitsu.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).