Files
chromium_depot_tools/git_migrate_default_branch.py
Aravind Vasudevan 1f441eb375 [fix_encoding] Remove fix_encoding
The fix_encoding module within depot_tools was included back in the python2[1] days to as a be all encoding fix boilerplate that is called across depot_tools scripts.

However, now that depot_tools officially deprecated support for py2 and support >= 3.8[2], the boilerplate is not needed anymore.

* `fix_win_codec()`[3] The 'cp65001' codec issue this fixes is fixed in python 3.3[4].
* `fix_default_encoding()`[5] python3 defaults to utf8.
* `fix_win_sys_argv()`[6] sys.srgv unicode issue is fixed in python3[7].
* `fix_win_console()`[8] Fixed[9].

[1] https://codereview.chromium.org/6721029
[2] https://crrev.com/371aa997c04791d21e222ed43a1a0d55b450dd53/README.md
[3] https://source.chromium.org/chromium/chromium/tools/depot_tools/+/main:fix_encoding.py;l=123-132;drc=cfa826c9845122d445dce4f51f556381865dbed3
[4] https://github.com/python/cpython/issues/57425#issuecomment-1093559969
[5] https://source.chromium.org/chromium/chromium/tools/depot_tools/+/main:fix_encoding.py;l=29-66;drc=cfa826c9845122d445dce4f51f556381865dbed3
[6] https://crsrc.org/d/fix_encoding.py;l=73-120;drc=cfa826c9845122d445dce4f51f556381865dbed3
[7] https://github.com/python/cpython/issues/46381#issuecomment-1093409968
[8] https://source.chromium.org/chromium/chromium/tools/depot_tools/+/main:fix_encoding.py;l=315-344;drc=cfa826c9845122d445dce4f51f556381865dbed3
[9] https://github.com/python/cpython/issues/45943#issuecomment-1093402603

Bug: 1501984
Change-Id: I1d512a4b1bfe14e680ac0aa08027849b999cc638
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5263016
Reviewed-by: Josip Sokcevic <sokcevic@chromium.org>
Reviewed-by: Dirk Pranke <dpranke@google.com>
Commit-Queue: Aravind Vasudevan <aravindvasudev@google.com>
2024-02-06 18:59:48 +00:00

102 lines
3.5 KiB
Python

#!/usr/bin/env vpython3
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Migrate local repository onto new default branch."""
import gerrit_util
import git_common
import metrics
import scm
import sys
import logging
import urllib.parse
def GetGerritProject(remote_url):
"""Returns Gerrit project name based on remote git URL."""
if remote_url is None:
raise RuntimeError('can\'t detect Gerrit project.')
project = urllib.parse.urlparse(remote_url).path.strip('/')
if project.endswith('.git'):
project = project[:-len('.git')]
# *.googlesource.com hosts ensure that Git/Gerrit projects don't start with
# 'a/' prefix, because 'a/' prefix is used to force authentication in
# gitiles/git-over-https protocol. E.g.,
# https://chromium.googlesource.com/a/v8/v8 refers to the same repo/project
# as
# https://chromium.googlesource.com/v8/v8
if project.startswith('a/'):
project = project[len('a/'):]
return project
def GetGerritHost(git_host):
parts = git_host.split('.')
parts[0] = parts[0] + '-review'
return '.'.join(parts)
def main():
remote = git_common.run('remote')
# Use first remote as source of truth
remote = remote.split("\n")[0]
if not remote:
raise RuntimeError('Could not find any remote')
url = scm.GIT.GetConfig(git_common.repo_root(), 'remote.%s.url' % remote)
host = urllib.parse.urlparse(url).netloc
if not host:
raise RuntimeError('Could not find remote host')
project_head = gerrit_util.GetProjectHead(GetGerritHost(host),
GetGerritProject(url))
if project_head != 'refs/heads/main':
raise RuntimeError("The repository is not migrated yet.")
# User may have set to fetch only old default branch. Ensure fetch is
# tracking main too.
git_common.run('config', '--unset-all', 'remote.origin.fetch',
'refs/heads/*')
git_common.run('config', '--add', 'remote.origin.fetch',
'+refs/heads/*:refs/remotes/origin/*')
logging.info("Running fetch...")
git_common.run('fetch', remote)
logging.info("Updating remote HEAD...")
git_common.run('remote', 'set-head', '-a', remote)
branches = git_common.get_branches_info(True)
if 'master' in branches:
logging.info("Migrating master branch...")
if 'main' in branches:
logging.info(
'You already have master and main branch, consider removing '
'master manually:\n'
' $ git branch -d master\n')
else:
git_common.run('branch', '-m', 'master', 'main')
branches = git_common.get_branches_info(True)
for name in branches:
branch = branches[name]
if not branch:
continue
if 'master' in branch.upstream:
logging.info("Migrating %s branch..." % name)
new_upstream = branch.upstream.replace('master', 'main')
git_common.run('branch', '--set-upstream-to', new_upstream, name)
git_common.remove_merge_base(name)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
with metrics.collector.print_notice_and_exit():
try:
logging.info("Starting migration")
main()
logging.info("Migration completed")
except RuntimeError as e:
logging.error("Error %s" % str(e))
sys.exit(1)