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: |
Shawn O. Pearce | b0ca41e | 2009-07-03 20:01:47 -0700 | [diff] [blame] | 265 | if os.path.exists(self._pickle): |
| 266 | os.remove(self._pickle) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 267 | except cPickle.PickleError: |
Shawn O. Pearce | b0ca41e | 2009-07-03 20:01:47 -0700 | [diff] [blame] | 268 | if os.path.exists(self._pickle): |
| 269 | os.remove(self._pickle) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 270 | |
| 271 | def _ReadGit(self): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 272 | """ |
| 273 | Read configuration data from git. |
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 | This internal method populates the GitConfig cache. |
| 276 | |
| 277 | """ |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 278 | c = {} |
Shawn O. Pearce | c24c720 | 2009-07-02 16:12:57 -0700 | [diff] [blame] | 279 | d = self._do('--null', '--list') |
| 280 | if d is None: |
| 281 | return c |
| 282 | for line in d.rstrip('\0').split('\0'): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 283 | if '\n' in line: |
| 284 | key, val = line.split('\n', 1) |
| 285 | else: |
| 286 | key = line |
| 287 | val = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 288 | |
| 289 | if key in c: |
| 290 | c[key].append(val) |
| 291 | else: |
| 292 | c[key] = [val] |
| 293 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 294 | return c |
| 295 | |
| 296 | def _do(self, *args): |
| 297 | command = ['config', '--file', self.file] |
| 298 | command.extend(args) |
| 299 | |
| 300 | p = GitCommand(None, |
| 301 | command, |
| 302 | capture_stdout = True, |
| 303 | capture_stderr = True) |
| 304 | if p.Wait() == 0: |
| 305 | return p.stdout |
| 306 | else: |
| 307 | GitError('git config %s: %s' % (str(args), p.stderr)) |
| 308 | |
| 309 | |
| 310 | class RefSpec(object): |
| 311 | """A Git refspec line, split into its components: |
| 312 | |
| 313 | forced: True if the line starts with '+' |
| 314 | src: Left side of the line |
| 315 | dst: Right side of the line |
| 316 | """ |
| 317 | |
| 318 | @classmethod |
| 319 | def FromString(cls, rs): |
| 320 | lhs, rhs = rs.split(':', 2) |
| 321 | if lhs.startswith('+'): |
| 322 | lhs = lhs[1:] |
| 323 | forced = True |
| 324 | else: |
| 325 | forced = False |
| 326 | return cls(forced, lhs, rhs) |
| 327 | |
| 328 | def __init__(self, forced, lhs, rhs): |
| 329 | self.forced = forced |
| 330 | self.src = lhs |
| 331 | self.dst = rhs |
| 332 | |
| 333 | def SourceMatches(self, rev): |
| 334 | if self.src: |
| 335 | if rev == self.src: |
| 336 | return True |
| 337 | if self.src.endswith('/*') and rev.startswith(self.src[:-1]): |
| 338 | return True |
| 339 | return False |
| 340 | |
| 341 | def DestMatches(self, ref): |
| 342 | if self.dst: |
| 343 | if ref == self.dst: |
| 344 | return True |
| 345 | if self.dst.endswith('/*') and ref.startswith(self.dst[:-1]): |
| 346 | return True |
| 347 | return False |
| 348 | |
| 349 | def MapSource(self, rev): |
| 350 | if self.src.endswith('/*'): |
| 351 | return self.dst[:-1] + rev[len(self.src) - 1:] |
| 352 | return self.dst |
| 353 | |
| 354 | def __str__(self): |
| 355 | s = '' |
| 356 | if self.forced: |
| 357 | s += '+' |
| 358 | if self.src: |
| 359 | s += self.src |
| 360 | if self.dst: |
| 361 | s += ':' |
| 362 | s += self.dst |
| 363 | return s |
| 364 | |
| 365 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 366 | _ssh_cache = {} |
| 367 | _ssh_master = True |
| 368 | |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 369 | def _open_ssh(host, port): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 370 | global _ssh_master |
| 371 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 372 | key = '%s:%s' % (host, port) |
| 373 | if key in _ssh_cache: |
| 374 | return True |
| 375 | |
| 376 | if not _ssh_master \ |
| 377 | or 'GIT_SSH' in os.environ \ |
Shawn O. Pearce | 2b5b4ac | 2009-04-23 17:22:18 -0700 | [diff] [blame] | 378 | or sys.platform in ('win32', 'cygwin'): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 379 | # failed earlier, or cygwin ssh can't do this |
| 380 | # |
| 381 | return False |
| 382 | |
| 383 | command = ['ssh', |
| 384 | '-o','ControlPath %s' % _ssh_sock(), |
| 385 | '-p',str(port), |
| 386 | '-M', |
| 387 | '-N', |
| 388 | host] |
| 389 | try: |
| 390 | Trace(': %s', ' '.join(command)) |
| 391 | p = subprocess.Popen(command) |
| 392 | except Exception, e: |
| 393 | _ssh_master = False |
| 394 | print >>sys.stderr, \ |
| 395 | '\nwarn: cannot enable ssh control master for %s:%s\n%s' \ |
| 396 | % (host,port, str(e)) |
| 397 | return False |
| 398 | |
| 399 | _ssh_cache[key] = p |
| 400 | time.sleep(1) |
| 401 | return True |
| 402 | |
| 403 | def close_ssh(): |
| 404 | for key,p in _ssh_cache.iteritems(): |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 405 | try: |
| 406 | os.kill(p.pid, SIGTERM) |
| 407 | p.wait() |
Shawn O. Pearce | fb5c8fd | 2009-06-16 14:57:46 -0700 | [diff] [blame] | 408 | except OSError: |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 409 | pass |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 410 | _ssh_cache.clear() |
| 411 | |
| 412 | d = _ssh_sock(create=False) |
| 413 | if d: |
| 414 | try: |
| 415 | os.rmdir(os.path.dirname(d)) |
| 416 | except OSError: |
| 417 | pass |
| 418 | |
| 419 | URI_SCP = re.compile(r'^([^@:]*@?[^:/]{1,}):') |
Shawn O. Pearce | 2f968c9 | 2009-04-30 14:30:28 -0700 | [diff] [blame] | 420 | URI_ALL = re.compile(r'^([a-z][a-z+]*)://([^@/]*@?[^/]*)/') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 421 | |
| 422 | def _preconnect(url): |
| 423 | m = URI_ALL.match(url) |
| 424 | if m: |
| 425 | scheme = m.group(1) |
| 426 | host = m.group(2) |
| 427 | if ':' in host: |
| 428 | host, port = host.split(':') |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 429 | else: |
| 430 | port = 22 |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 431 | if scheme in ('ssh', 'git+ssh', 'ssh+git'): |
| 432 | return _open_ssh(host, port) |
| 433 | return False |
| 434 | |
| 435 | m = URI_SCP.match(url) |
| 436 | if m: |
| 437 | host = m.group(1) |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 438 | return _open_ssh(host, 22) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 439 | |
Shawn O. Pearce | 7b4f435 | 2009-06-12 09:06:35 -0700 | [diff] [blame] | 440 | return False |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 441 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 442 | class Remote(object): |
| 443 | """Configuration options related to a remote. |
| 444 | """ |
| 445 | def __init__(self, config, name): |
| 446 | self._config = config |
| 447 | self.name = name |
| 448 | self.url = self._Get('url') |
| 449 | self.review = self._Get('review') |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 450 | self.projectname = self._Get('projectname') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 451 | self.fetch = map(lambda x: RefSpec.FromString(x), |
| 452 | self._Get('fetch', all=True)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 453 | self._review_protocol = None |
| 454 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 455 | def PreConnectFetch(self): |
| 456 | return _preconnect(self.url) |
| 457 | |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 458 | @property |
| 459 | def ReviewProtocol(self): |
| 460 | if self._review_protocol is None: |
| 461 | if self.review is None: |
| 462 | return None |
| 463 | |
| 464 | u = self.review |
| 465 | if not u.startswith('http:') and not u.startswith('https:'): |
| 466 | u = 'http://%s' % u |
Shawn O. Pearce | 13cc384 | 2009-03-25 13:54:54 -0700 | [diff] [blame] | 467 | if u.endswith('/Gerrit'): |
| 468 | u = u[:len(u) - len('/Gerrit')] |
| 469 | if not u.endswith('/ssh_info'): |
| 470 | if not u.endswith('/'): |
| 471 | u += '/' |
| 472 | u += 'ssh_info' |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 473 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 474 | if u in REVIEW_CACHE: |
| 475 | info = REVIEW_CACHE[u] |
| 476 | self._review_protocol = info[0] |
| 477 | self._review_host = info[1] |
| 478 | self._review_port = info[2] |
| 479 | else: |
| 480 | try: |
| 481 | info = urlopen(u).read() |
| 482 | if info == 'NOT_AVAILABLE': |
| 483 | raise UploadError('Upload over ssh unavailable') |
| 484 | if '<' in info: |
| 485 | # Assume the server gave us some sort of HTML |
| 486 | # response back, like maybe a login page. |
| 487 | # |
| 488 | raise UploadError('Cannot read %s:\n%s' % (u, info)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 489 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 490 | self._review_protocol = 'ssh' |
| 491 | self._review_host = info.split(" ")[0] |
| 492 | self._review_port = info.split(" ")[1] |
| 493 | except HTTPError, e: |
| 494 | if e.code == 404: |
| 495 | self._review_protocol = 'http-post' |
| 496 | self._review_host = None |
| 497 | self._review_port = None |
| 498 | else: |
| 499 | raise UploadError('Cannot guess Gerrit version') |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 500 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 501 | REVIEW_CACHE[u] = ( |
| 502 | self._review_protocol, |
| 503 | self._review_host, |
| 504 | self._review_port) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 505 | return self._review_protocol |
| 506 | |
| 507 | def SshReviewUrl(self, userEmail): |
| 508 | if self.ReviewProtocol != 'ssh': |
| 509 | return None |
| 510 | return 'ssh://%s@%s:%s/%s' % ( |
| 511 | userEmail.split("@")[0], |
| 512 | self._review_host, |
| 513 | self._review_port, |
| 514 | self.projectname) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 515 | |
| 516 | def ToLocal(self, rev): |
| 517 | """Convert a remote revision string to something we have locally. |
| 518 | """ |
| 519 | if IsId(rev): |
| 520 | return rev |
| 521 | if rev.startswith(R_TAGS): |
| 522 | return rev |
| 523 | |
| 524 | if not rev.startswith('refs/'): |
| 525 | rev = R_HEADS + rev |
| 526 | |
| 527 | for spec in self.fetch: |
| 528 | if spec.SourceMatches(rev): |
| 529 | return spec.MapSource(rev) |
| 530 | raise GitError('remote %s does not have %s' % (self.name, rev)) |
| 531 | |
| 532 | def WritesTo(self, ref): |
| 533 | """True if the remote stores to the tracking ref. |
| 534 | """ |
| 535 | for spec in self.fetch: |
| 536 | if spec.DestMatches(ref): |
| 537 | return True |
| 538 | return False |
| 539 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 540 | def ResetFetch(self, mirror=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 541 | """Set the fetch refspec to its default value. |
| 542 | """ |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 543 | if mirror: |
| 544 | dst = 'refs/heads/*' |
| 545 | else: |
| 546 | dst = 'refs/remotes/%s/*' % self.name |
| 547 | self.fetch = [RefSpec(True, 'refs/heads/*', dst)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 548 | |
| 549 | def Save(self): |
| 550 | """Save this remote to the configuration. |
| 551 | """ |
| 552 | self._Set('url', self.url) |
| 553 | self._Set('review', self.review) |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 554 | self._Set('projectname', self.projectname) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 555 | self._Set('fetch', map(lambda x: str(x), self.fetch)) |
| 556 | |
| 557 | def _Set(self, key, value): |
| 558 | key = 'remote.%s.%s' % (self.name, key) |
| 559 | return self._config.SetString(key, value) |
| 560 | |
| 561 | def _Get(self, key, all=False): |
| 562 | key = 'remote.%s.%s' % (self.name, key) |
| 563 | return self._config.GetString(key, all = all) |
| 564 | |
| 565 | |
| 566 | class Branch(object): |
| 567 | """Configuration options related to a single branch. |
| 568 | """ |
| 569 | def __init__(self, config, name): |
| 570 | self._config = config |
| 571 | self.name = name |
| 572 | self.merge = self._Get('merge') |
| 573 | |
| 574 | r = self._Get('remote') |
| 575 | if r: |
| 576 | self.remote = self._config.GetRemote(r) |
| 577 | else: |
| 578 | self.remote = None |
| 579 | |
| 580 | @property |
| 581 | def LocalMerge(self): |
| 582 | """Convert the merge spec to a local name. |
| 583 | """ |
| 584 | if self.remote and self.merge: |
| 585 | return self.remote.ToLocal(self.merge) |
| 586 | return None |
| 587 | |
| 588 | def Save(self): |
| 589 | """Save this branch back into the configuration. |
| 590 | """ |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 591 | if self._config.HasSection('branch', self.name): |
| 592 | if self.remote: |
| 593 | self._Set('remote', self.remote.name) |
| 594 | else: |
| 595 | self._Set('remote', None) |
| 596 | self._Set('merge', self.merge) |
| 597 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 598 | else: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 599 | fd = open(self._config.file, 'ab') |
| 600 | try: |
| 601 | fd.write('[branch "%s"]\n' % self.name) |
| 602 | if self.remote: |
| 603 | fd.write('\tremote = %s\n' % self.remote.name) |
| 604 | if self.merge: |
| 605 | fd.write('\tmerge = %s\n' % self.merge) |
| 606 | finally: |
| 607 | fd.close() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 608 | |
| 609 | def _Set(self, key, value): |
| 610 | key = 'branch.%s.%s' % (self.name, key) |
| 611 | return self._config.SetString(key, value) |
| 612 | |
| 613 | def _Get(self, key, all=False): |
| 614 | key = 'branch.%s.%s' % (self.name, key) |
| 615 | return self._config.GetString(key, all = all) |