From 600f49278ae93ee93c3511562eb81d6690319c75 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sat, 3 Aug 2019 02:14:28 -0400 Subject: [PATCH] project: fix encoding handling with git commands The GitCommand Wait helper takes care of decoding bytes to strings for us. That means we don't have to decode stdout ourselves which is what our local rev list, ls-remote, and generic get_attr helpers were doing. If we don't use Wait though to capture the output but instead go directly to the subprocess stdout, we do have to handle decoding ourselves. This is what the diff helpers were doing. Bug: https://crbug.com/gerrit/10418 Change-Id: I057ca245af3ff18d6b4a074e3900887f06a5617d Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/233076 Reviewed-by: David Pursehouse Tested-by: Mike Frysinger --- project.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/project.py b/project.py index 58942514..428cab51 100755 --- a/project.py +++ b/project.py @@ -1141,6 +1141,8 @@ class Project(object): capture_stderr=True) has_diff = False for line in p.process.stdout: + if not hasattr(line, 'encode'): + line = line.decode() if not has_diff: out.nl() out.project('project %s/' % self.relpath) @@ -1595,7 +1597,7 @@ class Project(object): last_mine = None cnt_mine = 0 for commit in local_changes: - commit_id, committer_email = commit.decode('utf-8').split(' ', 1) + commit_id, committer_email = commit.split(' ', 1) if committer_email == self.UserEmail: last_mine = commit_id cnt_mine += 1 @@ -2406,10 +2408,7 @@ class Project(object): cmd = ['ls-remote', self.remote.name, refs] p = GitCommand(self, cmd, capture_stdout=True) if p.Wait() == 0: - if hasattr(p.stdout, 'decode'): - return p.stdout.decode('utf-8') - else: - return p.stdout + return p.stdout return None def _Revert(self, rev): @@ -2820,6 +2819,8 @@ class Project(object): capture_stderr=True) try: out = p.process.stdout.read() + if not hasattr(out, 'encode'): + out = out.decode() r = {} if out: out = iter(out[:-1].split('\0')) @@ -2979,10 +2980,6 @@ class Project(object): raise GitError('%s %s: %s' % (self._project.name, name, p.stderr)) r = p.stdout - try: - r = r.decode('utf-8') - except AttributeError: - pass if r.endswith('\n') and r.index('\n') == len(r) - 1: return r[:-1] return r