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 | |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 16 | from __future__ import print_function |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 17 | import cPickle |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 18 | import os |
| 19 | import re |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 20 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import sys |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 22 | try: |
| 23 | import threading as _threading |
| 24 | except ImportError: |
| 25 | import dummy_threading as _threading |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 26 | import time |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 27 | try: |
| 28 | import urllib2 |
| 29 | except ImportError: |
| 30 | # For python3 |
| 31 | import urllib.request |
| 32 | import urllib.error |
| 33 | else: |
| 34 | # For python2 |
| 35 | import imp |
| 36 | urllib = imp.new_module('urllib') |
| 37 | urllib.request = urllib2 |
| 38 | urllib.error = urllib2 |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 39 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 40 | from signal import SIGTERM |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 41 | from error import GitError, UploadError |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 42 | from trace import Trace |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 43 | |
| 44 | from git_command import GitCommand |
| 45 | from git_command import ssh_sock |
| 46 | from git_command import terminate_ssh_clients |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | |
| 48 | R_HEADS = 'refs/heads/' |
| 49 | R_TAGS = 'refs/tags/' |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 50 | ID_RE = re.compile(r'^[0-9a-f]{40}$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 52 | REVIEW_CACHE = dict() |
| 53 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 54 | def IsId(rev): |
| 55 | return ID_RE.match(rev) |
| 56 | |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 57 | def _key(name): |
| 58 | parts = name.split('.') |
| 59 | if len(parts) < 2: |
| 60 | return name.lower() |
| 61 | parts[ 0] = parts[ 0].lower() |
| 62 | parts[-1] = parts[-1].lower() |
| 63 | return '.'.join(parts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | |
| 65 | class GitConfig(object): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 66 | _ForUser = None |
| 67 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 68 | @classmethod |
| 69 | def ForUser(cls): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 70 | if cls._ForUser is None: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 71 | cls._ForUser = cls(configfile = os.path.expanduser('~/.gitconfig')) |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 72 | return cls._ForUser |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | |
| 74 | @classmethod |
| 75 | def ForRepository(cls, gitdir, defaults=None): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 76 | return cls(configfile = os.path.join(gitdir, 'config'), |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | defaults = defaults) |
| 78 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 79 | def __init__(self, configfile, defaults=None, pickleFile=None): |
| 80 | self.file = configfile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | self.defaults = defaults |
| 82 | self._cache_dict = None |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 83 | self._section_dict = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | self._remotes = {} |
| 85 | self._branches = {} |
Shawn O. Pearce | 1b34c91 | 2009-05-21 18:52:49 -0700 | [diff] [blame] | 86 | |
| 87 | if pickleFile is None: |
| 88 | self._pickle = os.path.join( |
| 89 | os.path.dirname(self.file), |
| 90 | '.repopickle_' + os.path.basename(self.file)) |
| 91 | else: |
| 92 | self._pickle = pickleFile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | |
| 94 | def Has(self, name, include_defaults = True): |
| 95 | """Return true if this configuration file has the key. |
| 96 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 97 | if _key(name) in self._cache: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | return True |
| 99 | if include_defaults and self.defaults: |
| 100 | return self.defaults.Has(name, include_defaults = True) |
| 101 | return False |
| 102 | |
| 103 | def GetBoolean(self, name): |
| 104 | """Returns a boolean from the configuration file. |
| 105 | None : The value was not defined, or is not a boolean. |
| 106 | True : The value was set to true or yes. |
| 107 | False: The value was set to false or no. |
| 108 | """ |
| 109 | v = self.GetString(name) |
| 110 | if v is None: |
| 111 | return None |
| 112 | v = v.lower() |
| 113 | if v in ('true', 'yes'): |
| 114 | return True |
| 115 | if v in ('false', 'no'): |
| 116 | return False |
| 117 | return None |
| 118 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 119 | def GetString(self, name, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 120 | """Get the first value for a key, or None if it is not defined. |
| 121 | |
| 122 | This configuration file is used first, if the key is not |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 123 | 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] | 124 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 125 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 126 | v = self._cache[_key(name)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | except KeyError: |
| 128 | if self.defaults: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 129 | return self.defaults.GetString(name, all_keys = all_keys) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 130 | v = [] |
| 131 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 132 | if not all_keys: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 133 | if v: |
| 134 | return v[0] |
| 135 | return None |
| 136 | |
| 137 | r = [] |
| 138 | r.extend(v) |
| 139 | if self.defaults: |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 140 | r.extend(self.defaults.GetString(name, all_keys = True)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 141 | return r |
| 142 | |
| 143 | def SetString(self, name, value): |
| 144 | """Set the value(s) for a key. |
| 145 | Only this configuration file is modified. |
| 146 | |
| 147 | The supplied value should be either a string, |
| 148 | or a list of strings (to store multiple values). |
| 149 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 150 | key = _key(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | |
| 152 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 153 | old = self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 154 | except KeyError: |
| 155 | old = [] |
| 156 | |
| 157 | if value is None: |
| 158 | if old: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 159 | del self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 160 | self._do('--unset-all', name) |
| 161 | |
| 162 | elif isinstance(value, list): |
| 163 | if len(value) == 0: |
| 164 | self.SetString(name, None) |
| 165 | |
| 166 | elif len(value) == 1: |
| 167 | self.SetString(name, value[0]) |
| 168 | |
| 169 | elif old != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 170 | self._cache[key] = list(value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 171 | self._do('--replace-all', name, value[0]) |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 172 | for i in range(1, len(value)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | self._do('--add', name, value[i]) |
| 174 | |
| 175 | elif len(old) != 1 or old[0] != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 176 | self._cache[key] = [value] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | self._do('--replace-all', name, value) |
| 178 | |
| 179 | def GetRemote(self, name): |
| 180 | """Get the remote.$name.* configuration values as an object. |
| 181 | """ |
| 182 | try: |
| 183 | r = self._remotes[name] |
| 184 | except KeyError: |
| 185 | r = Remote(self, name) |
| 186 | self._remotes[r.name] = r |
| 187 | return r |
| 188 | |
| 189 | def GetBranch(self, name): |
| 190 | """Get the branch.$name.* configuration values as an object. |
| 191 | """ |
| 192 | try: |
| 193 | b = self._branches[name] |
| 194 | except KeyError: |
| 195 | b = Branch(self, name) |
| 196 | self._branches[b.name] = b |
| 197 | return b |
| 198 | |
Shawn O. Pearce | 366ad21 | 2009-05-19 12:47:37 -0700 | [diff] [blame] | 199 | def GetSubSections(self, section): |
| 200 | """List all subsection names matching $section.*.* |
| 201 | """ |
| 202 | return self._sections.get(section, set()) |
| 203 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 204 | def HasSection(self, section, subsection = ''): |
| 205 | """Does at least one key in section.subsection exist? |
| 206 | """ |
| 207 | try: |
| 208 | return subsection in self._sections[section] |
| 209 | except KeyError: |
| 210 | return False |
| 211 | |
Shawn O. Pearce | 13111b4 | 2011-09-19 11:00:31 -0700 | [diff] [blame] | 212 | def UrlInsteadOf(self, url): |
| 213 | """Resolve any url.*.insteadof references. |
| 214 | """ |
| 215 | for new_url in self.GetSubSections('url'): |
| 216 | old_url = self.GetString('url.%s.insteadof' % new_url) |
| 217 | if old_url is not None and url.startswith(old_url): |
| 218 | return new_url + url[len(old_url):] |
| 219 | return url |
| 220 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 221 | @property |
| 222 | def _sections(self): |
| 223 | d = self._section_dict |
| 224 | if d is None: |
| 225 | d = {} |
| 226 | for name in self._cache.keys(): |
| 227 | p = name.split('.') |
| 228 | if 2 == len(p): |
| 229 | section = p[0] |
| 230 | subsect = '' |
| 231 | else: |
| 232 | section = p[0] |
| 233 | subsect = '.'.join(p[1:-1]) |
| 234 | if section not in d: |
| 235 | d[section] = set() |
| 236 | d[section].add(subsect) |
| 237 | self._section_dict = d |
| 238 | return d |
| 239 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 240 | @property |
| 241 | def _cache(self): |
| 242 | if self._cache_dict is None: |
| 243 | self._cache_dict = self._Read() |
| 244 | return self._cache_dict |
| 245 | |
| 246 | def _Read(self): |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 247 | d = self._ReadPickle() |
| 248 | if d is None: |
| 249 | d = self._ReadGit() |
| 250 | self._SavePickle(d) |
| 251 | return d |
| 252 | |
| 253 | def _ReadPickle(self): |
| 254 | try: |
| 255 | if os.path.getmtime(self._pickle) \ |
| 256 | <= os.path.getmtime(self.file): |
| 257 | os.remove(self._pickle) |
| 258 | return None |
| 259 | except OSError: |
| 260 | return None |
| 261 | try: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 262 | Trace(': unpickle %s', self.file) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 263 | fd = open(self._pickle, 'rb') |
| 264 | try: |
| 265 | return cPickle.load(fd) |
| 266 | finally: |
| 267 | fd.close() |
Shawn O. Pearce | 2a3a81b | 2009-06-12 09:10:07 -0700 | [diff] [blame] | 268 | except EOFError: |
| 269 | os.remove(self._pickle) |
| 270 | return None |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 271 | except IOError: |
| 272 | os.remove(self._pickle) |
| 273 | return None |
| 274 | except cPickle.PickleError: |
| 275 | os.remove(self._pickle) |
| 276 | return None |
| 277 | |
| 278 | def _SavePickle(self, cache): |
| 279 | try: |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 280 | fd = open(self._pickle, 'wb') |
| 281 | try: |
| 282 | cPickle.dump(cache, fd, cPickle.HIGHEST_PROTOCOL) |
| 283 | finally: |
| 284 | fd.close() |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 285 | except IOError: |
Ulrik Sjölin | 99482ae | 2010-10-29 08:23:30 -0700 | [diff] [blame] | 286 | if os.path.exists(self._pickle): |
| 287 | os.remove(self._pickle) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 288 | except cPickle.PickleError: |
Ulrik Sjölin | 99482ae | 2010-10-29 08:23:30 -0700 | [diff] [blame] | 289 | if os.path.exists(self._pickle): |
| 290 | os.remove(self._pickle) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 291 | |
| 292 | def _ReadGit(self): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 293 | """ |
| 294 | Read configuration data from git. |
| 295 | |
| 296 | This internal method populates the GitConfig cache. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 297 | |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 298 | """ |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 299 | c = {} |
Shawn O. Pearce | c24c720 | 2009-07-02 16:12:57 -0700 | [diff] [blame] | 300 | d = self._do('--null', '--list') |
| 301 | if d is None: |
| 302 | return c |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 303 | for line in d.rstrip('\0').split('\0'): # pylint: disable=W1401 |
| 304 | # Backslash is not anomalous |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 305 | if '\n' in line: |
| 306 | key, val = line.split('\n', 1) |
| 307 | else: |
| 308 | key = line |
| 309 | val = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 310 | |
| 311 | if key in c: |
| 312 | c[key].append(val) |
| 313 | else: |
| 314 | c[key] = [val] |
| 315 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 316 | return c |
| 317 | |
| 318 | def _do(self, *args): |
| 319 | command = ['config', '--file', self.file] |
| 320 | command.extend(args) |
| 321 | |
| 322 | p = GitCommand(None, |
| 323 | command, |
| 324 | capture_stdout = True, |
| 325 | capture_stderr = True) |
| 326 | if p.Wait() == 0: |
| 327 | return p.stdout |
| 328 | else: |
| 329 | GitError('git config %s: %s' % (str(args), p.stderr)) |
| 330 | |
| 331 | |
| 332 | class RefSpec(object): |
| 333 | """A Git refspec line, split into its components: |
| 334 | |
| 335 | forced: True if the line starts with '+' |
| 336 | src: Left side of the line |
| 337 | dst: Right side of the line |
| 338 | """ |
| 339 | |
| 340 | @classmethod |
| 341 | def FromString(cls, rs): |
| 342 | lhs, rhs = rs.split(':', 2) |
| 343 | if lhs.startswith('+'): |
| 344 | lhs = lhs[1:] |
| 345 | forced = True |
| 346 | else: |
| 347 | forced = False |
| 348 | return cls(forced, lhs, rhs) |
| 349 | |
| 350 | def __init__(self, forced, lhs, rhs): |
| 351 | self.forced = forced |
| 352 | self.src = lhs |
| 353 | self.dst = rhs |
| 354 | |
| 355 | def SourceMatches(self, rev): |
| 356 | if self.src: |
| 357 | if rev == self.src: |
| 358 | return True |
| 359 | if self.src.endswith('/*') and rev.startswith(self.src[:-1]): |
| 360 | return True |
| 361 | return False |
| 362 | |
| 363 | def DestMatches(self, ref): |
| 364 | if self.dst: |
| 365 | if ref == self.dst: |
| 366 | return True |
| 367 | if self.dst.endswith('/*') and ref.startswith(self.dst[:-1]): |
| 368 | return True |
| 369 | return False |
| 370 | |
| 371 | def MapSource(self, rev): |
| 372 | if self.src.endswith('/*'): |
| 373 | return self.dst[:-1] + rev[len(self.src) - 1:] |
| 374 | return self.dst |
| 375 | |
| 376 | def __str__(self): |
| 377 | s = '' |
| 378 | if self.forced: |
| 379 | s += '+' |
| 380 | if self.src: |
| 381 | s += self.src |
| 382 | if self.dst: |
| 383 | s += ':' |
| 384 | s += self.dst |
| 385 | return s |
| 386 | |
| 387 | |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 388 | _master_processes = [] |
| 389 | _master_keys = set() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 390 | _ssh_master = True |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 391 | _master_keys_lock = None |
| 392 | |
| 393 | def init_ssh(): |
| 394 | """Should be called once at the start of repo to init ssh master handling. |
| 395 | |
| 396 | At the moment, all we do is to create our lock. |
| 397 | """ |
| 398 | global _master_keys_lock |
| 399 | assert _master_keys_lock is None, "Should only call init_ssh once" |
| 400 | _master_keys_lock = _threading.Lock() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 401 | |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 402 | def _open_ssh(host, port=None): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 403 | global _ssh_master |
| 404 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 405 | # Acquire the lock. This is needed to prevent opening multiple masters for |
| 406 | # the same host when we're running "repo sync -jN" (for N > 1) _and_ the |
| 407 | # manifest <remote fetch="ssh://xyz"> specifies a different host from the |
| 408 | # one that was passed to repo init. |
| 409 | _master_keys_lock.acquire() |
| 410 | try: |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 411 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 412 | # Check to see whether we already think that the master is running; if we |
| 413 | # think it's already running, return right away. |
| 414 | if port is not None: |
| 415 | key = '%s:%s' % (host, port) |
| 416 | else: |
| 417 | key = host |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 418 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 419 | if key in _master_keys: |
| 420 | return True |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 421 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 422 | if not _ssh_master \ |
| 423 | or 'GIT_SSH' in os.environ \ |
| 424 | or sys.platform in ('win32', 'cygwin'): |
| 425 | # failed earlier, or cygwin ssh can't do this |
| 426 | # |
| 427 | return False |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 428 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 429 | # We will make two calls to ssh; this is the common part of both calls. |
| 430 | command_base = ['ssh', |
| 431 | '-o','ControlPath %s' % ssh_sock(), |
| 432 | host] |
| 433 | if port is not None: |
| 434 | command_base[1:1] = ['-p',str(port)] |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 435 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 436 | # Since the key wasn't in _master_keys, we think that master isn't running. |
| 437 | # ...but before actually starting a master, we'll double-check. This can |
| 438 | # be important because we can't tell that that 'git@myhost.com' is the same |
| 439 | # as 'myhost.com' where "User git" is setup in the user's ~/.ssh/config file. |
| 440 | check_command = command_base + ['-O','check'] |
| 441 | try: |
| 442 | Trace(': %s', ' '.join(check_command)) |
| 443 | check_process = subprocess.Popen(check_command, |
| 444 | stdout=subprocess.PIPE, |
| 445 | stderr=subprocess.PIPE) |
| 446 | check_process.communicate() # read output, but ignore it... |
| 447 | isnt_running = check_process.wait() |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 448 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 449 | if not isnt_running: |
| 450 | # Our double-check found that the master _was_ infact running. Add to |
| 451 | # the list of keys. |
| 452 | _master_keys.add(key) |
| 453 | return True |
| 454 | except Exception: |
| 455 | # Ignore excpetions. We we will fall back to the normal command and print |
| 456 | # to the log there. |
| 457 | pass |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 458 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 459 | command = command_base[:1] + \ |
| 460 | ['-M', '-N'] + \ |
| 461 | command_base[1:] |
| 462 | try: |
| 463 | Trace(': %s', ' '.join(command)) |
| 464 | p = subprocess.Popen(command) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 465 | except Exception as e: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 466 | _ssh_master = False |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 467 | print('\nwarn: cannot enable ssh control master for %s:%s\n%s' |
| 468 | % (host,port, str(e)), file=sys.stderr) |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 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') |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 540 | self.fetch = map(RefSpec.FromString, |
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) |
David Pursehouse | 7e6dd2d | 2012-10-25 12:40:51 +0900 | [diff] [blame] | 660 | self._Set('fetch', map(str, self.fetch)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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) |