David Pursehouse | 8898e2f | 2012-11-14 07:51:03 +0900 | [diff] [blame] | 1 | #!/usr/bin/env python |
Mike Frysinger | f601376 | 2019-06-13 02:30:51 -0400 | [diff] [blame] | 2 | # -*- coding:utf-8 -*- |
Mike Frysinger | f241f8c | 2020-02-20 17:08:43 -0500 | [diff] [blame] | 3 | # |
| 4 | # Copyright (C) 2008 The Android Open Source Project |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 17 | |
Mike Frysinger | 87fb5a1 | 2019-06-13 01:54:46 -0400 | [diff] [blame] | 18 | """Repo launcher. |
| 19 | |
| 20 | This is a standalone tool that people may copy to anywhere in their system. |
| 21 | It is used to get an initial repo client checkout, and after that it runs the |
| 22 | copy of repo in the checkout. |
| 23 | """ |
| 24 | |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 25 | from __future__ import print_function |
| 26 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 27 | import datetime |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 28 | import os |
| 29 | import platform |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 30 | import shlex |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 31 | import subprocess |
| 32 | import sys |
| 33 | |
| 34 | |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 35 | # These should never be newer than the main.py version since this needs to be a |
| 36 | # bit more flexible with older systems. See that file for more details on the |
| 37 | # versions we select. |
| 38 | MIN_PYTHON_VERSION_SOFT = (3, 6) |
| 39 | MIN_PYTHON_VERSION_HARD = (3, 5) |
| 40 | |
| 41 | |
Mike Frysinger | 6fb0cb5 | 2020-02-12 09:39:23 -0500 | [diff] [blame] | 42 | # Keep basic logic in sync with repo_trace.py. |
| 43 | class Trace(object): |
| 44 | """Trace helper logic.""" |
| 45 | |
| 46 | REPO_TRACE = 'REPO_TRACE' |
| 47 | |
| 48 | def __init__(self): |
| 49 | self.set(os.environ.get(self.REPO_TRACE) == '1') |
| 50 | |
| 51 | def set(self, value): |
| 52 | self.enabled = bool(value) |
| 53 | |
| 54 | def print(self, *args, **kwargs): |
| 55 | if self.enabled: |
| 56 | print(*args, **kwargs) |
| 57 | |
| 58 | |
| 59 | trace = Trace() |
| 60 | |
| 61 | |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 62 | def exec_command(cmd): |
| 63 | """Execute |cmd| or return None on failure.""" |
Mike Frysinger | 6fb0cb5 | 2020-02-12 09:39:23 -0500 | [diff] [blame] | 64 | trace.print(':', ' '.join(cmd)) |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 65 | try: |
| 66 | if platform.system() == 'Windows': |
| 67 | ret = subprocess.call(cmd) |
| 68 | sys.exit(ret) |
| 69 | else: |
| 70 | os.execvp(cmd[0], cmd) |
Mike Frysinger | 72b6dc8 | 2020-02-12 17:04:32 -0500 | [diff] [blame] | 71 | except Exception: |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 72 | pass |
| 73 | |
| 74 | |
| 75 | def check_python_version(): |
| 76 | """Make sure the active Python version is recent enough.""" |
| 77 | def reexec(prog): |
| 78 | exec_command([prog] + sys.argv) |
| 79 | |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 80 | ver = sys.version_info |
| 81 | major = ver.major |
| 82 | minor = ver.minor |
| 83 | |
| 84 | # Abort on very old Python 2 versions. |
| 85 | if (major, minor) < (2, 7): |
| 86 | print('repo: error: Your Python version is too old. ' |
| 87 | 'Please use Python {}.{} or newer instead.'.format( |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 88 | *MIN_PYTHON_VERSION_SOFT), file=sys.stderr) |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 89 | sys.exit(1) |
| 90 | |
| 91 | # Try to re-exec the version specific Python 3 if needed. |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 92 | if (major, minor) < MIN_PYTHON_VERSION_SOFT: |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 93 | # Python makes releases ~once a year, so try our min version +10 to help |
| 94 | # bridge the gap. This is the fallback anyways so perf isn't critical. |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 95 | min_major, min_minor = MIN_PYTHON_VERSION_SOFT |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 96 | for inc in range(0, 10): |
| 97 | reexec('python{}.{}'.format(min_major, min_minor + inc)) |
| 98 | |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 99 | # Fallback to older versions if possible. |
| 100 | for inc in range(MIN_PYTHON_VERSION_SOFT[1] - MIN_PYTHON_VERSION_HARD[1], 0, -1): |
| 101 | # Don't downgrade, and don't reexec ourselves (which would infinite loop). |
| 102 | if (min_major, min_minor - inc) <= (major, minor): |
| 103 | break |
| 104 | reexec('python{}.{}'.format(min_major, min_minor - inc)) |
| 105 | |
Mike Frysinger | ce9b6c4 | 2021-01-04 22:07:19 -0500 | [diff] [blame] | 106 | # Try the generic Python 3 wrapper, but only if it's new enough. If it |
| 107 | # isn't, we want to just give up below and make the user resolve things. |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 108 | try: |
| 109 | proc = subprocess.Popen( |
| 110 | ['python3', '-c', 'import sys; ' |
| 111 | 'print(sys.version_info.major, sys.version_info.minor)'], |
| 112 | stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 113 | (output, _) = proc.communicate() |
| 114 | python3_ver = tuple(int(x) for x in output.decode('utf-8').split()) |
| 115 | except (OSError, subprocess.CalledProcessError): |
| 116 | python3_ver = None |
| 117 | |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 118 | # If the python3 version looks like it's new enough, give it a try. |
| 119 | if (python3_ver and python3_ver >= MIN_PYTHON_VERSION_HARD |
Craig Northway | 0e8828c | 2021-05-05 08:49:26 -0700 | [diff] [blame] | 120 | and python3_ver != (major, minor)): |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 121 | reexec('python3') |
| 122 | |
| 123 | # We're still here, so diagnose things for the user. |
| 124 | if major < 3: |
Mike Frysinger | ce9b6c4 | 2021-01-04 22:07:19 -0500 | [diff] [blame] | 125 | print('repo: error: Python 2 is no longer supported; ' |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 126 | 'Please upgrade to Python {}.{}+.'.format(*MIN_PYTHON_VERSION_HARD), |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 127 | file=sys.stderr) |
Mike Frysinger | ce9b6c4 | 2021-01-04 22:07:19 -0500 | [diff] [blame] | 128 | sys.exit(1) |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 129 | elif (major, minor) < MIN_PYTHON_VERSION_HARD: |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 130 | print('repo: error: Python 3 version is too old; ' |
Mike Frysinger | 4769201 | 2021-01-04 21:55:26 -0500 | [diff] [blame] | 131 | 'Please use Python {}.{} or newer.'.format(*MIN_PYTHON_VERSION_HARD), |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 132 | file=sys.stderr) |
| 133 | sys.exit(1) |
| 134 | |
| 135 | |
| 136 | if __name__ == '__main__': |
Mike Frysinger | 19ec797 | 2020-02-16 12:02:01 -0500 | [diff] [blame] | 137 | check_python_version() |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 138 | |
| 139 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 140 | # repo default configuration |
| 141 | # |
Mark E. Hamilton | 5553628 | 2016-02-03 15:49:43 -0700 | [diff] [blame] | 142 | REPO_URL = os.environ.get('REPO_URL', None) |
| 143 | if not REPO_URL: |
| 144 | REPO_URL = 'https://gerrit.googlesource.com/git-repo' |
Mike Frysinger | 563f1a6 | 2020-02-05 23:52:07 -0500 | [diff] [blame] | 145 | REPO_REV = os.environ.get('REPO_REV') |
| 146 | if not REPO_REV: |
| 147 | REPO_REV = 'stable' |
Mike Frysinger | a1cd770 | 2021-04-20 23:38:04 -0400 | [diff] [blame] | 148 | # URL to file bug reports for repo tool issues. |
| 149 | BUG_URL = 'https://bugs.chromium.org/p/gerrit/issues/entry?template=Repo+tool+issue' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 150 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 151 | # increment this whenever we make important changes to this script |
Josip Sokcevic | a24185e | 2022-11-22 16:27:24 -0800 | [diff] [blame] | 152 | VERSION = (2, 30) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 153 | |
| 154 | # increment this if the MAINTAINER_KEYS block is modified |
Mike Frysinger | 9cc1d70 | 2020-02-13 18:28:03 -0500 | [diff] [blame] | 155 | KEYRING_VERSION = (2, 3) |
Mike Frysinger | e443365 | 2016-09-13 18:06:07 -0400 | [diff] [blame] | 156 | |
| 157 | # Each individual key entry is created by using: |
| 158 | # gpg --armor --export keyid |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 159 | MAINTAINER_KEYS = """ |
| 160 | |
| 161 | Repo Maintainer <repo@android.kernel.org> |
| 162 | -----BEGIN PGP PUBLIC KEY BLOCK----- |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | |
| 164 | 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 |
Mike Frysinger | 9cc1d70 | 2020-02-13 18:28:03 -0500 | [diff] [blame] | 198 | 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 |
| 229 | cZ7aFsJF4PtcDrfdejyAxbtsSHI= |
| 230 | =82Tj |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 231 | -----END PGP PUBLIC KEY BLOCK----- |
| 232 | """ |
| 233 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 234 | GIT = 'git' # our git command |
Mike Frysinger | 82caef6 | 2020-02-11 18:51:08 -0500 | [diff] [blame] | 235 | # NB: The version of git that the repo launcher requires may be much older than |
| 236 | # the version of git that the main repo source tree requires. Keeping this at |
| 237 | # an older version also makes it easier for users to upgrade/rollback as needed. |
| 238 | # |
| 239 | # git-1.7 is in (EOL) Ubuntu Precise. |
| 240 | MIN_GIT_VERSION = (1, 7, 2) # minimum supported git version |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 241 | repodir = '.repo' # name of repo's private directory |
| 242 | S_repo = 'repo' # special repo repository |
| 243 | S_manifests = 'manifests' # special manifest repository |
| 244 | REPO_MAIN = S_repo + '/main.py' # main script |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 245 | GITC_CONFIG_FILE = '/gitc/.config' |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 246 | GITC_FS_ROOT_DIR = '/gitc/manifest-rw/' |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 247 | |
| 248 | |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 249 | import collections |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 250 | import errno |
Mike Frysinger | e5670c8 | 2021-01-07 22:14:25 -0500 | [diff] [blame] | 251 | import json |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 252 | import optparse |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | import re |
Mitchel Humpherys | eb5acc9 | 2014-03-12 10:48:15 -0700 | [diff] [blame] | 254 | import shutil |
Sarah Owens | 60798a3 | 2012-10-25 17:53:09 -0700 | [diff] [blame] | 255 | import stat |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 256 | |
| 257 | if sys.version_info[0] == 3: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 258 | import urllib.request |
| 259 | import urllib.error |
| 260 | else: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 261 | import imp |
David Pursehouse | 59bbb58 | 2013-05-17 10:49:33 +0900 | [diff] [blame] | 262 | import urllib2 |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 263 | urllib = imp.new_module('urllib') |
| 264 | urllib.request = urllib2 |
| 265 | urllib.error = urllib2 |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 266 | |
Conley Owens | 5e0ee14 | 2013-09-26 15:50:49 -0700 | [diff] [blame] | 267 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 268 | home_dot_repo = os.path.expanduser('~/.repoconfig') |
| 269 | gpg_dir = os.path.join(home_dot_repo, 'gnupg') |
| 270 | |
David Pursehouse | 31b9b4b | 2020-02-13 08:20:14 +0900 | [diff] [blame] | 271 | |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 272 | def GetParser(gitc_init=False): |
| 273 | """Setup the CLI parser.""" |
| 274 | if gitc_init: |
Mike Frysinger | 401c6f0 | 2021-02-18 15:20:15 -0500 | [diff] [blame] | 275 | usage = 'repo gitc-init -c client [options] [-u] url' |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 276 | else: |
Mike Frysinger | 401c6f0 | 2021-02-18 15:20:15 -0500 | [diff] [blame] | 277 | usage = 'repo init [options] [-u] url' |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 278 | |
| 279 | parser = optparse.OptionParser(usage=usage) |
Mike Frysinger | 9a734a3 | 2021-04-08 19:14:15 -0400 | [diff] [blame] | 280 | InitParser(parser, gitc_init=gitc_init) |
| 281 | return parser |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 282 | |
Mike Frysinger | 9a734a3 | 2021-04-08 19:14:15 -0400 | [diff] [blame] | 283 | |
| 284 | def InitParser(parser, gitc_init=False): |
| 285 | """Setup the CLI parser.""" |
Mike Frysinger | 9180a07 | 2021-04-13 14:57:40 -0400 | [diff] [blame] | 286 | # NB: Keep in sync with command.py:_CommonOptions(). |
| 287 | |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 288 | # Logging. |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 289 | group = parser.add_option_group('Logging options') |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 290 | group.add_option('-v', '--verbose', |
| 291 | dest='output_mode', action='store_true', |
| 292 | help='show all output') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 293 | group.add_option('-q', '--quiet', |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 294 | dest='output_mode', action='store_false', |
| 295 | help='only show errors') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 297 | # Manifest. |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 298 | group = parser.add_option_group('Manifest options') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 299 | group.add_option('-u', '--manifest-url', |
| 300 | help='manifest repository location', metavar='URL') |
Mike Frysinger | 23882b3 | 2021-02-23 15:43:07 -0500 | [diff] [blame] | 301 | group.add_option('-b', '--manifest-branch', metavar='REVISION', |
| 302 | help='manifest branch or revision (use HEAD for default)') |
Mike Frysinger | d2b086b | 2021-04-21 00:25:24 -0400 | [diff] [blame] | 303 | group.add_option('-m', '--manifest-name', default='default.xml', |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 304 | help='initial manifest file', metavar='NAME.xml') |
Mike Frysinger | a1051d8 | 2021-04-08 23:04:36 -0400 | [diff] [blame] | 305 | group.add_option('-g', '--groups', default='default', |
| 306 | help='restrict manifest projects to ones with specified ' |
| 307 | 'group(s) [default|all|G1,G2,G3|G4,-G5,-G6]', |
| 308 | metavar='GROUP') |
| 309 | group.add_option('-p', '--platform', default='auto', |
| 310 | help='restrict manifest projects to ones with a specified ' |
| 311 | 'platform group [auto|all|none|linux|darwin|...]', |
| 312 | metavar='PLATFORM') |
| 313 | group.add_option('--submodules', action='store_true', |
| 314 | help='sync any submodules associated with the manifest repo') |
Jack Neus | c474c9c | 2021-07-26 23:08:54 +0000 | [diff] [blame] | 315 | group.add_option('--standalone-manifest', action='store_true', |
| 316 | help='download the manifest as a static file ' |
| 317 | 'rather then create a git checkout of ' |
| 318 | 'the manifest repo') |
LaMont Jones | b750b48 | 2022-08-18 07:20:57 -0400 | [diff] [blame] | 319 | group.add_option('--manifest-depth', type='int', default=0, metavar='DEPTH', |
| 320 | help='create a shallow clone of the manifest repo with ' |
| 321 | 'given depth (0 for full clone); see git clone ' |
| 322 | '(default: %default)') |
Mike Frysinger | a1051d8 | 2021-04-08 23:04:36 -0400 | [diff] [blame] | 323 | |
| 324 | # Options that only affect manifest project, and not any of the projects |
| 325 | # specified in the manifest itself. |
| 326 | group = parser.add_option_group('Manifest (only) checkout options') |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 327 | cbr_opts = ['--current-branch'] |
| 328 | # The gitc-init subcommand allocates -c itself, but a lot of init users |
| 329 | # want -c, so try to satisfy both as best we can. |
| 330 | if not gitc_init: |
| 331 | cbr_opts += ['-c'] |
Mike Frysinger | 39cb17f | 2022-08-18 07:28:42 -0400 | [diff] [blame] | 332 | group.add_option(*cbr_opts, default=True, |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 333 | dest='current_branch_only', action='store_true', |
Mike Frysinger | 39cb17f | 2022-08-18 07:28:42 -0400 | [diff] [blame] | 334 | help='fetch only current manifest branch from server (default)') |
Mike Frysinger | 7356114 | 2021-05-03 01:10:09 -0400 | [diff] [blame] | 335 | group.add_option('--no-current-branch', |
| 336 | dest='current_branch_only', action='store_false', |
| 337 | help='fetch all manifest branches from server') |
Mike Frysinger | d68ed63 | 2021-05-03 01:21:35 -0400 | [diff] [blame] | 338 | group.add_option('--tags', |
| 339 | action='store_true', |
| 340 | help='fetch tags in the manifest') |
Mike Frysinger | a1051d8 | 2021-04-08 23:04:36 -0400 | [diff] [blame] | 341 | group.add_option('--no-tags', |
Mike Frysinger | d68ed63 | 2021-05-03 01:21:35 -0400 | [diff] [blame] | 342 | dest='tags', action='store_false', |
Mike Frysinger | a1051d8 | 2021-04-08 23:04:36 -0400 | [diff] [blame] | 343 | help="don't fetch tags in the manifest") |
| 344 | |
| 345 | # These are fundamentally different ways of structuring the checkout. |
| 346 | group = parser.add_option_group('Checkout modes') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 347 | group.add_option('--mirror', action='store_true', |
| 348 | help='create a replica of the remote repositories ' |
| 349 | 'rather than a client working directory') |
Mike Frysinger | a1051d8 | 2021-04-08 23:04:36 -0400 | [diff] [blame] | 350 | group.add_option('--archive', action='store_true', |
| 351 | help='checkout an archive instead of a git repository for ' |
| 352 | 'each project. See git archive.') |
| 353 | group.add_option('--worktree', action='store_true', |
| 354 | help='use git-worktree to manage projects') |
| 355 | |
| 356 | # These are fundamentally different ways of structuring the checkout. |
| 357 | group = parser.add_option_group('Project checkout optimizations') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 358 | group.add_option('--reference', |
| 359 | help='location of mirror directory', metavar='DIR') |
| 360 | group.add_option('--dissociate', action='store_true', |
| 361 | help='dissociate from reference mirrors after clone') |
| 362 | group.add_option('--depth', type='int', default=None, |
| 363 | help='create a shallow clone with given depth; ' |
| 364 | 'see git clone') |
| 365 | group.add_option('--partial-clone', action='store_true', |
| 366 | help='perform partial clone (https://git-scm.com/' |
| 367 | 'docs/gitrepository-layout#_code_partialclone_code)') |
Raman Tenneti | 6a2f4fb | 2021-04-02 10:55:33 -0700 | [diff] [blame] | 368 | group.add_option('--no-partial-clone', action='store_false', |
| 369 | help='disable use of partial clone (https://git-scm.com/' |
| 370 | 'docs/gitrepository-layout#_code_partialclone_code)') |
Raman Tenneti | f32f243 | 2021-04-12 20:57:25 -0700 | [diff] [blame] | 371 | group.add_option('--partial-clone-exclude', action='store', |
| 372 | help='exclude the specified projects (a comma-delimited ' |
| 373 | 'project names) from partial clone (https://git-scm.com' |
| 374 | '/docs/gitrepository-layout#_code_partialclone_code)') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 375 | group.add_option('--clone-filter', action='store', default='blob:none', |
| 376 | help='filter for use with --partial-clone ' |
| 377 | '[default: %default]') |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 378 | group.add_option('--use-superproject', action='store_true', default=None, |
Raman Tenneti | 6251729 | 2021-11-01 14:49:16 -0700 | [diff] [blame] | 379 | help='use the manifest superproject to sync projects; implies -c') |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 380 | group.add_option('--no-use-superproject', action='store_false', |
| 381 | dest='use_superproject', |
| 382 | help='disable use of manifest superprojects') |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 383 | group.add_option('--clone-bundle', action='store_true', |
Raman Tenneti | 21dce3d | 2021-02-09 00:26:31 -0800 | [diff] [blame] | 384 | help='enable use of /clone.bundle on HTTP/HTTPS ' |
| 385 | '(default if not --partial-clone)') |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 386 | group.add_option('--no-clone-bundle', |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 387 | dest='clone_bundle', action='store_false', |
| 388 | help='disable use of /clone.bundle on HTTP/HTTPS (default if --partial-clone)') |
XD Trol | 630876f | 2022-01-17 23:29:04 +0800 | [diff] [blame] | 389 | group.add_option('--git-lfs', action='store_true', |
| 390 | help='enable Git LFS support') |
| 391 | group.add_option('--no-git-lfs', |
| 392 | dest='git_lfs', action='store_false', |
| 393 | help='disable Git LFS support') |
Doug Anderson | 49cd59b | 2011-06-13 21:42:06 -0700 | [diff] [blame] | 394 | |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 395 | # Tool. |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 396 | group = parser.add_option_group('repo Version options') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 397 | group.add_option('--repo-url', metavar='URL', |
| 398 | help='repo repository location ($REPO_URL)') |
Mike Frysinger | 58ac167 | 2020-03-14 14:35:26 -0400 | [diff] [blame] | 399 | group.add_option('--repo-rev', metavar='REV', |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 400 | help='repo branch or revision ($REPO_REV)') |
Mike Frysinger | 58ac167 | 2020-03-14 14:35:26 -0400 | [diff] [blame] | 401 | group.add_option('--repo-branch', dest='repo_rev', |
| 402 | help=optparse.SUPPRESS_HELP) |
Mike Frysinger | c58ec4d | 2020-02-17 14:36:08 -0500 | [diff] [blame] | 403 | group.add_option('--no-repo-verify', |
| 404 | dest='repo_verify', default=True, action='store_false', |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 405 | help='do not verify repo source code') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 406 | |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 407 | # Other. |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 408 | group = parser.add_option_group('Other options') |
Mike Frysinger | f700ac7 | 2020-02-06 00:04:21 -0500 | [diff] [blame] | 409 | group.add_option('--config-name', |
| 410 | action='store_true', default=False, |
| 411 | help='Always prompt for name/e-mail') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 412 | |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 413 | # gitc-init specific settings. |
| 414 | if gitc_init: |
| 415 | group = parser.add_option_group('GITC options') |
| 416 | group.add_option('-f', '--manifest-file', |
| 417 | help='Optional manifest file to use for this GITC client.') |
| 418 | group.add_option('-c', '--gitc-client', |
| 419 | help='Name of the gitc_client instance to create or modify.') |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 420 | |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 421 | return parser |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 422 | |
David Pursehouse | 31b9b4b | 2020-02-13 08:20:14 +0900 | [diff] [blame] | 423 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 424 | # This is a poor replacement for subprocess.run until we require Python 3.6+. |
| 425 | RunResult = collections.namedtuple( |
| 426 | 'RunResult', ('returncode', 'stdout', 'stderr')) |
| 427 | |
| 428 | |
| 429 | class RunError(Exception): |
| 430 | """Error when running a command failed.""" |
| 431 | |
| 432 | |
| 433 | def run_command(cmd, **kwargs): |
| 434 | """Run |cmd| and return its output.""" |
| 435 | check = kwargs.pop('check', False) |
| 436 | if kwargs.pop('capture_output', False): |
| 437 | kwargs.setdefault('stdout', subprocess.PIPE) |
| 438 | kwargs.setdefault('stderr', subprocess.PIPE) |
| 439 | cmd_input = kwargs.pop('input', None) |
| 440 | |
Mike Frysinger | 6a784ff | 2020-02-14 23:38:28 -0500 | [diff] [blame] | 441 | def decode(output): |
| 442 | """Decode |output| to text.""" |
| 443 | if output is None: |
| 444 | return output |
| 445 | try: |
| 446 | return output.decode('utf-8') |
| 447 | except UnicodeError: |
| 448 | print('repo: warning: Invalid UTF-8 output:\ncmd: %r\n%r' % (cmd, output), |
| 449 | file=sys.stderr) |
| 450 | # TODO(vapier): Once we require Python 3, use 'backslashreplace'. |
| 451 | return output.decode('utf-8', 'replace') |
| 452 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 453 | # Run & package the results. |
| 454 | proc = subprocess.Popen(cmd, **kwargs) |
| 455 | (stdout, stderr) = proc.communicate(input=cmd_input) |
Mike Frysinger | 71928c1 | 2020-02-21 23:45:08 -0500 | [diff] [blame] | 456 | dbg = ': ' + ' '.join(cmd) |
| 457 | if cmd_input is not None: |
| 458 | dbg += ' 0<|' |
| 459 | if stdout == subprocess.PIPE: |
| 460 | dbg += ' 1>|' |
| 461 | if stderr == subprocess.PIPE: |
| 462 | dbg += ' 2>|' |
| 463 | elif stderr == subprocess.STDOUT: |
| 464 | dbg += ' 2>&1' |
| 465 | trace.print(dbg) |
Mike Frysinger | 6a784ff | 2020-02-14 23:38:28 -0500 | [diff] [blame] | 466 | ret = RunResult(proc.returncode, decode(stdout), decode(stderr)) |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 467 | |
| 468 | # If things failed, print useful debugging output. |
| 469 | if check and ret.returncode: |
| 470 | print('repo: error: "%s" failed with exit status %s' % |
| 471 | (cmd[0], ret.returncode), file=sys.stderr) |
| 472 | print(' cwd: %s\n cmd: %r' % |
| 473 | (kwargs.get('cwd', os.getcwd()), cmd), file=sys.stderr) |
David Pursehouse | c19cc5c | 2020-02-14 09:18:15 +0900 | [diff] [blame] | 474 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 475 | def _print_output(name, output): |
| 476 | if output: |
| 477 | print(' %s:\n >> %s' % (name, '\n >> '.join(output.splitlines())), |
| 478 | file=sys.stderr) |
David Pursehouse | c19cc5c | 2020-02-14 09:18:15 +0900 | [diff] [blame] | 479 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 480 | _print_output('stdout', ret.stdout) |
| 481 | _print_output('stderr', ret.stderr) |
| 482 | raise RunError(ret) |
| 483 | |
| 484 | return ret |
| 485 | |
| 486 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 487 | _gitc_manifest_dir = None |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 488 | |
| 489 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 490 | def get_gitc_manifest_dir(): |
| 491 | global _gitc_manifest_dir |
| 492 | if _gitc_manifest_dir is None: |
Dan Willemsen | 2487cb7 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 493 | _gitc_manifest_dir = '' |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 494 | try: |
| 495 | with open(GITC_CONFIG_FILE, 'r') as gitc_config: |
| 496 | for line in gitc_config: |
| 497 | match = re.match('gitc_dir=(?P<gitc_manifest_dir>.*)', line) |
| 498 | if match: |
| 499 | _gitc_manifest_dir = match.group('gitc_manifest_dir') |
| 500 | except IOError: |
Dan Willemsen | 2487cb7 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 501 | pass |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 502 | return _gitc_manifest_dir |
| 503 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 504 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 505 | def gitc_parse_clientdir(gitc_fs_path): |
| 506 | """Parse a path in the GITC FS and return its client name. |
| 507 | |
Mike Frysinger | 0a849b6 | 2020-12-11 03:26:42 -0500 | [diff] [blame] | 508 | Args: |
| 509 | gitc_fs_path: A subdirectory path within the GITC_FS_ROOT_DIR. |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 510 | |
Mike Frysinger | 0a849b6 | 2020-12-11 03:26:42 -0500 | [diff] [blame] | 511 | Returns: |
| 512 | The GITC client name. |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 513 | """ |
| 514 | if gitc_fs_path == GITC_FS_ROOT_DIR: |
| 515 | return None |
| 516 | if not gitc_fs_path.startswith(GITC_FS_ROOT_DIR): |
| 517 | manifest_dir = get_gitc_manifest_dir() |
| 518 | if manifest_dir == '': |
| 519 | return None |
| 520 | if manifest_dir[-1] != '/': |
| 521 | manifest_dir += '/' |
| 522 | if gitc_fs_path == manifest_dir: |
| 523 | return None |
| 524 | if not gitc_fs_path.startswith(manifest_dir): |
| 525 | return None |
| 526 | return gitc_fs_path.split(manifest_dir)[1].split('/')[0] |
| 527 | return gitc_fs_path.split(GITC_FS_ROOT_DIR)[1].split('/')[0] |
| 528 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 529 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 530 | class CloneFailure(Exception): |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 531 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 532 | """Indicate the remote clone of repo itself failed. |
| 533 | """ |
| 534 | |
| 535 | |
Mike Frysinger | 3599cc3 | 2020-02-29 02:53:41 -0500 | [diff] [blame] | 536 | def check_repo_verify(repo_verify, quiet=False): |
| 537 | """Check the --repo-verify state.""" |
| 538 | if not repo_verify: |
| 539 | print('repo: warning: verification of repo code has been disabled;\n' |
| 540 | 'repo will not be able to verify the integrity of itself.\n', |
| 541 | file=sys.stderr) |
| 542 | return False |
| 543 | |
| 544 | if NeedSetupGnuPG(): |
| 545 | return SetupGnuPG(quiet) |
| 546 | |
| 547 | return True |
| 548 | |
| 549 | |
| 550 | def check_repo_rev(dst, rev, repo_verify=True, quiet=False): |
| 551 | """Check that |rev| is valid.""" |
| 552 | do_verify = check_repo_verify(repo_verify, quiet=quiet) |
| 553 | remote_ref, local_rev = resolve_repo_rev(dst, rev) |
| 554 | if not quiet and not remote_ref.startswith('refs/heads/'): |
| 555 | print('warning: repo is not tracking a remote branch, so it will not ' |
| 556 | 'receive updates', file=sys.stderr) |
| 557 | if do_verify: |
| 558 | rev = verify_rev(dst, remote_ref, local_rev, quiet) |
| 559 | else: |
| 560 | rev = local_rev |
| 561 | return (remote_ref, rev) |
| 562 | |
| 563 | |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 564 | def _Init(args, gitc_init=False): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 565 | """Installs repo by cloning it over the network. |
| 566 | """ |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 567 | parser = GetParser(gitc_init=gitc_init) |
| 568 | opt, args = parser.parse_args(args) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 569 | if args: |
Mike Frysinger | 401c6f0 | 2021-02-18 15:20:15 -0500 | [diff] [blame] | 570 | if not opt.manifest_url: |
| 571 | opt.manifest_url = args.pop(0) |
| 572 | if args: |
| 573 | parser.print_usage() |
| 574 | sys.exit(1) |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 575 | opt.quiet = opt.output_mode is False |
| 576 | opt.verbose = opt.output_mode is True |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 577 | |
Xin Li | d79a4bc | 2020-05-20 16:03:45 -0700 | [diff] [blame] | 578 | if opt.clone_bundle is None: |
| 579 | opt.clone_bundle = False if opt.partial_clone else True |
| 580 | |
Mike Frysinger | e1111f5 | 2020-03-14 16:28:31 -0400 | [diff] [blame] | 581 | url = opt.repo_url or REPO_URL |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 582 | rev = opt.repo_rev or REPO_REV |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 583 | |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 584 | try: |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 585 | if gitc_init: |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 586 | gitc_manifest_dir = get_gitc_manifest_dir() |
| 587 | if not gitc_manifest_dir: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 588 | print('fatal: GITC filesystem is not available. Exiting...', |
| 589 | file=sys.stderr) |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 590 | sys.exit(1) |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 591 | gitc_client = opt.gitc_client |
| 592 | if not gitc_client: |
| 593 | gitc_client = gitc_parse_clientdir(os.getcwd()) |
| 594 | if not gitc_client: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 595 | print('fatal: GITC client (-c) is required.', file=sys.stderr) |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 596 | sys.exit(1) |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 597 | client_dir = os.path.join(gitc_manifest_dir, gitc_client) |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 598 | if not os.path.exists(client_dir): |
| 599 | os.makedirs(client_dir) |
| 600 | os.chdir(client_dir) |
| 601 | if os.path.exists(repodir): |
| 602 | # This GITC Client has already initialized repo so continue. |
| 603 | return |
| 604 | |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 605 | os.mkdir(repodir) |
| 606 | except OSError as e: |
| 607 | if e.errno != errno.EEXIST: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 608 | print('fatal: cannot make %s directory: %s' |
| 609 | % (repodir, e.strerror), file=sys.stderr) |
David Pursehouse | 3794a78 | 2012-11-15 06:17:30 +0900 | [diff] [blame] | 610 | # Don't raise CloneFailure; that would delete the |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 611 | # name. Instead exit immediately. |
| 612 | # |
| 613 | sys.exit(1) |
| 614 | |
| 615 | _CheckGitVersion() |
| 616 | try: |
Mike Frysinger | dcbfadf | 2020-02-22 00:04:39 -0500 | [diff] [blame] | 617 | if not opt.quiet: |
| 618 | print('Downloading Repo source from', url) |
Mike Frysinger | a010a9f | 2022-08-19 05:16:46 -0400 | [diff] [blame] | 619 | dst_final = os.path.abspath(os.path.join(repodir, S_repo)) |
| 620 | dst = dst_final + '.tmp' |
| 621 | shutil.rmtree(dst, ignore_errors=True) |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 622 | _Clone(url, dst, opt.clone_bundle, opt.quiet, opt.verbose) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 623 | |
Mike Frysinger | 3599cc3 | 2020-02-29 02:53:41 -0500 | [diff] [blame] | 624 | remote_ref, rev = check_repo_rev(dst, rev, opt.repo_verify, quiet=opt.quiet) |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 625 | _Checkout(dst, remote_ref, rev, opt.quiet) |
Sebastian Schuberth | 993dcac | 2018-07-13 10:25:52 +0200 | [diff] [blame] | 626 | |
| 627 | if not os.path.isfile(os.path.join(dst, 'repo')): |
Mike Frysinger | d75ca2e | 2022-08-19 05:17:49 -0400 | [diff] [blame] | 628 | print("fatal: '%s' does not look like a git-repo repository, is " |
| 629 | "--repo-url set correctly?" % url, file=sys.stderr) |
| 630 | raise CloneFailure() |
Sebastian Schuberth | 993dcac | 2018-07-13 10:25:52 +0200 | [diff] [blame] | 631 | |
Mike Frysinger | a010a9f | 2022-08-19 05:16:46 -0400 | [diff] [blame] | 632 | os.rename(dst, dst_final) |
| 633 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 634 | except CloneFailure: |
Mike Frysinger | 4aa8584 | 2022-01-25 02:10:28 -0500 | [diff] [blame] | 635 | print('fatal: double check your --repo-rev setting.', file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 636 | if opt.quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 637 | print('fatal: repo init failed; run without --quiet to see why', |
| 638 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 639 | raise |
| 640 | |
| 641 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 642 | def run_git(*args, **kwargs): |
| 643 | """Run git and return execution details.""" |
| 644 | kwargs.setdefault('capture_output', True) |
| 645 | kwargs.setdefault('check', True) |
| 646 | try: |
| 647 | return run_command([GIT] + list(args), **kwargs) |
| 648 | except OSError as e: |
| 649 | print(file=sys.stderr) |
| 650 | print('repo: error: "%s" is not available' % GIT, file=sys.stderr) |
| 651 | print('repo: error: %s' % e, file=sys.stderr) |
| 652 | print(file=sys.stderr) |
| 653 | print('Please make sure %s is installed and in your path.' % GIT, |
| 654 | file=sys.stderr) |
| 655 | sys.exit(1) |
| 656 | except RunError: |
| 657 | raise CloneFailure() |
| 658 | |
| 659 | |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 660 | # The git version info broken down into components for easy analysis. |
| 661 | # Similar to Python's sys.version_info. |
| 662 | GitVersion = collections.namedtuple( |
| 663 | 'GitVersion', ('major', 'minor', 'micro', 'full')) |
| 664 | |
David Pursehouse | 31b9b4b | 2020-02-13 08:20:14 +0900 | [diff] [blame] | 665 | |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 666 | def ParseGitVersion(ver_str=None): |
| 667 | if ver_str is None: |
| 668 | # Load the version ourselves. |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 669 | ver_str = run_git('--version').stdout |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 670 | |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 671 | if not ver_str.startswith('git version '): |
| 672 | return None |
| 673 | |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 674 | full_version = ver_str[len('git version '):].strip() |
| 675 | num_ver_str = full_version.split('-')[0] |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 676 | to_tuple = [] |
| 677 | for num_str in num_ver_str.split('.')[:3]: |
| 678 | if num_str.isdigit(): |
| 679 | to_tuple.append(int(num_str)) |
| 680 | else: |
| 681 | to_tuple.append(0) |
Mike Frysinger | 6db1b9e | 2019-07-10 15:32:36 -0400 | [diff] [blame] | 682 | to_tuple.append(full_version) |
| 683 | return GitVersion(*to_tuple) |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 684 | |
| 685 | |
Mike Frysinger | f88b2fe | 2019-07-10 15:37:43 -0400 | [diff] [blame] | 686 | def _CheckGitVersion(): |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 687 | ver_act = ParseGitVersion() |
Conley Owens | ff0a3c8 | 2014-01-30 14:46:03 -0800 | [diff] [blame] | 688 | if ver_act is None: |
Mike Frysinger | 4c263b5 | 2019-09-11 04:04:16 -0400 | [diff] [blame] | 689 | print('fatal: unable to detect git version', file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 690 | raise CloneFailure() |
| 691 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 692 | if ver_act < MIN_GIT_VERSION: |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 693 | need = '.'.join(map(str, MIN_GIT_VERSION)) |
Mike Frysinger | 587f162 | 2020-03-23 16:49:11 -0400 | [diff] [blame] | 694 | print('fatal: git %s or later required; found %s' % (need, ver_act.full), |
| 695 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 696 | raise CloneFailure() |
| 697 | |
| 698 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 699 | def SetGitTrace2ParentSid(env=None): |
| 700 | """Set up GIT_TRACE2_PARENT_SID for git tracing.""" |
| 701 | # We roughly follow the format git itself uses in trace2/tr2_sid.c. |
| 702 | # (1) Be unique (2) be valid filename (3) be fixed length. |
| 703 | # |
| 704 | # Since we always export this variable, we try to avoid more expensive calls. |
| 705 | # e.g. We don't attempt hostname lookups or hashing the results. |
| 706 | if env is None: |
| 707 | env = os.environ |
| 708 | |
| 709 | KEY = 'GIT_TRACE2_PARENT_SID' |
| 710 | |
| 711 | now = datetime.datetime.utcnow() |
| 712 | value = 'repo-%s-P%08x' % (now.strftime('%Y%m%dT%H%M%SZ'), os.getpid()) |
| 713 | |
| 714 | # If it's already set, then append ourselves. |
| 715 | if KEY in env: |
| 716 | value = env[KEY] + '/' + value |
| 717 | |
| 718 | _setenv(KEY, value, env=env) |
| 719 | |
| 720 | |
| 721 | def _setenv(key, value, env=None): |
| 722 | """Set |key| in the OS environment |env| to |value|.""" |
| 723 | if env is None: |
| 724 | env = os.environ |
| 725 | # Environment handling across systems is messy. |
| 726 | try: |
| 727 | env[key] = value |
| 728 | except UnicodeEncodeError: |
| 729 | env[key] = value.encode() |
| 730 | |
| 731 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 732 | def NeedSetupGnuPG(): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 733 | if not os.path.isdir(home_dot_repo): |
| 734 | return True |
| 735 | |
| 736 | kv = os.path.join(home_dot_repo, 'keyring-version') |
| 737 | if not os.path.exists(kv): |
| 738 | return True |
| 739 | |
| 740 | kv = open(kv).read() |
| 741 | if not kv: |
| 742 | return True |
| 743 | |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 744 | kv = tuple(map(int, kv.split('.'))) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 745 | if kv < KEYRING_VERSION: |
| 746 | return True |
| 747 | return False |
| 748 | |
| 749 | |
Conley Owens | c9129d9 | 2012-10-01 16:12:28 -0700 | [diff] [blame] | 750 | def SetupGnuPG(quiet): |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 751 | try: |
| 752 | os.mkdir(home_dot_repo) |
| 753 | except OSError as e: |
| 754 | if e.errno != errno.EEXIST: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 755 | print('fatal: cannot make %s directory: %s' |
| 756 | % (home_dot_repo, e.strerror), file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 757 | sys.exit(1) |
| 758 | |
David James | bf79c66 | 2013-12-26 14:20:13 -0800 | [diff] [blame] | 759 | try: |
| 760 | os.mkdir(gpg_dir, stat.S_IRWXU) |
| 761 | except OSError as e: |
| 762 | if e.errno != errno.EEXIST: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 763 | print('fatal: cannot make %s directory: %s' % (gpg_dir, e.strerror), |
| 764 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 765 | sys.exit(1) |
| 766 | |
Mike Frysinger | 19a1f22 | 2020-02-14 16:28:13 -0500 | [diff] [blame] | 767 | if not quiet: |
| 768 | print('repo: Updating release signing keys to keyset ver %s' % |
| 769 | ('.'.join(str(x) for x in KEYRING_VERSION),)) |
| 770 | # NB: We use --homedir (and cwd below) because some environments (Windows) do |
| 771 | # not correctly handle full native paths. We avoid the issue by changing to |
| 772 | # the right dir with cwd=gpg_dir before executing gpg, and then telling gpg to |
| 773 | # use the cwd (.) as its homedir which leaves the path resolution logic to it. |
| 774 | cmd = ['gpg', '--homedir', '.', '--import'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 775 | try: |
Mike Frysinger | 19a1f22 | 2020-02-14 16:28:13 -0500 | [diff] [blame] | 776 | # gpg can be pretty chatty. Always capture the output and if something goes |
| 777 | # wrong, the builtin check failure will dump stdout & stderr for debugging. |
| 778 | run_command(cmd, stdin=subprocess.PIPE, capture_output=True, |
| 779 | cwd=gpg_dir, check=True, |
David Pursehouse | c19cc5c | 2020-02-14 09:18:15 +0900 | [diff] [blame] | 780 | input=MAINTAINER_KEYS.encode('utf-8')) |
David Pursehouse | 22dbfb9 | 2020-02-13 08:20:55 +0900 | [diff] [blame] | 781 | except OSError: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 782 | if not quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 783 | print('warning: gpg (GnuPG) is not available.', file=sys.stderr) |
| 784 | print('warning: Installing it is strongly encouraged.', file=sys.stderr) |
| 785 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 786 | return False |
| 787 | |
Mike Frysinger | 3164d40 | 2019-11-11 05:40:22 -0500 | [diff] [blame] | 788 | with open(os.path.join(home_dot_repo, 'keyring-version'), 'w') as fd: |
| 789 | fd.write('.'.join(map(str, KEYRING_VERSION)) + '\n') |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 790 | return True |
| 791 | |
| 792 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 793 | def _SetConfig(cwd, name, value): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 794 | """Set a git configuration option to the specified value. |
| 795 | """ |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 796 | run_git('config', name, value, cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 797 | |
| 798 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 799 | def _GetRepoConfig(name): |
| 800 | """Read a repo configuration option.""" |
| 801 | config = os.path.join(home_dot_repo, 'config') |
| 802 | if not os.path.exists(config): |
| 803 | return None |
| 804 | |
| 805 | cmd = ['config', '--file', config, '--get', name] |
| 806 | ret = run_git(*cmd, check=False) |
| 807 | if ret.returncode == 0: |
| 808 | return ret.stdout |
| 809 | elif ret.returncode == 1: |
| 810 | return None |
| 811 | else: |
| 812 | print('repo: error: git %s failed:\n%s' % (' '.join(cmd), ret.stderr), |
| 813 | file=sys.stderr) |
| 814 | raise RunError() |
| 815 | |
| 816 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 817 | def _InitHttp(): |
| 818 | handlers = [] |
| 819 | |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 820 | mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 821 | try: |
| 822 | import netrc |
| 823 | n = netrc.netrc() |
| 824 | for host in n.hosts: |
| 825 | p = n.hosts[host] |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 826 | mgr.add_password(p[1], 'http://%s/' % host, p[0], p[2]) |
Xiaodong Xu | ae0a36c | 2012-01-31 11:10:09 +0800 | [diff] [blame] | 827 | mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2]) |
David Pursehouse | 58a8b5c | 2020-02-13 08:21:28 +0900 | [diff] [blame] | 828 | except Exception: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 829 | pass |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 830 | handlers.append(urllib.request.HTTPBasicAuthHandler(mgr)) |
| 831 | handlers.append(urllib.request.HTTPDigestAuthHandler(mgr)) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 832 | |
| 833 | if 'http_proxy' in os.environ: |
| 834 | url = os.environ['http_proxy'] |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 835 | handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url})) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 836 | if 'REPO_CURL_VERBOSE' in os.environ: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 837 | handlers.append(urllib.request.HTTPHandler(debuglevel=1)) |
| 838 | handlers.append(urllib.request.HTTPSHandler(debuglevel=1)) |
| 839 | urllib.request.install_opener(urllib.request.build_opener(*handlers)) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 840 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 841 | |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 842 | def _Fetch(url, cwd, src, quiet, verbose): |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 843 | cmd = ['fetch'] |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 844 | if not verbose: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 845 | cmd.append('--quiet') |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 846 | err = None |
| 847 | if not quiet and sys.stdout.isatty(): |
| 848 | cmd.append('--progress') |
| 849 | elif not verbose: |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 850 | err = subprocess.PIPE |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 851 | cmd.append(src) |
| 852 | cmd.append('+refs/heads/*:refs/remotes/origin/*') |
Xin Li | 6e53844 | 2018-12-10 11:33:16 -0800 | [diff] [blame] | 853 | cmd.append('+refs/tags/*:refs/tags/*') |
Mike Frysinger | 4847e05 | 2020-02-22 00:07:35 -0500 | [diff] [blame] | 854 | run_git(*cmd, stderr=err, capture_output=False, cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 855 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 856 | |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 857 | def _DownloadBundle(url, cwd, quiet, verbose): |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 858 | if not url.endswith('/'): |
| 859 | url += '/' |
| 860 | url += 'clone.bundle' |
| 861 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 862 | ret = run_git('config', '--get-regexp', 'url.*.insteadof', cwd=cwd, |
| 863 | check=False) |
| 864 | for line in ret.stdout.splitlines(): |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 865 | m = re.compile(r'^url\.(.*)\.insteadof (.*)$').match(line) |
| 866 | if m: |
| 867 | new_url = m.group(1) |
| 868 | old_url = m.group(2) |
| 869 | if url.startswith(old_url): |
| 870 | url = new_url + url[len(old_url):] |
| 871 | break |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 872 | |
| 873 | if not url.startswith('http:') and not url.startswith('https:'): |
| 874 | return False |
| 875 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 876 | dest = open(os.path.join(cwd, '.git', 'clone.bundle'), 'w+b') |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 877 | try: |
| 878 | try: |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 879 | r = urllib.request.urlopen(url) |
| 880 | except urllib.error.HTTPError as e: |
Craig Northway | 0e8828c | 2021-05-05 08:49:26 -0700 | [diff] [blame] | 881 | if e.code not in [400, 401, 403, 404, 501]: |
| 882 | print('warning: Cannot get %s' % url, file=sys.stderr) |
| 883 | print('warning: HTTP error %s' % e.code, file=sys.stderr) |
| 884 | return False |
Sarah Owens | 1f7627f | 2012-10-31 09:21:55 -0700 | [diff] [blame] | 885 | except urllib.error.URLError as e: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 886 | print('fatal: Cannot get %s' % url, file=sys.stderr) |
| 887 | print('fatal: error %s' % e.reason, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 888 | raise CloneFailure() |
| 889 | try: |
Mike Frysinger | dcbfadf | 2020-02-22 00:04:39 -0500 | [diff] [blame] | 890 | if verbose: |
| 891 | print('Downloading clone bundle %s' % url, file=sys.stderr) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 892 | while True: |
| 893 | buf = r.read(8192) |
Mike Frysinger | 1b9adab | 2019-07-04 17:54:54 -0400 | [diff] [blame] | 894 | if not buf: |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 895 | return True |
| 896 | dest.write(buf) |
| 897 | finally: |
| 898 | r.close() |
| 899 | finally: |
| 900 | dest.close() |
| 901 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 902 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 903 | def _ImportBundle(cwd): |
| 904 | path = os.path.join(cwd, '.git', 'clone.bundle') |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 905 | try: |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 906 | _Fetch(cwd, cwd, path, True, False) |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 907 | finally: |
| 908 | os.remove(path) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 909 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 910 | |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 911 | def _Clone(url, cwd, clone_bundle, quiet, verbose): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 912 | """Clones a git repository to a new subdirectory of repodir |
| 913 | """ |
Mike Frysinger | dcbfadf | 2020-02-22 00:04:39 -0500 | [diff] [blame] | 914 | if verbose: |
| 915 | print('Cloning git repository', url) |
| 916 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 917 | try: |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 918 | os.mkdir(cwd) |
Sarah Owens | a5be53f | 2012-09-09 15:37:57 -0700 | [diff] [blame] | 919 | except OSError as e: |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 920 | print('fatal: cannot make %s directory: %s' % (cwd, e.strerror), |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 921 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 922 | raise CloneFailure() |
| 923 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 924 | run_git('init', '--quiet', cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 925 | |
Shawn O. Pearce | f322b9a | 2011-09-19 14:50:58 -0700 | [diff] [blame] | 926 | _InitHttp() |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 927 | _SetConfig(cwd, 'remote.origin.url', url) |
| 928 | _SetConfig(cwd, |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 929 | 'remote.origin.fetch', |
| 930 | '+refs/heads/*:refs/remotes/origin/*') |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 931 | if clone_bundle and _DownloadBundle(url, cwd, quiet, verbose): |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 932 | _ImportBundle(cwd) |
Mike Frysinger | edd3d45 | 2020-02-21 23:55:07 -0500 | [diff] [blame] | 933 | _Fetch(url, cwd, 'origin', quiet, verbose) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 934 | |
| 935 | |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 936 | def resolve_repo_rev(cwd, committish): |
| 937 | """Figure out what REPO_REV represents. |
| 938 | |
| 939 | We support: |
| 940 | * refs/heads/xxx: Branch. |
| 941 | * refs/tags/xxx: Tag. |
| 942 | * xxx: Branch or tag or commit. |
| 943 | |
| 944 | Args: |
| 945 | cwd: The git checkout to run in. |
| 946 | committish: The REPO_REV argument to resolve. |
| 947 | |
| 948 | Returns: |
| 949 | A tuple of (remote ref, commit) as makes sense for the committish. |
| 950 | For branches, this will look like ('refs/heads/stable', <revision>). |
| 951 | For tags, this will look like ('refs/tags/v1.0', <revision>). |
| 952 | For commits, this will be (<revision>, <revision>). |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 953 | """ |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 954 | def resolve(committish): |
| 955 | ret = run_git('rev-parse', '--verify', '%s^{commit}' % (committish,), |
| 956 | cwd=cwd, check=False) |
| 957 | return None if ret.returncode else ret.stdout.strip() |
| 958 | |
| 959 | # An explicit branch. |
| 960 | if committish.startswith('refs/heads/'): |
| 961 | remote_ref = committish |
| 962 | committish = committish[len('refs/heads/'):] |
| 963 | rev = resolve('refs/remotes/origin/%s' % committish) |
| 964 | if rev is None: |
| 965 | print('repo: error: unknown branch "%s"' % (committish,), |
| 966 | file=sys.stderr) |
| 967 | raise CloneFailure() |
| 968 | return (remote_ref, rev) |
| 969 | |
| 970 | # An explicit tag. |
| 971 | if committish.startswith('refs/tags/'): |
| 972 | remote_ref = committish |
| 973 | committish = committish[len('refs/tags/'):] |
| 974 | rev = resolve(remote_ref) |
| 975 | if rev is None: |
| 976 | print('repo: error: unknown tag "%s"' % (committish,), |
| 977 | file=sys.stderr) |
| 978 | raise CloneFailure() |
| 979 | return (remote_ref, rev) |
| 980 | |
| 981 | # See if it's a short branch name. |
| 982 | rev = resolve('refs/remotes/origin/%s' % committish) |
| 983 | if rev: |
| 984 | return ('refs/heads/%s' % (committish,), rev) |
| 985 | |
| 986 | # See if it's a tag. |
| 987 | rev = resolve('refs/tags/%s' % committish) |
| 988 | if rev: |
| 989 | return ('refs/tags/%s' % (committish,), rev) |
| 990 | |
| 991 | # See if it's a commit. |
| 992 | rev = resolve(committish) |
| 993 | if rev and rev.lower().startswith(committish.lower()): |
| 994 | return (rev, rev) |
| 995 | |
| 996 | # Give up! |
| 997 | print('repo: error: unable to resolve "%s"' % (committish,), file=sys.stderr) |
| 998 | raise CloneFailure() |
| 999 | |
| 1000 | |
Mike Frysinger | 3599cc3 | 2020-02-29 02:53:41 -0500 | [diff] [blame] | 1001 | def verify_rev(cwd, remote_ref, rev, quiet): |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 1002 | """Verify the commit has been signed by a tag.""" |
| 1003 | ret = run_git('describe', rev, cwd=cwd) |
| 1004 | cur = ret.stdout.strip() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1005 | |
| 1006 | m = re.compile(r'^(.*)-[0-9]{1,}-g[0-9a-f]{1,}$').match(cur) |
| 1007 | if m: |
| 1008 | cur = m.group(1) |
| 1009 | if not quiet: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1010 | print(file=sys.stderr) |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 1011 | print("warning: '%s' is not signed; falling back to signed release '%s'" |
| 1012 | % (remote_ref, cur), file=sys.stderr) |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1013 | print(file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1014 | |
Shawn O. Pearce | f18cb76 | 2010-12-07 11:41:05 -0800 | [diff] [blame] | 1015 | env = os.environ.copy() |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 1016 | _setenv('GNUPGHOME', gpg_dir, env) |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 1017 | run_git('tag', '-v', cur, cwd=cwd, env=env) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1018 | return '%s^0' % cur |
| 1019 | |
| 1020 | |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 1021 | def _Checkout(cwd, remote_ref, rev, quiet): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1022 | """Checkout an upstream branch into the repository and track it. |
| 1023 | """ |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 1024 | run_git('update-ref', 'refs/heads/default', rev, cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1025 | |
| 1026 | _SetConfig(cwd, 'branch.default.remote', 'origin') |
Mike Frysinger | cfc8111 | 2020-02-29 02:56:32 -0500 | [diff] [blame] | 1027 | _SetConfig(cwd, 'branch.default.merge', remote_ref) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1028 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 1029 | run_git('symbolic-ref', 'HEAD', 'refs/heads/default', cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1030 | |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 1031 | cmd = ['read-tree', '--reset', '-u'] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1032 | if not quiet: |
| 1033 | cmd.append('-v') |
| 1034 | cmd.append('HEAD') |
Mike Frysinger | 62285d2 | 2020-02-12 08:01:38 -0500 | [diff] [blame] | 1035 | run_git(*cmd, cwd=cwd) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1036 | |
| 1037 | |
| 1038 | def _FindRepo(): |
| 1039 | """Look for a repo installation, starting at the current directory. |
| 1040 | """ |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1041 | curdir = os.getcwd() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1042 | repo = None |
| 1043 | |
Anthony Newnam | df14a70 | 2011-01-09 17:31:57 -0800 | [diff] [blame] | 1044 | olddir = None |
Jonathan Nieder | 6248e0f | 2020-10-28 11:27:41 -0700 | [diff] [blame] | 1045 | while curdir != olddir and not repo: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1046 | repo = os.path.join(curdir, repodir, REPO_MAIN) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1047 | if not os.path.isfile(repo): |
| 1048 | repo = None |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1049 | olddir = curdir |
| 1050 | curdir = os.path.dirname(curdir) |
| 1051 | return (repo, os.path.join(curdir, repodir)) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1052 | |
| 1053 | |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1054 | class _Options(object): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1055 | help = False |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 1056 | version = False |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1057 | |
| 1058 | |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 1059 | def _ExpandAlias(name): |
| 1060 | """Look up user registered aliases.""" |
| 1061 | # We don't resolve aliases for existing subcommands. This matches git. |
| 1062 | if name in {'gitc-init', 'help', 'init'}: |
| 1063 | return name, [] |
| 1064 | |
| 1065 | alias = _GetRepoConfig('alias.%s' % (name,)) |
| 1066 | if alias is None: |
| 1067 | return name, [] |
| 1068 | |
| 1069 | args = alias.strip().split(' ', 1) |
| 1070 | name = args[0] |
| 1071 | if len(args) == 2: |
| 1072 | args = shlex.split(args[1]) |
| 1073 | else: |
| 1074 | args = [] |
| 1075 | return name, args |
| 1076 | |
| 1077 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1078 | def _ParseArguments(args): |
| 1079 | cmd = None |
| 1080 | opt = _Options() |
| 1081 | arg = [] |
| 1082 | |
Sarah Owens | a6053d5 | 2012-11-01 13:36:50 -0700 | [diff] [blame] | 1083 | for i in range(len(args)): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1084 | a = args[i] |
| 1085 | if a == '-h' or a == '--help': |
| 1086 | opt.help = True |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 1087 | elif a == '--version': |
| 1088 | opt.version = True |
Mike Frysinger | 6fb0cb5 | 2020-02-12 09:39:23 -0500 | [diff] [blame] | 1089 | elif a == '--trace': |
| 1090 | trace.set(True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1091 | elif not a.startswith('-'): |
| 1092 | cmd = a |
| 1093 | arg = args[i + 1:] |
| 1094 | break |
| 1095 | return cmd, opt, arg |
| 1096 | |
| 1097 | |
Mike Frysinger | e5670c8 | 2021-01-07 22:14:25 -0500 | [diff] [blame] | 1098 | class Requirements(object): |
| 1099 | """Helper for checking repo's system requirements.""" |
| 1100 | |
| 1101 | REQUIREMENTS_NAME = 'requirements.json' |
| 1102 | |
| 1103 | def __init__(self, requirements): |
| 1104 | """Initialize. |
| 1105 | |
| 1106 | Args: |
| 1107 | requirements: A dictionary of settings. |
| 1108 | """ |
| 1109 | self.requirements = requirements |
| 1110 | |
| 1111 | @classmethod |
| 1112 | def from_dir(cls, path): |
| 1113 | return cls.from_file(os.path.join(path, cls.REQUIREMENTS_NAME)) |
| 1114 | |
| 1115 | @classmethod |
| 1116 | def from_file(cls, path): |
| 1117 | try: |
| 1118 | with open(path, 'rb') as f: |
| 1119 | data = f.read() |
| 1120 | except EnvironmentError: |
| 1121 | # NB: EnvironmentError is used for Python 2 & 3 compatibility. |
| 1122 | # If we couldn't open the file, assume it's an old source tree. |
| 1123 | return None |
| 1124 | |
| 1125 | return cls.from_data(data) |
| 1126 | |
| 1127 | @classmethod |
| 1128 | def from_data(cls, data): |
| 1129 | comment_line = re.compile(br'^ *#') |
| 1130 | strip_data = b''.join(x for x in data.splitlines() if not comment_line.match(x)) |
| 1131 | try: |
| 1132 | json_data = json.loads(strip_data) |
| 1133 | except Exception: # pylint: disable=broad-except |
| 1134 | # If we couldn't parse it, assume it's incompatible. |
| 1135 | return None |
| 1136 | |
| 1137 | return cls(json_data) |
| 1138 | |
| 1139 | def _get_soft_ver(self, pkg): |
| 1140 | """Return the soft version for |pkg| if it exists.""" |
| 1141 | return self.requirements.get(pkg, {}).get('soft', ()) |
| 1142 | |
| 1143 | def _get_hard_ver(self, pkg): |
| 1144 | """Return the hard version for |pkg| if it exists.""" |
| 1145 | return self.requirements.get(pkg, {}).get('hard', ()) |
| 1146 | |
| 1147 | @staticmethod |
| 1148 | def _format_ver(ver): |
| 1149 | """Return a dotted version from |ver|.""" |
| 1150 | return '.'.join(str(x) for x in ver) |
| 1151 | |
| 1152 | def assert_ver(self, pkg, curr_ver): |
| 1153 | """Verify |pkg|'s |curr_ver| is new enough.""" |
| 1154 | curr_ver = tuple(curr_ver) |
| 1155 | soft_ver = tuple(self._get_soft_ver(pkg)) |
| 1156 | hard_ver = tuple(self._get_hard_ver(pkg)) |
| 1157 | if curr_ver < hard_ver: |
| 1158 | print('repo: error: Your version of "%s" (%s) is unsupported; ' |
| 1159 | 'Please upgrade to at least version %s to continue.' % |
| 1160 | (pkg, self._format_ver(curr_ver), self._format_ver(soft_ver)), |
| 1161 | file=sys.stderr) |
| 1162 | sys.exit(1) |
| 1163 | |
| 1164 | if curr_ver < soft_ver: |
| 1165 | print('repo: warning: Your version of "%s" (%s) is no longer supported; ' |
| 1166 | 'Please upgrade to at least version %s to avoid breakage.' % |
| 1167 | (pkg, self._format_ver(curr_ver), self._format_ver(soft_ver)), |
| 1168 | file=sys.stderr) |
| 1169 | |
| 1170 | def assert_all(self): |
| 1171 | """Assert all of the requirements are satisified.""" |
| 1172 | # See if we need a repo launcher upgrade first. |
| 1173 | self.assert_ver('repo', VERSION) |
| 1174 | |
| 1175 | # Check python before we try to import the repo code. |
| 1176 | self.assert_ver('python', sys.version_info) |
| 1177 | |
| 1178 | # Check git while we're at it. |
| 1179 | self.assert_ver('git', ParseGitVersion()) |
| 1180 | |
| 1181 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1182 | def _Usage(): |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 1183 | gitc_usage = "" |
| 1184 | if get_gitc_manifest_dir(): |
| 1185 | gitc_usage = " gitc-init Initialize a GITC Client.\n" |
| 1186 | |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1187 | print( |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1188 | """usage: repo COMMAND [ARGS] |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1189 | |
| 1190 | repo is not yet installed. Use "repo init" to install it here. |
| 1191 | |
| 1192 | The most commonly used repo commands are: |
| 1193 | |
| 1194 | init Install repo in the current working directory |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 1195 | """ + gitc_usage + |
Mark E. Hamilton | 4088eb4 | 2016-02-10 10:44:30 -0700 | [diff] [blame] | 1196 | """ help Display detailed help on a command |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1197 | |
| 1198 | For access to the full online help, install repo ("repo init"). |
Mike Frysinger | 35159ab | 2019-06-13 00:07:13 -0400 | [diff] [blame] | 1199 | """) |
Mike Frysinger | a1cd770 | 2021-04-20 23:38:04 -0400 | [diff] [blame] | 1200 | print('Bug reports:', BUG_URL) |
Mike Frysinger | 35159ab | 2019-06-13 00:07:13 -0400 | [diff] [blame] | 1201 | sys.exit(0) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1202 | |
| 1203 | |
| 1204 | def _Help(args): |
| 1205 | if args: |
Mike Frysinger | d8fda90 | 2020-02-14 00:24:38 -0500 | [diff] [blame] | 1206 | if args[0] in {'init', 'gitc-init'}: |
| 1207 | parser = GetParser(gitc_init=args[0] == 'gitc-init') |
| 1208 | parser.print_help() |
Dan Willemsen | 9ff2ece | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 1209 | sys.exit(0) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1210 | else: |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1211 | print("error: '%s' is not a bootstrap command.\n" |
| 1212 | ' For access to online help, install repo ("repo init").' |
| 1213 | % args[0], file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1214 | else: |
| 1215 | _Usage() |
| 1216 | sys.exit(1) |
| 1217 | |
| 1218 | |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 1219 | def _Version(): |
| 1220 | """Show version information.""" |
| 1221 | print('<repo not installed>') |
| 1222 | print('repo launcher version %s' % ('.'.join(str(x) for x in VERSION),)) |
| 1223 | print(' (from %s)' % (__file__,)) |
| 1224 | print('git %s' % (ParseGitVersion().full,)) |
| 1225 | print('Python %s' % sys.version) |
Mike Frysinger | 5f11eac | 2020-02-25 15:09:01 -0500 | [diff] [blame] | 1226 | uname = platform.uname() |
Mike Frysinger | e257d56 | 2020-03-23 16:55:02 -0400 | [diff] [blame] | 1227 | if sys.version_info.major < 3: |
| 1228 | # Python 3 returns a named tuple, but Python 2 is simpler. |
| 1229 | print(uname) |
| 1230 | else: |
| 1231 | print('OS %s %s (%s)' % (uname.system, uname.release, uname.version)) |
| 1232 | print('CPU %s (%s)' % |
| 1233 | (uname.machine, uname.processor if uname.processor else 'unknown')) |
Mike Frysinger | a1cd770 | 2021-04-20 23:38:04 -0400 | [diff] [blame] | 1234 | print('Bug reports:', BUG_URL) |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 1235 | sys.exit(0) |
| 1236 | |
| 1237 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1238 | def _NotInstalled(): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1239 | print('error: repo is not installed. Use "repo init" to install it here.', |
| 1240 | file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1241 | sys.exit(1) |
| 1242 | |
| 1243 | |
| 1244 | def _NoCommands(cmd): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1245 | print("""error: command '%s' requires repo to be installed first. |
| 1246 | Use "repo init" to install it here.""" % cmd, file=sys.stderr) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1247 | sys.exit(1) |
| 1248 | |
| 1249 | |
| 1250 | def _RunSelf(wrapper_path): |
| 1251 | my_dir = os.path.dirname(wrapper_path) |
| 1252 | my_main = os.path.join(my_dir, 'main.py') |
| 1253 | my_git = os.path.join(my_dir, '.git') |
| 1254 | |
| 1255 | if os.path.isfile(my_main) and os.path.isdir(my_git): |
Shawn O. Pearce | c8a300f | 2009-05-18 13:19:57 -0700 | [diff] [blame] | 1256 | for name in ['git_config.py', |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1257 | 'project.py', |
| 1258 | 'subcmds']: |
| 1259 | if not os.path.exists(os.path.join(my_dir, name)): |
| 1260 | return None, None |
| 1261 | return my_main, my_git |
| 1262 | return None, None |
| 1263 | |
| 1264 | |
| 1265 | def _SetDefaultsTo(gitdir): |
| 1266 | global REPO_URL |
| 1267 | global REPO_REV |
| 1268 | |
| 1269 | REPO_URL = gitdir |
Mike Frysinger | cdb344c | 2020-03-24 02:43:46 -0400 | [diff] [blame] | 1270 | ret = run_git('--git-dir=%s' % gitdir, 'symbolic-ref', 'HEAD', check=False) |
| 1271 | if ret.returncode: |
| 1272 | # If we're not tracking a branch (bisect/etc...), then fall back to commit. |
| 1273 | print('repo: warning: %s has no current branch; using HEAD' % gitdir, |
| 1274 | file=sys.stderr) |
| 1275 | try: |
| 1276 | ret = run_git('rev-parse', 'HEAD', cwd=gitdir) |
| 1277 | except CloneFailure: |
| 1278 | print('fatal: %s has invalid HEAD' % gitdir, file=sys.stderr) |
| 1279 | sys.exit(1) |
| 1280 | |
| 1281 | REPO_REV = ret.stdout.strip() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1282 | |
| 1283 | |
| 1284 | def main(orig_args): |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1285 | cmd, opt, args = _ParseArguments(orig_args) |
| 1286 | |
Mike Frysinger | 8409410 | 2020-02-11 02:10:28 -0500 | [diff] [blame] | 1287 | # We run this early as we run some git commands ourselves. |
| 1288 | SetGitTrace2ParentSid() |
| 1289 | |
Dan Willemsen | 745b4ad | 2015-10-06 15:23:19 -0700 | [diff] [blame] | 1290 | repo_main, rel_repo_dir = None, None |
| 1291 | # Don't use the local repo copy, make sure to switch to the gitc client first. |
| 1292 | if cmd != 'gitc-init': |
| 1293 | repo_main, rel_repo_dir = _FindRepo() |
| 1294 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1295 | wrapper_path = os.path.abspath(__file__) |
| 1296 | my_main, my_git = _RunSelf(wrapper_path) |
| 1297 | |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 1298 | cwd = os.getcwd() |
Dan Willemsen | 2487cb7 | 2015-08-31 15:45:06 -0700 | [diff] [blame] | 1299 | if get_gitc_manifest_dir() and cwd.startswith(get_gitc_manifest_dir()): |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1300 | print('error: repo cannot be used in the GITC local manifest directory.' |
| 1301 | '\nIf you want to work on this GITC client please rerun this ' |
| 1302 | 'command from the corresponding client under /gitc/', |
| 1303 | file=sys.stderr) |
Simran Basi | 8ce5041 | 2015-08-28 14:25:44 -0700 | [diff] [blame] | 1304 | sys.exit(1) |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1305 | if not repo_main: |
Mike Frysinger | 949bc34 | 2020-02-18 21:37:00 -0500 | [diff] [blame] | 1306 | # Only expand aliases here since we'll be parsing the CLI ourselves. |
| 1307 | # If we had repo_main, alias expansion would happen in main.py. |
| 1308 | cmd, alias_args = _ExpandAlias(cmd) |
| 1309 | args = alias_args + args |
| 1310 | |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1311 | if opt.help: |
| 1312 | _Usage() |
| 1313 | if cmd == 'help': |
| 1314 | _Help(args) |
Mike Frysinger | 8ddff5c | 2020-02-09 15:00:25 -0500 | [diff] [blame] | 1315 | if opt.version or cmd == 'version': |
| 1316 | _Version() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1317 | if not cmd: |
| 1318 | _NotInstalled() |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 1319 | if cmd == 'init' or cmd == 'gitc-init': |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1320 | if my_git: |
| 1321 | _SetDefaultsTo(my_git) |
| 1322 | try: |
Simran Basi | 1efc2b4 | 2015-08-05 15:04:22 -0700 | [diff] [blame] | 1323 | _Init(args, gitc_init=(cmd == 'gitc-init')) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1324 | except CloneFailure: |
Sebastian Schuberth | 27226e7 | 2016-10-28 14:27:43 +0200 | [diff] [blame] | 1325 | path = os.path.join(repodir, S_repo) |
Mike Frysinger | c92ce5c | 2019-06-13 01:14:23 -0400 | [diff] [blame] | 1326 | print("fatal: cloning the git-repo repository failed, will remove " |
| 1327 | "'%s' " % path, file=sys.stderr) |
Sebastian Schuberth | 27226e7 | 2016-10-28 14:27:43 +0200 | [diff] [blame] | 1328 | shutil.rmtree(path, ignore_errors=True) |
Mike Frysinger | a010a9f | 2022-08-19 05:16:46 -0400 | [diff] [blame] | 1329 | shutil.rmtree(path + '.tmp', ignore_errors=True) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1330 | sys.exit(1) |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1331 | repo_main, rel_repo_dir = _FindRepo() |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1332 | else: |
| 1333 | _NoCommands(cmd) |
| 1334 | |
| 1335 | if my_main: |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1336 | repo_main = my_main |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1337 | |
Konrad Weihmann | 433977e | 2020-04-17 21:39:32 +0200 | [diff] [blame] | 1338 | if not repo_main: |
| 1339 | print("fatal: unable to find repo entry point", file=sys.stderr) |
| 1340 | sys.exit(1) |
| 1341 | |
Mike Frysinger | e5670c8 | 2021-01-07 22:14:25 -0500 | [diff] [blame] | 1342 | reqs = Requirements.from_dir(os.path.dirname(repo_main)) |
| 1343 | if reqs: |
| 1344 | reqs.assert_all() |
| 1345 | |
David Pursehouse | 685f080 | 2012-11-14 08:34:39 +0900 | [diff] [blame] | 1346 | ver_str = '.'.join(map(str, VERSION)) |
anatoly techtonik | 3a2a59e | 2013-09-21 19:29:10 +0300 | [diff] [blame] | 1347 | me = [sys.executable, repo_main, |
Mickaël Salaün | 2f6ab7f | 2012-09-30 00:37:55 +0200 | [diff] [blame] | 1348 | '--repo-dir=%s' % rel_repo_dir, |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1349 | '--wrapper-version=%s' % ver_str, |
| 1350 | '--wrapper-path=%s' % wrapper_path, |
| 1351 | '--'] |
| 1352 | me.extend(orig_args) |
Mike Frysinger | 3ba716f | 2019-06-13 01:48:12 -0400 | [diff] [blame] | 1353 | exec_command(me) |
| 1354 | print("fatal: unable to start %s" % repo_main, file=sys.stderr) |
| 1355 | sys.exit(148) |
The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1356 | |
| 1357 | |
| 1358 | if __name__ == '__main__': |
| 1359 | main(sys.argv[1:]) |