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