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