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 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 22 | from error import GitError |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 23 | from 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' |
| 27 | MIN_GIT_VERSION = (1, 5, 4) |
| 28 | GIT_DIR = 'GIT_DIR' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | |
| 30 | LAST_GITDIR = None |
| 31 | LAST_CWD = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 32 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 33 | _ssh_proxy_path = None |
| 34 | _ssh_sock_path = None |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 35 | _ssh_clients = [] |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 36 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 37 | def ssh_sock(create=True): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 38 | global _ssh_sock_path |
| 39 | if _ssh_sock_path is None: |
| 40 | if not create: |
| 41 | return None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 42 | tmp_dir = '/tmp' |
| 43 | if not os.path.exists(tmp_dir): |
| 44 | tmp_dir = tempfile.gettempdir() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 45 | _ssh_sock_path = os.path.join( |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 46 | tempfile.mkdtemp('', 'ssh-', tmp_dir), |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 47 | 'master-%r@%h:%p') |
| 48 | return _ssh_sock_path |
| 49 | |
| 50 | def _ssh_proxy(): |
| 51 | global _ssh_proxy_path |
| 52 | if _ssh_proxy_path is None: |
| 53 | _ssh_proxy_path = os.path.join( |
| 54 | os.path.dirname(__file__), |
| 55 | 'git_ssh') |
| 56 | return _ssh_proxy_path |
| 57 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 58 | def _add_ssh_client(p): |
| 59 | _ssh_clients.append(p) |
| 60 | |
| 61 | def _remove_ssh_client(p): |
| 62 | try: |
| 63 | _ssh_clients.remove(p) |
| 64 | except ValueError: |
| 65 | pass |
| 66 | |
| 67 | def terminate_ssh_clients(): |
| 68 | global _ssh_clients |
| 69 | for p in _ssh_clients: |
| 70 | try: |
| 71 | os.kill(p.pid, SIGTERM) |
| 72 | p.wait() |
| 73 | except OSError: |
| 74 | pass |
| 75 | _ssh_clients = [] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 77 | _git_version = None |
| 78 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 79 | class _GitCall(object): |
| 80 | def version(self): |
| 81 | p = GitCommand(None, ['--version'], capture_stdout=True) |
| 82 | if p.Wait() == 0: |
Chirayu Desai | c46de69 | 2014-08-20 09:34:10 +0530 | [diff] [blame^] | 83 | return p.stdout.decode('utf-8') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | return None |
| 85 | |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 86 | def version_tuple(self): |
| 87 | global _git_version |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 88 | if _git_version is None: |
Chirayu Desai | c46de69 | 2014-08-20 09:34:10 +0530 | [diff] [blame^] | 89 | ver_str = git.version() |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 90 | _git_version = Wrapper().ParseGitVersion(ver_str) |
| 91 | if _git_version is None: |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 92 | print('fatal: "%s" unsupported' % ver_str, file=sys.stderr) |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 93 | sys.exit(1) |
| 94 | return _git_version |
| 95 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 96 | def __getattr__(self, name): |
| 97 | name = name.replace('_','-') |
| 98 | def fun(*cmdv): |
| 99 | command = [name] |
| 100 | command.extend(cmdv) |
| 101 | return GitCommand(None, command).Wait() == 0 |
| 102 | return fun |
| 103 | git = _GitCall() |
| 104 | |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 105 | def git_require(min_version, fail=False): |
Shawn O. Pearce | 334851e | 2011-09-19 08:05:31 -0700 | [diff] [blame] | 106 | git_version = git.version_tuple() |
| 107 | if min_version <= git_version: |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 108 | return True |
| 109 | if fail: |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 110 | need = '.'.join(map(str, min_version)) |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 111 | print('fatal: git %s or later required' % need, file=sys.stderr) |
Shawn O. Pearce | 2ec00b9 | 2009-06-12 09:32:50 -0700 | [diff] [blame] | 112 | sys.exit(1) |
| 113 | return False |
| 114 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 115 | def _setenv(env, name, value): |
| 116 | env[name] = value.encode() |
| 117 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | class GitCommand(object): |
| 119 | def __init__(self, |
| 120 | project, |
| 121 | cmdv, |
| 122 | bare = False, |
| 123 | provide_stdin = False, |
| 124 | capture_stdout = False, |
| 125 | capture_stderr = False, |
| 126 | disable_editor = False, |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 127 | ssh_proxy = False, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 128 | cwd = None, |
| 129 | gitdir = None): |
Shawn O. Pearce | 727ee98 | 2010-12-07 08:46:14 -0800 | [diff] [blame] | 130 | env = os.environ.copy() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 132 | for key in [REPO_TRACE, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | GIT_DIR, |
| 134 | 'GIT_ALTERNATE_OBJECT_DIRECTORIES', |
| 135 | 'GIT_OBJECT_DIRECTORY', |
| 136 | 'GIT_WORK_TREE', |
| 137 | 'GIT_GRAFT_FILE', |
| 138 | 'GIT_INDEX_FILE']: |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 139 | if key in env: |
| 140 | del env[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | |
| 142 | if disable_editor: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 143 | _setenv(env, 'GIT_EDITOR', ':') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 144 | if ssh_proxy: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 145 | _setenv(env, 'REPO_SSH_SOCK', ssh_sock()) |
| 146 | _setenv(env, 'GIT_SSH', _ssh_proxy()) |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 147 | if 'http_proxy' in env and 'darwin' == sys.platform: |
Shawn O. Pearce | 337aee0 | 2012-06-13 10:40:46 -0700 | [diff] [blame] | 148 | s = "'http.proxy=%s'" % (env['http_proxy'],) |
Shawn O. Pearce | 62d0b10 | 2012-06-05 15:11:15 -0700 | [diff] [blame] | 149 | p = env.get('GIT_CONFIG_PARAMETERS') |
| 150 | if p is not None: |
| 151 | s = p + ' ' + s |
| 152 | _setenv(env, 'GIT_CONFIG_PARAMETERS', s) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | |
| 154 | if project: |
| 155 | if not cwd: |
| 156 | cwd = project.worktree |
| 157 | if not gitdir: |
| 158 | gitdir = project.gitdir |
| 159 | |
| 160 | command = [GIT] |
| 161 | if bare: |
| 162 | if gitdir: |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 163 | _setenv(env, GIT_DIR, gitdir) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 164 | cwd = None |
| 165 | command.extend(cmdv) |
| 166 | |
| 167 | if provide_stdin: |
| 168 | stdin = subprocess.PIPE |
| 169 | else: |
| 170 | stdin = None |
| 171 | |
| 172 | if capture_stdout: |
| 173 | stdout = subprocess.PIPE |
| 174 | else: |
| 175 | stdout = None |
| 176 | |
| 177 | if capture_stderr: |
| 178 | stderr = subprocess.PIPE |
| 179 | else: |
| 180 | stderr = None |
| 181 | |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 182 | if IsTrace(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 183 | global LAST_CWD |
| 184 | global LAST_GITDIR |
| 185 | |
| 186 | dbg = '' |
| 187 | |
| 188 | if cwd and LAST_CWD != cwd: |
| 189 | if LAST_GITDIR or LAST_CWD: |
| 190 | dbg += '\n' |
| 191 | dbg += ': cd %s\n' % cwd |
| 192 | LAST_CWD = cwd |
| 193 | |
| 194 | if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]: |
| 195 | if LAST_GITDIR or LAST_CWD: |
| 196 | dbg += '\n' |
| 197 | dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR] |
| 198 | LAST_GITDIR = env[GIT_DIR] |
| 199 | |
| 200 | dbg += ': ' |
| 201 | dbg += ' '.join(command) |
| 202 | if stdin == subprocess.PIPE: |
| 203 | dbg += ' 0<|' |
| 204 | if stdout == subprocess.PIPE: |
| 205 | dbg += ' 1>|' |
| 206 | if stderr == subprocess.PIPE: |
| 207 | dbg += ' 2>|' |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 208 | Trace('%s', dbg) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 209 | |
| 210 | try: |
| 211 | p = subprocess.Popen(command, |
| 212 | cwd = cwd, |
| 213 | env = env, |
| 214 | stdin = stdin, |
| 215 | stdout = stdout, |
| 216 | stderr = stderr) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 217 | except Exception as e: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 218 | raise GitError('%s: %s' % (command[1], e)) |
| 219 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 220 | if ssh_proxy: |
| 221 | _add_ssh_client(p) |
| 222 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 223 | self.process = p |
| 224 | self.stdin = p.stdin |
| 225 | |
| 226 | def Wait(self): |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 227 | try: |
Ulrik Sjölin | 498fe90 | 2011-09-11 22:59:37 +0200 | [diff] [blame] | 228 | p = self.process |
| 229 | (self.stdout, self.stderr) = p.communicate() |
| 230 | rc = p.returncode |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 231 | finally: |
| 232 | _remove_ssh_client(p) |
| 233 | return rc |