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 | |
Mike Frysinger | 8e768ea | 2021-05-06 00:28:32 -0400 | [diff] [blame] | 15 | import functools |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 16 | import os |
| 17 | import sys |
| 18 | import subprocess |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 19 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | from error import GitError |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 21 | from git_refs import HEAD |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 22 | import platform_utils |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 23 | from repo_trace import REPO_TRACE, IsTrace, Trace |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 24 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 25 | |
| 26 | GIT = 'git' |
Mike Frysinger | 82caef6 | 2020-02-11 18:51:08 -0500 | [diff] [blame] | 27 | # NB: These do not need to be kept in sync with the repo launcher script. |
| 28 | # These may be much newer as it allows the repo launcher to roll between |
| 29 | # different repo releases while source versions might require a newer git. |
| 30 | # |
| 31 | # The soft version is when we start warning users that the version is old and |
| 32 | # we'll be dropping support for it. We'll refuse to work with versions older |
| 33 | # than the hard version. |
| 34 | # |
| 35 | # git-1.7 is in (EOL) Ubuntu Precise. git-1.9 is in Ubuntu Trusty. |
| 36 | MIN_GIT_VERSION_SOFT = (1, 9, 1) |
| 37 | MIN_GIT_VERSION_HARD = (1, 7, 2) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | GIT_DIR = 'GIT_DIR' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | |
| 40 | LAST_GITDIR = None |
| 41 | LAST_CWD = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 43 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | class _GitCall(object): |
Mike Frysinger | 8e768ea | 2021-05-06 00:28:32 -0400 | [diff] [blame] | 45 | @functools.lru_cache(maxsize=None) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 46 | def version_tuple(self): |
Mike Frysinger | 8e768ea | 2021-05-06 00:28:32 -0400 | [diff] [blame] | 47 | ret = Wrapper().ParseGitVersion() |
| 48 | if ret is None: |
| 49 | print('fatal: unable to detect git version', file=sys.stderr) |
| 50 | sys.exit(1) |
| 51 | return ret |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 52 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | def __getattr__(self, name): |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 54 | name = name.replace('_', '-') |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 55 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 56 | def fun(*cmdv): |
| 57 | command = [name] |
| 58 | command.extend(cmdv) |
| 59 | return GitCommand(None, command).Wait() == 0 |
| 60 | return fun |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 61 | |
| 62 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | git = _GitCall() |
| 64 | |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 65 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 66 | def RepoSourceVersion(): |
| 67 | """Return the version of the repo.git tree.""" |
| 68 | ver = getattr(RepoSourceVersion, 'version', None) |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 69 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 70 | # We avoid GitCommand so we don't run into circular deps -- GitCommand needs |
| 71 | # to initialize version info we provide. |
| 72 | if ver is None: |
| 73 | env = GitCommand._GetBasicEnv() |
| 74 | |
| 75 | proj = os.path.dirname(os.path.abspath(__file__)) |
| 76 | env[GIT_DIR] = os.path.join(proj, '.git') |
Mike Frysinger | f307916 | 2021-02-16 02:38:21 -0500 | [diff] [blame] | 77 | result = subprocess.run([GIT, 'describe', HEAD], stdout=subprocess.PIPE, |
Xin Li | 0cb6e92 | 2021-06-16 10:19:00 -0700 | [diff] [blame] | 78 | stderr=subprocess.DEVNULL, encoding='utf-8', |
| 79 | env=env, check=False) |
Mike Frysinger | f307916 | 2021-02-16 02:38:21 -0500 | [diff] [blame] | 80 | if result.returncode == 0: |
| 81 | ver = result.stdout.strip() |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 82 | if ver.startswith('v'): |
| 83 | ver = ver[1:] |
| 84 | else: |
| 85 | ver = 'unknown' |
| 86 | setattr(RepoSourceVersion, 'version', ver) |
| 87 | |
| 88 | return ver |
| 89 | |
| 90 | |
| 91 | class UserAgent(object): |
| 92 | """Mange User-Agent settings when talking to external services |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 93 | |
| 94 | We follow the style as documented here: |
| 95 | https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent |
| 96 | """ |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 97 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 98 | _os = None |
| 99 | _repo_ua = None |
Mike Frysinger | 2f0951b | 2019-07-10 17:13:46 -0400 | [diff] [blame] | 100 | _git_ua = None |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 101 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 102 | @property |
| 103 | def os(self): |
| 104 | """The operating system name.""" |
| 105 | if self._os is None: |
| 106 | os_name = sys.platform |
| 107 | if os_name.lower().startswith('linux'): |
| 108 | os_name = 'Linux' |
| 109 | elif os_name == 'win32': |
| 110 | os_name = 'Win32' |
| 111 | elif os_name == 'cygwin': |
| 112 | os_name = 'Cygwin' |
| 113 | elif os_name == 'darwin': |
| 114 | os_name = 'Darwin' |
| 115 | self._os = os_name |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 116 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 117 | return self._os |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 118 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 119 | @property |
| 120 | def repo(self): |
| 121 | """The UA when connecting directly from repo.""" |
| 122 | if self._repo_ua is None: |
| 123 | py_version = sys.version_info |
| 124 | self._repo_ua = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 125 | RepoSourceVersion(), |
| 126 | self.os, |
| 127 | git.version_tuple().full, |
| 128 | py_version.major, py_version.minor, py_version.micro) |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 129 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 130 | return self._repo_ua |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 131 | |
Mike Frysinger | 2f0951b | 2019-07-10 17:13:46 -0400 | [diff] [blame] | 132 | @property |
| 133 | def git(self): |
| 134 | """The UA when running git.""" |
| 135 | if self._git_ua is None: |
| 136 | self._git_ua = 'git/%s (%s) git-repo/%s' % ( |
| 137 | git.version_tuple().full, |
| 138 | self.os, |
| 139 | RepoSourceVersion()) |
| 140 | |
| 141 | return self._git_ua |
| 142 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 143 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 144 | user_agent = UserAgent() |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame] | 145 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 146 | |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 147 | def git_require(min_version, fail=False, msg=''): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 148 | git_version = git.version_tuple() |
| 149 | if min_version <= git_version: |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 150 | return True |
| 151 | if fail: |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 152 | need = '.'.join(map(str, min_version)) |
Xin Li | 745be2e | 2019-06-03 11:24:30 -0700 | [diff] [blame] | 153 | if msg: |
| 154 | msg = ' for ' + msg |
| 155 | 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] | 156 | sys.exit(1) |
| 157 | return False |
| 158 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 159 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | class GitCommand(object): |
| 161 | def __init__(self, |
| 162 | project, |
| 163 | cmdv, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 164 | bare=False, |
Mike Frysinger | f37b982 | 2021-02-16 15:38:53 -0500 | [diff] [blame] | 165 | input=None, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 166 | capture_stdout=False, |
| 167 | capture_stderr=False, |
Mike Frysinger | 31990f0 | 2020-02-17 01:35:18 -0500 | [diff] [blame] | 168 | merge_output=False, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 169 | disable_editor=False, |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 170 | ssh_proxy=None, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 171 | cwd=None, |
| 172 | gitdir=None): |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 173 | env = self._GetBasicEnv() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 174 | |
| 175 | if disable_editor: |
Mike Frysinger | 56ce346 | 2019-12-04 19:30:48 -0500 | [diff] [blame] | 176 | env['GIT_EDITOR'] = ':' |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 177 | if ssh_proxy: |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 178 | env['REPO_SSH_SOCK'] = ssh_proxy.sock() |
| 179 | env['GIT_SSH'] = ssh_proxy.proxy |
Mike Frysinger | 56ce346 | 2019-12-04 19:30:48 -0500 | [diff] [blame] | 180 | env['GIT_SSH_VARIANT'] = 'ssh' |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 181 | if 'http_proxy' in env and 'darwin' == sys.platform: |
Shawn O. Pearce | 337aee0 | 2012-06-13 10:40:46 -0700 | [diff] [blame] | 182 | s = "'http.proxy=%s'" % (env['http_proxy'],) |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 183 | p = env.get('GIT_CONFIG_PARAMETERS') |
| 184 | if p is not None: |
| 185 | s = p + ' ' + s |
Mike Frysinger | 56ce346 | 2019-12-04 19:30:48 -0500 | [diff] [blame] | 186 | env['GIT_CONFIG_PARAMETERS'] = s |
Dan Willemsen | 466b8c4 | 2015-11-25 13:26:39 -0800 | [diff] [blame] | 187 | if 'GIT_ALLOW_PROTOCOL' not in env: |
Mike Frysinger | 56ce346 | 2019-12-04 19:30:48 -0500 | [diff] [blame] | 188 | env['GIT_ALLOW_PROTOCOL'] = ( |
| 189 | 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc') |
| 190 | env['GIT_HTTP_USER_AGENT'] = user_agent.git |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | |
| 192 | if project: |
| 193 | if not cwd: |
| 194 | cwd = project.worktree |
| 195 | if not gitdir: |
| 196 | gitdir = project.gitdir |
| 197 | |
| 198 | command = [GIT] |
| 199 | if bare: |
| 200 | if gitdir: |
Mike Frysinger | 4510be5 | 2021-02-27 13:06:27 -0500 | [diff] [blame] | 201 | # Git on Windows wants its paths only using / for reliability. |
| 202 | if platform_utils.isWindows(): |
| 203 | gitdir = gitdir.replace('\\', '/') |
Mike Frysinger | 56ce346 | 2019-12-04 19:30:48 -0500 | [diff] [blame] | 204 | env[GIT_DIR] = gitdir |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 205 | cwd = None |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 206 | command.append(cmdv[0]) |
| 207 | # Need to use the --progress flag for fetch/clone so output will be |
| 208 | # displayed as by default git only does progress output if stderr is a TTY. |
| 209 | if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'): |
| 210 | if '--progress' not in cmdv and '--quiet' not in cmdv: |
| 211 | command.append('--progress') |
| 212 | command.extend(cmdv[1:]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | |
Mike Frysinger | f37b982 | 2021-02-16 15:38:53 -0500 | [diff] [blame] | 214 | stdin = subprocess.PIPE if input else None |
Mike Frysinger | c87c186 | 2021-02-16 17:18:12 -0500 | [diff] [blame] | 215 | stdout = subprocess.PIPE if capture_stdout else None |
| 216 | stderr = (subprocess.STDOUT if merge_output else |
| 217 | (subprocess.PIPE if capture_stderr else None)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 219 | if IsTrace(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | global LAST_CWD |
| 221 | global LAST_GITDIR |
| 222 | |
| 223 | dbg = '' |
| 224 | |
| 225 | if cwd and LAST_CWD != cwd: |
| 226 | if LAST_GITDIR or LAST_CWD: |
| 227 | dbg += '\n' |
| 228 | dbg += ': cd %s\n' % cwd |
| 229 | LAST_CWD = cwd |
| 230 | |
| 231 | if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]: |
| 232 | if LAST_GITDIR or LAST_CWD: |
| 233 | dbg += '\n' |
| 234 | dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR] |
| 235 | LAST_GITDIR = env[GIT_DIR] |
| 236 | |
| 237 | dbg += ': ' |
| 238 | dbg += ' '.join(command) |
| 239 | if stdin == subprocess.PIPE: |
| 240 | dbg += ' 0<|' |
| 241 | if stdout == subprocess.PIPE: |
| 242 | dbg += ' 1>|' |
| 243 | if stderr == subprocess.PIPE: |
| 244 | dbg += ' 2>|' |
Mike Frysinger | 31990f0 | 2020-02-17 01:35:18 -0500 | [diff] [blame] | 245 | elif stderr == subprocess.STDOUT: |
| 246 | dbg += ' 2>&1' |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 247 | Trace('%s', dbg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 248 | |
| 249 | try: |
| 250 | p = subprocess.Popen(command, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 251 | cwd=cwd, |
| 252 | env=env, |
Mike Frysinger | c87c186 | 2021-02-16 17:18:12 -0500 | [diff] [blame] | 253 | encoding='utf-8', |
| 254 | errors='backslashreplace', |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 255 | stdin=stdin, |
| 256 | stdout=stdout, |
| 257 | stderr=stderr) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 258 | except Exception as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | raise GitError('%s: %s' % (command[1], e)) |
| 260 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 261 | if ssh_proxy: |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 262 | ssh_proxy.add_client(p) |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 263 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | self.process = p |
Mike Frysinger | f37b982 | 2021-02-16 15:38:53 -0500 | [diff] [blame] | 265 | if input: |
| 266 | if isinstance(input, str): |
| 267 | input = input.encode('utf-8') |
| 268 | p.stdin.write(input) |
| 269 | p.stdin.close() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | |
Mike Frysinger | c5bbea8 | 2021-02-16 15:45:19 -0500 | [diff] [blame] | 271 | try: |
Mike Frysinger | c87c186 | 2021-02-16 17:18:12 -0500 | [diff] [blame] | 272 | self.stdout, self.stderr = p.communicate() |
Mike Frysinger | c5bbea8 | 2021-02-16 15:45:19 -0500 | [diff] [blame] | 273 | finally: |
Mike Frysinger | 339f2df | 2021-05-06 00:44:42 -0400 | [diff] [blame] | 274 | if ssh_proxy: |
| 275 | ssh_proxy.remove_client(p) |
Mike Frysinger | c87c186 | 2021-02-16 17:18:12 -0500 | [diff] [blame] | 276 | self.rc = p.wait() |
Mike Frysinger | c5bbea8 | 2021-02-16 15:45:19 -0500 | [diff] [blame] | 277 | |
Mike Frysinger | 71b0f31 | 2019-09-30 22:39:49 -0400 | [diff] [blame] | 278 | @staticmethod |
| 279 | def _GetBasicEnv(): |
| 280 | """Return a basic env for running git under. |
| 281 | |
| 282 | This is guaranteed to be side-effect free. |
| 283 | """ |
| 284 | env = os.environ.copy() |
| 285 | for key in (REPO_TRACE, |
| 286 | GIT_DIR, |
| 287 | 'GIT_ALTERNATE_OBJECT_DIRECTORIES', |
| 288 | 'GIT_OBJECT_DIRECTORY', |
| 289 | 'GIT_WORK_TREE', |
| 290 | 'GIT_GRAFT_FILE', |
| 291 | 'GIT_INDEX_FILE'): |
| 292 | env.pop(key, None) |
| 293 | return env |
| 294 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 295 | def Wait(self): |
Mike Frysinger | c5bbea8 | 2021-02-16 15:45:19 -0500 | [diff] [blame] | 296 | return self.rc |