Josip Sokcevic | dfa44da | 2020-10-27 22:34:08 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env vpython3 |
| 2 | # Copyright 2020 The Chromium 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 | """Migrate local repository onto new default branch.""" |
| 6 | |
| 7 | import fix_encoding |
| 8 | import gerrit_util |
| 9 | import git_common |
| 10 | import metrics |
| 11 | import scm |
| 12 | import sys |
| 13 | import logging |
| 14 | from six.moves import urllib |
| 15 | |
| 16 | |
| 17 | def GetGerritProject(remote_url): |
| 18 | """Returns Gerrit project name based on remote git URL.""" |
| 19 | if remote_url is None: |
| 20 | raise RuntimeError('can\'t detect Gerrit project.') |
| 21 | project = urllib.parse.urlparse(remote_url).path.strip('/') |
| 22 | if project.endswith('.git'): |
| 23 | project = project[:-len('.git')] |
| 24 | # *.googlesource.com hosts ensure that Git/Gerrit projects don't start with |
| 25 | # 'a/' prefix, because 'a/' prefix is used to force authentication in |
| 26 | # gitiles/git-over-https protocol. E.g., |
| 27 | # https://chromium.googlesource.com/a/v8/v8 refers to the same repo/project |
| 28 | # as |
| 29 | # https://chromium.googlesource.com/v8/v8 |
| 30 | if project.startswith('a/'): |
| 31 | project = project[len('a/'):] |
| 32 | return project |
| 33 | |
| 34 | |
| 35 | def GetGerritHost(git_host): |
| 36 | parts = git_host.split('.') |
| 37 | parts[0] = parts[0] + '-review' |
| 38 | return '.'.join(parts) |
| 39 | |
| 40 | |
| 41 | def main(): |
| 42 | remote = git_common.run('remote') |
| 43 | # Use first remote as source of truth |
| 44 | remote = remote.split("\n")[0] |
| 45 | if not remote: |
| 46 | raise RuntimeError('Could not find any remote') |
| 47 | url = scm.GIT.GetConfig(git_common.repo_root(), 'remote.%s.url' % remote) |
| 48 | host = urllib.parse.urlparse(url).netloc |
| 49 | if not host: |
| 50 | raise RuntimeError('Could not find remote host') |
| 51 | |
| 52 | project_head = gerrit_util.GetProjectHead(GetGerritHost(host), |
| 53 | GetGerritProject(url)) |
| 54 | if project_head != 'refs/heads/main': |
| 55 | raise RuntimeError("The repository is not migrated yet.") |
| 56 | |
| 57 | git_common.run('fetch', remote) |
| 58 | |
| 59 | branches = git_common.get_branches_info(True) |
| 60 | |
| 61 | if 'master' in branches: |
| 62 | logging.info("Migrating master branch...") |
| 63 | if 'main' in branches: |
| 64 | logging.info('You already have master and main branch, consider removing ' |
| 65 | 'master manually:\n' |
| 66 | ' $ git branch -d master\n') |
| 67 | else: |
| 68 | git_common.run('branch', '-m', 'master', 'main') |
| 69 | branches = git_common.get_branches_info(True) |
| 70 | |
| 71 | for name in branches: |
| 72 | branch = branches[name] |
| 73 | if not branch: |
| 74 | continue |
| 75 | |
| 76 | if 'master' in branch.upstream: |
| 77 | logging.info("Migrating %s branch..." % name) |
| 78 | new_upstream = branch.upstream.replace('master', 'main') |
| 79 | git_common.run('branch', '--set-upstream-to', new_upstream, name) |
| 80 | git_common.remove_merge_base(name) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | fix_encoding.fix_encoding() |
| 85 | logging.basicConfig(level=logging.INFO) |
| 86 | with metrics.collector.print_notice_and_exit(): |
| 87 | try: |
| 88 | logging.info("Starting migration") |
| 89 | main() |
| 90 | logging.info("Migration completed") |
| 91 | except RuntimeError as e: |
| 92 | logging.error("Error %s" % str(e)) |
| 93 | sys.exit(1) |