assume environment always accepts strings

Different Python & OS versions have different environ behavior wrt
accepted types & encoding.  Since we're migrating to be Python 3 only,
lets change our code to assume strings always work as that's what the
newer Python 3 does.  This will fail under Python 2 for some env vars,
mostly on Windows, but the effort of maintaining shim layers that can
handle these edge cases isn't worth it when we're dropping that code.

We leave the logic in the `repo` launcher for now as it is simple, and
we want it to be able to switch versions a bit longer than the rest of
the tree.

Here's the support table:
          |    *NIX      |         Windows           |
 Python 2 | ASCII string | str or bytes, not unicode |
 Python 3 | str or bytes | str only                  |

Windows uses strings natively in its environment all the time.  But it
doesn't allow unicode strings under Python 2, so we have to encode.

Python 2 on *NIX is funky in that it always lowers to ASCII, so we had
to manually encode to avoid errors regardless of unicode or str.

Python 3 on Windows & *NIX will accept strings.  *NIX will also accept
bytes but Windows will not.

Bug: https://crbug.com/gerrit/12145
Change-Id: I3cf8f95a06902754ea1f08ad4b28503f7063531b
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/248972
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Michael Mortensen <mmortensen@google.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2019-12-04 19:30:48 -05:00
parent 02aa889ecd
commit 56ce3468b4
3 changed files with 17 additions and 23 deletions

View File

@ -214,10 +214,6 @@ def git_require(min_version, fail=False, msg=''):
return False return False
def _setenv(env, name, value):
env[name] = value.encode()
class GitCommand(object): class GitCommand(object):
def __init__(self, def __init__(self,
project, project,
@ -237,21 +233,21 @@ class GitCommand(object):
self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr} self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}
if disable_editor: if disable_editor:
_setenv(env, 'GIT_EDITOR', ':') env['GIT_EDITOR'] = ':'
if ssh_proxy: if ssh_proxy:
_setenv(env, 'REPO_SSH_SOCK', ssh_sock()) env['REPO_SSH_SOCK'] = ssh_sock()
_setenv(env, 'GIT_SSH', _ssh_proxy()) env['GIT_SSH'] = _ssh_proxy()
_setenv(env, 'GIT_SSH_VARIANT', 'ssh') env['GIT_SSH_VARIANT'] = 'ssh'
if 'http_proxy' in env and 'darwin' == sys.platform: if 'http_proxy' in env and 'darwin' == sys.platform:
s = "'http.proxy=%s'" % (env['http_proxy'],) s = "'http.proxy=%s'" % (env['http_proxy'],)
p = env.get('GIT_CONFIG_PARAMETERS') p = env.get('GIT_CONFIG_PARAMETERS')
if p is not None: if p is not None:
s = p + ' ' + s s = p + ' ' + s
_setenv(env, 'GIT_CONFIG_PARAMETERS', s) env['GIT_CONFIG_PARAMETERS'] = s
if 'GIT_ALLOW_PROTOCOL' not in env: if 'GIT_ALLOW_PROTOCOL' not in env:
_setenv(env, 'GIT_ALLOW_PROTOCOL', env['GIT_ALLOW_PROTOCOL'] = (
'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc') 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
_setenv(env, 'GIT_HTTP_USER_AGENT', user_agent.git) env['GIT_HTTP_USER_AGENT'] = user_agent.git
if project: if project:
if not cwd: if not cwd:
@ -262,7 +258,7 @@ class GitCommand(object):
command = [GIT] command = [GIT]
if bare: if bare:
if gitdir: if gitdir:
_setenv(env, GIT_DIR, gitdir) env[GIT_DIR] = gitdir
cwd = None cwd = None
command.append(cmdv[0]) command.append(cmdv[0])
# Need to use the --progress flag for fetch/clone so output will be # Need to use the --progress flag for fetch/clone so output will be

View File

@ -310,8 +310,6 @@ def DoWork(project, mirror, opt, cmd, shell, cnt, config):
def setenv(name, val): def setenv(name, val):
if val is None: if val is None:
val = '' val = ''
if hasattr(val, 'encode'):
val = val.encode()
env[name] = val env[name] = val
setenv('REPO_PROJECT', project['name']) setenv('REPO_PROJECT', project['name'])

View File

@ -794,13 +794,13 @@ later is required to fix a server side protocol bug.
if branch.startswith(R_HEADS): if branch.startswith(R_HEADS):
branch = branch[len(R_HEADS):] branch = branch[len(R_HEADS):]
env = os.environ.copy() if 'SYNC_TARGET' in os.environ:
if 'SYNC_TARGET' in env: target = os.environ('SYNC_TARGET')
target = env['SYNC_TARGET']
[success, manifest_str] = server.GetApprovedManifest(branch, target) [success, manifest_str] = server.GetApprovedManifest(branch, target)
elif 'TARGET_PRODUCT' in env and 'TARGET_BUILD_VARIANT' in env: elif ('TARGET_PRODUCT' in os.environ and
target = '%s-%s' % (env['TARGET_PRODUCT'], 'TARGET_BUILD_VARIANT' in os.environ):
env['TARGET_BUILD_VARIANT']) target = '%s-%s' % (os.environ('TARGET_PRODUCT'),
os.environ('TARGET_BUILD_VARIANT'))
[success, manifest_str] = server.GetApprovedManifest(branch, target) [success, manifest_str] = server.GetApprovedManifest(branch, target)
else: else:
[success, manifest_str] = server.GetApprovedManifest(branch) [success, manifest_str] = server.GetApprovedManifest(branch)
@ -1111,8 +1111,8 @@ def _VerifyTag(project):
return False return False
env = os.environ.copy() env = os.environ.copy()
env['GIT_DIR'] = project.gitdir.encode() env['GIT_DIR'] = project.gitdir
env['GNUPGHOME'] = gpg_dir.encode() env['GNUPGHOME'] = gpg_dir
cmd = [GIT, 'tag', '-v', cur] cmd = [GIT, 'tag', '-v', cur]
proc = subprocess.Popen(cmd, proc = subprocess.Popen(cmd,