diff options
author | George Burgess IV <gbiv@google.com> | 2024-05-07 10:33:36 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-05-07 22:58:51 +0000 |
commit | 31b68a055f1a764034898e43d7aa7f860483aa5c (patch) | |
tree | a70d622577fae64343e3b36b1c63602014d08622 | |
parent | 4a3e54bd34814bd1666e2fb3370399deb444a484 (diff) | |
download | toolchain-utils-31b68a055f1a764034898e43d7aa7f860483aa5c.tar.gz |
rust_tools: remove llvm_tools.git usage
Looks like _nearly_ all of this can be replaced by existing bits in
`git_utils`.
This also has us call `repo init --head` instead of `repo init`. The
former works on `chromiumos-overlay`'s HEAD, rather than resetting it to
`cros/main`.
BUG=b:332713739
TEST=Unittests
Change-Id: I4cdcb3e9b0aea2e098445dd2ce81595235ea61b5
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5522854
Commit-Queue: George Burgess <gbiv@chromium.org>
Reviewed-by: Bob Haarman <inglorion@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
-rw-r--r-- | cros_utils/git_utils.py | 14 | ||||
-rw-r--r-- | rust_tools/rust_uprev.py | 43 | ||||
-rw-r--r-- | rust_tools/rust_uprev_test.py | 23 |
3 files changed, 59 insertions, 21 deletions
diff --git a/cros_utils/git_utils.py b/cros_utils/git_utils.py index 33d52140..f02bc907 100644 --- a/cros_utils/git_utils.py +++ b/cros_utils/git_utils.py @@ -46,6 +46,20 @@ def is_full_git_sha(s: str) -> bool: return len(s) == 40 and all(x.isdigit() or "a" <= x <= "f" for x in s) +def create_branch(git_repo: Path, branch_name: str) -> None: + """Creates a branch in the given repo. + + Args: + git_repo: The path to the repo. + branch_name: The name of the branch to create. + """ + subprocess.run( + ["repo", "start", branch_name, "--head"], + check=True, + cwd=git_repo, + ) + + def upload_to_gerrit( git_repo: Path, remote: str, diff --git a/rust_tools/rust_uprev.py b/rust_tools/rust_uprev.py index 43c66324..a87025a1 100644 --- a/rust_tools/rust_uprev.py +++ b/rust_tools/rust_uprev.py @@ -35,6 +35,7 @@ import re import shlex import shutil import subprocess +import textwrap import threading import time from typing import ( @@ -52,8 +53,8 @@ from typing import ( ) import urllib.request +from cros_utils import git_utils from llvm_tools import chroot -from llvm_tools import git T = TypeVar("T") @@ -991,7 +992,7 @@ def rust_bootstrap_path() -> Path: return EBUILD_PREFIX.joinpath("dev-lang/rust-bootstrap") -def create_new_repo(rust_version: RustVersion) -> None: +def create_rust_uprev_branch(rust_version: RustVersion) -> None: output = get_command_output( ["git", "status", "--porcelain"], cwd=EBUILD_PREFIX ) @@ -1000,7 +1001,9 @@ def create_new_repo(rust_version: RustVersion) -> None: f"{EBUILD_PREFIX} has uncommitted changes, please either discard " "them or commit them." ) - git.CreateBranch(EBUILD_PREFIX, f"rust-to-{rust_version}") + git_utils.create_branch( + EBUILD_PREFIX, branch_name=f"rust-to-{rust_version}" + ) def build_cross_compiler(template_version: RustVersion) -> None: @@ -1034,16 +1037,25 @@ def build_cross_compiler(template_version: RustVersion) -> None: def create_new_commit(rust_version: RustVersion) -> None: subprocess.check_call(["git", "add", "-A"], cwd=EBUILD_PREFIX) - messages = [ - f"[DO NOT SUBMIT] dev-lang/rust: upgrade to Rust {rust_version}", - "", - "This CL is created by rust_uprev tool automatically." "", - "BUG=None", - "TEST=Use CQ to test the new Rust version", - ] - branch = f"rust-to-{rust_version}" - git.CommitChanges(EBUILD_PREFIX, messages) - git.UploadChanges(EBUILD_PREFIX, branch) + sha = git_utils.commit_all_changes( + EBUILD_PREFIX, + message=textwrap.dedent( + f"""\ + [DO NOT SUBMIT] dev-lang/rust: upgrade to Rust {rust_version} + + This CL is created by rust_uprev tool automatically. + + BUG=None + TEST=Use CQ to test the new Rust version + """ + ), + ) + git_utils.upload_to_gerrit( + EBUILD_PREFIX, + remote=git_utils.CROS_EXTERNAL_REMOTE, + branch=git_utils.CROS_MAIN_BRANCH, + ref=sha, + ) def run_in_chroot(cmd: Command, *args, **kwargs) -> subprocess.CompletedProcess: @@ -1174,7 +1186,10 @@ def main() -> None: rust_ebuild = find_ebuild_for_package("dev-lang/rust") template_version = RustVersion.parse_from_ebuild(rust_ebuild) - run_step("create new repo", lambda: create_new_repo(args.uprev)) + run_step( + "create rust upgrade branch", + lambda: create_rust_uprev_branch(args.uprev), + ) if not args.skip_cross_compiler: run_step( "build cross compiler", diff --git a/rust_tools/rust_uprev_test.py b/rust_tools/rust_uprev_test.py index e7942cad..730f07d8 100644 --- a/rust_tools/rust_uprev_test.py +++ b/rust_tools/rust_uprev_test.py @@ -1,4 +1,3 @@ -#!/usr/bin/env python3 # Copyright 2020 The ChromiumOS Authors # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. @@ -13,7 +12,7 @@ import tempfile import unittest from unittest import mock -from llvm_tools import git +from cros_utils import git_utils # rust_uprev sets SOURCE_ROOT to the output of `repo --show-toplevel`. @@ -748,14 +747,24 @@ class RustUprevOtherStagesTests(unittest.TestCase): ) @mock.patch.object(rust_uprev, "get_command_output") - @mock.patch.object(git, "CreateBranch") - def test_create_new_repo(self, mock_branch, mock_output): + @mock.patch.object(git_utils, "create_branch") + def test_create_rust_upgrade_branch(self, mock_create_branch, mock_output): mock_output.return_value = "" - rust_uprev.create_new_repo(self.new_version) - mock_branch.assert_called_once_with( - rust_uprev.EBUILD_PREFIX, f"rust-to-{self.new_version}" + rust_uprev.create_rust_uprev_branch(self.new_version) + mock_create_branch.assert_called_once_with( + rust_uprev.EBUILD_PREFIX, branch_name=f"rust-to-{self.new_version}" ) + @mock.patch.object(rust_uprev, "get_command_output") + @mock.patch.object(git_utils, "create_branch") + def test_create_rust_upgrade_branch_raises_if_unclean( + self, mock_create_branch, mock_output + ): + mock_output.return_value = "some file has modifications" + with self.assertRaisesRegex(RuntimeError, ".*uncommitted changes.*"): + rust_uprev.create_rust_uprev_branch(self.new_version) + mock_create_branch.assert_not_called() + @mock.patch.object(rust_uprev, "run_in_chroot") def test_build_cross_compiler(self, mock_run_in_chroot): cros_targets = [ |