The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Copyright (C) 2008 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 15 | import contextlib |
| 16 | import errno |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 17 | from http.client import HTTPException |
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 |
Mike Frysinger | af1e5de | 2020-02-17 14:58:37 -0500 | [diff] [blame] | 21 | import signal |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 22 | import ssl |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 23 | import subprocess |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | import sys |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 25 | try: |
| 26 | import threading as _threading |
| 27 | except ImportError: |
| 28 | import dummy_threading as _threading |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 29 | import time |
Mike Frysinger | acf63b2 | 2019-06-13 02:24:21 -0400 | [diff] [blame] | 30 | import urllib.error |
| 31 | import urllib.request |
Shawn O. Pearce | f00e0ce | 2009-08-22 18:39:49 -0700 | [diff] [blame] | 32 | |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 33 | from error import GitError, UploadError |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 34 | import platform_utils |
Mike Frysinger | 8a11f6f | 2019-08-27 00:26:15 -0400 | [diff] [blame] | 35 | from repo_trace import Trace |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 36 | |
| 37 | from git_command import GitCommand |
| 38 | from git_command import ssh_sock |
| 39 | from git_command import terminate_ssh_clients |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 40 | from git_refs import R_CHANGES, R_HEADS, R_TAGS |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | |
David Pursehouse | 1d947b3 | 2012-10-25 12:23:11 +0900 | [diff] [blame] | 42 | ID_RE = re.compile(r'^[0-9a-f]{40}$') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 44 | REVIEW_CACHE = dict() |
| 45 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 46 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 47 | def IsChange(rev): |
| 48 | return rev.startswith(R_CHANGES) |
| 49 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 50 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 51 | def IsId(rev): |
| 52 | return ID_RE.match(rev) |
| 53 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 54 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 55 | def IsTag(rev): |
| 56 | return rev.startswith(R_TAGS) |
| 57 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 58 | |
Zac Livingston | 9ead97b | 2017-06-13 08:29:04 -0600 | [diff] [blame] | 59 | def IsImmutable(rev): |
| 60 | return IsChange(rev) or IsId(rev) or IsTag(rev) |
| 61 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 62 | |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 63 | def _key(name): |
| 64 | parts = name.split('.') |
| 65 | if len(parts) < 2: |
| 66 | return name.lower() |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 67 | parts[0] = parts[0].lower() |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 68 | parts[-1] = parts[-1].lower() |
| 69 | return '.'.join(parts) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 70 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 71 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | class GitConfig(object): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 73 | _ForUser = None |
| 74 | |
Mike Frysinger | f841ca4 | 2020-02-18 21:31:51 -0500 | [diff] [blame] | 75 | _USER_CONFIG = '~/.gitconfig' |
| 76 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | @classmethod |
| 78 | def ForUser(cls): |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 79 | if cls._ForUser is None: |
Mike Frysinger | f841ca4 | 2020-02-18 21:31:51 -0500 | [diff] [blame] | 80 | cls._ForUser = cls(configfile=os.path.expanduser(cls._USER_CONFIG)) |
Shawn O. Pearce | 90be5c0 | 2008-10-29 15:21:24 -0700 | [diff] [blame] | 81 | return cls._ForUser |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | |
| 83 | @classmethod |
| 84 | def ForRepository(cls, gitdir, defaults=None): |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 85 | return cls(configfile=os.path.join(gitdir, 'config'), |
| 86 | defaults=defaults) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 88 | def __init__(self, configfile, defaults=None, jsonFile=None): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 89 | self.file = configfile |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 90 | self.defaults = defaults |
| 91 | self._cache_dict = None |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 92 | self._section_dict = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 93 | self._remotes = {} |
| 94 | self._branches = {} |
Shawn O. Pearce | 1b34c91 | 2009-05-21 18:52:49 -0700 | [diff] [blame] | 95 | |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 96 | self._json = jsonFile |
| 97 | if self._json is None: |
| 98 | self._json = os.path.join( |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 99 | os.path.dirname(self.file), |
| 100 | '.repo_' + os.path.basename(self.file) + '.json') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 101 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 102 | def Has(self, name, include_defaults=True): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | """Return true if this configuration file has the key. |
| 104 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 105 | if _key(name) in self._cache: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 106 | return True |
| 107 | if include_defaults and self.defaults: |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 108 | return self.defaults.Has(name, include_defaults=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 109 | return False |
| 110 | |
Mike Frysinger | 77b4397 | 2020-02-19 17:55:22 -0500 | [diff] [blame] | 111 | def GetInt(self, name): |
| 112 | """Returns an integer from the configuration file. |
| 113 | |
| 114 | This follows the git config syntax. |
| 115 | |
| 116 | Args: |
| 117 | name: The key to lookup. |
| 118 | |
| 119 | Returns: |
| 120 | None if the value was not defined, or is not a boolean. |
| 121 | Otherwise, the number itself. |
| 122 | """ |
| 123 | v = self.GetString(name) |
| 124 | if v is None: |
| 125 | return None |
| 126 | v = v.strip() |
| 127 | |
| 128 | mult = 1 |
| 129 | if v.endswith('k'): |
| 130 | v = v[:-1] |
| 131 | mult = 1024 |
| 132 | elif v.endswith('m'): |
| 133 | v = v[:-1] |
| 134 | mult = 1024 * 1024 |
| 135 | elif v.endswith('g'): |
| 136 | v = v[:-1] |
| 137 | mult = 1024 * 1024 * 1024 |
| 138 | |
| 139 | base = 10 |
| 140 | if v.startswith('0x'): |
| 141 | base = 16 |
| 142 | |
| 143 | try: |
| 144 | return int(v, base=base) * mult |
| 145 | except ValueError: |
| 146 | return None |
| 147 | |
Ian Kasprzak | 835a34b | 2021-03-05 11:04:49 -0800 | [diff] [blame] | 148 | def DumpConfigDict(self): |
| 149 | """Returns the current configuration dict. |
| 150 | |
| 151 | Configuration data is information only (e.g. logging) and |
| 152 | should not be considered a stable data-source. |
| 153 | |
| 154 | Returns: |
| 155 | dict of {<key>, <value>} for git configuration cache. |
| 156 | <value> are strings converted by GetString. |
| 157 | """ |
| 158 | config_dict = {} |
| 159 | for key in self._cache: |
| 160 | config_dict[key] = self.GetString(key) |
| 161 | return config_dict |
| 162 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | def GetBoolean(self, name): |
| 164 | """Returns a boolean from the configuration file. |
| 165 | None : The value was not defined, or is not a boolean. |
| 166 | True : The value was set to true or yes. |
| 167 | False: The value was set to false or no. |
| 168 | """ |
| 169 | v = self.GetString(name) |
| 170 | if v is None: |
| 171 | return None |
| 172 | v = v.lower() |
| 173 | if v in ('true', 'yes'): |
| 174 | return True |
| 175 | if v in ('false', 'no'): |
| 176 | return False |
| 177 | return None |
| 178 | |
Mike Frysinger | 38867fb | 2021-02-09 23:14:41 -0500 | [diff] [blame] | 179 | def SetBoolean(self, name, value): |
| 180 | """Set the truthy value for a key.""" |
| 181 | if value is not None: |
| 182 | value = 'true' if value else 'false' |
| 183 | self.SetString(name, value) |
| 184 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 185 | def GetString(self, name, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | """Get the first value for a key, or None if it is not defined. |
| 187 | |
| 188 | This configuration file is used first, if the key is not |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 189 | 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] | 190 | """ |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 191 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 192 | v = self._cache[_key(name)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 193 | except KeyError: |
| 194 | if self.defaults: |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 195 | return self.defaults.GetString(name, all_keys=all_keys) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 196 | v = [] |
| 197 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 198 | if not all_keys: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | if v: |
| 200 | return v[0] |
| 201 | return None |
| 202 | |
| 203 | r = [] |
| 204 | r.extend(v) |
| 205 | if self.defaults: |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 206 | r.extend(self.defaults.GetString(name, all_keys=True)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 207 | return r |
| 208 | |
| 209 | def SetString(self, name, value): |
| 210 | """Set the value(s) for a key. |
| 211 | Only this configuration file is modified. |
| 212 | |
| 213 | The supplied value should be either a string, |
| 214 | or a list of strings (to store multiple values). |
| 215 | """ |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 216 | key = _key(name) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 217 | |
| 218 | try: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 219 | old = self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 220 | except KeyError: |
| 221 | old = [] |
| 222 | |
| 223 | if value is None: |
| 224 | if old: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 225 | del self._cache[key] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 226 | self._do('--unset-all', name) |
| 227 | |
| 228 | elif isinstance(value, list): |
| 229 | if len(value) == 0: |
| 230 | self.SetString(name, None) |
| 231 | |
| 232 | elif len(value) == 1: |
| 233 | self.SetString(name, value[0]) |
| 234 | |
| 235 | elif old != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 236 | self._cache[key] = list(value) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 237 | self._do('--replace-all', name, value[0]) |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 238 | for i in range(1, len(value)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | self._do('--add', name, value[i]) |
| 240 | |
| 241 | elif len(old) != 1 or old[0] != value: |
Shawn O. Pearce | f8e3273 | 2009-04-17 11:00:31 -0700 | [diff] [blame] | 242 | self._cache[key] = [value] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 243 | self._do('--replace-all', name, value) |
| 244 | |
| 245 | def GetRemote(self, name): |
| 246 | """Get the remote.$name.* configuration values as an object. |
| 247 | """ |
| 248 | try: |
| 249 | r = self._remotes[name] |
| 250 | except KeyError: |
| 251 | r = Remote(self, name) |
| 252 | self._remotes[r.name] = r |
| 253 | return r |
| 254 | |
| 255 | def GetBranch(self, name): |
| 256 | """Get the branch.$name.* configuration values as an object. |
| 257 | """ |
| 258 | try: |
| 259 | b = self._branches[name] |
| 260 | except KeyError: |
| 261 | b = Branch(self, name) |
| 262 | self._branches[b.name] = b |
| 263 | return b |
| 264 | |
Shawn O. Pearce | 366ad21 | 2009-05-19 12:47:37 -0700 | [diff] [blame] | 265 | def GetSubSections(self, section): |
| 266 | """List all subsection names matching $section.*.* |
| 267 | """ |
| 268 | return self._sections.get(section, set()) |
| 269 | |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 270 | def HasSection(self, section, subsection=''): |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 271 | """Does at least one key in section.subsection exist? |
| 272 | """ |
| 273 | try: |
| 274 | return subsection in self._sections[section] |
| 275 | except KeyError: |
| 276 | return False |
| 277 | |
Shawn O. Pearce | 13111b4 | 2011-09-19 11:00:31 -0700 | [diff] [blame] | 278 | def UrlInsteadOf(self, url): |
| 279 | """Resolve any url.*.insteadof references. |
| 280 | """ |
| 281 | for new_url in self.GetSubSections('url'): |
Dan Willemsen | 4e4d40f | 2013-10-28 22:28:42 -0700 | [diff] [blame] | 282 | for old_url in self.GetString('url.%s.insteadof' % new_url, True): |
| 283 | if old_url is not None and url.startswith(old_url): |
| 284 | return new_url + url[len(old_url):] |
Shawn O. Pearce | 13111b4 | 2011-09-19 11:00:31 -0700 | [diff] [blame] | 285 | return url |
| 286 | |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 287 | @property |
| 288 | def _sections(self): |
| 289 | d = self._section_dict |
| 290 | if d is None: |
| 291 | d = {} |
| 292 | for name in self._cache.keys(): |
| 293 | p = name.split('.') |
| 294 | if 2 == len(p): |
| 295 | section = p[0] |
| 296 | subsect = '' |
| 297 | else: |
| 298 | section = p[0] |
| 299 | subsect = '.'.join(p[1:-1]) |
| 300 | if section not in d: |
| 301 | d[section] = set() |
| 302 | d[section].add(subsect) |
| 303 | self._section_dict = d |
| 304 | return d |
| 305 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 306 | @property |
| 307 | def _cache(self): |
| 308 | if self._cache_dict is None: |
| 309 | self._cache_dict = self._Read() |
| 310 | return self._cache_dict |
| 311 | |
| 312 | def _Read(self): |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 313 | d = self._ReadJson() |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 314 | if d is None: |
| 315 | d = self._ReadGit() |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 316 | self._SaveJson(d) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 317 | return d |
| 318 | |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 319 | def _ReadJson(self): |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 320 | try: |
David Pursehouse | 16a5c3a | 2020-02-12 15:54:26 +0900 | [diff] [blame] | 321 | if os.path.getmtime(self._json) <= os.path.getmtime(self.file): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 322 | platform_utils.remove(self._json) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 323 | return None |
| 324 | except OSError: |
| 325 | return None |
| 326 | try: |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 327 | Trace(': parsing %s', self.file) |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 328 | with open(self._json) as fd: |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 329 | return json.load(fd) |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 330 | except (IOError, ValueError): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 331 | platform_utils.remove(self._json) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 332 | return None |
| 333 | |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 334 | def _SaveJson(self, cache): |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 335 | try: |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 336 | with open(self._json, 'w') as fd: |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 337 | json.dump(cache, fd, indent=2) |
Anthony King | 85b24ac | 2014-05-06 15:57:48 +0100 | [diff] [blame] | 338 | except (IOError, TypeError): |
Anthony King | b1d1fd7 | 2015-06-03 17:02:26 +0100 | [diff] [blame] | 339 | if os.path.exists(self._json): |
Renaud Paquay | 010fed7 | 2016-11-11 14:25:29 -0800 | [diff] [blame] | 340 | platform_utils.remove(self._json) |
Shawn O. Pearce | c12c360 | 2009-04-17 21:03:32 -0700 | [diff] [blame] | 341 | |
| 342 | def _ReadGit(self): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 343 | """ |
| 344 | Read configuration data from git. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 346 | This internal method populates the GitConfig cache. |
| 347 | |
| 348 | """ |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 349 | c = {} |
Shawn O. Pearce | c24c720 | 2009-07-02 16:12:57 -0700 | [diff] [blame] | 350 | d = self._do('--null', '--list') |
| 351 | if d is None: |
| 352 | return c |
Dylan Deng | e469a0c | 2018-06-23 15:02:26 +0800 | [diff] [blame] | 353 | for line in d.rstrip('\0').split('\0'): |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 354 | if '\n' in line: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 355 | key, val = line.split('\n', 1) |
David Aguilar | 438c547 | 2009-06-28 15:09:16 -0700 | [diff] [blame] | 356 | else: |
David Pursehouse | c1b86a2 | 2012-11-14 11:36:51 +0900 | [diff] [blame] | 357 | key = line |
| 358 | val = None |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 | |
| 360 | if key in c: |
| 361 | c[key].append(val) |
| 362 | else: |
| 363 | c[key] = [val] |
| 364 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 365 | return c |
| 366 | |
| 367 | def _do(self, *args): |
Ulrik Laurén | d0ca0f6 | 2020-04-28 01:09:57 +0200 | [diff] [blame] | 368 | command = ['config', '--file', self.file, '--includes'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 369 | command.extend(args) |
| 370 | |
| 371 | p = GitCommand(None, |
| 372 | command, |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 373 | capture_stdout=True, |
| 374 | capture_stderr=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 375 | if p.Wait() == 0: |
| 376 | return p.stdout |
| 377 | else: |
| 378 | GitError('git config %s: %s' % (str(args), p.stderr)) |
| 379 | |
| 380 | |
Mike Frysinger | f841ca4 | 2020-02-18 21:31:51 -0500 | [diff] [blame] | 381 | class RepoConfig(GitConfig): |
| 382 | """User settings for repo itself.""" |
| 383 | |
| 384 | _USER_CONFIG = '~/.repoconfig/config' |
| 385 | |
| 386 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 387 | class RefSpec(object): |
| 388 | """A Git refspec line, split into its components: |
| 389 | |
| 390 | forced: True if the line starts with '+' |
| 391 | src: Left side of the line |
| 392 | dst: Right side of the line |
| 393 | """ |
| 394 | |
| 395 | @classmethod |
| 396 | def FromString(cls, rs): |
| 397 | lhs, rhs = rs.split(':', 2) |
| 398 | if lhs.startswith('+'): |
| 399 | lhs = lhs[1:] |
| 400 | forced = True |
| 401 | else: |
| 402 | forced = False |
| 403 | return cls(forced, lhs, rhs) |
| 404 | |
| 405 | def __init__(self, forced, lhs, rhs): |
| 406 | self.forced = forced |
| 407 | self.src = lhs |
| 408 | self.dst = rhs |
| 409 | |
| 410 | def SourceMatches(self, rev): |
| 411 | if self.src: |
| 412 | if rev == self.src: |
| 413 | return True |
| 414 | if self.src.endswith('/*') and rev.startswith(self.src[:-1]): |
| 415 | return True |
| 416 | return False |
| 417 | |
| 418 | def DestMatches(self, ref): |
| 419 | if self.dst: |
| 420 | if ref == self.dst: |
| 421 | return True |
| 422 | if self.dst.endswith('/*') and ref.startswith(self.dst[:-1]): |
| 423 | return True |
| 424 | return False |
| 425 | |
| 426 | def MapSource(self, rev): |
| 427 | if self.src.endswith('/*'): |
| 428 | return self.dst[:-1] + rev[len(self.src) - 1:] |
| 429 | return self.dst |
| 430 | |
| 431 | def __str__(self): |
| 432 | s = '' |
| 433 | if self.forced: |
| 434 | s += '+' |
| 435 | if self.src: |
| 436 | s += self.src |
| 437 | if self.dst: |
| 438 | s += ':' |
| 439 | s += self.dst |
| 440 | return s |
| 441 | |
| 442 | |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 443 | _master_processes = [] |
| 444 | _master_keys = set() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 445 | _ssh_master = True |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 446 | _master_keys_lock = None |
| 447 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 448 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 449 | def init_ssh(): |
| 450 | """Should be called once at the start of repo to init ssh master handling. |
| 451 | |
| 452 | At the moment, all we do is to create our lock. |
| 453 | """ |
| 454 | global _master_keys_lock |
| 455 | assert _master_keys_lock is None, "Should only call init_ssh once" |
| 456 | _master_keys_lock = _threading.Lock() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 457 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 458 | |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 459 | def _open_ssh(host, port=None): |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 460 | global _ssh_master |
| 461 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 462 | # Acquire the lock. This is needed to prevent opening multiple masters for |
| 463 | # the same host when we're running "repo sync -jN" (for N > 1) _and_ the |
| 464 | # manifest <remote fetch="ssh://xyz"> specifies a different host from the |
| 465 | # one that was passed to repo init. |
| 466 | _master_keys_lock.acquire() |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 467 | try: |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 468 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 469 | # Check to see whether we already think that the master is running; if we |
| 470 | # think it's already running, return right away. |
| 471 | if port is not None: |
| 472 | key = '%s:%s' % (host, port) |
| 473 | else: |
| 474 | key = host |
| 475 | |
| 476 | if key in _master_keys: |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 477 | return True |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 478 | |
David Pursehouse | 16a5c3a | 2020-02-12 15:54:26 +0900 | [diff] [blame] | 479 | if (not _ssh_master |
| 480 | or 'GIT_SSH' in os.environ |
| 481 | or sys.platform in ('win32', 'cygwin')): |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 482 | # failed earlier, or cygwin ssh can't do this |
| 483 | # |
| 484 | return False |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 485 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 486 | # We will make two calls to ssh; this is the common part of both calls. |
| 487 | command_base = ['ssh', |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 488 | '-o', 'ControlPath %s' % ssh_sock(), |
| 489 | host] |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 490 | if port is not None: |
David Pursehouse | 8f62fb7 | 2012-11-14 12:09:38 +0900 | [diff] [blame] | 491 | command_base[1:1] = ['-p', str(port)] |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 492 | |
| 493 | # Since the key wasn't in _master_keys, we think that master isn't running. |
| 494 | # ...but before actually starting a master, we'll double-check. This can |
| 495 | # be important because we can't tell that that 'git@myhost.com' is the same |
| 496 | # as 'myhost.com' where "User git" is setup in the user's ~/.ssh/config file. |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 497 | check_command = command_base + ['-O', 'check'] |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 498 | try: |
| 499 | Trace(': %s', ' '.join(check_command)) |
| 500 | check_process = subprocess.Popen(check_command, |
| 501 | stdout=subprocess.PIPE, |
| 502 | stderr=subprocess.PIPE) |
David Pursehouse | 54a4e60 | 2020-02-12 14:31:05 +0900 | [diff] [blame] | 503 | check_process.communicate() # read output, but ignore it... |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 504 | isnt_running = check_process.wait() |
| 505 | |
| 506 | if not isnt_running: |
| 507 | # Our double-check found that the master _was_ infact running. Add to |
| 508 | # the list of keys. |
| 509 | _master_keys.add(key) |
| 510 | return True |
| 511 | except Exception: |
| 512 | # Ignore excpetions. We we will fall back to the normal command and print |
| 513 | # to the log there. |
| 514 | pass |
| 515 | |
David Pursehouse | 0ab95ba | 2020-02-12 15:01:59 +0900 | [diff] [blame] | 516 | command = command_base[:1] + ['-M', '-N'] + command_base[1:] |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 517 | try: |
| 518 | Trace(': %s', ' '.join(command)) |
| 519 | p = subprocess.Popen(command) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 520 | except Exception as e: |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 521 | _ssh_master = False |
Sarah Owens | cecd1d8 | 2012-11-01 22:59:27 -0700 | [diff] [blame] | 522 | print('\nwarn: cannot enable ssh control master for %s:%s\n%s' |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 523 | % (host, port, str(e)), file=sys.stderr) |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 524 | return False |
| 525 | |
Timo Lotterbach | 05dc46b | 2016-07-15 16:48:42 +0200 | [diff] [blame] | 526 | time.sleep(1) |
| 527 | ssh_died = (p.poll() is not None) |
| 528 | if ssh_died: |
| 529 | return False |
| 530 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 531 | _master_processes.append(p) |
| 532 | _master_keys.add(key) |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 533 | return True |
| 534 | finally: |
| 535 | _master_keys_lock.release() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 536 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 537 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 538 | def close_ssh(): |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 539 | global _master_keys_lock |
| 540 | |
Shawn O. Pearce | ca8c32c | 2010-05-11 18:21:33 -0700 | [diff] [blame] | 541 | terminate_ssh_clients() |
| 542 | |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 543 | for p in _master_processes: |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 544 | try: |
Mike Frysinger | af1e5de | 2020-02-17 14:58:37 -0500 | [diff] [blame] | 545 | os.kill(p.pid, signal.SIGTERM) |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 546 | p.wait() |
Shawn O. Pearce | fb5c8fd | 2009-06-16 14:57:46 -0700 | [diff] [blame] | 547 | except OSError: |
Shawn O. Pearce | 26120ca | 2009-06-16 11:49:10 -0700 | [diff] [blame] | 548 | pass |
Doug Anderson | 06d029c | 2010-10-27 17:06:01 -0700 | [diff] [blame] | 549 | del _master_processes[:] |
| 550 | _master_keys.clear() |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 551 | |
Nico Sallembien | 1c85f4e | 2010-04-27 14:35:27 -0700 | [diff] [blame] | 552 | d = ssh_sock(create=False) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 553 | if d: |
| 554 | try: |
Renaud Paquay | bed8b62 | 2018-09-27 10:46:58 -0700 | [diff] [blame] | 555 | platform_utils.rmdir(os.path.dirname(d)) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 556 | except OSError: |
| 557 | pass |
| 558 | |
Doug Anderson | 0048b69 | 2010-12-21 13:39:23 -0800 | [diff] [blame] | 559 | # We're done with the lock, so we can delete it. |
| 560 | _master_keys_lock = None |
| 561 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 562 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 563 | URI_SCP = re.compile(r'^([^@:]*@?[^:/]{1,}):') |
Shawn O. Pearce | 898e12a | 2012-03-14 15:22:28 -0700 | [diff] [blame] | 564 | URI_ALL = re.compile(r'^([a-z][a-z+-]*)://([^@/]*@?[^/]*)/') |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 565 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 566 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 567 | def GetSchemeFromUrl(url): |
| 568 | m = URI_ALL.match(url) |
| 569 | if m: |
| 570 | return m.group(1) |
| 571 | return None |
| 572 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 573 | |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 574 | @contextlib.contextmanager |
| 575 | def GetUrlCookieFile(url, quiet): |
| 576 | if url.startswith('persistent-'): |
| 577 | try: |
| 578 | p = subprocess.Popen( |
| 579 | ['git-remote-persistent-https', '-print_config', url], |
| 580 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, |
| 581 | stderr=subprocess.PIPE) |
| 582 | try: |
| 583 | cookieprefix = 'http.cookiefile=' |
| 584 | proxyprefix = 'http.proxy=' |
| 585 | cookiefile = None |
| 586 | proxy = None |
| 587 | for line in p.stdout: |
Mike Frysinger | ded477d | 2020-02-07 23:18:23 -0500 | [diff] [blame] | 588 | line = line.strip().decode('utf-8') |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 589 | if line.startswith(cookieprefix): |
Daichi Ueura | ce7e026 | 2018-02-26 08:49:36 +0900 | [diff] [blame] | 590 | cookiefile = os.path.expanduser(line[len(cookieprefix):]) |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 591 | if line.startswith(proxyprefix): |
| 592 | proxy = line[len(proxyprefix):] |
| 593 | # Leave subprocess open, as cookie file may be transient. |
| 594 | if cookiefile or proxy: |
| 595 | yield cookiefile, proxy |
| 596 | return |
| 597 | finally: |
| 598 | p.stdin.close() |
| 599 | if p.wait(): |
Mike Frysinger | ded477d | 2020-02-07 23:18:23 -0500 | [diff] [blame] | 600 | err_msg = p.stderr.read().decode('utf-8') |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 601 | if ' -print_config' in err_msg: |
| 602 | pass # Persistent proxy doesn't support -print_config. |
| 603 | elif not quiet: |
| 604 | print(err_msg, file=sys.stderr) |
| 605 | except OSError as e: |
| 606 | if e.errno == errno.ENOENT: |
| 607 | pass # No persistent proxy. |
| 608 | raise |
Daichi Ueura | ce7e026 | 2018-02-26 08:49:36 +0900 | [diff] [blame] | 609 | cookiefile = GitConfig.ForUser().GetString('http.cookiefile') |
| 610 | if cookiefile: |
| 611 | cookiefile = os.path.expanduser(cookiefile) |
| 612 | yield cookiefile, None |
Dan Willemsen | 0745bb2 | 2015-08-17 13:41:45 -0700 | [diff] [blame] | 613 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 614 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 615 | def _preconnect(url): |
| 616 | m = URI_ALL.match(url) |
| 617 | if m: |
| 618 | scheme = m.group(1) |
| 619 | host = m.group(2) |
| 620 | if ':' in host: |
| 621 | host, port = host.split(':') |
Shawn O. Pearce | 896d5df | 2009-04-21 14:51:04 -0700 | [diff] [blame] | 622 | else: |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 623 | port = None |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 624 | if scheme in ('ssh', 'git+ssh', 'ssh+git'): |
| 625 | return _open_ssh(host, port) |
| 626 | return False |
| 627 | |
| 628 | m = URI_SCP.match(url) |
| 629 | if m: |
| 630 | host = m.group(1) |
Josh Guilfoyle | 7198572 | 2009-08-16 09:44:40 -0700 | [diff] [blame] | 631 | return _open_ssh(host) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 632 | |
Shawn O. Pearce | 7b4f435 | 2009-06-12 09:06:35 -0700 | [diff] [blame] | 633 | return False |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 634 | |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 635 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 636 | class Remote(object): |
| 637 | """Configuration options related to a remote. |
| 638 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 639 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 640 | def __init__(self, config, name): |
| 641 | self._config = config |
| 642 | self.name = name |
| 643 | self.url = self._Get('url') |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 644 | self.pushUrl = self._Get('pushurl') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 645 | self.review = self._Get('review') |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 646 | self.projectname = self._Get('projectname') |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 647 | self.fetch = list(map(RefSpec.FromString, |
David Pursehouse | abdf750 | 2020-02-12 14:58:39 +0900 | [diff] [blame] | 648 | self._Get('fetch', all_keys=True))) |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 649 | self._review_url = None |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 650 | |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 651 | def _InsteadOf(self): |
| 652 | globCfg = GitConfig.ForUser() |
| 653 | urlList = globCfg.GetSubSections('url') |
| 654 | longest = "" |
| 655 | longestUrl = "" |
| 656 | |
| 657 | for url in urlList: |
| 658 | key = "url." + url + ".insteadOf" |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 659 | insteadOfList = globCfg.GetString(key, all_keys=True) |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 660 | |
| 661 | for insteadOf in insteadOfList: |
David Pursehouse | 16a5c3a | 2020-02-12 15:54:26 +0900 | [diff] [blame] | 662 | if (self.url.startswith(insteadOf) |
| 663 | and len(insteadOf) > len(longest)): |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 664 | longest = insteadOf |
| 665 | longestUrl = url |
| 666 | |
| 667 | if len(longest) == 0: |
| 668 | return self.url |
| 669 | |
| 670 | return self.url.replace(longest, longestUrl, 1) |
| 671 | |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 672 | def PreConnectFetch(self): |
Ulrik Sjolin | b6ea3bf | 2010-01-03 18:20:17 +0100 | [diff] [blame] | 673 | connectionUrl = self._InsteadOf() |
| 674 | return _preconnect(connectionUrl) |
Shawn O. Pearce | fb23161 | 2009-04-10 18:53:46 -0700 | [diff] [blame] | 675 | |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 676 | def ReviewUrl(self, userEmail, validate_certs): |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 677 | if self._review_url is None: |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 678 | if self.review is None: |
| 679 | return None |
| 680 | |
| 681 | u = self.review |
Conley Owens | 7e12e0a | 2014-10-23 15:40:00 -0700 | [diff] [blame] | 682 | if u.startswith('persistent-'): |
| 683 | u = u[len('persistent-'):] |
Christian Koestlin | 2ec2a5d | 2016-12-05 20:32:45 +0100 | [diff] [blame] | 684 | if u.split(':')[0] not in ('http', 'https', 'sso', 'ssh'): |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 685 | u = 'http://%s' % u |
Shawn O. Pearce | 13cc384 | 2009-03-25 13:54:54 -0700 | [diff] [blame] | 686 | if u.endswith('/Gerrit'): |
| 687 | u = u[:len(u) - len('/Gerrit')] |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 688 | if u.endswith('/ssh_info'): |
| 689 | u = u[:len(u) - len('/ssh_info')] |
| 690 | if not u.endswith('/'): |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 691 | u += '/' |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 692 | http_url = u |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 693 | |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 694 | if u in REVIEW_CACHE: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 695 | self._review_url = REVIEW_CACHE[u] |
Shawn O. Pearce | 1a68dc5 | 2011-10-11 14:12:46 -0700 | [diff] [blame] | 696 | elif 'REPO_HOST_PORT_INFO' in os.environ: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 697 | host, port = os.environ['REPO_HOST_PORT_INFO'].split() |
| 698 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
| 699 | REVIEW_CACHE[u] = self._review_url |
Christian Koestlin | 2ec2a5d | 2016-12-05 20:32:45 +0100 | [diff] [blame] | 700 | elif u.startswith('sso:') or u.startswith('ssh:'): |
Steve Pucci | 143d8a7 | 2014-01-30 09:45:53 -0800 | [diff] [blame] | 701 | self._review_url = u # Assume it's right |
| 702 | REVIEW_CACHE[u] = self._review_url |
Timo Lotterbach | eec726c | 2016-10-07 10:52:08 +0200 | [diff] [blame] | 703 | elif 'REPO_IGNORE_SSH_INFO' in os.environ: |
| 704 | self._review_url = http_url |
| 705 | REVIEW_CACHE[u] = self._review_url |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 706 | else: |
| 707 | try: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 708 | info_url = u + 'ssh_info' |
Łukasz Gardoń | bed59ce | 2017-08-08 10:18:11 +0200 | [diff] [blame] | 709 | if not validate_certs: |
| 710 | context = ssl._create_unverified_context() |
| 711 | info = urllib.request.urlopen(info_url, context=context).read() |
| 712 | else: |
| 713 | info = urllib.request.urlopen(info_url).read() |
Mike Frysinger | 1b9adab | 2019-07-04 17:54:54 -0400 | [diff] [blame] | 714 | if info == b'NOT_AVAILABLE' or b'<' in info: |
Conley Owens | 745a39b | 2013-06-05 13:16:18 -0700 | [diff] [blame] | 715 | # If `info` contains '<', we assume the server gave us some sort |
| 716 | # of HTML response back, like maybe a login page. |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 717 | # |
Conley Owens | 745a39b | 2013-06-05 13:16:18 -0700 | [diff] [blame] | 718 | # 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] | 719 | self._review_url = http_url |
Shawn O. Pearce | 146fe90 | 2009-03-25 14:06:43 -0700 | [diff] [blame] | 720 | else: |
Mike Frysinger | 1b9adab | 2019-07-04 17:54:54 -0400 | [diff] [blame] | 721 | info = info.decode('utf-8') |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 722 | host, port = info.split() |
Dan Willemsen | 16889ba | 2016-09-22 16:39:06 +0000 | [diff] [blame] | 723 | self._review_url = self._SshReviewUrl(userEmail, host, port) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 724 | except urllib.error.HTTPError as e: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 725 | raise UploadError('%s: %s' % (self.review, str(e))) |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 726 | except urllib.error.URLError as e: |
Shawn O. Pearce | bf1fbb2 | 2011-10-11 09:31:58 -0700 | [diff] [blame] | 727 | raise UploadError('%s: %s' % (self.review, str(e))) |
David Pursehouse | ecf8f2b | 2013-05-24 12:12:23 +0900 | [diff] [blame] | 728 | except HTTPException as e: |
| 729 | raise UploadError('%s: %s' % (self.review, e.__class__.__name__)) |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 730 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 731 | REVIEW_CACHE[u] = self._review_url |
| 732 | return self._review_url + self.projectname |
Shawn O. Pearce | b54a392 | 2009-01-05 16:18:58 -0800 | [diff] [blame] | 733 | |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 734 | def _SshReviewUrl(self, userEmail, host, port): |
Shawn O. Pearce | 3575b8f | 2010-07-15 17:00:14 -0700 | [diff] [blame] | 735 | username = self._config.GetString('review.%s.username' % self.review) |
| 736 | if username is None: |
Shawn O. Pearce | c957142 | 2012-01-11 14:58:54 -0800 | [diff] [blame] | 737 | username = userEmail.split('@')[0] |
| 738 | return 'ssh://%s@%s:%s/' % (username, host, port) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 739 | |
| 740 | def ToLocal(self, rev): |
| 741 | """Convert a remote revision string to something we have locally. |
| 742 | """ |
Yann Droneaud | 936183a | 2013-09-12 10:51:18 +0200 | [diff] [blame] | 743 | if self.name == '.' or IsId(rev): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 744 | return rev |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | |
| 746 | if not rev.startswith('refs/'): |
| 747 | rev = R_HEADS + rev |
| 748 | |
| 749 | for spec in self.fetch: |
| 750 | if spec.SourceMatches(rev): |
| 751 | return spec.MapSource(rev) |
Nasser Grainawi | 909d58b | 2014-09-19 12:13:04 -0600 | [diff] [blame] | 752 | |
| 753 | if not rev.startswith(R_HEADS): |
| 754 | return rev |
| 755 | |
Mike Frysinger | 1f2462e | 2019-08-03 01:57:09 -0400 | [diff] [blame] | 756 | raise GitError('%s: remote %s does not have %s' % |
| 757 | (self.projectname, self.name, rev)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 758 | |
| 759 | def WritesTo(self, ref): |
| 760 | """True if the remote stores to the tracking ref. |
| 761 | """ |
| 762 | for spec in self.fetch: |
| 763 | if spec.DestMatches(ref): |
| 764 | return True |
| 765 | return False |
| 766 | |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 767 | def ResetFetch(self, mirror=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 768 | """Set the fetch refspec to its default value. |
| 769 | """ |
Shawn O. Pearce | e284ad1 | 2008-11-04 07:37:10 -0800 | [diff] [blame] | 770 | if mirror: |
| 771 | dst = 'refs/heads/*' |
| 772 | else: |
| 773 | dst = 'refs/remotes/%s/*' % self.name |
| 774 | self.fetch = [RefSpec(True, 'refs/heads/*', dst)] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 775 | |
| 776 | def Save(self): |
| 777 | """Save this remote to the configuration. |
| 778 | """ |
| 779 | self._Set('url', self.url) |
Steve Rae | d648045 | 2016-08-10 15:00:00 -0700 | [diff] [blame] | 780 | if self.pushUrl is not None: |
| 781 | self._Set('pushurl', self.pushUrl + '/' + self.projectname) |
| 782 | else: |
| 783 | self._Set('pushurl', self.pushUrl) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 784 | self._Set('review', self.review) |
Shawn O. Pearce | 339ba9f | 2008-11-06 09:52:51 -0800 | [diff] [blame] | 785 | self._Set('projectname', self.projectname) |
Chirayu Desai | 217ea7d | 2013-03-01 19:14:38 +0530 | [diff] [blame] | 786 | self._Set('fetch', list(map(str, self.fetch))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 787 | |
| 788 | def _Set(self, key, value): |
| 789 | key = 'remote.%s.%s' % (self.name, key) |
| 790 | return self._config.SetString(key, value) |
| 791 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 792 | def _Get(self, key, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 793 | key = 'remote.%s.%s' % (self.name, key) |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 794 | return self._config.GetString(key, all_keys=all_keys) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 795 | |
| 796 | |
| 797 | class Branch(object): |
| 798 | """Configuration options related to a single branch. |
| 799 | """ |
David Pursehouse | 819827a | 2020-02-12 15:20:19 +0900 | [diff] [blame] | 800 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 801 | def __init__(self, config, name): |
| 802 | self._config = config |
| 803 | self.name = name |
| 804 | self.merge = self._Get('merge') |
| 805 | |
| 806 | r = self._Get('remote') |
| 807 | if r: |
| 808 | self.remote = self._config.GetRemote(r) |
| 809 | else: |
| 810 | self.remote = None |
| 811 | |
| 812 | @property |
| 813 | def LocalMerge(self): |
| 814 | """Convert the merge spec to a local name. |
| 815 | """ |
| 816 | if self.remote and self.merge: |
| 817 | return self.remote.ToLocal(self.merge) |
| 818 | return None |
| 819 | |
| 820 | def Save(self): |
| 821 | """Save this branch back into the configuration. |
| 822 | """ |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 823 | if self._config.HasSection('branch', self.name): |
| 824 | if self.remote: |
| 825 | self._Set('remote', self.remote.name) |
| 826 | else: |
| 827 | self._Set('remote', None) |
| 828 | self._Set('merge', self.merge) |
| 829 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 830 | else: |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 831 | with open(self._config.file, 'a') as fd: |
Shawn O. Pearce | accc56d | 2009-04-18 14:45:51 -0700 | [diff] [blame] | 832 | fd.write('[branch "%s"]\n' % self.name) |
| 833 | if self.remote: |
| 834 | fd.write('\tremote = %s\n' % self.remote.name) |
| 835 | if self.merge: |
| 836 | fd.write('\tmerge = %s\n' % self.merge) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 837 | |
| 838 | def _Set(self, key, value): |
| 839 | key = 'branch.%s.%s' % (self.name, key) |
| 840 | return self._config.SetString(key, value) |
| 841 | |
David Pursehouse | 8a68ff9 | 2012-09-24 12:15:13 +0900 | [diff] [blame] | 842 | def _Get(self, key, all_keys=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 843 | key = 'branch.%s.%s' % (self.name, key) |
David Pursehouse | e5913ae | 2020-02-12 13:56:59 +0900 | [diff] [blame] | 844 | return self._config.GetString(key, all_keys=all_keys) |