blob: 3095fda1f84f38cb6ff5ed2241c39ba0967f97b3 [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
John L. Villalovos9c76f672015-03-16 20:49:10 -070017import fcntl
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070018import os
John L. Villalovos9c76f672015-03-16 20:49:10 -070019import select
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070020import sys
21import subprocess
Shawn O. Pearcefb231612009-04-10 18:53:46 -070022import tempfile
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -070023from signal import SIGTERM
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070024from error import GitError
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070025from trace import REPO_TRACE, IsTrace, Trace
Conley Owensff0a3c82014-01-30 14:46:03 -080026from wrapper import Wrapper
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
28GIT = 'git'
29MIN_GIT_VERSION = (1, 5, 4)
30GIT_DIR = 'GIT_DIR'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070031
32LAST_GITDIR = None
33LAST_CWD = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070034
Shawn O. Pearcefb231612009-04-10 18:53:46 -070035_ssh_proxy_path = None
36_ssh_sock_path = None
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -070037_ssh_clients = []
Shawn O. Pearcefb231612009-04-10 18:53:46 -070038
Nico Sallembien1c85f4e2010-04-27 14:35:27 -070039def ssh_sock(create=True):
Shawn O. Pearcefb231612009-04-10 18:53:46 -070040 global _ssh_sock_path
41 if _ssh_sock_path is None:
42 if not create:
43 return None
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +020044 tmp_dir = '/tmp'
45 if not os.path.exists(tmp_dir):
46 tmp_dir = tempfile.gettempdir()
Shawn O. Pearcefb231612009-04-10 18:53:46 -070047 _ssh_sock_path = os.path.join(
Mickaël Salaün2f6ab7f2012-09-30 00:37:55 +020048 tempfile.mkdtemp('', 'ssh-', tmp_dir),
Shawn O. Pearcefb231612009-04-10 18:53:46 -070049 'master-%r@%h:%p')
50 return _ssh_sock_path
51
52def _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. Pearceca8c32c2010-05-11 18:21:33 -070060def _add_ssh_client(p):
61 _ssh_clients.append(p)
62
63def _remove_ssh_client(p):
64 try:
65 _ssh_clients.remove(p)
66 except ValueError:
67 pass
68
69def 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 Projectcf31fe92008-10-21 07:00:00 -070078
Shawn O. Pearce334851e2011-09-19 08:05:31 -070079_git_version = None
80
John L. Villalovos9c76f672015-03-16 20:49:10 -070081class _sfd(object):
82 """select file descriptor class"""
83 def __init__(self, fd, dest, std_name):
84 assert std_name in ('stdout', 'stderr')
85 self.fd = fd
86 self.dest = dest
87 self.std_name = std_name
88 def fileno(self):
89 return self.fd.fileno()
90
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070091class _GitCall(object):
92 def version(self):
93 p = GitCommand(None, ['--version'], capture_stdout=True)
94 if p.Wait() == 0:
Chirayu Desaic46de692014-08-20 09:34:10 +053095 return p.stdout.decode('utf-8')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070096 return None
97
Shawn O. Pearce334851e2011-09-19 08:05:31 -070098 def version_tuple(self):
99 global _git_version
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700100 if _git_version is None:
Chirayu Desaic46de692014-08-20 09:34:10 +0530101 ver_str = git.version()
Conley Owensff0a3c82014-01-30 14:46:03 -0800102 _git_version = Wrapper().ParseGitVersion(ver_str)
103 if _git_version is None:
Sarah Owenscecd1d82012-11-01 22:59:27 -0700104 print('fatal: "%s" unsupported' % ver_str, file=sys.stderr)
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700105 sys.exit(1)
106 return _git_version
107
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700108 def __getattr__(self, name):
109 name = name.replace('_','-')
110 def fun(*cmdv):
111 command = [name]
112 command.extend(cmdv)
113 return GitCommand(None, command).Wait() == 0
114 return fun
115git = _GitCall()
116
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700117def git_require(min_version, fail=False):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700118 git_version = git.version_tuple()
119 if min_version <= git_version:
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700120 return True
121 if fail:
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900122 need = '.'.join(map(str, min_version))
Sarah Owenscecd1d82012-11-01 22:59:27 -0700123 print('fatal: git %s or later required' % need, file=sys.stderr)
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700124 sys.exit(1)
125 return False
126
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800127def _setenv(env, name, value):
128 env[name] = value.encode()
129
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700130class GitCommand(object):
131 def __init__(self,
132 project,
133 cmdv,
134 bare = False,
135 provide_stdin = False,
136 capture_stdout = False,
137 capture_stderr = False,
138 disable_editor = False,
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700139 ssh_proxy = False,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700140 cwd = None,
141 gitdir = None):
Shawn O. Pearce727ee982010-12-07 08:46:14 -0800142 env = os.environ.copy()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700143
David Pursehouse1d947b32012-10-25 12:23:11 +0900144 for key in [REPO_TRACE,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700145 GIT_DIR,
146 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
147 'GIT_OBJECT_DIRECTORY',
148 'GIT_WORK_TREE',
149 'GIT_GRAFT_FILE',
150 'GIT_INDEX_FILE']:
David Pursehouse1d947b32012-10-25 12:23:11 +0900151 if key in env:
152 del env[key]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700153
John L. Villalovos9c76f672015-03-16 20:49:10 -0700154 # If we are not capturing std* then need to print it.
155 self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}
156
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700157 if disable_editor:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800158 _setenv(env, 'GIT_EDITOR', ':')
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700159 if ssh_proxy:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800160 _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
161 _setenv(env, 'GIT_SSH', _ssh_proxy())
Shawn O. Pearce62d0b102012-06-05 15:11:15 -0700162 if 'http_proxy' in env and 'darwin' == sys.platform:
Shawn O. Pearce337aee02012-06-13 10:40:46 -0700163 s = "'http.proxy=%s'" % (env['http_proxy'],)
Shawn O. Pearce62d0b102012-06-05 15:11:15 -0700164 p = env.get('GIT_CONFIG_PARAMETERS')
165 if p is not None:
166 s = p + ' ' + s
167 _setenv(env, 'GIT_CONFIG_PARAMETERS', s)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700168
169 if project:
170 if not cwd:
171 cwd = project.worktree
172 if not gitdir:
173 gitdir = project.gitdir
174
175 command = [GIT]
176 if bare:
177 if gitdir:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800178 _setenv(env, GIT_DIR, gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700179 cwd = None
John L. Villalovos9c76f672015-03-16 20:49:10 -0700180 command.append(cmdv[0])
181 # Need to use the --progress flag for fetch/clone so output will be
182 # displayed as by default git only does progress output if stderr is a TTY.
183 if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
184 if '--progress' not in cmdv and '--quiet' not in cmdv:
185 command.append('--progress')
186 command.extend(cmdv[1:])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700187
188 if provide_stdin:
189 stdin = subprocess.PIPE
190 else:
191 stdin = None
192
John L. Villalovos9c76f672015-03-16 20:49:10 -0700193 stdout = subprocess.PIPE
194 stderr = subprocess.PIPE
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700195
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700196 if IsTrace():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700197 global LAST_CWD
198 global LAST_GITDIR
199
200 dbg = ''
201
202 if cwd and LAST_CWD != cwd:
203 if LAST_GITDIR or LAST_CWD:
204 dbg += '\n'
205 dbg += ': cd %s\n' % cwd
206 LAST_CWD = cwd
207
208 if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
209 if LAST_GITDIR or LAST_CWD:
210 dbg += '\n'
211 dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
212 LAST_GITDIR = env[GIT_DIR]
213
214 dbg += ': '
215 dbg += ' '.join(command)
216 if stdin == subprocess.PIPE:
217 dbg += ' 0<|'
218 if stdout == subprocess.PIPE:
219 dbg += ' 1>|'
220 if stderr == subprocess.PIPE:
221 dbg += ' 2>|'
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700222 Trace('%s', dbg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700223
224 try:
225 p = subprocess.Popen(command,
226 cwd = cwd,
227 env = env,
228 stdin = stdin,
229 stdout = stdout,
230 stderr = stderr)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700231 except Exception as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700232 raise GitError('%s: %s' % (command[1], e))
233
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700234 if ssh_proxy:
235 _add_ssh_client(p)
236
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700237 self.process = p
238 self.stdin = p.stdin
239
240 def Wait(self):
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700241 try:
Ulrik Sjölin498fe902011-09-11 22:59:37 +0200242 p = self.process
John L. Villalovos9c76f672015-03-16 20:49:10 -0700243 rc = self._CaptureOutput()
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700244 finally:
245 _remove_ssh_client(p)
246 return rc
John L. Villalovos9c76f672015-03-16 20:49:10 -0700247
248 def _CaptureOutput(self):
249 p = self.process
250 s_in = [_sfd(p.stdout, sys.stdout, 'stdout'),
251 _sfd(p.stderr, sys.stderr, 'stderr')]
252 self.stdout = ''
253 self.stderr = ''
254
255 for s in s_in:
256 flags = fcntl.fcntl(s.fd, fcntl.F_GETFL)
257 fcntl.fcntl(s.fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)
258
259 while s_in:
260 in_ready, _, _ = select.select(s_in, [], [])
261 for s in in_ready:
262 buf = s.fd.read(4096)
263 if not buf:
264 s_in.remove(s)
265 continue
Anthony King6cfc68e2015-06-03 16:39:32 +0100266 if not hasattr(buf, 'encode'):
267 buf = buf.decode()
John L. Villalovos9c76f672015-03-16 20:49:10 -0700268 if s.std_name == 'stdout':
269 self.stdout += buf
270 else:
271 self.stderr += buf
272 if self.tee[s.std_name]:
273 s.dest.write(buf)
274 s.dest.flush()
275 return p.wait()