diff options
author | George Burgess IV <gbiv@google.com> | 2024-05-08 10:09:36 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-05-08 16:36:23 +0000 |
commit | 76c4b128255f5da4d9607b4eeb4c3b88e9e45d99 (patch) | |
tree | ce1043fc2413f030021d14c9ffb901e3b9424a81 | |
parent | 6aa55d73067176796a4fde35d9d7217ad6cb5c95 (diff) | |
download | toolchain-utils-76c4b128255f5da4d9607b4eeb4c3b88e9e45d99.tar.gz |
llvm_tools: remove git module
This is no longer used except by its own tests.
BUG=b:327058411
TEST=repo upload
Change-Id: I7555c7fcd743d8e44417b6e285c3a849fc8b3aab
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5525662
Auto-Submit: George Burgess <gbiv@chromium.org>
Commit-Queue: Bob Haarman <inglorion@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Bob Haarman <inglorion@chromium.org>
-rw-r--r-- | llvm_tools/git.py | 142 | ||||
-rw-r--r-- | llvm_tools/git_test.py | 156 |
2 files changed, 0 insertions, 298 deletions
diff --git a/llvm_tools/git.py b/llvm_tools/git.py deleted file mode 100644 index 7ca44b04..00000000 --- a/llvm_tools/git.py +++ /dev/null @@ -1,142 +0,0 @@ -#!/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. - -"""Git helper functions.""" - -import collections -import os -from pathlib import Path -import re -import subprocess -import tempfile -from typing import Iterable, Optional, Union - - -CommitContents = collections.namedtuple("CommitContents", ["url", "cl_number"]) - - -def CreateBranch(repo: Union[Path, str], branch: str) -> None: - """Creates a branch in the given repo. - - Args: - repo: The absolute path to the repo. - branch: The name of the branch to create. - - Raises: - ValueError: Failed to create a repo in that directory. - """ - - if not os.path.isdir(repo): - raise ValueError("Invalid directory path provided: %s" % repo) - - subprocess.check_output(["git", "-C", repo, "reset", "HEAD", "--hard"]) - - subprocess.check_output(["repo", "start", branch], cwd=repo) - - -def DeleteBranch(repo: Union[Path, str], branch: str) -> None: - """Deletes a branch in the given repo. - - Args: - repo: The absolute path of the repo. - branch: The name of the branch to delete. - - Raises: - ValueError: Failed to delete the repo in that directory. - """ - - if not os.path.isdir(repo): - raise ValueError("Invalid directory path provided: %s" % repo) - - def run_checked(cmd): - subprocess.run(["git", "-C", repo] + cmd, check=True) - - run_checked(["checkout", "-q", "m/main"]) - run_checked(["reset", "-q", "HEAD", "--hard"]) - run_checked(["branch", "-q", "-D", branch]) - - -def CommitChanges( - repo: Union[Path, str], commit_messages: Iterable[str] -) -> None: - """Commit changes without uploading them. - - Args: - repo: The absolute path to the repo where changes were made. - commit_messages: Messages to concatenate to form the commit message. - """ - if not os.path.isdir(repo): - raise ValueError("Invalid path provided: %s" % repo) - - # Create a git commit. - with tempfile.NamedTemporaryFile(mode="w+t", encoding="utf-8") as f: - f.write("\n".join(commit_messages)) - f.flush() - - subprocess.check_output(["git", "commit", "-F", f.name], cwd=repo) - - -def UploadChanges( - repo: Union[Path, str], - branch: str, - reviewers: Optional[Iterable[str]] = None, - cc: Optional[Iterable[str]] = None, - wip: bool = False, -) -> CommitContents: - """Uploads the changes in the specifed branch of the given repo for review. - - Args: - repo: The absolute path to the repo where changes were made. - branch: The name of the branch to upload. - of the changes made. - reviewers: A list of reviewers to add to the CL. - cc: A list of contributors to CC about the CL. - wip: Whether to upload the change as a work-in-progress. - - Returns: - A CommitContents value containing the commit URL and change list number. - - Raises: - ValueError: Failed to create a commit or failed to upload the - changes for review. - """ - - if not os.path.isdir(repo): - raise ValueError("Invalid path provided: %s" % repo) - - # Upload the changes for review. - git_args = [ - "repo", - "upload", - "--yes", - f'--reviewers={",".join(reviewers)}' if reviewers else "--ne", - "--no-verify", - f"--br={branch}", - ] - - if cc: - git_args.append(f'--cc={",".join(cc)}') - if wip: - git_args.append("--wip") - - out = subprocess.check_output( - git_args, - stderr=subprocess.STDOUT, - cwd=repo, - encoding="utf-8", - ) - - print(out) - # Matches both internal and external CLs. - found_url = re.search( - r"https?://[\w-]*-review.googlesource.com/c/.*/([0-9]+)", - out.rstrip(), - ) - if not found_url: - raise ValueError("Failed to find change list URL.") - - return CommitContents( - url=found_url.group(0), cl_number=int(found_url.group(1)) - ) diff --git a/llvm_tools/git_test.py b/llvm_tools/git_test.py deleted file mode 100644 index e58bb491..00000000 --- a/llvm_tools/git_test.py +++ /dev/null @@ -1,156 +0,0 @@ -#!/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. - -"""Unit tests for git helper functions.""" - -import os -import subprocess -import tempfile -import unittest -from unittest import mock - -from llvm_tools import git - - -# These are unittests; protected access is OK to a point. -# pylint: disable=protected-access - - -class HelperFunctionsTest(unittest.TestCase): - """Test class for updating LLVM hashes of packages.""" - - @mock.patch.object(os.path, "isdir", return_value=False) - def testFailedToCreateBranchForInvalidDirectoryPath(self, mock_isdir): - path_to_repo = "/invalid/path/to/repo" - branch = "branch-name" - - # Verify the exception is raised when provided an invalid directory - # path. - with self.assertRaises(ValueError) as err: - git.CreateBranch(path_to_repo, branch) - - self.assertEqual( - str(err.exception), - "Invalid directory path provided: %s" % path_to_repo, - ) - - mock_isdir.assert_called_once() - - @mock.patch.object(os.path, "isdir", return_value=True) - @mock.patch.object(subprocess, "check_output", return_value=None) - def testSuccessfullyCreatedBranch(self, mock_command_output, mock_isdir): - path_to_repo = "/path/to/repo" - branch = "branch-name" - - git.CreateBranch(path_to_repo, branch) - - mock_isdir.assert_called_once_with(path_to_repo) - - self.assertEqual(mock_command_output.call_count, 2) - - @mock.patch.object(os.path, "isdir", return_value=False) - def testFailedToDeleteBranchForInvalidDirectoryPath(self, mock_isdir): - path_to_repo = "/invalid/path/to/repo" - branch = "branch-name" - - # Verify the exception is raised on an invalid repo path. - with self.assertRaises(ValueError) as err: - git.DeleteBranch(path_to_repo, branch) - - self.assertEqual( - str(err.exception), - "Invalid directory path provided: %s" % path_to_repo, - ) - - mock_isdir.assert_called_once() - - @mock.patch.object(os.path, "isdir", return_value=True) - @mock.patch.object(subprocess, "run", return_value=None) - def testSuccessfullyDeletedBranch(self, mock_command_output, mock_isdir): - path_to_repo = "/valid/path/to/repo" - branch = "branch-name" - - git.DeleteBranch(path_to_repo, branch) - - mock_isdir.assert_called_once_with(path_to_repo) - - self.assertEqual(mock_command_output.call_count, 3) - - @mock.patch.object(os.path, "isdir", return_value=False) - def testFailedToUploadChangesForInvalidDirectoryPath(self, mock_isdir): - path_to_repo = "/some/path/to/repo" - branch = "update-LLVM_NEXT_HASH-a123testhash3" - - # Verify exception is raised when on an invalid repo path. - with self.assertRaises(ValueError) as err: - git.UploadChanges(path_to_repo, branch) - - self.assertEqual( - str(err.exception), "Invalid path provided: %s" % path_to_repo - ) - - mock_isdir.assert_called_once() - - @mock.patch.object(os.path, "isdir", return_value=True) - @mock.patch.object(subprocess, "check_output") - @mock.patch.object(tempfile, "NamedTemporaryFile") - def testSuccessfullyUploadedChangesForReview( - self, mock_tempfile, mock_commands, mock_isdir - ): - path_to_repo = "/some/path/to/repo" - branch = "branch-name" - commit_messages = ["Test message"] - mock_tempfile.return_value.__enter__.return_value.name = "tmp" - - # A test CL generated by `repo upload`. - mock_commands.side_effect = [ - None, - ( - "remote: https://chromium-review.googlesource." - "com/c/chromiumos/overlays/chromiumos-overlay/" - "+/193147 Fix stdout" - ), - ] - git.CommitChanges(path_to_repo, commit_messages) - change_list = git.UploadChanges(path_to_repo, branch) - - self.assertEqual(change_list.cl_number, 193147) - - mock_isdir.assert_called_with(path_to_repo) - - expected_command = [ - "git", - "commit", - "-F", - mock_tempfile.return_value.__enter__.return_value.name, - ] - self.assertEqual( - mock_commands.call_args_list[0], - mock.call(expected_command, cwd=path_to_repo), - ) - - expected_cmd = [ - "repo", - "upload", - "--yes", - "--ne", - "--no-verify", - "--br=%s" % branch, - ] - self.assertEqual( - mock_commands.call_args_list[1], - mock.call( - expected_cmd, - stderr=subprocess.STDOUT, - cwd=path_to_repo, - encoding="utf-8", - ), - ) - - self.assertEqual( - change_list.url, - "https://chromium-review.googlesource.com/c/chromiumos/overlays/" - "chromiumos-overlay/+/193147", - ) |