diff options
author | George Burgess IV <gbiv@google.com> | 2024-04-16 08:46:20 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2024-04-16 17:39:50 +0000 |
commit | 54e65f7949aca046aaa703514913ff5cd50f1626 (patch) | |
tree | 2d6bd3cd7dad0e2951070488215d55f219dee416 | |
parent | f46a883d0fc95495cbc2b819590e91579e8c32e5 (diff) | |
download | toolchain-utils-54e65f7949aca046aaa703514913ff5cd50f1626.tar.gz |
git: add helper to determine if something's a full SHA
BUG=b:333462347
TEST=repo upload
Change-Id: I83ba2269e45c8a14ef548a8c1f4b6a352356e23e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/5458711
Commit-Queue: George Burgess <gbiv@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
Reviewed-by: Ryan Beltran <ryanbeltran@chromium.org>
-rwxr-xr-x | llvm_tools/git.py | 5 | ||||
-rwxr-xr-x | llvm_tools/git_unittest.py | 18 |
2 files changed, 23 insertions, 0 deletions
diff --git a/llvm_tools/git.py b/llvm_tools/git.py index 7ca44b04..ea4ce3d2 100755 --- a/llvm_tools/git.py +++ b/llvm_tools/git.py @@ -17,6 +17,11 @@ from typing import Iterable, Optional, Union CommitContents = collections.namedtuple("CommitContents", ["url", "cl_number"]) +def IsFullGitSHA(s: str) -> bool: + """Returns if `s` looks like a git SHA.""" + return len(s) == 40 and all(x.isdigit() or "a" <= x <= "f" for x in s) + + def CreateBranch(repo: Union[Path, str], branch: str) -> None: """Creates a branch in the given repo. diff --git a/llvm_tools/git_unittest.py b/llvm_tools/git_unittest.py index 940f0dba..fa756ddf 100755 --- a/llvm_tools/git_unittest.py +++ b/llvm_tools/git_unittest.py @@ -17,10 +17,28 @@ import git # These are unittests; protected access is OK to a point. # pylint: disable=protected-access +EXAMPLE_GIT_SHA = "d46d9c1a23118e3943f43fe2dfc9f9c9c0b4aefe" + class HelperFunctionsTest(unittest.TestCase): """Test class for updating LLVM hashes of packages.""" + def testIsFullGitSHASuccessCases(self): + shas = ("a" * 40, EXAMPLE_GIT_SHA) + for s in shas: + self.assertTrue(git.IsFullGitSHA(s), s) + + def testIsFullGitSHAFailureCases(self): + shas = ( + "", + "A" * 40, + "g" * 40, + EXAMPLE_GIT_SHA[1:], + EXAMPLE_GIT_SHA + "a", + ) + for s in shas: + self.assertFalse(git.IsFullGitSHA(s), s) + @mock.patch.object(os.path, "isdir", return_value=False) def testFailedToCreateBranchForInvalidDirectoryPath(self, mock_isdir): path_to_repo = "/invalid/path/to/repo" |