aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/git_llvm_rev_test.py
diff options
context:
space:
mode:
authorGeorge Burgess IV <gbiv@google.com>2019-11-08 15:13:19 -0800
committerGeorge Burgess <gbiv@chromium.org>2019-11-20 18:39:07 +0000
commit55f4933ede7a2b87e759e879b93b73a4570444e6 (patch)
tree05930c1068982361819121850638677516511c67 /llvm_tools/git_llvm_rev_test.py
parenta1c0899146a516acf1134505db0a44a2596aeee7 (diff)
downloadtoolchain-utils-55f4933ede7a2b87e759e879b93b73a4570444e6.tar.gz
Add a git-llvm-rev tool
The main down-side to this is sort of inherent in any git-based numbering scheme: as time goes on, our distance from r375505 is going to increase, so any tool that takes this approach will take progressively longer. To get an idea of what that means in practice, I set `base_llvm_revision = 199999` (from 2014) and messed around. Converting a near-ToT SHA to a rev took 250ms, and converting that rev back into a SHA took 191ms. For reference, at their current settings, they take 83ms and 106ms to perform those same options, respectively. Hence, I doubt this will become a problem in the next 5 years. If it does start to bite us, we can add complexity to bound our searches. The only op known to take quite a while on this is converting a branch-only commit to a rev, because `git branch -r --contains ${X}` takes quite a while. I don't think that's a common op, so we can worry about that later. BUG=None TEST=A few random commits round-tripped. Tests passed. `mypy --strict` was happy. Change-Id: If093f3593a9f1aa3c6275d9dcd2785864dbfce1d Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1907408 Tested-by: George Burgess <gbiv@chromium.org> Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Diffstat (limited to 'llvm_tools/git_llvm_rev_test.py')
-rwxr-xr-xllvm_tools/git_llvm_rev_test.py122
1 files changed, 122 insertions, 0 deletions
diff --git a/llvm_tools/git_llvm_rev_test.py b/llvm_tools/git_llvm_rev_test.py
new file mode 100755
index 00000000..ebb654ff
--- /dev/null
+++ b/llvm_tools/git_llvm_rev_test.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+# Copyright 2019 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import os
+import sys
+import unittest
+
+import git_llvm_rev
+
+
+def get_llvm_checkout() -> str:
+ my_dir = os.path.dirname(os.path.abspath(__file__))
+ return os.path.join(my_dir, 'llvm-project-copy')
+
+
+def get_llvm_config() -> git_llvm_rev.LLVMConfig:
+ return git_llvm_rev.LLVMConfig(dir=get_llvm_checkout(), remote='origin')
+
+
+class Test(unittest.TestCase):
+
+ def rev_to_sha_with_round_trip(self, rev: git_llvm_rev.Rev) -> str:
+ config = get_llvm_config()
+ sha = git_llvm_rev.translate_rev_to_sha(config, rev)
+ roundtrip_rev = git_llvm_rev.translate_sha_to_rev(config, sha)
+ self.assertEqual(roundtrip_rev, rev)
+ return sha
+
+ def test_sha_to_rev_on_base_sha_works(self) -> None:
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(
+ branch='master', number=git_llvm_rev.base_llvm_revision))
+ self.assertEqual(sha, git_llvm_rev.base_llvm_sha)
+
+ def test_sha_to_rev_prior_to_base_rev_works(self) -> None:
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='master', number=375000))
+ self.assertEqual(sha, '2f6da767f13b8fd81f840c211d405fea32ac9db7')
+
+ def test_sha_to_rev_after_base_rev_works(self) -> None:
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='master', number=375506))
+ self.assertEqual(sha, '3bf7fddeb05655d9baed4cc69e13535c677ed1dd')
+
+ def test_llvm_svn_parsing_runs_ignore_reverts(self) -> None:
+ # This commit has a revert that mentions the reverted llvm-svn in the
+ # commit message.
+
+ # Commit which performed the revert
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='master', number=374895))
+ self.assertEqual(sha, '1731fc88d1fa1fa55edd056db73a339b415dd5d6')
+
+ # Commit that was reverted
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='master', number=374841))
+ self.assertEqual(sha, '2a1386c81de504b5bda44fbecf3f7b4cdfd748fc')
+
+ def test_imaginary_revs_raise(self) -> None:
+ with self.assertRaises(ValueError) as r:
+ git_llvm_rev.translate_rev_to_sha(
+ get_llvm_config(), git_llvm_rev.Rev(branch='master', number=9999999))
+
+ self.assertIn('Try updating your tree?', str(r.exception))
+
+ # NOTE: The below tests have _zz_ in their name as an optimization. Iterating
+ # on a quick test is painful when these larger tests come before it and take
+ # 7secs to run. Python's unittest module guarantees tests are run in
+ # alphabetical order by their method name, so...
+ #
+ # If you're wondering, the slow part is `git branch -r --contains`. I imagine
+ # it's going to be very cold code, so I'm not inclined to optimize it much.
+
+ def test_zz_branch_revs_work_after_merge_points_and_svn_cutoff(self) -> None:
+ # Arbitrary 9.x commit without an attached llvm-svn: value.
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='release/9.x', number=366670))
+ self.assertEqual(sha, '4e858e4ac00b59f064da4e1f7e276916e7d296aa')
+
+ def test_zz_branch_revs_work_at_merge_points(self) -> None:
+ rev_number = 366426
+ backing_sha = 'c89a3d78f43d81b9cff7b9248772ddf14d21b749'
+
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='master', number=rev_number))
+ self.assertEqual(sha, backing_sha)
+
+ # Note that this won't round-trip: since this commit is on the master
+ # branch, we'll pick master for this. That's fine
+ sha = git_llvm_rev.translate_rev_to_sha(
+ get_llvm_config(),
+ git_llvm_rev.Rev(branch='release/9.x', number=rev_number))
+ self.assertEqual(sha, backing_sha)
+
+ def test_zz_branch_revs_work_after_merge_points(self) -> None:
+ # Picking the commit on the 9.x branch after the merge-base for that +
+ # master. Note that this is where llvm-svn numbers should diverge from
+ # ours, and are therefore untrustworthy. The commit for this *does* have a
+ # different `llvm-svn:` string than we should have.
+ sha = self.rev_to_sha_with_round_trip(
+ git_llvm_rev.Rev(branch='release/9.x', number=366427))
+ self.assertEqual(sha, '2cf681a11aea459b50d712abc7136f7129e4d57f')
+
+
+# FIXME: When release/10.x happens, it may be nice to have a test-case
+# generally covering that, since it's the first branch that we have to travel
+# back to the base commit for.
+
+if __name__ == '__main__':
+ # We have exactly one concrete target that we often have checked out anyway.
+ # Rather than building tests that hopefully match that target, use it
+ # directly.
+ if not os.path.isdir(get_llvm_checkout()):
+ print(
+ 'Please checkout llvm-project-copy to run these tests. A simple way '
+ 'to do that is running `./get_llvm_hash.py --llvm_version 370000`',
+ file=sys.stderr)
+ sys.exit(1)
+ unittest.main()