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