Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 1 | # -*- coding:utf-8 -*- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2008 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 17 | from __future__ import print_function |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
| 19 | import sys |
| 20 | import subprocess |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 21 | import tempfile |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 22 | from signal import SIGTERM |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 23 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | from error import GitError |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 25 | from git_refs import HEAD |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 26 | import platform_utils |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 27 | from repo_trace import REPO_TRACE, IsTrace, Trace |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 28 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
| 30 | GIT = 'git' |
Mike Frysinger | 82caef6 | 2020-02-11 18:51:08 -0500 | [diff] [blame] | 31 | # NB: These do not need to be kept in sync with the repo launcher script. |
| 32 | # These may be much newer as it allows the repo launcher to roll between |
| 33 | # different repo releases while source versions might require a newer git. |
| 34 | # |
| 35 | # The soft version is when we start warning users that the version is old and |
| 36 | # we'll be dropping support for it. We'll refuse to work with versions older |
| 37 | # than the hard version. |
| 38 | # |
| 39 | # git-1.7 is in (EOL) Ubuntu Precise. git-1.9 is in Ubuntu Trusty. |
| 40 | MIN_GIT_VERSION_SOFT = (1, 9, 1) |
| 41 | MIN_GIT_VERSION_HARD = (1, 7, 2) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | GIT_DIR = 'GIT_DIR' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | |
| 44 | LAST_GITDIR = None |
| 45 | LAST_CWD = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 47 | _ssh_proxy_path = None |
| 48 | _ssh_sock_path = None |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 49 | _ssh_clients = [] |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 50 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 51 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 52 | def ssh_sock(create=True): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 53 | global _ssh_sock_path |
| 54 | if _ssh_sock_path is None: |
| 55 | if not create: |
| 56 | return None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 57 | tmp_dir = '/tmp' |
| 58 | if not os.path.exists(tmp_dir): |
| 59 | tmp_dir = tempfile.gettempdir() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 60 | _ssh_sock_path = os.path.join( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 61 | tempfile.mkdtemp('', 'ssh-', tmp_dir), |
| 62 | 'master-%r@%h:%p') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 63 | return _ssh_sock_path |
| 64 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 65 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 66 | def _ssh_proxy(): |
| 67 | global _ssh_proxy_path |
| 68 | if _ssh_proxy_path is None: |
| 69 | _ssh_proxy_path = os.path.join( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 70 | os.path.dirname(__file__), |
| 71 | 'git_ssh') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 72 | return _ssh_proxy_path |
| 73 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 74 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 75 | def _add_ssh_client(p): |
| 76 | _ssh_clients.append(p) |
| 77 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 78 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 79 | def _remove_ssh_client(p): |
| 80 | try: |
| 81 | _ssh_clients.remove(p) |
| 82 | except ValueError: |
| 83 | pass |
| 84 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 85 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 86 | def terminate_ssh_clients(): |
| 87 | global _ssh_clients |
| 88 | for p in _ssh_clients: |
| 89 | try: |
| 90 | os.kill(p.pid, SIGTERM) |
| 91 | p.wait() |
| 92 | except OSError: |
| 93 | pass |
| 94 | _ssh_clients = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 96 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 97 | _git_version = None |
| 98 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 99 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 100 | class _GitCall(object): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 101 | def version_tuple(self): |
| 102 | global _git_version |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 103 | if _git_version is None: |
Mike Frysinger | ca540ae | 2019-07-10 15:42:30 -0400 | [diff] [blame] | 104 | _git_version = Wrapper().ParseGitVersion() |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 105 | if _git_version is None: |
Mike Frysinger | ca540ae | 2019-07-10 15:42:30 -0400 | [diff] [blame] | 106 | print('fatal: unable to detect git version', file=sys.stderr) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 107 | sys.exit(1) |
| 108 | return _git_version |
| 109 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | def __getattr__(self, name): |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 111 | name = name.replace('_', '-') |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 112 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | def fun(*cmdv): |
| 114 | command = [name] |
| 115 | command.extend(cmdv) |
| 116 | return GitCommand(None, command).Wait() == 0 |
| 117 | return fun |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 118 | |
| 119 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 120 | git = _GitCall() |
| 121 | |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 122 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 123 | def RepoSourceVersion(): |
| 124 | """Return the version of the repo.git tree.""" |
| 125 | ver = getattr(RepoSourceVersion, 'version', None) |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 126 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 127 | # We avoid GitCommand so we don't run into circular deps -- GitCommand needs |
| 128 | # to initialize version info we provide. |
| 129 | if ver is None: |
| 130 | env = GitCommand._GetBasicEnv() |
| 131 | |
| 132 | proj = os.path.dirname(os.path.abspath(__file__)) |
| 133 | env[GIT_DIR] = os.path.join(proj, '.git') |
| 134 | |
| 135 | p = subprocess.Popen([GIT, 'describe', HEAD], stdout=subprocess.PIPE, |
| 136 | env=env) |
| 137 | if p.wait() == 0: |
| 138 | ver = p.stdout.read().strip().decode('utf-8') |
| 139 | if ver.startswith('v'): |
| 140 | ver = ver[1:] |
| 141 | else: |
| 142 | ver = 'unknown' |
| 143 | setattr(RepoSourceVersion, 'version', ver) |
| 144 | |
| 145 | return ver |
| 146 | |
| 147 | |
| 148 | class UserAgent(object): |
| 149 | """Mange User-Agent settings when talking to external services |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 150 | |
| 151 | We follow the style as documented here: |
| 152 | https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent |
| 153 | """ |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 154 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 155 | _os = None |
| 156 | _repo_ua = None |
Mike Frysinger | 2f0951b | 2019-07-10 17:13:46 -0400 | [diff] [blame] | 157 | _git_ua = None |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 158 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 159 | @property |
| 160 | def os(self): |
| 161 | """The operating system name.""" |
| 162 | if self._os is None: |
| 163 | os_name = sys.platform |
| 164 | if os_name.lower().startswith('linux'): |
| 165 | os_name = 'Linux' |
| 166 | elif os_name == 'win32': |
| 167 | os_name = 'Win32' |
| 168 | elif os_name == 'cygwin': |
| 169 | os_name = 'Cygwin' |
| 170 | elif os_name == 'darwin': |
| 171 | os_name = 'Darwin' |
| 172 | self._os = os_name |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 173 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 174 | return self._os |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 175 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 176 | @property |
| 177 | def repo(self): |
| 178 | """The UA when connecting directly from repo.""" |
| 179 | if self._repo_ua is None: |
| 180 | py_version = sys.version_info |
| 181 | self._repo_ua = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 182 | RepoSourceVersion(), |
| 183 | self.os, |
| 184 | git.version_tuple().full, |
| 185 | py_version.major, py_version.minor, py_version.micro) |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 186 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 187 | return self._repo_ua |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 188 | |
Mike Frysinger | 2f0951b | 2019-07-10 17:13:46 -0400 | [diff] [blame] | 189 | @property |
| 190 | def git(self): |
| 191 | """The UA when running git.""" |
| 192 | if self._git_ua is None: |
| 193 | self._git_ua = 'git/%s (%s) git-repo/%s' % ( |
| 194 | git.version_tuple().full, |
| 195 | self.os, |
| 196 | RepoSourceVersion()) |
| 197 | |
| 198 | return self._git_ua |
| 199 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 200 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 201 | user_agent = UserAgent() |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 202 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 203 | |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 204 | def git_require(min_version, fail=False, msg=''): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 205 | git_version = git.version_tuple() |
| 206 | if min_version <= git_version: |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 207 | return True |
| 208 | if fail: |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 209 | need = '.'.join(map(str, min_version)) |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 210 | if msg: |
| 211 | msg = ' for ' + msg |
| 212 | print('fatal: git %s or later required%s' % (need, msg), file=sys.stderr) |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 213 | sys.exit(1) |
| 214 | return False |
| 215 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 216 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 217 | def _setenv(env, name, value): |
| 218 | env[name] = value.encode() |
| 219 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 220 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 221 | class GitCommand(object): |
| 222 | def __init__(self, |
| 223 | project, |
| 224 | cmdv, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 225 | bare=False, |
| 226 | provide_stdin=False, |
| 227 | capture_stdout=False, |
| 228 | capture_stderr=False, |
| 229 | disable_editor=False, |
| 230 | ssh_proxy=False, |
| 231 | cwd=None, |
| 232 | gitdir=None): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 233 | env = self._GetBasicEnv() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 235 | # If we are not capturing std* then need to print it. |
| 236 | self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr} |
| 237 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 238 | if disable_editor: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 239 | _setenv(env, 'GIT_EDITOR', ':') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 240 | if ssh_proxy: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 241 | _setenv(env, 'REPO_SSH_SOCK', ssh_sock()) |
| 242 | _setenv(env, 'GIT_SSH', _ssh_proxy()) |
Jonathan Nieder | c00d28b | 2017-10-19 14:23:10 -0700 | [diff] [blame] | 243 | _setenv(env, 'GIT_SSH_VARIANT', 'ssh') |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 244 | if 'http_proxy' in env and 'darwin' == sys.platform: |
Shawn O. Pearce | 337aee0 | 2012-06-13 10:40:46 -0700 | [diff] [blame] | 245 | s = "'http.proxy=%s'" % (env['http_proxy'],) |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 246 | p = env.get('GIT_CONFIG_PARAMETERS') |
| 247 | if p is not None: |
| 248 | s = p + ' ' + s |
| 249 | _setenv(env, 'GIT_CONFIG_PARAMETERS', s) |
Dan Willemsen | 466b8c4 | 2015-11-25 13:26:39 -0800 | [diff] [blame] | 250 | if 'GIT_ALLOW_PROTOCOL' not in env: |
| 251 | _setenv(env, 'GIT_ALLOW_PROTOCOL', |
Jonathan Nieder | 203153e | 2016-02-26 18:53:54 -0800 | [diff] [blame] | 252 | 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc') |
Mike Frysinger | 2f0951b | 2019-07-10 17:13:46 -0400 | [diff] [blame] | 253 | _setenv(env, 'GIT_HTTP_USER_AGENT', user_agent.git) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 254 | |
| 255 | if project: |
| 256 | if not cwd: |
| 257 | cwd = project.worktree |
| 258 | if not gitdir: |
| 259 | gitdir = project.gitdir |
| 260 | |
| 261 | command = [GIT] |
| 262 | if bare: |
| 263 | if gitdir: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 264 | _setenv(env, GIT_DIR, gitdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 265 | cwd = None |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 266 | command.append(cmdv[0]) |
| 267 | # Need to use the --progress flag for fetch/clone so output will be |
| 268 | # displayed as by default git only does progress output if stderr is a TTY. |
| 269 | if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'): |
| 270 | if '--progress' not in cmdv and '--quiet' not in cmdv: |
| 271 | command.append('--progress') |
| 272 | command.extend(cmdv[1:]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | |
| 274 | if provide_stdin: |
| 275 | stdin = subprocess.PIPE |
| 276 | else: |
| 277 | stdin = None |
| 278 | |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 279 | stdout = subprocess.PIPE |
| 280 | stderr = subprocess.PIPE |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 281 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 282 | if IsTrace(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 283 | global LAST_CWD |
| 284 | global LAST_GITDIR |
| 285 | |
| 286 | dbg = '' |
| 287 | |
| 288 | if cwd and LAST_CWD != cwd: |
| 289 | if LAST_GITDIR or LAST_CWD: |
| 290 | dbg += '\n' |
| 291 | dbg += ': cd %s\n' % cwd |
| 292 | LAST_CWD = cwd |
| 293 | |
| 294 | if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]: |
| 295 | if LAST_GITDIR or LAST_CWD: |
| 296 | dbg += '\n' |
| 297 | dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR] |
| 298 | LAST_GITDIR = env[GIT_DIR] |
| 299 | |
| 300 | dbg += ': ' |
| 301 | dbg += ' '.join(command) |
| 302 | if stdin == subprocess.PIPE: |
| 303 | dbg += ' 0<|' |
| 304 | if stdout == subprocess.PIPE: |
| 305 | dbg += ' 1>|' |
| 306 | if stderr == subprocess.PIPE: |
| 307 | dbg += ' 2>|' |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 308 | Trace('%s', dbg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | |
| 310 | try: |
| 311 | p = subprocess.Popen(command, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 312 | cwd=cwd, |
| 313 | env=env, |
| 314 | stdin=stdin, |
| 315 | stdout=stdout, |
| 316 | stderr=stderr) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 317 | except Exception as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 318 | raise GitError('%s: %s' % (command[1], e)) |
| 319 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 320 | if ssh_proxy: |
| 321 | _add_ssh_client(p) |
| 322 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | self.process = p |
| 324 | self.stdin = p.stdin |
| 325 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 326 | @staticmethod |
| 327 | def _GetBasicEnv(): |
| 328 | """Return a basic env for running git under. |
| 329 | |
| 330 | This is guaranteed to be side-effect free. |
| 331 | """ |
| 332 | env = os.environ.copy() |
| 333 | for key in (REPO_TRACE, |
| 334 | GIT_DIR, |
| 335 | 'GIT_ALTERNATE_OBJECT_DIRECTORIES', |
| 336 | 'GIT_OBJECT_DIRECTORY', |
| 337 | 'GIT_WORK_TREE', |
| 338 | 'GIT_GRAFT_FILE', |
| 339 | 'GIT_INDEX_FILE'): |
| 340 | env.pop(key, None) |
| 341 | return env |
| 342 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 343 | def Wait(self): |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 344 | try: |
Ulrik Sjölin | 498fe90 | 2011-09-11 22:59:37 +0200 | [diff] [blame] | 345 | p = self.process |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 346 | rc = self._CaptureOutput() |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 347 | finally: |
| 348 | _remove_ssh_client(p) |
| 349 | return rc |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 350 | |
| 351 | def _CaptureOutput(self): |
| 352 | p = self.process |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 353 | s_in = platform_utils.FileDescriptorStreams.create() |
| 354 | s_in.add(p.stdout, sys.stdout, 'stdout') |
| 355 | s_in.add(p.stderr, sys.stderr, 'stderr') |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 356 | self.stdout = '' |
| 357 | self.stderr = '' |
| 358 | |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 359 | while not s_in.is_done: |
| 360 | in_ready = s_in.select() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 361 | for s in in_ready: |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 362 | buf = s.read() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 363 | if not buf: |
| 364 | s_in.remove(s) |
| 365 | continue |
Anthony King | 6cfc68e | 2015-06-03 16:39:32 +0100 | [diff] [blame] | 366 | if not hasattr(buf, 'encode'): |
| 367 | buf = buf.decode() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 368 | if s.std_name == 'stdout': |
| 369 | self.stdout += buf |
| 370 | else: |
| 371 | self.stderr += buf |
| 372 | if self.tee[s.std_name]: |
| 373 | s.dest.write(buf) |
| 374 | s.dest.flush() |
| 375 | return p.wait() |