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 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 21 | import time |
| 22 | from signal import SIGTERM |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 23 | from urllib2 import urlopen, HTTPError |
| 24 | from error import GitError, UploadError |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 25 | from trace import Trace |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 26 | from git_command import GitCommand, _ssh_sock |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 27 | |
| 28 | R_HEADS = 'refs/heads/' |
| 29 | R_TAGS = 'refs/tags/' |
| 30 | ID_RE = re.compile('^[0-9a-f]{40}$') |
| 31 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 32 | REVIEW_CACHE = dict() |
| 33 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 34 | def IsId(rev): |
| 35 | return ID_RE.match(rev) |
| 36 | |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 37 | def _key(name): |
| 38 | parts = name.split('.') |
| 39 | if len(parts) < 2: |
| 40 | return name.lower() |
| 41 | parts[ 0] = parts[ 0].lower() |
| 42 | parts[-1] = parts[-1].lower() |
| 43 | return '.'.join(parts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | |
| 45 | class GitConfig(object): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 46 | _ForUser = None |
| 47 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 | @classmethod |
| 49 | def ForUser(cls): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 50 | if cls._ForUser is None: |
| 51 | cls._ForUser = cls(file = os.path.expanduser('~/.gitconfig')) |
| 52 | return cls._ForUser |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | |
| 54 | @classmethod |
| 55 | def ForRepository(cls, gitdir, defaults=None): |
| 56 | return cls(file = os.path.join(gitdir, 'config'), |
| 57 | defaults = defaults) |
| 58 | |
Shawn O. Pearce | 1b34c91 | 2009-05-21 18:52:49 -0700 | [diff] [blame] | 59 | def __init__(self, file, defaults=None, pickleFile=None): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | self.file = file |
| 61 | self.defaults = defaults |
| 62 | self._cache_dict = None |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 63 | self._section_dict = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | self._remotes = {} |
| 65 | self._branches = {} |
Shawn O. Pearce | 1b34c91 | 2009-05-21 18:52:49 -0700 | [diff] [blame] | 66 | |
| 67 | if pickleFile is None: |
| 68 | self._pickle = os.path.join( |
| 69 | os.path.dirname(self.file), |
| 70 | '.repopickle_' + os.path.basename(self.file)) |
| 71 | else: |
| 72 | self._pickle = pickleFile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | |
Shawn O. Pearce | 98ea26b | 2009-06-04 19:19:11 -0700 | [diff] [blame] | 74 | def ClearCache(self): |
| 75 | if os.path.exists(self._pickle): |
| 76 | os.remove(self._pickle) |
| 77 | self._cache_dict = None |
| 78 | self._section_dict = None |
| 79 | self._remotes = {} |
| 80 | self._branches = {} |
| 81 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | def Has(self, name, include_defaults = True): |
| 83 | """Return true if this configuration file has the key. |
| 84 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 85 | if _key(name) in self._cache: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 86 | return True |
| 87 | if include_defaults and self.defaults: |
| 88 | return self.defaults.Has(name, include_defaults = True) |
| 89 | return False |
| 90 | |
| 91 | def GetBoolean(self, name): |
| 92 | """Returns a boolean from the configuration file. |
| 93 | None : The value was not defined, or is not a boolean. |
| 94 | True : The value was set to true or yes. |
| 95 | False: The value was set to false or no. |
| 96 | """ |
| 97 | v = self.GetString(name) |
| 98 | if v is None: |
| 99 | return None |
| 100 | v = v.lower() |
| 101 | if v in ('true', 'yes'): |
| 102 | return True |
| 103 | if v in ('false', 'no'): |
| 104 | return False |
| 105 | return None |
| 106 | |
| 107 | def GetString(self, name, all=False): |
| 108 | """Get the first value for a key, or None if it is not defined. |
| 109 | |
| 110 | This configuration file is used first, if the key is not |
| 111 | defined or all = True then the defaults are also searched. |
| 112 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 113 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 114 | v = self._cache[_key(name)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | except KeyError: |
| 116 | if self.defaults: |
| 117 | return self.defaults.GetString(name, all = all) |
| 118 | v = [] |
| 119 | |
| 120 | if not all: |
| 121 | if v: |
| 122 | return v[0] |
| 123 | return None |
| 124 | |
| 125 | r = [] |
| 126 | r.extend(v) |
| 127 | if self.defaults: |
| 128 | r.extend(self.defaults.GetString(name, all = True)) |
| 129 | return r |
| 130 | |
| 131 | def SetString(self, name, value): |
| 132 | """Set the value(s) for a key. |
| 133 | Only this configuration file is modified. |
| 134 | |
| 135 | The supplied value should be either a string, |
| 136 | or a list of strings (to store multiple values). |
| 137 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 138 | key = _key(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 139 | |
| 140 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 141 | old = self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 142 | except KeyError: |
| 143 | old = [] |
| 144 | |
| 145 | if value is None: |
| 146 | if old: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 147 | del self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | self._do('--unset-all', name) |
| 149 | |
| 150 | elif isinstance(value, list): |
| 151 | if len(value) == 0: |
| 152 | self.SetString(name, None) |
| 153 | |
| 154 | elif len(value) == 1: |
| 155 | self.SetString(name, value[0]) |
| 156 | |
| 157 | elif old != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 158 | self._cache[key] = list(value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | self._do('--replace-all', name, value[0]) |
| 160 | for i in xrange(1, len(value)): |
| 161 | self._do('--add', name, value[i]) |
| 162 | |
| 163 | elif len(old) != 1 or old[0] != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 164 | self._cache[key] = [value] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | self._do('--replace-all', name, value) |
| 166 | |
| 167 | def GetRemote(self, name): |
| 168 | """Get the remote.$name.* configuration values as an object. |
| 169 | """ |
| 170 | try: |
| 171 | r = self._remotes[name] |
| 172 | except KeyError: |
| 173 | r = Remote(self, name) |
| 174 | self._remotes[r.name] = r |
| 175 | return r |
| 176 | |
| 177 | def GetBranch(self, name): |
| 178 | """Get the branch.$name.* configuration values as an object. |
| 179 | """ |
| 180 | try: |
| 181 | b = self._branches[name] |
| 182 | except KeyError: |
| 183 | b = Branch(self, name) |
| 184 | self._branches[b.name] = b |
| 185 | return b |
| 186 | |
Shawn O. Pearce | 366ad21 | 2009-05-19 12:47:37 -0700 | [diff] [blame] | 187 | def GetSubSections(self, section): |
| 188 | """List all subsection names matching $section.*.* |
| 189 | """ |
| 190 | return self._sections.get(section, set()) |
| 191 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 192 | def HasSection(self, section, subsection = ''): |
| 193 | """Does at least one key in section.subsection exist? |
| 194 | """ |
| 195 | try: |
| 196 | return subsection in self._sections[section] |
| 197 | except KeyError: |
| 198 | return False |
| 199 | |
| 200 | @property |
| 201 | def _sections(self): |
| 202 | d = self._section_dict |
| 203 | if d is None: |
| 204 | d = {} |
| 205 | for name in self._cache.keys(): |
| 206 | p = name.split('.') |
| 207 | if 2 == len(p): |
| 208 | section = p[0] |
| 209 | subsect = '' |
| 210 | else: |
| 211 | section = p[0] |
| 212 | subsect = '.'.join(p[1:-1]) |
| 213 | if section not in d: |
| 214 | d[section] = set() |
| 215 | d[section].add(subsect) |
| 216 | self._section_dict = d |
| 217 | return d |
| 218 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | @property |
| 220 | def _cache(self): |
| 221 | if self._cache_dict is None: |
| 222 | self._cache_dict = self._Read() |
| 223 | return self._cache_dict |
| 224 | |
| 225 | def _Read(self): |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 226 | d = self._ReadPickle() |
| 227 | if d is None: |
| 228 | d = self._ReadGit() |
| 229 | self._SavePickle(d) |
| 230 | return d |
| 231 | |
| 232 | def _ReadPickle(self): |
| 233 | try: |
| 234 | if os.path.getmtime(self._pickle) \ |
| 235 | <= os.path.getmtime(self.file): |
| 236 | os.remove(self._pickle) |
| 237 | return None |
| 238 | except OSError: |
| 239 | return None |
| 240 | try: |
Shawn O. Pearce | ad3193a | 2009-04-18 09:54:51 -0700 | [diff] [blame] | 241 | Trace(': unpickle %s', self.file) |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 242 | fd = open(self._pickle, 'rb') |
| 243 | try: |
| 244 | return cPickle.load(fd) |
| 245 | finally: |
| 246 | fd.close() |
Shawn O. Pearce | 2a3a81b | 2009-06-12 09:10:07 -0700 | [diff] [blame] | 247 | except EOFError: |
| 248 | os.remove(self._pickle) |
| 249 | return None |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 250 | except IOError: |
| 251 | os.remove(self._pickle) |
| 252 | return None |
| 253 | except cPickle.PickleError: |
| 254 | os.remove(self._pickle) |
| 255 | return None |
| 256 | |
| 257 | def _SavePickle(self, cache): |
| 258 | try: |
Shawn O. Pearce | 76ca9f8 | 2009-04-18 14:48:03 -0700 | [diff] [blame] | 259 | fd = open(self._pickle, 'wb') |
| 260 | try: |
| 261 | cPickle.dump(cache, fd, cPickle.HIGHEST_PROTOCOL) |
| 262 | finally: |
| 263 | fd.close() |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 264 | except IOError: |
| 265 | os.remove(self._pickle) |
| 266 | except cPickle.PickleError: |
| 267 | os.remove(self._pickle) |
| 268 | |
| 269 | def _ReadGit(self): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 270 | """ |
| 271 | Read configuration data from git. |
| 272 | |
| 273 | This internal method populates the GitConfig cache. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 274 | |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 275 | """ |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 276 | c = {} |
Shawn O. Pearce | c24c720 | 2009-07-02 16:12:57 -0700 | [diff] [blame] | 277 | d = self._do('--null', '--list') |
| 278 | if d is None: |
| 279 | return c |
| 280 | for line in d.rstrip('\0').split('\0'): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 281 | if '\n' in line: |
| 282 | key, val = line.split('\n', 1) |
| 283 | else: |
| 284 | key = line |
| 285 | val = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 286 | |
| 287 | if key in c: |
| 288 | c[key].append(val) |
| 289 | else: |
| 290 | c[key] = [val] |
| 291 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 292 | return c |
| 293 | |
| 294 | def _do(self, *args): |
| 295 | command = ['config', '--file', self.file] |
| 296 | command.extend(args) |
| 297 | |
| 298 | p = GitCommand(None, |
| 299 | command, |
| 300 | capture_stdout = True, |
| 301 | capture_stderr = True) |
| 302 | if p.Wait() == 0: |
| 303 | return p.stdout |
| 304 | else: |
| 305 | GitError('git config %s: %s' % (str(args), p.stderr)) |
| 306 | |
| 307 | |
| 308 | class RefSpec(object): |
| 309 | """A Git refspec line, split into its components: |
| 310 | |
| 311 | forced: True if the line starts with '+' |
| 312 | src: Left side of the line |
| 313 | dst: Right side of the line |
| 314 | """ |
| 315 | |
| 316 | @classmethod |
| 317 | def FromString(cls, rs): |
| 318 | lhs, rhs = rs.split(':', 2) |
| 319 | if lhs.startswith('+'): |
| 320 | lhs = lhs[1:] |
| 321 | forced = True |
| 322 | else: |
| 323 | forced = False |
| 324 | return cls(forced, lhs, rhs) |
| 325 | |
| 326 | def __init__(self, forced, lhs, rhs): |
| 327 | self.forced = forced |
| 328 | self.src = lhs |
| 329 | self.dst = rhs |
| 330 | |
| 331 | def SourceMatches(self, rev): |
| 332 | if self.src: |
| 333 | if rev == self.src: |
| 334 | return True |
| 335 | if self.src.endswith('/*') and rev.startswith(self.src[:-1]): |
| 336 | return True |
| 337 | return False |
| 338 | |
| 339 | def DestMatches(self, ref): |
| 340 | if self.dst: |
| 341 | if ref == self.dst: |
| 342 | return True |
| 343 | if self.dst.endswith('/*') and ref.startswith(self.dst[:-1]): |
| 344 | return True |
| 345 | return False |
| 346 | |
| 347 | def MapSource(self, rev): |
| 348 | if self.src.endswith('/*'): |
| 349 | return self.dst[:-1] + rev[len(self.src) - 1:] |
| 350 | return self.dst |
| 351 | |
| 352 | def __str__(self): |
| 353 | s = '' |
| 354 | if self.forced: |
| 355 | s += '+' |
| 356 | if self.src: |
| 357 | s += self.src |
| 358 | if self.dst: |
| 359 | s += ':' |
| 360 | s += self.dst |
| 361 | return s |
| 362 | |
| 363 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 364 | _ssh_cache = {} |
| 365 | _ssh_master = True |
| 366 | |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 367 | def _open_ssh(host, port): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 368 | global _ssh_master |
| 369 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 370 | key = '%s:%s' % (host, port) |
| 371 | if key in _ssh_cache: |
| 372 | return True |
| 373 | |
| 374 | if not _ssh_master \ |
| 375 | or 'GIT_SSH' in os.environ \ |
Shawn O. Pearce | 2b5b4ac | 2009-04-23 17:22:18 -0700 | [diff] [blame] | 376 | or sys.platform in ('win32', 'cygwin'): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 377 | # failed earlier, or cygwin ssh can't do this |
| 378 | # |
| 379 | return False |
| 380 | |
| 381 | command = ['ssh', |
| 382 | '-o','ControlPath %s' % _ssh_sock(), |
| 383 | '-p',str(port), |
| 384 | '-M', |
| 385 | '-N', |
| 386 | host] |
| 387 | try: |
| 388 | Trace(': %s', ' '.join(command)) |
| 389 | p = subprocess.Popen(command) |
| 390 | except Exception, e: |
| 391 | _ssh_master = False |
| 392 | print >>sys.stderr, \ |
| 393 | '\nwarn: cannot enable ssh control master for %s:%s\n%s' \ |
| 394 | % (host,port, str(e)) |
| 395 | return False |
| 396 | |
| 397 | _ssh_cache[key] = p |
| 398 | time.sleep(1) |
| 399 | return True |
| 400 | |
| 401 | def close_ssh(): |
| 402 | for key,p in _ssh_cache.iteritems(): |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 403 | try: |
| 404 | os.kill(p.pid, SIGTERM) |
| 405 | p.wait() |
Shawn O. Pearce | fb5c8fd | 2009-06-16 14:57:46 -0700 | [diff] [blame] | 406 | except OSError: |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 407 | pass |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 408 | _ssh_cache.clear() |
| 409 | |
| 410 | d = _ssh_sock(create=False) |
| 411 | if d: |
| 412 | try: |
| 413 | os.rmdir(os.path.dirname(d)) |
| 414 | except OSError: |
| 415 | pass |
| 416 | |
| 417 | URI_SCP = re.compile(r'^([^@:]*@?[^:/]{1,}):') |
Shawn O. Pearce | 2f968c9 | 2009-04-30 14:30:28 -0700 | [diff] [blame] | 418 | URI_ALL = re.compile(r'^([a-z][a-z+]*)://([^@/]*@?[^/]*)/') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 419 | |
| 420 | def _preconnect(url): |
| 421 | m = URI_ALL.match(url) |
| 422 | if m: |
| 423 | scheme = m.group(1) |
| 424 | host = m.group(2) |
| 425 | if ':' in host: |
| 426 | host, port = host.split(':') |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 427 | else: |
| 428 | port = 22 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 429 | if scheme in ('ssh', 'git+ssh', 'ssh+git'): |
| 430 | return _open_ssh(host, port) |
| 431 | return False |
| 432 | |
| 433 | m = URI_SCP.match(url) |
| 434 | if m: |
| 435 | host = m.group(1) |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 436 | return _open_ssh(host, 22) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 437 | |
Shawn O. Pearce | 7b4f435 | 2009-06-12 09:06:35 -0700 | [diff] [blame] | 438 | return False |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 439 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 440 | class Remote(object): |
| 441 | """Configuration options related to a remote. |
| 442 | """ |
| 443 | def __init__(self, config, name): |
| 444 | self._config = config |
| 445 | self.name = name |
| 446 | self.url = self._Get('url') |
| 447 | self.review = self._Get('review') |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 448 | self.projectname = self._Get('projectname') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 449 | self.fetch = map(lambda x: RefSpec.FromString(x), |
| 450 | self._Get('fetch', all=True)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 451 | self._review_protocol = None |
| 452 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 453 | def PreConnectFetch(self): |
| 454 | return _preconnect(self.url) |
| 455 | |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 456 | @property |
| 457 | def ReviewProtocol(self): |
| 458 | if self._review_protocol is None: |
| 459 | if self.review is None: |
| 460 | return None |
| 461 | |
| 462 | u = self.review |
| 463 | if not u.startswith('http:') and not u.startswith('https:'): |
| 464 | u = 'http://%s' % u |
Shawn O. Pearce | 13cc384 | 2009-03-25 13:54:54 -0700 | [diff] [blame] | 465 | if u.endswith('/Gerrit'): |
| 466 | u = u[:len(u) - len('/Gerrit')] |
| 467 | if not u.endswith('/ssh_info'): |
| 468 | if not u.endswith('/'): |
| 469 | u += '/' |
| 470 | u += 'ssh_info' |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 471 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 472 | if u in REVIEW_CACHE: |
| 473 | info = REVIEW_CACHE[u] |
| 474 | self._review_protocol = info[0] |
| 475 | self._review_host = info[1] |
| 476 | self._review_port = info[2] |
| 477 | else: |
| 478 | try: |
| 479 | info = urlopen(u).read() |
| 480 | if info == 'NOT_AVAILABLE': |
| 481 | raise UploadError('Upload over ssh unavailable') |
| 482 | if '<' in info: |
| 483 | # Assume the server gave us some sort of HTML |
| 484 | # response back, like maybe a login page. |
| 485 | # |
| 486 | raise UploadError('Cannot read %s:\n%s' % (u, info)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 487 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 488 | self._review_protocol = 'ssh' |
| 489 | self._review_host = info.split(" ")[0] |
| 490 | self._review_port = info.split(" ")[1] |
| 491 | except HTTPError, e: |
| 492 | if e.code == 404: |
| 493 | self._review_protocol = 'http-post' |
| 494 | self._review_host = None |
| 495 | self._review_port = None |
| 496 | else: |
| 497 | raise UploadError('Cannot guess Gerrit version') |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 498 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 499 | REVIEW_CACHE[u] = ( |
| 500 | self._review_protocol, |
| 501 | self._review_host, |
| 502 | self._review_port) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 503 | return self._review_protocol |
| 504 | |
| 505 | def SshReviewUrl(self, userEmail): |
| 506 | if self.ReviewProtocol != 'ssh': |
| 507 | return None |
| 508 | return 'ssh://%s@%s:%s/%s' % ( |
| 509 | userEmail.split("@")[0], |
| 510 | self._review_host, |
| 511 | self._review_port, |
| 512 | self.projectname) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 513 | |
| 514 | def ToLocal(self, rev): |
| 515 | """Convert a remote revision string to something we have locally. |
| 516 | """ |
| 517 | if IsId(rev): |
| 518 | return rev |
| 519 | if rev.startswith(R_TAGS): |
| 520 | return rev |
| 521 | |
| 522 | if not rev.startswith('refs/'): |
| 523 | rev = R_HEADS + rev |
| 524 | |
| 525 | for spec in self.fetch: |
| 526 | if spec.SourceMatches(rev): |
| 527 | return spec.MapSource(rev) |
| 528 | raise GitError('remote %s does not have %s' % (self.name, rev)) |
| 529 | |
| 530 | def WritesTo(self, ref): |
| 531 | """True if the remote stores to the tracking ref. |
| 532 | """ |
| 533 | for spec in self.fetch: |
| 534 | if spec.DestMatches(ref): |
| 535 | return True |
| 536 | return False |
| 537 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 538 | def ResetFetch(self, mirror=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 539 | """Set the fetch refspec to its default value. |
| 540 | """ |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 541 | if mirror: |
| 542 | dst = 'refs/heads/*' |
| 543 | else: |
| 544 | dst = 'refs/remotes/%s/*' % self.name |
| 545 | self.fetch = [RefSpec(True, 'refs/heads/*', dst)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 546 | |
| 547 | def Save(self): |
| 548 | """Save this remote to the configuration. |
| 549 | """ |
| 550 | self._Set('url', self.url) |
| 551 | self._Set('review', self.review) |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 552 | self._Set('projectname', self.projectname) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 553 | self._Set('fetch', map(lambda x: str(x), self.fetch)) |
| 554 | |
| 555 | def _Set(self, key, value): |
| 556 | key = 'remote.%s.%s' % (self.name, key) |
| 557 | return self._config.SetString(key, value) |
| 558 | |
| 559 | def _Get(self, key, all=False): |
| 560 | key = 'remote.%s.%s' % (self.name, key) |
| 561 | return self._config.GetString(key, all = all) |
| 562 | |
| 563 | |
| 564 | class Branch(object): |
| 565 | """Configuration options related to a single branch. |
| 566 | """ |
| 567 | def __init__(self, config, name): |
| 568 | self._config = config |
| 569 | self.name = name |
| 570 | self.merge = self._Get('merge') |
| 571 | |
| 572 | r = self._Get('remote') |
| 573 | if r: |
| 574 | self.remote = self._config.GetRemote(r) |
| 575 | else: |
| 576 | self.remote = None |
| 577 | |
| 578 | @property |
| 579 | def LocalMerge(self): |
| 580 | """Convert the merge spec to a local name. |
| 581 | """ |
| 582 | if self.remote and self.merge: |
| 583 | return self.remote.ToLocal(self.merge) |
| 584 | return None |
| 585 | |
| 586 | def Save(self): |
| 587 | """Save this branch back into the configuration. |
| 588 | """ |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 589 | if self._config.HasSection('branch', self.name): |
| 590 | if self.remote: |
| 591 | self._Set('remote', self.remote.name) |
| 592 | else: |
| 593 | self._Set('remote', None) |
| 594 | self._Set('merge', self.merge) |
| 595 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 596 | else: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 597 | fd = open(self._config.file, 'ab') |
| 598 | try: |
| 599 | fd.write('[branch "%s"]\n' % self.name) |
| 600 | if self.remote: |
| 601 | fd.write('\tremote = %s\n' % self.remote.name) |
| 602 | if self.merge: |
| 603 | fd.write('\tmerge = %s\n' % self.merge) |
| 604 | finally: |
| 605 | fd.close() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 606 | |
| 607 | def _Set(self, key, value): |
| 608 | key = 'branch.%s.%s' % (self.name, key) |
| 609 | return self._config.SetString(key, value) |
| 610 | |
| 611 | def _Get(self, key, all=False): |
| 612 | key = 'branch.%s.%s' % (self.name, key) |
| 613 | return self._config.GetString(key, all = all) |