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