The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # |
| 2 | # Copyright (C) 2008 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
| 18 | import sys |
| 19 | import subprocess |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 20 | import tempfile |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 21 | from signal import SIGTERM |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | from error import GitError |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 24 | import platform_utils |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 25 | from 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' |
| 29 | MIN_GIT_VERSION = (1, 5, 4) |
| 30 | GIT_DIR = 'GIT_DIR' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | |
| 32 | LAST_GITDIR = None |
| 33 | LAST_CWD = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 35 | _ssh_proxy_path = None |
| 36 | _ssh_sock_path = None |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 37 | _ssh_clients = [] |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 38 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 39 | def ssh_sock(create=True): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 40 | global _ssh_sock_path |
| 41 | if _ssh_sock_path is None: |
| 42 | if not create: |
| 43 | return None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 44 | tmp_dir = '/tmp' |
| 45 | if not os.path.exists(tmp_dir): |
| 46 | tmp_dir = tempfile.gettempdir() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 47 | _ssh_sock_path = os.path.join( |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 48 | tempfile.mkdtemp('', 'ssh-', tmp_dir), |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 49 | 'master-%r@%h:%p') |
| 50 | return _ssh_sock_path |
| 51 | |
| 52 | def _ssh_proxy(): |
| 53 | global _ssh_proxy_path |
| 54 | if _ssh_proxy_path is None: |
| 55 | _ssh_proxy_path = os.path.join( |
| 56 | os.path.dirname(__file__), |
| 57 | 'git_ssh') |
| 58 | return _ssh_proxy_path |
| 59 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 60 | def _add_ssh_client(p): |
| 61 | _ssh_clients.append(p) |
| 62 | |
| 63 | def _remove_ssh_client(p): |
| 64 | try: |
| 65 | _ssh_clients.remove(p) |
| 66 | except ValueError: |
| 67 | pass |
| 68 | |
| 69 | def terminate_ssh_clients(): |
| 70 | global _ssh_clients |
| 71 | for p in _ssh_clients: |
| 72 | try: |
| 73 | os.kill(p.pid, SIGTERM) |
| 74 | p.wait() |
| 75 | except OSError: |
| 76 | pass |
| 77 | _ssh_clients = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 78 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 79 | _git_version = None |
| 80 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | class _GitCall(object): |
| 82 | def version(self): |
| 83 | p = GitCommand(None, ['--version'], capture_stdout=True) |
| 84 | if p.Wait() == 0: |
Anthony King | cf738ed | 2015-06-03 16:50:39 +0100 | [diff] [blame] | 85 | if hasattr(p.stdout, 'decode'): |
| 86 | return p.stdout.decode('utf-8') |
| 87 | else: |
| 88 | return p.stdout |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | return None |
| 90 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 91 | def version_tuple(self): |
| 92 | global _git_version |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 93 | if _git_version is None: |
Chirayu Desai | c46de69 | 2014-08-20 09:34:10 +0530 | [diff] [blame] | 94 | ver_str = git.version() |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 95 | _git_version = Wrapper().ParseGitVersion(ver_str) |
| 96 | if _git_version is None: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 97 | print('fatal: "%s" unsupported' % ver_str, file=sys.stderr) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 98 | sys.exit(1) |
| 99 | return _git_version |
| 100 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | def __getattr__(self, name): |
| 102 | name = name.replace('_','-') |
| 103 | def fun(*cmdv): |
| 104 | command = [name] |
| 105 | command.extend(cmdv) |
| 106 | return GitCommand(None, command).Wait() == 0 |
| 107 | return fun |
| 108 | git = _GitCall() |
| 109 | |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 110 | def git_require(min_version, fail=False): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 111 | git_version = git.version_tuple() |
| 112 | if min_version <= git_version: |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 113 | return True |
| 114 | if fail: |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 115 | need = '.'.join(map(str, min_version)) |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 116 | print('fatal: git %s or later required' % need, file=sys.stderr) |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 117 | sys.exit(1) |
| 118 | return False |
| 119 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 120 | def _setenv(env, name, value): |
| 121 | env[name] = value.encode() |
| 122 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | class GitCommand(object): |
| 124 | def __init__(self, |
| 125 | project, |
| 126 | cmdv, |
| 127 | bare = False, |
| 128 | provide_stdin = False, |
| 129 | capture_stdout = False, |
| 130 | capture_stderr = False, |
| 131 | disable_editor = False, |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 132 | ssh_proxy = False, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | cwd = None, |
| 134 | gitdir = None): |
Shawn O. Pearce | 727ee98 | 2010-12-07 08:46:14 -0800 | [diff] [blame] | 135 | env = os.environ.copy() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 137 | for key in [REPO_TRACE, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | GIT_DIR, |
| 139 | 'GIT_ALTERNATE_OBJECT_DIRECTORIES', |
| 140 | 'GIT_OBJECT_DIRECTORY', |
| 141 | 'GIT_WORK_TREE', |
| 142 | 'GIT_GRAFT_FILE', |
| 143 | 'GIT_INDEX_FILE']: |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 144 | if key in env: |
| 145 | del env[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 146 | |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 147 | # If we are not capturing std* then need to print it. |
| 148 | self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr} |
| 149 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | if disable_editor: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 151 | _setenv(env, 'GIT_EDITOR', ':') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 152 | if ssh_proxy: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 153 | _setenv(env, 'REPO_SSH_SOCK', ssh_sock()) |
| 154 | _setenv(env, 'GIT_SSH', _ssh_proxy()) |
Jonathan Nieder | c00d28b | 2017-10-19 14:23:10 -0700 | [diff] [blame^] | 155 | _setenv(env, 'GIT_SSH_VARIANT', 'ssh') |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 156 | if 'http_proxy' in env and 'darwin' == sys.platform: |
Shawn O. Pearce | 337aee0 | 2012-06-13 10:40:46 -0700 | [diff] [blame] | 157 | s = "'http.proxy=%s'" % (env['http_proxy'],) |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 158 | p = env.get('GIT_CONFIG_PARAMETERS') |
| 159 | if p is not None: |
| 160 | s = p + ' ' + s |
| 161 | _setenv(env, 'GIT_CONFIG_PARAMETERS', s) |
Dan Willemsen | 466b8c4 | 2015-11-25 13:26:39 -0800 | [diff] [blame] | 162 | if 'GIT_ALLOW_PROTOCOL' not in env: |
| 163 | _setenv(env, 'GIT_ALLOW_PROTOCOL', |
Jonathan Nieder | 203153e | 2016-02-26 18:53:54 -0800 | [diff] [blame] | 164 | '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] | 165 | |
| 166 | if project: |
| 167 | if not cwd: |
| 168 | cwd = project.worktree |
| 169 | if not gitdir: |
| 170 | gitdir = project.gitdir |
| 171 | |
| 172 | command = [GIT] |
| 173 | if bare: |
| 174 | if gitdir: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 175 | _setenv(env, GIT_DIR, gitdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 176 | cwd = None |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 177 | command.append(cmdv[0]) |
| 178 | # Need to use the --progress flag for fetch/clone so output will be |
| 179 | # displayed as by default git only does progress output if stderr is a TTY. |
| 180 | if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'): |
| 181 | if '--progress' not in cmdv and '--quiet' not in cmdv: |
| 182 | command.append('--progress') |
| 183 | command.extend(cmdv[1:]) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | |
| 185 | if provide_stdin: |
| 186 | stdin = subprocess.PIPE |
| 187 | else: |
| 188 | stdin = None |
| 189 | |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 190 | stdout = subprocess.PIPE |
| 191 | stderr = subprocess.PIPE |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 193 | if IsTrace(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 194 | global LAST_CWD |
| 195 | global LAST_GITDIR |
| 196 | |
| 197 | dbg = '' |
| 198 | |
| 199 | if cwd and LAST_CWD != cwd: |
| 200 | if LAST_GITDIR or LAST_CWD: |
| 201 | dbg += '\n' |
| 202 | dbg += ': cd %s\n' % cwd |
| 203 | LAST_CWD = cwd |
| 204 | |
| 205 | if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]: |
| 206 | if LAST_GITDIR or LAST_CWD: |
| 207 | dbg += '\n' |
| 208 | dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR] |
| 209 | LAST_GITDIR = env[GIT_DIR] |
| 210 | |
| 211 | dbg += ': ' |
| 212 | dbg += ' '.join(command) |
| 213 | if stdin == subprocess.PIPE: |
| 214 | dbg += ' 0<|' |
| 215 | if stdout == subprocess.PIPE: |
| 216 | dbg += ' 1>|' |
| 217 | if stderr == subprocess.PIPE: |
| 218 | dbg += ' 2>|' |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 219 | Trace('%s', dbg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | |
| 221 | try: |
| 222 | p = subprocess.Popen(command, |
| 223 | cwd = cwd, |
| 224 | env = env, |
| 225 | stdin = stdin, |
| 226 | stdout = stdout, |
| 227 | stderr = stderr) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 228 | except Exception as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 229 | raise GitError('%s: %s' % (command[1], e)) |
| 230 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 231 | if ssh_proxy: |
| 232 | _add_ssh_client(p) |
| 233 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 234 | self.process = p |
| 235 | self.stdin = p.stdin |
| 236 | |
| 237 | def Wait(self): |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 238 | try: |
Ulrik Sjölin | 498fe90 | 2011-09-11 22:59:37 +0200 | [diff] [blame] | 239 | p = self.process |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 240 | rc = self._CaptureOutput() |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 241 | finally: |
| 242 | _remove_ssh_client(p) |
| 243 | return rc |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 244 | |
| 245 | def _CaptureOutput(self): |
| 246 | p = self.process |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 247 | s_in = platform_utils.FileDescriptorStreams.create() |
| 248 | s_in.add(p.stdout, sys.stdout, 'stdout') |
| 249 | s_in.add(p.stderr, sys.stderr, 'stderr') |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 250 | self.stdout = '' |
| 251 | self.stderr = '' |
| 252 | |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 253 | while not s_in.is_done: |
| 254 | in_ready = s_in.select() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 255 | for s in in_ready: |
Renaud Paquay | 2e70291 | 2016-11-01 11:23:38 -0700 | [diff] [blame] | 256 | buf = s.read() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 257 | if not buf: |
| 258 | s_in.remove(s) |
| 259 | continue |
Anthony King | 6cfc68e | 2015-06-03 16:39:32 +0100 | [diff] [blame] | 260 | if not hasattr(buf, 'encode'): |
| 261 | buf = buf.decode() |
John L. Villalovos | 9c76f67 | 2015-03-16 20:49:10 -0700 | [diff] [blame] | 262 | if s.std_name == 'stdout': |
| 263 | self.stdout += buf |
| 264 | else: |
| 265 | self.stderr += buf |
| 266 | if self.tee[s.std_name]: |
| 267 | s.dest.write(buf) |
| 268 | s.dest.flush() |
| 269 | return p.wait() |