blob: ea0053e35521a281958c87900f495b7e662bbebd [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
16import os
17import sys
18import subprocess
Shawn O. Pearcefb231612009-04-10 18:53:46 -070019import tempfile
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -070020from signal import SIGTERM
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070021from error import GitError
Shawn O. Pearcead3193a2009-04-18 09:54:51 -070022from trace import REPO_TRACE, IsTrace, Trace
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070023
24GIT = 'git'
25MIN_GIT_VERSION = (1, 5, 4)
26GIT_DIR = 'GIT_DIR'
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070027
28LAST_GITDIR = None
29LAST_CWD = None
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070030
Shawn O. Pearcefb231612009-04-10 18:53:46 -070031_ssh_proxy_path = None
32_ssh_sock_path = None
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -070033_ssh_clients = []
Shawn O. Pearcefb231612009-04-10 18:53:46 -070034
Nico Sallembien1c85f4e2010-04-27 14:35:27 -070035def ssh_sock(create=True):
Shawn O. Pearcefb231612009-04-10 18:53:46 -070036 global _ssh_sock_path
37 if _ssh_sock_path is None:
38 if not create:
39 return None
Shawn O. Pearced63bbf42009-04-21 08:05:27 -070040 dir = '/tmp'
41 if not os.path.exists(dir):
42 dir = tempfile.gettempdir()
Shawn O. Pearcefb231612009-04-10 18:53:46 -070043 _ssh_sock_path = os.path.join(
Shawn O. Pearced63bbf42009-04-21 08:05:27 -070044 tempfile.mkdtemp('', 'ssh-', dir),
Shawn O. Pearcefb231612009-04-10 18:53:46 -070045 'master-%r@%h:%p')
46 return _ssh_sock_path
47
48def _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. Pearceca8c32c2010-05-11 18:21:33 -070056def _add_ssh_client(p):
57 _ssh_clients.append(p)
58
59def _remove_ssh_client(p):
60 try:
61 _ssh_clients.remove(p)
62 except ValueError:
63 pass
64
65def 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 Projectcf31fe92008-10-21 07:00:00 -070074
Shawn O. Pearce334851e2011-09-19 08:05:31 -070075_git_version = None
76
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -070077class _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. Pearce334851e2011-09-19 08:05:31 -070084 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),
92 ver_str[len('git version '):].strip().split('.')[0:3]
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 Projectcf31fe92008-10-21 07:00:00 -070099 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
106git = _GitCall()
107
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700108def git_require(min_version, fail=False):
Shawn O. Pearce334851e2011-09-19 08:05:31 -0700109 git_version = git.version_tuple()
110 if min_version <= git_version:
Shawn O. Pearce2ec00b92009-06-12 09:32:50 -0700111 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. Pearcef18cb762010-12-07 11:41:05 -0800118def _setenv(env, name, value):
119 env[name] = value.encode()
120
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700121class 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. Pearcefb231612009-04-10 18:53:46 -0700130 ssh_proxy = False,
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700131 cwd = None,
132 gitdir = None):
Shawn O. Pearce727ee982010-12-07 08:46:14 -0800133 env = os.environ.copy()
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700134
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. Pearcef18cb762010-12-07 11:41:05 -0800146 _setenv(env, 'GIT_EDITOR', ':')
Shawn O. Pearcefb231612009-04-10 18:53:46 -0700147 if ssh_proxy:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800148 _setenv(env, 'REPO_SSH_SOCK', ssh_sock())
149 _setenv(env, 'GIT_SSH', _ssh_proxy())
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700150
151 if project:
152 if not cwd:
153 cwd = project.worktree
154 if not gitdir:
155 gitdir = project.gitdir
156
157 command = [GIT]
Shawn O. Pearce9fae8052012-05-25 07:57:44 -0700158 if 'http_proxy' in env and 'darwin' == sys.platform:
159 command.extend(['-c', 'http.proxy=' + env['http_proxy']])
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700160 if bare:
161 if gitdir:
Shawn O. Pearcef18cb762010-12-07 11:41:05 -0800162 _setenv(env, GIT_DIR, gitdir)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700163 cwd = None
164 command.extend(cmdv)
165
166 if provide_stdin:
167 stdin = subprocess.PIPE
168 else:
169 stdin = None
170
171 if capture_stdout:
172 stdout = subprocess.PIPE
173 else:
174 stdout = None
175
176 if capture_stderr:
177 stderr = subprocess.PIPE
178 else:
179 stderr = None
180
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700181 if IsTrace():
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700182 global LAST_CWD
183 global LAST_GITDIR
184
185 dbg = ''
186
187 if cwd and LAST_CWD != cwd:
188 if LAST_GITDIR or LAST_CWD:
189 dbg += '\n'
190 dbg += ': cd %s\n' % cwd
191 LAST_CWD = cwd
192
193 if GIT_DIR in env and LAST_GITDIR != env[GIT_DIR]:
194 if LAST_GITDIR or LAST_CWD:
195 dbg += '\n'
196 dbg += ': export GIT_DIR=%s\n' % env[GIT_DIR]
197 LAST_GITDIR = env[GIT_DIR]
198
199 dbg += ': '
200 dbg += ' '.join(command)
201 if stdin == subprocess.PIPE:
202 dbg += ' 0<|'
203 if stdout == subprocess.PIPE:
204 dbg += ' 1>|'
205 if stderr == subprocess.PIPE:
206 dbg += ' 2>|'
Shawn O. Pearcead3193a2009-04-18 09:54:51 -0700207 Trace('%s', dbg)
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700208
209 try:
210 p = subprocess.Popen(command,
211 cwd = cwd,
212 env = env,
213 stdin = stdin,
214 stdout = stdout,
215 stderr = stderr)
216 except Exception, e:
217 raise GitError('%s: %s' % (command[1], e))
218
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700219 if ssh_proxy:
220 _add_ssh_client(p)
221
The Android Open Source Projectcf31fe92008-10-21 07:00:00 -0700222 self.process = p
223 self.stdin = p.stdin
224
225 def Wait(self):
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700226 try:
Ulrik Sjölin498fe902011-09-11 22:59:37 +0200227 p = self.process
228 (self.stdout, self.stderr) = p.communicate()
229 rc = p.returncode
Shawn O. Pearceca8c32c2010-05-11 18:21:33 -0700230 finally:
231 _remove_ssh_client(p)
232 return rc