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 | |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 16 | import cPickle |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | import os |
| 18 | import re |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 19 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 20 | import sys |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 21 | try: |
| 22 | import threading as _threading |
| 23 | except ImportError: |
| 24 | import dummy_threading as _threading |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 25 | import time |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 26 | try: |
| 27 | import urllib2 |
| 28 | except ImportError: |
| 29 | # For python3 |
| 30 | import urllib.request |
| 31 | import urllib.error |
| 32 | else: |
| 33 | # For python2 |
| 34 | import imp |
| 35 | urllib = imp.new_module('urllib') |
| 36 | urllib.request = urllib2 |
| 37 | urllib.error = urllib2 |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 38 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 39 | from signal import SIGTERM |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 40 | from error import GitError, UploadError |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 41 | from trace import Trace |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 42 | |
| 43 | from git_command import GitCommand |
| 44 | from git_command import ssh_sock |
| 45 | from git_command import terminate_ssh_clients |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 46 | |
| 47 | R_HEADS = 'refs/heads/' |
| 48 | R_TAGS = 'refs/tags/' |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 49 | ID_RE = re.compile(r'^[0-9a-f]{40}$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 51 | REVIEW_CACHE = dict() |
| 52 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | def IsId(rev): |
| 54 | return ID_RE.match(rev) |
| 55 | |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 56 | def _key(name): |
| 57 | parts = name.split('.') |
| 58 | if len(parts) < 2: |
| 59 | return name.lower() |
| 60 | parts[ 0] = parts[ 0].lower() |
| 61 | parts[-1] = parts[-1].lower() |
| 62 | return '.'.join(parts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 | |
| 64 | class GitConfig(object): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 65 | _ForUser = None |
| 66 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 67 | @classmethod |
| 68 | def ForUser(cls): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 69 | if cls._ForUser is None: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 70 | cls._ForUser = cls(configfile = os.path.expanduser('~/.gitconfig')) |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 71 | return cls._ForUser |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | |
| 73 | @classmethod |
| 74 | def ForRepository(cls, gitdir, defaults=None): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 75 | return cls(configfile = os.path.join(gitdir, 'config'), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 76 | defaults = defaults) |
| 77 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 78 | def __init__(self, configfile, defaults=None, pickleFile=None): |
| 79 | self.file = configfile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 80 | self.defaults = defaults |
| 81 | self._cache_dict = None |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 82 | self._section_dict = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | self._remotes = {} |
| 84 | self._branches = {} |
Shawn O. Pearce | 1b34c91 | 2009-05-21 18:52:49 -0700 | [diff] [blame] | 85 | |
| 86 | if pickleFile is None: |
| 87 | self._pickle = os.path.join( |
| 88 | os.path.dirname(self.file), |
| 89 | '.repopickle_' + os.path.basename(self.file)) |
| 90 | else: |
| 91 | self._pickle = pickleFile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 92 | |
| 93 | def Has(self, name, include_defaults = True): |
| 94 | """Return true if this configuration file has the key. |
| 95 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 96 | if _key(name) in self._cache: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 97 | return True |
| 98 | if include_defaults and self.defaults: |
| 99 | return self.defaults.Has(name, include_defaults = True) |
| 100 | return False |
| 101 | |
| 102 | def GetBoolean(self, name): |
| 103 | """Returns a boolean from the configuration file. |
| 104 | None : The value was not defined, or is not a boolean. |
| 105 | True : The value was set to true or yes. |
| 106 | False: The value was set to false or no. |
| 107 | """ |
| 108 | v = self.GetString(name) |
| 109 | if v is None: |
| 110 | return None |
| 111 | v = v.lower() |
| 112 | if v in ('true', 'yes'): |
| 113 | return True |
| 114 | if v in ('false', 'no'): |
| 115 | return False |
| 116 | return None |
| 117 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 118 | def GetString(self, name, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | """Get the first value for a key, or None if it is not defined. |
| 120 | |
| 121 | This configuration file is used first, if the key is not |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 122 | defined or all_keys = True then the defaults are also searched. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 124 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 125 | v = self._cache[_key(name)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | except KeyError: |
| 127 | if self.defaults: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 128 | return self.defaults.GetString(name, all_keys = all_keys) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 129 | v = [] |
| 130 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 131 | if not all_keys: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 132 | if v: |
| 133 | return v[0] |
| 134 | return None |
| 135 | |
| 136 | r = [] |
| 137 | r.extend(v) |
| 138 | if self.defaults: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 139 | r.extend(self.defaults.GetString(name, all_keys = True)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | return r |
| 141 | |
| 142 | def SetString(self, name, value): |
| 143 | """Set the value(s) for a key. |
| 144 | Only this configuration file is modified. |
| 145 | |
| 146 | The supplied value should be either a string, |
| 147 | or a list of strings (to store multiple values). |
| 148 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 149 | key = _key(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | |
| 151 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 152 | old = self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | except KeyError: |
| 154 | old = [] |
| 155 | |
| 156 | if value is None: |
| 157 | if old: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 158 | del self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | self._do('--unset-all', name) |
| 160 | |
| 161 | elif isinstance(value, list): |
| 162 | if len(value) == 0: |
| 163 | self.SetString(name, None) |
| 164 | |
| 165 | elif len(value) == 1: |
| 166 | self.SetString(name, value[0]) |
| 167 | |
| 168 | elif old != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 169 | self._cache[key] = list(value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 170 | self._do('--replace-all', name, value[0]) |
| 171 | for i in xrange(1, len(value)): |
| 172 | self._do('--add', name, value[i]) |
| 173 | |
| 174 | elif len(old) != 1 or old[0] != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 175 | self._cache[key] = [value] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 176 | self._do('--replace-all', name, value) |
| 177 | |
| 178 | def GetRemote(self, name): |
| 179 | """Get the remote.$name.* configuration values as an object. |
| 180 | """ |
| 181 | try: |
| 182 | r = self._remotes[name] |
| 183 | except KeyError: |
| 184 | r = Remote(self, name) |
| 185 | self._remotes[r.name] = r |
| 186 | return r |
| 187 | |
| 188 | def GetBranch(self, name): |
| 189 | """Get the branch.$name.* configuration values as an object. |
| 190 | """ |
| 191 | try: |
| 192 | b = self._branches[name] |
| 193 | except KeyError: |
| 194 | b = Branch(self, name) |
| 195 | self._branches[b.name] = b |
| 196 | return b |
| 197 | |
Shawn O. Pearce | 366ad21 | 2009-05-19 12:47:37 -0700 | [diff] [blame] | 198 | def GetSubSections(self, section): |
| 199 | """List all subsection names matching $section.*.* |
| 200 | """ |
| 201 | return self._sections.get(section, set()) |
| 202 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 203 | def HasSection(self, section, subsection = ''): |
| 204 | """Does at least one key in section.subsection exist? |
| 205 | """ |
| 206 | try: |
| 207 | return subsection in self._sections[section] |
| 208 | except KeyError: |
| 209 | return False |
| 210 | |
Shawn O. Pearce | 13111b4 | 2011-09-19 11:00:31 -0700 | [diff] [blame] | 211 | def UrlInsteadOf(self, url): |
| 212 | """Resolve any url.*.insteadof references. |
| 213 | """ |
| 214 | for new_url in self.GetSubSections('url'): |
| 215 | old_url = self.GetString('url.%s.insteadof' % new_url) |
| 216 | if old_url is not None and url.startswith(old_url): |
| 217 | return new_url + url[len(old_url):] |
| 218 | return url |
| 219 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 220 | @property |
| 221 | def _sections(self): |
| 222 | d = self._section_dict |
| 223 | if d is None: |
| 224 | d = {} |
| 225 | for name in self._cache.keys(): |
| 226 | p = name.split('.') |
| 227 | if 2 == len(p): |
| 228 | section = p[0] |
| 229 | subsect = '' |
| 230 | else: |
| 231 | section = p[0] |
| 232 | subsect = '.'.join(p[1:-1]) |
| 233 | if section not in d: |
| 234 | d[section] = set() |
| 235 | d[section].add(subsect) |
| 236 | self._section_dict = d |
| 237 | return d |
| 238 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | @property |
| 240 | def _cache(self): |
| 241 | if self._cache_dict is None: |
| 242 | self._cache_dict = self._Read() |
| 243 | return self._cache_dict |
| 244 | |
| 245 | def _Read(self): |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 246 | d = self._ReadPickle() |
| 247 | if d is None: |
| 248 | d = self._ReadGit() |
| 249 | self._SavePickle(d) |
| 250 | return d |
| 251 | |
| 252 | def _ReadPickle(self): |
| 253 | try: |
| 254 | if os.path.getmtime(self._pickle) \ |
| 255 | <= os.path.getmtime(self.file): |
| 256 | os.remove(self._pickle) |
| 257 | return None |
| 258 | except OSError: |
| 259 | return None |
| 260 | try: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 261 | Trace(': unpickle %s', self.file) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 262 | fd = open(self._pickle, 'rb') |
| 263 | try: |
| 264 | return cPickle.load(fd) |
| 265 | finally: |
| 266 | fd.close() |
Shawn O. Pearce | 2a3a81b | 2009-06-12 09:10:07 -0700 | [diff] [blame] | 267 | except EOFError: |
| 268 | os.remove(self._pickle) |
| 269 | return None |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 270 | except IOError: |
| 271 | os.remove(self._pickle) |
| 272 | return None |
| 273 | except cPickle.PickleError: |
| 274 | os.remove(self._pickle) |
| 275 | return None |
| 276 | |
| 277 | def _SavePickle(self, cache): |
| 278 | try: |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 279 | fd = open(self._pickle, 'wb') |
| 280 | try: |
| 281 | cPickle.dump(cache, fd, cPickle.HIGHEST_PROTOCOL) |
| 282 | finally: |
| 283 | fd.close() |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 284 | except IOError: |
Ulrik Sjölin | 99482ae | 2010-10-29 08:23:30 -0700 | [diff] [blame] | 285 | if os.path.exists(self._pickle): |
| 286 | os.remove(self._pickle) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 287 | except cPickle.PickleError: |
Ulrik Sjölin | 99482ae | 2010-10-29 08:23:30 -0700 | [diff] [blame] | 288 | if os.path.exists(self._pickle): |
| 289 | os.remove(self._pickle) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 290 | |
| 291 | def _ReadGit(self): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 292 | """ |
| 293 | Read configuration data from git. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 294 | |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 295 | This internal method populates the GitConfig cache. |
| 296 | |
| 297 | """ |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 298 | c = {} |
Shawn O. Pearce | c24c720 | 2009-07-02 16:12:57 -0700 | [diff] [blame] | 299 | d = self._do('--null', '--list') |
| 300 | if d is None: |
| 301 | return c |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 302 | for line in d.rstrip('\0').split('\0'): # pylint: disable=W1401 |
| 303 | # Backslash is not anomalous |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 304 | if '\n' in line: |
| 305 | key, val = line.split('\n', 1) |
| 306 | else: |
| 307 | key = line |
| 308 | val = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | |
| 310 | if key in c: |
| 311 | c[key].append(val) |
| 312 | else: |
| 313 | c[key] = [val] |
| 314 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 315 | return c |
| 316 | |
| 317 | def _do(self, *args): |
| 318 | command = ['config', '--file', self.file] |
| 319 | command.extend(args) |
| 320 | |
| 321 | p = GitCommand(None, |
| 322 | command, |
| 323 | capture_stdout = True, |
| 324 | capture_stderr = True) |
| 325 | if p.Wait() == 0: |
| 326 | return p.stdout |
| 327 | else: |
| 328 | GitError('git config %s: %s' % (str(args), p.stderr)) |
| 329 | |
| 330 | |
| 331 | class RefSpec(object): |
| 332 | """A Git refspec line, split into its components: |
| 333 | |
| 334 | forced: True if the line starts with '+' |
| 335 | src: Left side of the line |
| 336 | dst: Right side of the line |
| 337 | """ |
| 338 | |
| 339 | @classmethod |
| 340 | def FromString(cls, rs): |
| 341 | lhs, rhs = rs.split(':', 2) |
| 342 | if lhs.startswith('+'): |
| 343 | lhs = lhs[1:] |
| 344 | forced = True |
| 345 | else: |
| 346 | forced = False |
| 347 | return cls(forced, lhs, rhs) |
| 348 | |
| 349 | def __init__(self, forced, lhs, rhs): |
| 350 | self.forced = forced |
| 351 | self.src = lhs |
| 352 | self.dst = rhs |
| 353 | |
| 354 | def SourceMatches(self, rev): |
| 355 | if self.src: |
| 356 | if rev == self.src: |
| 357 | return True |
| 358 | if self.src.endswith('/*') and rev.startswith(self.src[:-1]): |
| 359 | return True |
| 360 | return False |
| 361 | |
| 362 | def DestMatches(self, ref): |
| 363 | if self.dst: |
| 364 | if ref == self.dst: |
| 365 | return True |
| 366 | if self.dst.endswith('/*') and ref.startswith(self.dst[:-1]): |
| 367 | return True |
| 368 | return False |
| 369 | |
| 370 | def MapSource(self, rev): |
| 371 | if self.src.endswith('/*'): |
| 372 | return self.dst[:-1] + rev[len(self.src) - 1:] |
| 373 | return self.dst |
| 374 | |
| 375 | def __str__(self): |
| 376 | s = '' |
| 377 | if self.forced: |
| 378 | s += '+' |
| 379 | if self.src: |
| 380 | s += self.src |
| 381 | if self.dst: |
| 382 | s += ':' |
| 383 | s += self.dst |
| 384 | return s |
| 385 | |
| 386 | |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 387 | _master_processes = [] |
| 388 | _master_keys = set() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 389 | _ssh_master = True |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 390 | _master_keys_lock = None |
| 391 | |
| 392 | def init_ssh(): |
| 393 | """Should be called once at the start of repo to init ssh master handling. |
| 394 | |
| 395 | At the moment, all we do is to create our lock. |
| 396 | """ |
| 397 | global _master_keys_lock |
| 398 | assert _master_keys_lock is None, "Should only call init_ssh once" |
| 399 | _master_keys_lock = _threading.Lock() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 400 | |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 401 | def _open_ssh(host, port=None): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 402 | global _ssh_master |
| 403 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 404 | # Acquire the lock. This is needed to prevent opening multiple masters for |
| 405 | # the same host when we're running "repo sync -jN" (for N > 1) _and_ the |
| 406 | # manifest <remote fetch="ssh://xyz"> specifies a different host from the |
| 407 | # one that was passed to repo init. |
| 408 | _master_keys_lock.acquire() |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 409 | try: |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 410 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 411 | # Check to see whether we already think that the master is running; if we |
| 412 | # think it's already running, return right away. |
| 413 | if port is not None: |
| 414 | key = '%s:%s' % (host, port) |
| 415 | else: |
| 416 | key = host |
| 417 | |
| 418 | if key in _master_keys: |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 419 | return True |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 420 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 421 | if not _ssh_master \ |
| 422 | or 'GIT_SSH' in os.environ \ |
| 423 | or sys.platform in ('win32', 'cygwin'): |
| 424 | # failed earlier, or cygwin ssh can't do this |
| 425 | # |
| 426 | return False |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 427 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 428 | # We will make two calls to ssh; this is the common part of both calls. |
| 429 | command_base = ['ssh', |
| 430 | '-o','ControlPath %s' % ssh_sock(), |
| 431 | host] |
| 432 | if port is not None: |
| 433 | command_base[1:1] = ['-p',str(port)] |
| 434 | |
| 435 | # Since the key wasn't in _master_keys, we think that master isn't running. |
| 436 | # ...but before actually starting a master, we'll double-check. This can |
| 437 | # be important because we can't tell that that 'git@myhost.com' is the same |
| 438 | # as 'myhost.com' where "User git" is setup in the user's ~/.ssh/config file. |
| 439 | check_command = command_base + ['-O','check'] |
| 440 | try: |
| 441 | Trace(': %s', ' '.join(check_command)) |
| 442 | check_process = subprocess.Popen(check_command, |
| 443 | stdout=subprocess.PIPE, |
| 444 | stderr=subprocess.PIPE) |
| 445 | check_process.communicate() # read output, but ignore it... |
| 446 | isnt_running = check_process.wait() |
| 447 | |
| 448 | if not isnt_running: |
| 449 | # Our double-check found that the master _was_ infact running. Add to |
| 450 | # the list of keys. |
| 451 | _master_keys.add(key) |
| 452 | return True |
| 453 | except Exception: |
| 454 | # Ignore excpetions. We we will fall back to the normal command and print |
| 455 | # to the log there. |
| 456 | pass |
| 457 | |
| 458 | command = command_base[:1] + \ |
| 459 | ['-M', '-N'] + \ |
| 460 | command_base[1:] |
| 461 | try: |
| 462 | Trace(': %s', ' '.join(command)) |
| 463 | p = subprocess.Popen(command) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 464 | except Exception as e: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 465 | _ssh_master = False |
| 466 | print >>sys.stderr, \ |
| 467 | '\nwarn: cannot enable ssh control master for %s:%s\n%s' \ |
| 468 | % (host,port, str(e)) |
| 469 | return False |
| 470 | |
| 471 | _master_processes.append(p) |
| 472 | _master_keys.add(key) |
| 473 | time.sleep(1) |
| 474 | return True |
| 475 | finally: |
| 476 | _master_keys_lock.release() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 477 | |
| 478 | def close_ssh(): |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 479 | global _master_keys_lock |
| 480 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 481 | terminate_ssh_clients() |
| 482 | |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 483 | for p in _master_processes: |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 484 | try: |
| 485 | os.kill(p.pid, SIGTERM) |
| 486 | p.wait() |
Shawn O. Pearce | fb5c8fd | 2009-06-16 14:57:46 -0700 | [diff] [blame] | 487 | except OSError: |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 488 | pass |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 489 | del _master_processes[:] |
| 490 | _master_keys.clear() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 491 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 492 | d = ssh_sock(create=False) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 493 | if d: |
| 494 | try: |
| 495 | os.rmdir(os.path.dirname(d)) |
| 496 | except OSError: |
| 497 | pass |
| 498 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 499 | # We're done with the lock, so we can delete it. |
| 500 | _master_keys_lock = None |
| 501 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 502 | URI_SCP = re.compile(r'^([^@:]*@?[^:/]{1,}):') |
Shawn O. Pearce | 898e12a | 2012-03-14 15:22:28 -0700 | [diff] [blame] | 503 | URI_ALL = re.compile(r'^([a-z][a-z+-]*)://([^@/]*@?[^/]*)/') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 504 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 505 | def GetSchemeFromUrl(url): |
| 506 | m = URI_ALL.match(url) |
| 507 | if m: |
| 508 | return m.group(1) |
| 509 | return None |
| 510 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 511 | def _preconnect(url): |
| 512 | m = URI_ALL.match(url) |
| 513 | if m: |
| 514 | scheme = m.group(1) |
| 515 | host = m.group(2) |
| 516 | if ':' in host: |
| 517 | host, port = host.split(':') |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 518 | else: |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 519 | port = None |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 520 | if scheme in ('ssh', 'git+ssh', 'ssh+git'): |
| 521 | return _open_ssh(host, port) |
| 522 | return False |
| 523 | |
| 524 | m = URI_SCP.match(url) |
| 525 | if m: |
| 526 | host = m.group(1) |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 527 | return _open_ssh(host) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 528 | |
Shawn O. Pearce | 7b4f435 | 2009-06-12 09:06:35 -0700 | [diff] [blame] | 529 | return False |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 530 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 531 | class Remote(object): |
| 532 | """Configuration options related to a remote. |
| 533 | """ |
| 534 | def __init__(self, config, name): |
| 535 | self._config = config |
| 536 | self.name = name |
| 537 | self.url = self._Get('url') |
| 538 | self.review = self._Get('review') |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 539 | self.projectname = self._Get('projectname') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 540 | self.fetch = map(lambda x: RefSpec.FromString(x), |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 541 | self._Get('fetch', all_keys=True)) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 542 | self._review_url = None |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 543 | |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 544 | def _InsteadOf(self): |
| 545 | globCfg = GitConfig.ForUser() |
| 546 | urlList = globCfg.GetSubSections('url') |
| 547 | longest = "" |
| 548 | longestUrl = "" |
| 549 | |
| 550 | for url in urlList: |
| 551 | key = "url." + url + ".insteadOf" |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 552 | insteadOfList = globCfg.GetString(key, all_keys=True) |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 553 | |
| 554 | for insteadOf in insteadOfList: |
| 555 | if self.url.startswith(insteadOf) \ |
| 556 | and len(insteadOf) > len(longest): |
| 557 | longest = insteadOf |
| 558 | longestUrl = url |
| 559 | |
| 560 | if len(longest) == 0: |
| 561 | return self.url |
| 562 | |
| 563 | return self.url.replace(longest, longestUrl, 1) |
| 564 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 565 | def PreConnectFetch(self): |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 566 | connectionUrl = self._InsteadOf() |
| 567 | return _preconnect(connectionUrl) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 568 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 569 | def ReviewUrl(self, userEmail): |
| 570 | if self._review_url is None: |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 571 | if self.review is None: |
| 572 | return None |
| 573 | |
| 574 | u = self.review |
| 575 | if not u.startswith('http:') and not u.startswith('https:'): |
| 576 | u = 'http://%s' % u |
Shawn O. Pearce | 13cc384 | 2009-03-25 13:54:54 -0700 | [diff] [blame] | 577 | if u.endswith('/Gerrit'): |
| 578 | u = u[:len(u) - len('/Gerrit')] |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 579 | if u.endswith('/ssh_info'): |
| 580 | u = u[:len(u) - len('/ssh_info')] |
| 581 | if not u.endswith('/'): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 582 | u += '/' |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 583 | http_url = u |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 584 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 585 | if u in REVIEW_CACHE: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 586 | self._review_url = REVIEW_CACHE[u] |
Shawn O. Pearce | 1a68dc5 | 2011-10-11 14:12:46 -0700 | [diff] [blame] | 587 | elif 'REPO_HOST_PORT_INFO' in os.environ: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 588 | host, port = os.environ['REPO_HOST_PORT_INFO'].split() |
| 589 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
| 590 | REVIEW_CACHE[u] = self._review_url |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 591 | else: |
| 592 | try: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 593 | info_url = u + 'ssh_info' |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 594 | info = urllib.request.urlopen(info_url).read() |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 595 | if '<' in info: |
| 596 | # Assume the server gave us some sort of HTML |
| 597 | # response back, like maybe a login page. |
| 598 | # |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 599 | raise UploadError('%s: Cannot parse response' % info_url) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 600 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 601 | if info == 'NOT_AVAILABLE': |
| 602 | # Assume HTTP if SSH is not enabled. |
| 603 | self._review_url = http_url + 'p/' |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 604 | else: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 605 | host, port = info.split() |
| 606 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 607 | except urllib.error.HTTPError as e: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 608 | raise UploadError('%s: %s' % (self.review, str(e))) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 609 | except urllib.error.URLError as e: |
Shawn O. Pearce | bf1fbb2 | 2011-10-11 09:31:58 -0700 | [diff] [blame] | 610 | raise UploadError('%s: %s' % (self.review, str(e))) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 611 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 612 | REVIEW_CACHE[u] = self._review_url |
| 613 | return self._review_url + self.projectname |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 614 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 615 | def _SshReviewUrl(self, userEmail, host, port): |
Shawn O. Pearce | 3575b8f | 2010-07-15 17:00:14 -0700 | [diff] [blame] | 616 | username = self._config.GetString('review.%s.username' % self.review) |
| 617 | if username is None: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 618 | username = userEmail.split('@')[0] |
| 619 | return 'ssh://%s@%s:%s/' % (username, host, port) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 620 | |
| 621 | def ToLocal(self, rev): |
| 622 | """Convert a remote revision string to something we have locally. |
| 623 | """ |
| 624 | if IsId(rev): |
| 625 | return rev |
| 626 | if rev.startswith(R_TAGS): |
| 627 | return rev |
| 628 | |
| 629 | if not rev.startswith('refs/'): |
| 630 | rev = R_HEADS + rev |
| 631 | |
| 632 | for spec in self.fetch: |
| 633 | if spec.SourceMatches(rev): |
| 634 | return spec.MapSource(rev) |
| 635 | raise GitError('remote %s does not have %s' % (self.name, rev)) |
| 636 | |
| 637 | def WritesTo(self, ref): |
| 638 | """True if the remote stores to the tracking ref. |
| 639 | """ |
| 640 | for spec in self.fetch: |
| 641 | if spec.DestMatches(ref): |
| 642 | return True |
| 643 | return False |
| 644 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 645 | def ResetFetch(self, mirror=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 646 | """Set the fetch refspec to its default value. |
| 647 | """ |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 648 | if mirror: |
| 649 | dst = 'refs/heads/*' |
| 650 | else: |
| 651 | dst = 'refs/remotes/%s/*' % self.name |
| 652 | self.fetch = [RefSpec(True, 'refs/heads/*', dst)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 653 | |
| 654 | def Save(self): |
| 655 | """Save this remote to the configuration. |
| 656 | """ |
| 657 | self._Set('url', self.url) |
| 658 | self._Set('review', self.review) |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 659 | self._Set('projectname', self.projectname) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 660 | self._Set('fetch', map(lambda x: str(x), self.fetch)) |
| 661 | |
| 662 | def _Set(self, key, value): |
| 663 | key = 'remote.%s.%s' % (self.name, key) |
| 664 | return self._config.SetString(key, value) |
| 665 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 666 | def _Get(self, key, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 667 | key = 'remote.%s.%s' % (self.name, key) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 668 | return self._config.GetString(key, all_keys = all_keys) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 669 | |
| 670 | |
| 671 | class Branch(object): |
| 672 | """Configuration options related to a single branch. |
| 673 | """ |
| 674 | def __init__(self, config, name): |
| 675 | self._config = config |
| 676 | self.name = name |
| 677 | self.merge = self._Get('merge') |
| 678 | |
| 679 | r = self._Get('remote') |
| 680 | if r: |
| 681 | self.remote = self._config.GetRemote(r) |
| 682 | else: |
| 683 | self.remote = None |
| 684 | |
| 685 | @property |
| 686 | def LocalMerge(self): |
| 687 | """Convert the merge spec to a local name. |
| 688 | """ |
| 689 | if self.remote and self.merge: |
| 690 | return self.remote.ToLocal(self.merge) |
| 691 | return None |
| 692 | |
| 693 | def Save(self): |
| 694 | """Save this branch back into the configuration. |
| 695 | """ |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 696 | if self._config.HasSection('branch', self.name): |
| 697 | if self.remote: |
| 698 | self._Set('remote', self.remote.name) |
| 699 | else: |
| 700 | self._Set('remote', None) |
| 701 | self._Set('merge', self.merge) |
| 702 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 703 | else: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 704 | fd = open(self._config.file, 'ab') |
| 705 | try: |
| 706 | fd.write('[branch "%s"]\n' % self.name) |
| 707 | if self.remote: |
| 708 | fd.write('\tremote = %s\n' % self.remote.name) |
| 709 | if self.merge: |
| 710 | fd.write('\tmerge = %s\n' % self.merge) |
| 711 | finally: |
| 712 | fd.close() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 713 | |
| 714 | def _Set(self, key, value): |
| 715 | key = 'branch.%s.%s' % (self.name, key) |
| 716 | return self._config.SetString(key, value) |
| 717 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 718 | def _Get(self, key, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 719 | key = 'branch.%s.%s' % (self.name, key) |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 720 | return self._config.GetString(key, all_keys = all_keys) |