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