Fix bug in git_credentials()

git_credentials() was converting the stdin argument to bytes. However
according to the Python3 documentation subprocess.communicate() expects
a string when universal_newlines=True is passed to Popen.

The symptom was that git review would fail in p.communicate(stdin) on
line 156 with the message "'bytes' object has no attribute 'encode'".
This was observed with Python version 3.6.9 when running git-review
against a gerrit repo over https, where the repo requires username and
password to authenticate.

Change-Id: I0c0314c3f7b0eb631e72e4ac187a9d443a2bc82b
This commit is contained in:
Alexander Szakaly 2020-09-24 07:56:04 +02:00 committed by Sorin Sbârnea
parent 4af1703a90
commit b0bf084d66
3 changed files with 5 additions and 5 deletions

View File

@ -171,7 +171,7 @@ def git_credentials(url):
"""Return credentials using git credential or None.""" """Return credentials using git credential or None."""
cmd = 'git', 'credential', 'fill' cmd = 'git', 'credential', 'fill'
stdin = 'url=%s' % url stdin = 'url=%s' % url
rc, out = run_command_status(*cmd, stdin=stdin.encode('utf-8')) rc, out = run_command_status(*cmd, stdin=stdin)
if rc: if rc:
return None return None
data = dict(line.split('=', 1) for line in out.splitlines()) data = dict(line.split('=', 1) for line in out.splitlines())

View File

@ -305,7 +305,7 @@ class GitReviewUnitTest(testtools.TestCase):
# This gets encoded to utf8 which means the type passed down # This gets encoded to utf8 which means the type passed down
# is bytes. # is bytes.
mock_run.assert_called_once_with('git', 'credential', 'fill', mock_run.assert_called_once_with('git', 'credential', 'fill',
stdin=b'url=%s' % url.encode('utf-8')) stdin='url=%s' % url)
calls = [mock.call(url), mock.call(url, auth=('user', 'pass'))] calls = [mock.call(url), mock.call(url, auth=('user', 'pass'))]
mock_get.assert_has_calls(calls) mock_get.assert_has_calls(calls)
@ -323,7 +323,7 @@ class GitReviewUnitTest(testtools.TestCase):
# This gets encoded to utf8 which means the type passed down # This gets encoded to utf8 which means the type passed down
# is bytes. # is bytes.
mock_run.assert_called_once_with('git', 'credential', 'fill', mock_run.assert_called_once_with('git', 'credential', 'fill',
stdin=b'url=%s' % url.encode('utf-8')) stdin='url=%s' % url)
calls = [mock.call(url), mock.call(url, auth=('user', 'pass'))] calls = [mock.call(url), mock.call(url, auth=('user', 'pass'))]
mock_get.assert_has_calls(calls) mock_get.assert_has_calls(calls)
@ -341,7 +341,7 @@ class GitReviewUnitTest(testtools.TestCase):
# This gets encoded to utf8 which means the type passed down # This gets encoded to utf8 which means the type passed down
# is bytes. # is bytes.
mock_run.assert_called_once_with('git', 'credential', 'fill', mock_run.assert_called_once_with('git', 'credential', 'fill',
stdin=b'url=%s' % url.encode('utf-8')) stdin='url=%s' % url)
mock_get.assert_called_once_with(url) mock_get.assert_called_once_with(url)
@mock.patch('sys.argv', ['argv0', '--track', 'branch']) @mock.patch('sys.argv', ['argv0', '--track', 'branch'])

View File

@ -1,5 +1,5 @@
hacking>=2.0.0,<2.1.0 hacking>=2.0.0,<2.1.0
mock mock
fixtures>=0.3.14 fixtures>=0.3.14
stestr>=2.2.0 stestr>=2.2.0,<3.0.0
testtools>=0.9.34 testtools>=0.9.34