Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | """Repo utility. |
| 6 | |
| 7 | This module provides wrapper for "repo" (a Google-built repository management |
| 8 | tool that runs on top of git) and related utility functions. |
| 9 | """ |
| 10 | |
| 11 | from __future__ import print_function |
| 12 | import logging |
| 13 | import os |
| 14 | import re |
Kuang-che Wu | 34ab7b4 | 2019-10-28 19:40:05 +0800 | [diff] [blame] | 15 | import subprocess |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 16 | import xml.etree.ElementTree |
| 17 | |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 18 | from six.moves import urllib |
| 19 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 20 | from bisect_kit import codechange |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 21 | from bisect_kit import errors |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 22 | from bisect_kit import git_util |
| 23 | from bisect_kit import util |
| 24 | |
| 25 | logger = logging.getLogger(__name__) |
| 26 | |
| 27 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 28 | def get_manifest_url(manifest_dir): |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 29 | """Get manifest URL of repo project. |
| 30 | |
| 31 | Args: |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 32 | manifest_dir: path of manifest directory |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 33 | |
| 34 | Returns: |
| 35 | manifest URL. |
| 36 | """ |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 37 | url = util.check_output( |
| 38 | 'git', 'config', 'remote.origin.url', cwd=manifest_dir) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 39 | return url |
| 40 | |
| 41 | |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 42 | def find_repo_root(path): |
| 43 | """Find the root path of a repo project |
| 44 | |
| 45 | Args: |
| 46 | path: path |
| 47 | |
| 48 | Returns: |
| 49 | project root if path is inside a repo project; otherwise None |
| 50 | """ |
| 51 | path = os.path.abspath(path) |
| 52 | while not os.path.exists(os.path.join(path, '.repo')): |
| 53 | if path == '/': |
| 54 | return None |
| 55 | path = os.path.dirname(path) |
| 56 | return path |
| 57 | |
| 58 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 59 | def init(repo_dir, |
| 60 | manifest_url, |
| 61 | manifest_branch=None, |
| 62 | manifest_name=None, |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 63 | repo_url=None, |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 64 | reference=None, |
| 65 | mirror=False): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 66 | """Repo init. |
| 67 | |
| 68 | Args: |
| 69 | repo_dir: root directory of repo |
| 70 | manifest_url: manifest repository location |
| 71 | manifest_branch: manifest branch or revision |
| 72 | manifest_name: initial manifest file name |
| 73 | repo_url: repo repository location |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 74 | reference: location of mirror directory |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 75 | mirror: indicates repo mirror |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 76 | """ |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 77 | root = find_repo_root(repo_dir) |
| 78 | if root and root != repo_dir: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 79 | raise errors.ExternalError( |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 80 | '%s should not be inside another repo project at %s' % (repo_dir, root)) |
| 81 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 82 | cmd = ['repo', 'init', '--manifest-url', manifest_url] |
| 83 | if manifest_name: |
| 84 | cmd += ['--manifest-name', manifest_name] |
| 85 | if manifest_branch: |
| 86 | cmd += ['--manifest-branch', manifest_branch] |
| 87 | if repo_url: |
| 88 | cmd += ['--repo-url', repo_url] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 89 | if reference: |
| 90 | cmd += ['--reference', reference] |
Kuang-che Wu | 41e8b59 | 2018-09-25 17:01:30 +0800 | [diff] [blame] | 91 | if mirror: |
| 92 | cmd.append('--mirror') |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 93 | util.check_call(*cmd, cwd=repo_dir) |
| 94 | |
| 95 | |
Kuang-che Wu | ea3abce | 2018-10-04 17:50:42 +0800 | [diff] [blame] | 96 | def cleanup_repo_generated_files(repo_dir, manifest_name='default.xml'): |
| 97 | """Cleanup files generated by <copyfile> <linkfile> tags. |
| 98 | |
| 99 | Args: |
| 100 | repo_dir: root directory of repo |
| 101 | manifest_name: filename of manifest |
| 102 | """ |
| 103 | manifest_dir = os.path.join(repo_dir, '.repo', 'manifests') |
Kuang-che Wu | 35080a7 | 2018-10-05 14:14:33 +0800 | [diff] [blame] | 104 | manifest_path = os.path.join(manifest_dir, manifest_name) |
| 105 | if os.path.islink(manifest_path): |
| 106 | manifest_name = os.readlink(manifest_path) |
Kuang-che Wu | ea3abce | 2018-10-04 17:50:42 +0800 | [diff] [blame] | 107 | parser = ManifestParser(manifest_dir) |
| 108 | manifest = parser.parse_xml_recursive('HEAD', manifest_name) |
| 109 | |
| 110 | for copyfile in manifest.findall('.//copyfile'): |
| 111 | dest = copyfile.get('dest') |
| 112 | if not dest: |
| 113 | continue |
| 114 | # `dest` is relative to the top of the tree |
| 115 | dest_path = os.path.join(repo_dir, dest) |
| 116 | if not os.path.isfile(dest_path): |
| 117 | continue |
| 118 | logger.debug('delete file %r', dest_path) |
| 119 | os.unlink(dest_path) |
| 120 | |
| 121 | for linkfile in manifest.findall('.//linkfile'): |
| 122 | dest = linkfile.get('dest') |
| 123 | if not dest: |
| 124 | continue |
| 125 | # `dest` is relative to the top of the tree |
| 126 | dest_path = os.path.join(repo_dir, dest) |
| 127 | if not os.path.islink(dest_path): |
| 128 | continue |
| 129 | logger.debug('delete link %r', dest_path) |
| 130 | os.unlink(dest_path) |
| 131 | |
| 132 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 133 | def sync(repo_dir, jobs=16, manifest_name=None, current_branch=False): |
| 134 | """Repo sync. |
| 135 | |
| 136 | Args: |
| 137 | repo_dir: root directory of repo |
| 138 | jobs: projects to fetch simultaneously |
| 139 | manifest_name: filename of manifest |
| 140 | current_branch: fetch only current branch |
| 141 | """ |
Kuang-che Wu | ea3abce | 2018-10-04 17:50:42 +0800 | [diff] [blame] | 142 | # Workaround to prevent garbage files left between repo syncs |
| 143 | # (http://crbug.com/881783). |
| 144 | cleanup_repo_generated_files(repo_dir) |
| 145 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 146 | cmd = ['repo', 'sync', '-q', '--force-sync'] |
| 147 | if jobs: |
| 148 | cmd += ['-j', str(jobs)] |
| 149 | if manifest_name: |
| 150 | cmd += ['--manifest-name', manifest_name] |
| 151 | if current_branch: |
| 152 | cmd += ['--current-branch'] |
| 153 | util.check_call(*cmd, cwd=repo_dir) |
| 154 | |
| 155 | |
| 156 | def abandon(repo_dir, branch_name): |
| 157 | """Repo abandon. |
| 158 | |
| 159 | Args: |
| 160 | repo_dir: root directory of repo |
| 161 | branch_name: branch name to abandon |
| 162 | """ |
| 163 | # Ignore errors if failed, which means the branch didn't exist beforehand. |
| 164 | util.call('repo', 'abandon', branch_name, cwd=repo_dir) |
| 165 | |
| 166 | |
| 167 | def info(repo_dir, query): |
| 168 | """Repo info. |
| 169 | |
| 170 | Args: |
| 171 | repo_dir: root directory of repo |
| 172 | query: key to query |
| 173 | """ |
Kuang-che Wu | 34ab7b4 | 2019-10-28 19:40:05 +0800 | [diff] [blame] | 174 | try: |
| 175 | output = util.check_output('repo', 'info', '.', cwd=repo_dir) |
| 176 | except subprocess.CalledProcessError as e: |
| 177 | if 'Manifest branch:' not in e.output: |
| 178 | raise |
| 179 | # "repo info" may exit with error while the data we want is already |
| 180 | # printed. Ignore errors for such case. |
| 181 | output = e.output |
| 182 | for line in output.splitlines(): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 183 | key, value = map(str.strip, line.split(':')) |
| 184 | if key == query: |
| 185 | return value |
Kuang-che Wu | 89ac2e7 | 2018-07-25 17:39:07 +0800 | [diff] [blame] | 186 | |
| 187 | return None |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 188 | |
| 189 | |
| 190 | def get_current_branch(repo_dir): |
| 191 | """Get manifest branch of existing repo directory.""" |
| 192 | return info(repo_dir, 'Manifest branch') |
| 193 | |
| 194 | |
| 195 | def get_manifest_groups(repo_dir): |
| 196 | """Get manifest group of existing repo directory.""" |
| 197 | return info(repo_dir, 'Manifest groups') |
| 198 | |
| 199 | |
Kuang-che Wu | 3d04eda | 2019-09-05 23:56:40 +0800 | [diff] [blame] | 200 | def list_projects(repo_dir): |
| 201 | """Repo list. |
| 202 | |
| 203 | Args: |
| 204 | repo_dir: root directory of repo |
| 205 | |
| 206 | Returns: |
| 207 | list of paths, relative to repo_dir |
| 208 | """ |
| 209 | result = [] |
| 210 | for line in util.check_output( |
| 211 | 'repo', 'list', '--path-only', cwd=repo_dir).splitlines(): |
| 212 | result.append(line) |
| 213 | return result |
| 214 | |
| 215 | |
| 216 | def cleanup_unexpected_files(repo_dir): |
| 217 | """Clean up unexpected files in repo tree. |
| 218 | |
| 219 | Note this is not fully equivalent to 'repo sync' from scratch because: |
| 220 | - This only handle git repo folders. In other words, directories under |
| 221 | repo_dir not inside any git repo will not be touched. |
| 222 | - It ignores files if matching gitignore pattern. |
| 223 | So we can keep cache files to speed up incremental build next time. |
| 224 | |
| 225 | If you want truly clean tree, delete entire tree and repo sync directly |
| 226 | instead. |
| 227 | |
| 228 | Args: |
| 229 | repo_dir: root directory of repo |
| 230 | """ |
| 231 | projects = list_projects(repo_dir) |
| 232 | |
| 233 | # When we clean up project X, we don't want to touch files under X's |
| 234 | # subprojects. Collect the nested project relationship here. |
| 235 | nested = {} |
| 236 | # By sorting, parent directory will loop before subdirectories. |
| 237 | for project_path in sorted(projects): |
| 238 | components = project_path.split(os.sep) |
| 239 | for i in range(len(components) - 1, 0, -1): |
| 240 | head = os.sep.join(components[:i]) |
| 241 | tail = os.sep.join(components[i:]) |
| 242 | if head in nested: |
| 243 | nested[head].append(tail) |
| 244 | break |
| 245 | nested[project_path] = [] |
| 246 | |
| 247 | for project_path in projects: |
| 248 | git_repo = os.path.join(repo_dir, project_path) |
| 249 | if not os.path.exists(git_repo): |
| 250 | # It should be harmless to ignore git repo nonexistence because 'repo |
| 251 | # sync' will restore them. |
| 252 | logger.warning('git repo not found: %s', git_repo) |
| 253 | continue |
| 254 | git_util.distclean(git_repo, nested[project_path]) |
| 255 | |
| 256 | |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame] | 257 | def _urljoin(base, url): |
| 258 | # urlparse.urljoin doesn't recognize "persistent-https://" protocol. |
| 259 | # Following hack replaces "persistent-https" by obsolete protocol "gopher" |
| 260 | # before urlparse.urljoin and replaces back after urlparse.urljoin calls. |
| 261 | dummy_scheme = 'gopher://' |
| 262 | new_scheme = 'persistent-https://' |
| 263 | assert not base.startswith(dummy_scheme) |
| 264 | assert not url.startswith(dummy_scheme) |
| 265 | base = re.sub('^' + new_scheme, dummy_scheme, base) |
| 266 | url = re.sub('^' + new_scheme, dummy_scheme, url) |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 267 | result = urllib.parse.urljoin(base, url) |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame] | 268 | result = re.sub('^' + dummy_scheme, new_scheme, result) |
| 269 | return result |
| 270 | |
| 271 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 272 | class ManifestParser(object): |
| 273 | """Enumerates historical manifest files and parses them.""" |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 274 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 275 | def __init__(self, manifest_dir): |
| 276 | self.manifest_dir = manifest_dir |
| 277 | self.manifest_url = get_manifest_url(self.manifest_dir) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 278 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 279 | def parse_single_xml(self, content, allow_include=False): |
| 280 | root = xml.etree.ElementTree.fromstring(content) |
| 281 | if not allow_include and root.find('include') is not None: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 282 | raise errors.InternalError( |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 283 | 'Expects self-contained manifest. <include> is not allowed') |
| 284 | return root |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 285 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 286 | def parse_xml_recursive(self, git_rev, path): |
| 287 | content = git_util.get_file_from_revision(self.manifest_dir, git_rev, path) |
| 288 | root = self.parse_single_xml(content, allow_include=True) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 289 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 290 | result = xml.etree.ElementTree.Element('manifest') |
| 291 | for node in root: |
| 292 | if node.tag == 'include': |
| 293 | for subnode in self.parse_xml_recursive(git_rev, node.get('name')): |
| 294 | result.append(subnode) |
| 295 | else: |
| 296 | result.append(node) |
| 297 | return result |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 298 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 299 | def process_parsed_result(self, root): |
| 300 | result = {} |
| 301 | default = root.find('default') |
| 302 | if default is None: |
| 303 | default = {} |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 304 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 305 | remote_fetch_map = {} |
| 306 | for remote in root.findall('.//remote'): |
| 307 | name = remote.get('name') |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame] | 308 | fetch_url = _urljoin(self.manifest_url, remote.get('fetch')) |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 309 | if urllib.parse.urlparse(fetch_url).path not in ('', '/'): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 310 | # TODO(kcwu): support remote url with sub folders |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 311 | raise errors.InternalError( |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 312 | 'only support git repo at root path of remote server: %s' % |
| 313 | fetch_url) |
| 314 | remote_fetch_map[name] = fetch_url |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 315 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 316 | assert root.find('include') is None |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 317 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 318 | for project in root.findall('.//project'): |
| 319 | if 'notdefault' in project.get('groups', ''): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 320 | continue |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 321 | for subproject in project.findall('.//project'): |
| 322 | logger.warning('nested project %s.%s is not supported and ignored', |
| 323 | project.get('name'), subproject.get('name')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 324 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 325 | # default path is its name |
| 326 | path = project.get('path', project.get('name')) |
| 327 | revision = project.get('revision', default.get('revision')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 328 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 329 | remote_name = project.get('remote', default.get('remote')) |
| 330 | if remote_name not in remote_fetch_map: |
Kuang-che Wu | e121fae | 2018-11-09 16:18:39 +0800 | [diff] [blame] | 331 | raise errors.InternalError('unknown remote name=%s' % remote_name) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 332 | fetch_url = remote_fetch_map.get(remote_name) |
Kuang-che Wu | bfa6448 | 2018-10-16 11:49:49 +0800 | [diff] [blame] | 333 | repo_url = _urljoin(fetch_url, project.get('name')) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 334 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 335 | result[path] = codechange.PathSpec(path, repo_url, revision) |
| 336 | return result |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 337 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 338 | def enumerate_manifest_commits(self, start_time, end_time, path): |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 339 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 340 | def parse_dependencies(path, content): |
Kuang-che Wu | 7d0c759 | 2019-09-16 09:59:28 +0800 | [diff] [blame] | 341 | try: |
| 342 | root = self.parse_single_xml(content, allow_include=True) |
| 343 | except xml.etree.ElementTree.ParseError: |
| 344 | logger.warning('%s syntax error, skip', path) |
| 345 | return None |
| 346 | |
| 347 | result = [] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 348 | for include in root.findall('.//include'): |
Kuang-che Wu | 7d0c759 | 2019-09-16 09:59:28 +0800 | [diff] [blame] | 349 | result.append(include.get('name')) |
| 350 | return result |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 351 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 352 | return git_util.get_history_recursively(self.manifest_dir, path, start_time, |
| 353 | end_time, parse_dependencies) |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 354 | |
| 355 | |
| 356 | class RepoMirror(codechange.CodeStorage): |
| 357 | """Repo git mirror.""" |
| 358 | |
| 359 | def __init__(self, mirror_dir): |
| 360 | self.mirror_dir = mirror_dir |
| 361 | |
| 362 | def _url_to_cache_dir(self, url): |
| 363 | # Here we assume remote fetch url is always at root of server url, so we can |
| 364 | # simply treat whole path as repo project name. |
Kuang-che Wu | a7ddf9b | 2019-11-25 18:59:57 +0800 | [diff] [blame] | 365 | path = urllib.parse.urlparse(url).path |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 366 | assert path[0] == '/' |
| 367 | return '%s.git' % path[1:] |
| 368 | |
| 369 | def cached_git_root(self, repo_url): |
| 370 | cache_path = self._url_to_cache_dir(repo_url) |
Kuang-che Wu | a4f14d6 | 2018-10-15 15:59:47 +0800 | [diff] [blame] | 371 | |
| 372 | # The location of chromeos manifest-internal repo mirror is irregular |
| 373 | # (http://crbug.com/895957). This is a workaround. |
| 374 | if cache_path == 'chromeos/manifest-internal.git': |
| 375 | cache_path = 'manifest-internal.git' |
| 376 | |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 377 | return os.path.join(self.mirror_dir, cache_path) |
Kuang-che Wu | 6948ecc | 2018-09-11 17:43:49 +0800 | [diff] [blame] | 378 | |
| 379 | def _load_project_list(self, project_root): |
| 380 | repo_project_list = os.path.join(project_root, '.repo', 'project.list') |
Kuang-che Wu | a572349 | 2019-11-25 20:59:34 +0800 | [diff] [blame] | 381 | with open(repo_project_list) as f: |
| 382 | return f.readlines() |
Kuang-che Wu | 6948ecc | 2018-09-11 17:43:49 +0800 | [diff] [blame] | 383 | |
| 384 | def _save_project_list(self, project_root, lines): |
| 385 | repo_project_list = os.path.join(project_root, '.repo', 'project.list') |
| 386 | with open(repo_project_list, 'w') as f: |
| 387 | f.write(''.join(sorted(lines))) |
| 388 | |
| 389 | def add_to_project_list(self, project_root, path, repo_url): |
| 390 | lines = self._load_project_list(project_root) |
| 391 | |
| 392 | line = path + '\n' |
| 393 | if line not in lines: |
| 394 | lines.append(line) |
| 395 | |
| 396 | self._save_project_list(project_root, lines) |
| 397 | |
| 398 | def remove_from_project_list(self, project_root, path): |
| 399 | lines = self._load_project_list(project_root) |
| 400 | |
| 401 | line = path + '\n' |
| 402 | if line in lines: |
| 403 | lines.remove(line) |
| 404 | |
| 405 | self._save_project_list(project_root, lines) |