@matheusbsbs / matheus-bagliano / commits / 86dca24b7b
Fork 0

git-p4: encode/decode communication with git for python3

Under python3, calls to write() on the stream to `git fast-import` must be encoded. This patch wraps the IO object such that this encoding is done transparently. Conversely, any text data read from subprocesses must also be decoded before running through the rest of the pipeline. Signed-off-by: Yang Zhao <yang.zhao@skyboxlabs.com> Reviewed-by: Ben Keene <seraphire@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Yang Zhao committed Dec 13, 2019 at 15:52 UTC 86dca24b7be6491fb8d4b7a499ccf4a43776d1ab
1 file changed +17 -4
git-p4.py
+17 -4
@@ -183,10 +183,12 @@ def read_pipe_full(c):
183 (out, err) = p.communicate()
184 return (p.returncode, out, decode_text_stream(err))
185
186 -def read_pipe(c, ignore_error=False):
186 +def read_pipe(c, ignore_error=False, raw=False):
187 """ Read output from command. Returns the output text on
188 success. On failure, terminates execution, unless
189 ignore_error is True, when it returns an empty string.
190 +
191 + If raw is True, do not attempt to decode output text.
192 """
193 (retcode, out, err) = read_pipe_full(c)
194 if retcode != 0:
@@ -194,6 +196,8 @@ def read_pipe(c, ignore_error=False):
196 out = ""
197 else:
198 die('Command failed: %s\nError: %s' % (str(c), err))
199 + if not raw:
200 + out = decode_text_stream(out)
201 return out
202
203 def read_pipe_text(c):
@@ -220,7 +224,6 @@ def read_pipe_lines(c):
224 val = [decode_text_stream(line) for line in pipe.readlines()]
225 if pipe.close() or p.wait():
226 die('Command failed: %s' % str(c))
223 -
227 return val
228
229 def p4_read_pipe_lines(c):
@@ -616,7 +619,8 @@ def p4CmdList(cmd, stdin=None, stdin_mode='w+b', cb=None, skip_info=False,
619 stdin_file.write(stdin)
620 else:
621 for i in stdin:
619 - stdin_file.write(i + '\n')
622 + stdin_file.write(encode_text_stream(i))
623 + stdin_file.write(b'\n')
624 stdin_file.flush()
625 stdin_file.seek(0)
626
@@ -1245,7 +1249,7 @@ class GitLFS(LargeFileSystem):
1249 ['git', 'lfs', 'pointer', '--file=' + contentFile],
1250 stdout=subprocess.PIPE
1251 )
1248 - pointerFile = pointerProcess.stdout.read()
1252 + pointerFile = decode_text_stream(pointerProcess.stdout.read())
1253 if pointerProcess.wait():
1254 os.remove(contentFile)
1255 die('git-lfs pointer command failed. Did you install the extension?')
@@ -3538,6 +3542,15 @@ class P4Sync(Command, P4UserMap):
3542 self.gitStream = self.importProcess.stdin
3543 self.gitError = self.importProcess.stderr
3544
3545 + if bytes is not str:
3546 + # Wrap gitStream.write() so that it can be called using `str` arguments
3547 + def make_encoded_write(write):
3548 + def encoded_write(s):
3549 + return write(s.encode() if isinstance(s, str) else s)
3550 + return encoded_write
3551 +
3552 + self.gitStream.write = make_encoded_write(self.gitStream.write)
3553 +
3554 def closeStreams(self):
3555 self.gitStream.close()
3556 if self.importProcess.wait() != 0: