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 |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 25 | import platform_utils |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 26 | from repo_trace import REPO_TRACE, IsTrace, Trace |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 27 | from wrapper import Wrapper |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | |
| 29 | GIT = 'git' |
| 30 | MIN_GIT_VERSION = (1, 5, 4) |
| 31 | GIT_DIR = 'GIT_DIR' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
| 33 | LAST_GITDIR = None |
| 34 | LAST_CWD = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 35 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 36 | _ssh_proxy_path = None |
| 37 | _ssh_sock_path = None |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 38 | _ssh_clients = [] |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 39 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 40 | def ssh_sock(create=True): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 41 | global _ssh_sock_path |
| 42 | if _ssh_sock_path is None: |
| 43 | if not create: |
| 44 | return None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 45 | tmp_dir = '/tmp' |
| 46 | if not os.path.exists(tmp_dir): |
| 47 | tmp_dir = tempfile.gettempdir() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 48 | _ssh_sock_path = os.path.join( |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 49 | tempfile.mkdtemp('', 'ssh-', tmp_dir), |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 50 | 'master-%r@%h:%p') |
| 51 | return _ssh_sock_path |
| 52 | |
| 53 | def _ssh_proxy(): |
| 54 | global _ssh_proxy_path |
| 55 | if _ssh_proxy_path is None: |
| 56 | _ssh_proxy_path = os.path.join( |
| 57 | os.path.dirname(__file__), |
| 58 | 'git_ssh') |
| 59 | return _ssh_proxy_path |
| 60 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 61 | def _add_ssh_client(p): |
| 62 | _ssh_clients.append(p) |
| 63 | |
| 64 | def _remove_ssh_client(p): |
| 65 | try: |
| 66 | _ssh_clients.remove(p) |
| 67 | except ValueError: |
| 68 | pass |
| 69 | |
| 70 | def terminate_ssh_clients(): |
| 71 | global _ssh_clients |
| 72 | for p in _ssh_clients: |
| 73 | try: |
| 74 | os.kill(p.pid, SIGTERM) |
| 75 | p.wait() |
| 76 | except OSError: |
| 77 | pass |
| 78 | _ssh_clients = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 80 | _git_version = None |
| 81 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | class _GitCall(object): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 83 | def version_tuple(self): |
| 84 | global _git_version |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 85 | if _git_version is None: |
Mike Frysinger | ca540ae | 2019-07-10 15:42:30 -0400 | [diff] [blame] | 86 | _git_version = Wrapper().ParseGitVersion() |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 87 | if _git_version is None: |
Mike Frysinger | ca540ae | 2019-07-10 15:42:30 -0400 | [diff] [blame] | 88 | print('fatal: unable to detect git version', file=sys.stderr) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 89 | sys.exit(1) |
| 90 | return _git_version |
| 91 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 92 | def __getattr__(self, name): |
| 93 | name = name.replace('_','-') |
| 94 | def fun(*cmdv): |
| 95 | command = [name] |
| 96 | command.extend(cmdv) |
| 97 | return GitCommand(None, command).Wait() == 0 |
| 98 | return fun |
| 99 | git = _GitCall() |
| 100 | |
Mike Frysinger | 369814b | 2019-07-10 17:10:07 -0400 | [diff] [blame^] | 101 | |
| 102 | _user_agent = None |
| 103 | |
| 104 | def RepoUserAgent(): |
| 105 | """Return a User-Agent string suitable for HTTP-like services. |
| 106 | |
| 107 | We follow the style as documented here: |
| 108 | https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent |
| 109 | """ |
| 110 | global _user_agent |
| 111 | |
| 112 | if _user_agent is None: |
| 113 | py_version = sys.version_info |
| 114 | |
| 115 | os_name = sys.platform |
| 116 | if os_name == 'linux2': |
| 117 | os_name = 'Linux' |
| 118 | elif os_name == 'win32': |
| 119 | os_name = 'Win32' |
| 120 | elif os_name == 'cygwin': |
| 121 | os_name = 'Cygwin' |
| 122 | elif os_name == 'darwin': |
| 123 | os_name = 'Darwin' |
| 124 | |
| 125 | p = GitCommand( |
| 126 | None, ['describe', 'HEAD'], |
| 127 | cwd=os.path.dirname(__file__), |
| 128 | capture_stdout=True) |
| 129 | if p.Wait() == 0: |
| 130 | repo_version = p.stdout |
| 131 | if repo_version and repo_version[-1] == '\n': |
| 132 | repo_version = repo_version[0:-1] |
| 133 | if repo_version and repo_version[0] == 'v': |
| 134 | repo_version = repo_version[1:] |
| 135 | else: |
| 136 | repo_version = 'unknown' |
| 137 | |
| 138 | _user_agent = 'git-repo/%s (%s) git/%s Python/%d.%d.%d' % ( |
| 139 | repo_version, |
| 140 | os_name, |
| 141 | git.version_tuple().full, |
| 142 | py_version.major, py_version.minor, py_version.micro) |
| 143 | |
| 144 | return _user_agent |
| 145 | |
| 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 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 159 | def _setenv(env, name, value): |
| 160 | env[name] = value.encode() |
| 161 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | class GitCommand(object): |
| 163 | def __init__(self, |
| 164 | project, |
| 165 | cmdv, |
| 166 | bare = False, |
| 167 | provide_stdin = False, |
| 168 | capture_stdout = False, |
| 169 | capture_stderr = False, |
| 170 | disable_editor = False, |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 171 | ssh_proxy = False, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 172 | cwd = None, |
| 173 | gitdir = None): |
Shawn O. Pearce | 727ee98 | 2010-12-07 08:46:14 -0800 | [diff] [blame] | 174 | env = os.environ.copy() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 175 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 176 | for key in [REPO_TRACE, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | GIT_DIR, |
| 178 | 'GIT_ALTERNATE_OBJECT_DIRECTORIES', |
| 179 | 'GIT_OBJECT_DIRECTORY', |
| 180 | 'GIT_WORK_TREE', |
| 181 | 'GIT_GRAFT_FILE', |
| 182 | 'GIT_INDEX_FILE']: |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 183 | if key in env: |
| 184 | del env[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 185 | |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 186 | # If we are not capturing std* then need to print it. |
| 187 | self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr} |
| 188 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 189 | if disable_editor: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 190 | _setenv(env, 'GIT_EDITOR', ':') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 191 | if ssh_proxy: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 192 | _setenv(env, 'REPO_SSH_SOCK', ssh_sock()) |
| 193 | _setenv(env, 'GIT_SSH', _ssh_proxy()) |
Jonathan Nieder | c00d28b | 2017-10-19 14:23:10 -0700 | [diff] [blame] | 194 | _setenv(env, 'GIT_SSH_VARIANT', 'ssh') |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 195 | if 'http_proxy' in env and 'darwin' == sys.platform: |
Shawn O. Pearce | 337aee0 | 2012-06-13 10:40:46 -0700 | [diff] [blame] | 196 | s = "'http.proxy=%s'" % (env['http_proxy'],) |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 197 | p = env.get('GIT_CONFIG_PARAMETERS') |
| 198 | if p is not None: |
| 199 | s = p + ' ' + s |
| 200 | _setenv(env, 'GIT_CONFIG_PARAMETERS', s) |
Dan Willemsen | 466b8c4 | 2015-11-25 13:26:39 -0800 | [diff] [blame] | 201 | if 'GIT_ALLOW_PROTOCOL' not in env: |
| 202 | _setenv(env, 'GIT_ALLOW_PROTOCOL', |
Jonathan Nieder | 203153e | 2016-02-26 18:53:54 -0800 | [diff] [blame] | 203 | 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | |
| 205 | if project: |
| 206 | if not cwd: |
| 207 | cwd = project.worktree |
| 208 | if not gitdir: |
| 209 | gitdir = project.gitdir |
| 210 | |
| 211 | command = [GIT] |
| 212 | if bare: |
| 213 | if gitdir: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 214 | _setenv(env, GIT_DIR, gitdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | cwd = None |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 216 | command.append(cmdv[0]) |
| 217 | # Need to use the --progress flag for fetch/clone so output will be |
| 218 | # displayed as by default git only does progress output if stderr is a TTY. |
| 219 | if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'): |
| 220 | if '--progress' not in cmdv and '--quiet' not in cmdv: |
| 221 | command.append('--progress') |
| 222 | command.extend(cmdv[1:]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 223 | |
| 224 | if provide_stdin: |
| 225 | stdin = subprocess.PIPE |
| 226 | else: |
| 227 | stdin = None |
| 228 | |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 229 | stdout = subprocess.PIPE |
| 230 | stderr = subprocess.PIPE |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 232 | if IsTrace(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | global LAST_CWD |
| 234 | global LAST_GITDIR |
| 235 | |
| 236 | dbg = '' |
| 237 | |
| 238 | if cwd and LAST_CWD != cwd: |
| 239 | if LAST_GITDIR or LAST_CWD: |
| 240 | dbg += '\n' |
| 241 | dbg += ': cd %s\n' % cwd |
| 242 | LAST_CWD = cwd |
| 243 | |
| 244 | if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]: |
| 245 | if LAST_GITDIR or LAST_CWD: |
| 246 | dbg += '\n' |
| 247 | dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR] |
| 248 | LAST_GITDIR = env[GIT_DIR] |
| 249 | |
| 250 | dbg += ': ' |
| 251 | dbg += ' '.join(command) |
| 252 | if stdin == subprocess.PIPE: |
| 253 | dbg += ' 0<|' |
| 254 | if stdout == subprocess.PIPE: |
| 255 | dbg += ' 1>|' |
| 256 | if stderr == subprocess.PIPE: |
| 257 | dbg += ' 2>|' |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 258 | Trace('%s', dbg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | |
| 260 | try: |
| 261 | p = subprocess.Popen(command, |
| 262 | cwd = cwd, |
| 263 | env = env, |
| 264 | stdin = stdin, |
| 265 | stdout = stdout, |
| 266 | stderr = stderr) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 267 | except Exception as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 268 | raise GitError('%s: %s' % (command[1], e)) |
| 269 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 270 | if ssh_proxy: |
| 271 | _add_ssh_client(p) |
| 272 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 273 | self.process = p |
| 274 | self.stdin = p.stdin |
| 275 | |
| 276 | def Wait(self): |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 277 | try: |
Ulrik Sjölin | 498fe90 | 2011-09-11 22:59:37 +0200 | [diff] [blame] | 278 | p = self.process |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 279 | rc = self._CaptureOutput() |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 280 | finally: |
| 281 | _remove_ssh_client(p) |
| 282 | return rc |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 283 | |
| 284 | def _CaptureOutput(self): |
| 285 | p = self.process |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 286 | s_in = platform_utils.FileDescriptorStreams.create() |
| 287 | s_in.add(p.stdout, sys.stdout, 'stdout') |
| 288 | s_in.add(p.stderr, sys.stderr, 'stderr') |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 289 | self.stdout = '' |
| 290 | self.stderr = '' |
| 291 | |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 292 | while not s_in.is_done: |
| 293 | in_ready = s_in.select() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 294 | for s in in_ready: |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 295 | buf = s.read() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 296 | if not buf: |
| 297 | s_in.remove(s) |
| 298 | continue |
Anthony King | 6cfc68e | 2015-06-03 16:39:32 +0100 | [diff] [blame] | 299 | if not hasattr(buf, 'encode'): |
| 300 | buf = buf.decode() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 301 | if s.std_name == 'stdout': |
| 302 | self.stdout += buf |
| 303 | else: |
| 304 | self.stderr += buf |
| 305 | if self.tee[s.std_name]: |
| 306 | s.dest.write(buf) |
| 307 | s.dest.flush() |
| 308 | return p.wait() |