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