[PATCH v4 08/11] git-p4: p4CmdList - support Unicode encoding
From: Ben Keene <seraphire@gmail.com>
The p4CmdList is a commonly used function in the git-p4 code. It is used to execute a command in P4 and return the results of the call in a list.
Change this code to take a new optional parameter, encode_data that will optionally convert the data AS_STRING() that isto be returned by the function.
Change the code so that the key will always be encoded AS_STRING()
Data that is passed for standard input (stdin) should be AS_BYTES() to ensure unicode text that is supplied will be written out as bytes.
Additionally, change literal text prior to conversion to be literal bytes.
Signed-off-by: Ben Keene <seraphire@gmail.com>
(cherry picked from commit 88306ac269186cbd0f6dc6cfd366b50b28ee4886)
---
git-p4.py | 27 +++++++++++++++++++++++----
1 file changed, 23 insertions(+), 4 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index 0da640be93..f7c0ef0c53 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -711,7 +711,23 @@ def isModeExecChanged(src_mode, dst_mode):
return isModeExec(src_mode) != isModeExec(dst_mode)
def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
- errors_as_exceptions=False):
+ errors_as_exceptions=False, encode_data=True):
+ """ Executes a P4 command: 'cmd' optionally passing 'stdin' to the command's
+ standard input via a temporary file with 'stdin_mode' mode.
+
+ Output from the command is optionally passed to the callback function 'cb'.
+ If 'cb' is None, the response from the command is parsed into a list
+ of resulting dictionaries. (For each block read from the process pipe.)
+
+ If 'skip_info' is true, information in a block read that has a code type of
+ 'info' will be skipped.
+
+ If 'errors_as_exceptions' is set to true (the default is false) the error
+ code returned from the execution will generate an exception.
+
+ If 'encode_data' is set to true (the default) the data that is returned
+ by this function will be passed through the "as_string" function.
+ """
if not isinstance(cmd, list):
cmd = "-G " + cmd
@@ -734,7 +750,7 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
stdin_file.write(stdin)
else:
for i in stdin:
- stdin_file.write(i + '\n')
+ stdin_file.write(as_bytes(i) + b'\n')
stdin_file.flush()
stdin_file.seek(0)
@@ -748,12 +764,15 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
while True:
entry = marshal.load(p4.stdout)
if skip_info:
- if 'code' in entry and entry['code'] == 'info':
+ if b'code' in entry and entry[b'code'] == b'info':
continue
if cb is not None:
cb(entry)
else:
- result.append(entry)
+ out = {}
+ for key, value in entry.items():
+ out[as_string(key)] = (as_string(value) if encode_data else value)
+ result.append(out)
except EOFError:
pass
exitCode = p4.wait()
--
gitgitgadget