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