blob: 53b3e75c8259d2fbca04bcbce435fd1252dc483b [file] [log] [blame]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -07001#
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 Owenscecd1d82012-11-01 22:59:27 -070016from __future__ import print_function
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070017import os
18import sys
19import subprocess
Shawn O. Pearcefb231612009-04-10 18:53:46 -070020import tempfile
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -070021from signal import SIGTERM
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070022from error import GitError
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070023from trace import REPO_TRACE, IsTrace, Trace
Conley Owensff0a3c82014-01-30 14:46:03 -080024from wrapper import Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070025
26GIT = 'git'
27MIN_GIT_VERSION = (1, 5, 4)
28GIT_DIR = 'GIT_DIR'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070029
30LAST_GITDIR = None
31LAST_CWD = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070032
Shawn O. Pearcefb231612009-04-10 18:53:46 -070033_ssh_proxy_path = None
34_ssh_sock_path = None
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -070035_ssh_clients = []
Shawn O. Pearcefb231612009-04-10 18:53:46 -070036
Nico Sallembien1c85f4e2010-04-27 14:35:27 -070037def ssh_sock(create=True):
Shawn O. Pearcefb231612009-04-10 18:53:46 -070038 global _ssh_sock_path
39 if _ssh_sock_path is None:
40 if not create:
41 return None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +020042 tmp_dir = '/tmp'
43 if not os.path.exists(tmp_dir):
44 tmp_dir = tempfile.gettempdir()
Shawn O. Pearcefb231612009-04-10 18:53:46 -070045 _ssh_sock_path = os.path.join(
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +020046 tempfile.mkdtemp('', 'ssh-', tmp_dir),
Shawn O. Pearcefb231612009-04-10 18:53:46 -070047 'master-%r@%h:%p')
48 return _ssh_sock_path
49
50def _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. Pearceca8c32c2010-05-11 18:21:33 -070058def _add_ssh_client(p):
59 _ssh_clients.append(p)
60
61def _remove_ssh_client(p):
62 try:
63 _ssh_clients.remove(p)
64 except ValueError:
65 pass
66
67def 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 Projectcf31fe92008-10-21 07:00:00 -070076
Shawn O. Pearce334851e2011-09-19 08:05:31 -070077_git_version = None
78
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070079class _GitCall(object):
80 def version(self):
81 p = GitCommand(None, ['--version'], capture_stdout=True)
82 if p.Wait() == 0:
Chirayu Desaic46de692014-08-20 09:34:10 +053083 return p.stdout.decode('utf-8')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070084 return None
85
Shawn O. Pearce334851e2011-09-19 08:05:31 -070086 def version_tuple(self):
87 global _git_version
Shawn O. Pearce334851e2011-09-19 08:05:31 -070088 if _git_version is None:
Chirayu Desaic46de692014-08-20 09:34:10 +053089 ver_str = git.version()
Conley Owensff0a3c82014-01-30 14:46:03 -080090 _git_version = Wrapper().ParseGitVersion(ver_str)
91 if _git_version is None:
Sarah Owenscecd1d82012-11-01 22:59:27 -070092 print('fatal: "%s" unsupported' % ver_str, file=sys.stderr)
Shawn O. Pearce334851e2011-09-19 08:05:31 -070093 sys.exit(1)
94 return _git_version
95
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096 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
103git = _GitCall()
104
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700105def git_require(min_version, fail=False):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700106 git_version = git.version_tuple()
107 if min_version <= git_version:
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700108 return True
109 if fail:
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900110 need = '.'.join(map(str, min_version))
Sarah Owenscecd1d82012-11-01 22:59:27 -0700111 print('fatal: git %s or later required' % need, file=sys.stderr)
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700112 sys.exit(1)
113 return False
114
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800115def _setenv(env, name, value):
116 env[name] = value.encode()
117
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700118class 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. Pearcefb231612009-04-10 18:53:46 -0700127 ssh_proxy = False,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700128 cwd = None,
129 gitdir = None):
Shawn O. Pearce727ee982010-12-07 08:46:14 -0800130 env = os.environ.copy()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131
David Pursehouse1d947b32012-10-25 12:23:11 +0900132 for key in [REPO_TRACE,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 GIT_DIR,
134 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
135 'GIT_OBJECT_DIRECTORY',
136 'GIT_WORK_TREE',
137 'GIT_GRAFT_FILE',
138 'GIT_INDEX_FILE']:
David Pursehouse1d947b32012-10-25 12:23:11 +0900139 if key in env:
140 del env[key]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700141
142 if disable_editor:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800143 _setenv(env, 'GIT_EDITOR', ':')
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700144 if ssh_proxy:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800145 _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
146 _setenv(env, 'GIT_SSH', _ssh_proxy())
Shawn O. Pearce62d0b102012-06-05 15:11:15 -0700147 if 'http_proxy' in env and 'darwin' == sys.platform:
Shawn O. Pearce337aee02012-06-13 10:40:46 -0700148 s = "'http.proxy=%s'" % (env['http_proxy'],)
Shawn O. Pearce62d0b102012-06-05 15:11:15 -0700149 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 Projectcf31fe92008-10-21 07:00:00 -0700153
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. Pearcef18cb762010-12-07 11:41:05 -0800163 _setenv(env, GIT_DIR, gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164 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. Pearcead3193a2009-04-18 09:54:51 -0700182 if IsTrace():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700183 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. Pearcead3193a2009-04-18 09:54:51 -0700208 Trace('%s', dbg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700209
210 try:
211 p = subprocess.Popen(command,
212 cwd = cwd,
213 env = env,
214 stdin = stdin,
215 stdout = stdout,
216 stderr = stderr)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700217 except Exception as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700218 raise GitError('%s: %s' % (command[1], e))
219
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700220 if ssh_proxy:
221 _add_ssh_client(p)
222
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700223 self.process = p
224 self.stdin = p.stdin
225
226 def Wait(self):
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700227 try:
Ulrik Sjölin498fe902011-09-11 22:59:37 +0200228 p = self.process
229 (self.stdout, self.stderr) = p.communicate()
230 rc = p.returncode
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700231 finally:
232 _remove_ssh_client(p)
233 return rc