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