templater: decode the result of diff()

Summary:
Template filters are expecting the output of the template to be a unicode
string, not a byte string. Thus we need to ensure that diff return a unicode
string.

Reviewed By: DurhamG

Differential Revision: D24290908

fbshipit-source-id: 70899249c3e26edd1714fd901fa6af405b61e544
This commit is contained in:
Xavier Deguillard 2020-10-14 08:59:03 -07:00 committed by Facebook GitHub Bot
parent 5865190b1e
commit bc351a5891
2 changed files with 5 additions and 2 deletions

View File

@ -712,7 +712,7 @@ def diff(context, mapping, args):
ctx = mapping["ctx"]
chunks = ctx.diff(match=ctx.match([], getpatterns(0), getpatterns(1)))
return b"".join(chunks)
return pycompat.decodeutf8(b"".join(chunks), errors="surrogateescape")
@templatefunc("extdata(source)", argspec="source")
@ -1455,7 +1455,7 @@ def _flatten(thing):
"""yield a single stream from a possibly nested set of iterators"""
thing = templatekw.unwraphybrid(thing)
if isinstance(thing, str):
yield pycompat.encodeutf8(thing)
yield pycompat.encodeutf8(thing, errors="surrogateescape")
elif isinstance(thing, bytes):
yield thing
elif thing is None:

View File

@ -2627,6 +2627,9 @@ sh % "hg log -r 8 -T '{diff('\\''FOURTH'\\''|lower)}'" == (
+ (" +🥈\udce2(\udca1" if is_py3 else " +🥈\xe2\x28\xa1")
)
sh % "hg log -r 8 -T '{diff()|json}'" == '"diff -r 88058a185da2 -r 209edb6a1848 fourth\\n--- /dev/null\\tThu Jan 01 00:00:00 1970 +0000\\n+++ b/fourth\\tWed Jan 01 10:01:00 2020 +0000\\n@@ -0,0 +1,1 @@\\n+\\ud83e\\udd48\\udce2(\\udca1\\ndiff -r 88058a185da2 -r 209edb6a1848 second\\n--- a/second\\tMon Jan 12 13:46:40 1970 +0000\\n+++ /dev/null\\tThu Jan 01 00:00:00 1970 +0000\\n@@ -1,1 +0,0 @@\\n-\\ud83e\\udd48\\udce2(\\udca1\\ndiff -r 88058a185da2 -r 209edb6a1848 third\\n--- /dev/null\\tThu Jan 01 00:00:00 1970 +0000\\n+++ b/third\\tWed Jan 01 10:01:00 2020 +0000\\n@@ -0,0 +1,1 @@\\n+third\\n"'
# ui verbosity:
sh % "hg log -l1 -T '{verbosity}\\n'"