blackbox: be graceful when the log message cannot be formatted

Summary:
It's legit for a file name to contain `%s` or `%r` strings. Previously,
blackbox always expect the first argument to be the "format string". That's
inconvenient and it's easy to just pass a string containing the troublesome
`%s` to `ui.log`.

Let's just do not even try formatting strings if there is only one argument,
and if there are multiple arguments, fallback to concatenate them if they
cannot be formatted.

Reviewed By: DurhamG

Differential Revision: D8364130

fbshipit-source-id: 75b2d0e0a460b9a86d4ecd6ecfbb77c0c0fbe98c
This commit is contained in:
Jun Wu 2018-06-11 17:27:20 -07:00 committed by Facebook Github Bot
parent 8b8944bf14
commit a4c9b8d134
2 changed files with 20 additions and 1 deletions

View File

@ -156,7 +156,17 @@ def wrapui(ui):
date = util.datestr(default, "%Y/%m/%d %H:%M:%S")
user = util.getuser()
pid = "%d" % util.getpid()
formattedmsg = msg[0] % msg[1:]
if len(msg) == 1:
# Don't even try to format the string if there is only one
# argument.
formattedmsg = msg[0]
else:
try:
formattedmsg = msg[0] % msg[1:]
except TypeError:
# If fails with `TypeError: not enough arguments for format
# string`, concatenate the arguments gracefully.
formattedmsg = " ".join(msg)
rev = "(unknown)"
changed = ""
if repo:

View File

@ -377,3 +377,12 @@ blackbox writes Request ID if HGREQUESTID is set
$ HGREQUESTID=aabea740-97d8-49e5-8453-14e5ae3d4099 hg root >/dev/null
$ hg blackbox | grep aabea740-97d8-49e5-8453-14e5ae3d4099
1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)[aabea740-97d8-49e5-8453-14e5ae3d4099]> root
blackbox should not fail with "TypeError: not enough arguments for format string"
$ hg debugshell --command "ui.log('foo', 'ba' + 'r %s %r')"
$ hg debugshell --command "ui.log('foo', 'ba' + 'r %s %r', 'arg1')"
$ hg blackbox | grep bar
1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)> bar %s %r
1970/01/01 00:00:00 bob @0000000000000000000000000000000000000000 (5000)> bar %s %r arg1