blob: dfa6a9248a3f0ac669b67f7bae72074790ef5465 [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
Renaud Paquay2e702912016-11-01 11:23:38 -070022
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023from error import GitError
Renaud Paquay2e702912016-11-01 11:23:38 -070024import platform_utils
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
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070081class _GitCall(object):
82 def version(self):
83 p = GitCommand(None, ['--version'], capture_stdout=True)
84 if p.Wait() == 0:
Anthony Kingcf738ed2015-06-03 16:50:39 +010085 if hasattr(p.stdout, 'decode'):
86 return p.stdout.decode('utf-8')
87 else:
88 return p.stdout
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070089 return None
90
Shawn O. Pearce334851e2011-09-19 08:05:31 -070091 def version_tuple(self):
92 global _git_version
Shawn O. Pearce334851e2011-09-19 08:05:31 -070093 if _git_version is None:
Chirayu Desaic46de692014-08-20 09:34:10 +053094 ver_str = git.version()
Conley Owensff0a3c82014-01-30 14:46:03 -080095 _git_version = Wrapper().ParseGitVersion(ver_str)
96 if _git_version is None:
Sarah Owenscecd1d82012-11-01 22:59:27 -070097 print('fatal: "%s" unsupported' % ver_str, file=sys.stderr)
Shawn O. Pearce334851e2011-09-19 08:05:31 -070098 sys.exit(1)
99 return _git_version
100
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700101 def __getattr__(self, name):
102 name = name.replace('_','-')
103 def fun(*cmdv):
104 command = [name]
105 command.extend(cmdv)
106 return GitCommand(None, command).Wait() == 0
107 return fun
108git = _GitCall()
109
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700110def git_require(min_version, fail=False):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700111 git_version = git.version_tuple()
112 if min_version <= git_version:
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700113 return True
114 if fail:
David Pursehouse7e6dd2d2012-10-25 12:40:51 +0900115 need = '.'.join(map(str, min_version))
Sarah Owenscecd1d82012-11-01 22:59:27 -0700116 print('fatal: git %s or later required' % need, file=sys.stderr)
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700117 sys.exit(1)
118 return False
119
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800120def _setenv(env, name, value):
121 env[name] = value.encode()
122
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700123class GitCommand(object):
124 def __init__(self,
125 project,
126 cmdv,
127 bare = False,
128 provide_stdin = False,
129 capture_stdout = False,
130 capture_stderr = False,
131 disable_editor = False,
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700132 ssh_proxy = False,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700133 cwd = None,
134 gitdir = None):
Shawn O. Pearce727ee982010-12-07 08:46:14 -0800135 env = os.environ.copy()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700136
David Pursehouse1d947b32012-10-25 12:23:11 +0900137 for key in [REPO_TRACE,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700138 GIT_DIR,
139 'GIT_ALTERNATE_OBJECT_DIRECTORIES',
140 'GIT_OBJECT_DIRECTORY',
141 'GIT_WORK_TREE',
142 'GIT_GRAFT_FILE',
143 'GIT_INDEX_FILE']:
David Pursehouse1d947b32012-10-25 12:23:11 +0900144 if key in env:
145 del env[key]
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700146
John L. Villalovos9c76f672015-03-16 20:49:10 -0700147 # If we are not capturing std* then need to print it.
148 self.tee = {'stdout': not capture_stdout, 'stderr': not capture_stderr}
149
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150 if disable_editor:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800151 _setenv(env, 'GIT_EDITOR', ':')
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700152 if ssh_proxy:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800153 _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
154 _setenv(env, 'GIT_SSH', _ssh_proxy())
Shawn O. Pearce62d0b102012-06-05 15:11:15 -0700155 if 'http_proxy' in env and 'darwin' == sys.platform:
Shawn O. Pearce337aee02012-06-13 10:40:46 -0700156 s = "'http.proxy=%s'" % (env['http_proxy'],)
Shawn O. Pearce62d0b102012-06-05 15:11:15 -0700157 p = env.get('GIT_CONFIG_PARAMETERS')
158 if p is not None:
159 s = p + ' ' + s
160 _setenv(env, 'GIT_CONFIG_PARAMETERS', s)
Dan Willemsen466b8c42015-11-25 13:26:39 -0800161 if 'GIT_ALLOW_PROTOCOL' not in env:
162 _setenv(env, 'GIT_ALLOW_PROTOCOL',
Jonathan Nieder203153e2016-02-26 18:53:54 -0800163 'file:git:http:https:ssh:persistent-http:persistent-https:sso:rpc')
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700164
165 if project:
166 if not cwd:
167 cwd = project.worktree
168 if not gitdir:
169 gitdir = project.gitdir
170
171 command = [GIT]
172 if bare:
173 if gitdir:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800174 _setenv(env, GIT_DIR, gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700175 cwd = None
John L. Villalovos9c76f672015-03-16 20:49:10 -0700176 command.append(cmdv[0])
177 # Need to use the --progress flag for fetch/clone so output will be
178 # displayed as by default git only does progress output if stderr is a TTY.
179 if sys.stderr.isatty() and cmdv[0] in ('fetch', 'clone'):
180 if '--progress' not in cmdv and '--quiet' not in cmdv:
181 command.append('--progress')
182 command.extend(cmdv[1:])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700183
184 if provide_stdin:
185 stdin = subprocess.PIPE
186 else:
187 stdin = None
188
John L. Villalovos9c76f672015-03-16 20:49:10 -0700189 stdout = subprocess.PIPE
190 stderr = subprocess.PIPE
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700191
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700192 if IsTrace():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700193 global LAST_CWD
194 global LAST_GITDIR
195
196 dbg = ''
197
198 if cwd and LAST_CWD != cwd:
199 if LAST_GITDIR or LAST_CWD:
200 dbg += '\n'
201 dbg += ': cd %s\n' % cwd
202 LAST_CWD = cwd
203
204 if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
205 if LAST_GITDIR or LAST_CWD:
206 dbg += '\n'
207 dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
208 LAST_GITDIR = env[GIT_DIR]
209
210 dbg += ': '
211 dbg += ' '.join(command)
212 if stdin == subprocess.PIPE:
213 dbg += ' 0<|'
214 if stdout == subprocess.PIPE:
215 dbg += ' 1>|'
216 if stderr == subprocess.PIPE:
217 dbg += ' 2>|'
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700218 Trace('%s', dbg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700219
220 try:
221 p = subprocess.Popen(command,
222 cwd = cwd,
223 env = env,
224 stdin = stdin,
225 stdout = stdout,
226 stderr = stderr)
Sarah Owensa5be53f2012-09-09 15:37:57 -0700227 except Exception as e:
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700228 raise GitError('%s: %s' % (command[1], e))
229
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700230 if ssh_proxy:
231 _add_ssh_client(p)
232
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700233 self.process = p
234 self.stdin = p.stdin
235
236 def Wait(self):
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700237 try:
Ulrik Sjölin498fe902011-09-11 22:59:37 +0200238 p = self.process
John L. Villalovos9c76f672015-03-16 20:49:10 -0700239 rc = self._CaptureOutput()
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700240 finally:
241 _remove_ssh_client(p)
242 return rc
John L. Villalovos9c76f672015-03-16 20:49:10 -0700243
244 def _CaptureOutput(self):
245 p = self.process
Renaud Paquay2e702912016-11-01 11:23:38 -0700246 s_in = platform_utils.FileDescriptorStreams.create()
247 s_in.add(p.stdout, sys.stdout, 'stdout')
248 s_in.add(p.stderr, sys.stderr, 'stderr')
John L. Villalovos9c76f672015-03-16 20:49:10 -0700249 self.stdout = ''
250 self.stderr = ''
251
Renaud Paquay2e702912016-11-01 11:23:38 -0700252 while not s_in.is_done:
253 in_ready = s_in.select()
John L. Villalovos9c76f672015-03-16 20:49:10 -0700254 for s in in_ready:
Renaud Paquay2e702912016-11-01 11:23:38 -0700255 buf = s.read()
John L. Villalovos9c76f672015-03-16 20:49:10 -0700256 if not buf:
257 s_in.remove(s)
258 continue
Anthony King6cfc68e2015-06-03 16:39:32 +0100259 if not hasattr(buf, 'encode'):
260 buf = buf.decode()
John L. Villalovos9c76f672015-03-16 20:49:10 -0700261 if s.std_name == 'stdout':
262 self.stdout += buf
263 else:
264 self.stderr += buf
265 if self.tee[s.std_name]:
266 s.dest.write(buf)
267 s.dest.flush()
268 return p.wait()