Skip to content

Commit c10766c

Browse files
authored
[utils] add stop_at_sha to revert_checker's API (#152011)
This is useful for downstream consumers of this as a module. It's unclear if interactive use wants this lever, but support can easily be added if so.
1 parent 01bc742 commit c10766c

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

llvm/utils/revert_checker.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
import argparse
4343
import collections
44+
import itertools
4445
import logging
4546
import re
4647
import subprocess
@@ -246,7 +247,11 @@ def _load_pr_commit_mappings(
246247
# enough for the 99% case of reverts: rarely should someone land a cleanish
247248
# revert of a >6 month old change...
248249
def find_reverts(
249-
git_dir: str, across_ref: str, root: str, max_pr_lookback: int = 20000
250+
git_dir: str,
251+
across_ref: str,
252+
root: str,
253+
max_pr_lookback: int = 20000,
254+
stop_at_sha: Optional[str] = None,
250255
) -> List[Revert]:
251256
"""Finds reverts across `across_ref` in `git_dir`, starting from `root`.
252257
@@ -260,6 +265,9 @@ def find_reverts(
260265
SHAs. These heuristics require that commit history from `root` to
261266
`some_parent_of_root` is loaded in memory. `max_pr_lookback` is how
262267
many commits behind `across_ref` should be loaded in memory.
268+
stop_at_sha: If non-None and `stop_at_sha` is encountered while walking
269+
to `across_ref` from `root`, stop checking for reverts. This allows for
270+
faster incremental checking between `find_reverts` calls.
263271
"""
264272
across_sha = _rev_parse(git_dir, across_ref)
265273
root_sha = _rev_parse(git_dir, root)
@@ -281,10 +289,16 @@ def find_reverts(
281289
root_sha,
282290
)
283291

292+
commit_log_stream: Iterable[_LogEntry] = _log_stream(git_dir, root_sha, across_sha)
293+
if stop_at_sha:
294+
commit_log_stream = itertools.takewhile(
295+
lambda x: x.sha != stop_at_sha, commit_log_stream
296+
)
297+
284298
all_reverts = []
285299
# Lazily load PR <-> commit mappings, since it can be expensive.
286300
pr_commit_mappings = None
287-
for sha, commit_message in _log_stream(git_dir, root_sha, across_sha):
301+
for sha, commit_message in commit_log_stream:
288302
reverts, pr_reverts = _try_parse_reverts_from_commit_message(
289303
commit_message,
290304
)

llvm/utils/revert_checker_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,45 @@ def test_known_reverts_across_arbitrary_llvm_rev(self) -> None:
130130
],
131131
)
132132

133+
def test_stop_at_sha_stops_early(self) -> None:
134+
reverts = revert_checker.find_reverts(
135+
git_dir=get_llvm_project_path(),
136+
# This SHA is a direct child of the reverted SHA expected below.
137+
across_ref="2d5f3b0a61fb171617012a2c3ba05fd31fb3bb1d",
138+
# This SHA is a direct child of the revert SHA listed below.
139+
root="2c01b278580212914ec037bb5dd9b73702dfe7f1",
140+
max_pr_lookback=50,
141+
# This SHA is the first revert that would be returned, if not for
142+
# `stop_at_sha`.
143+
stop_at_sha="50866e84d1da8462aeb96607bf6d9e5bbd5869c5",
144+
)
145+
self.assertEqual(reverts, [])
146+
147+
def test_stop_at_sha_still_catches_reverts_in_range(self) -> None:
148+
reverts = revert_checker.find_reverts(
149+
git_dir=get_llvm_project_path(),
150+
# This SHA is a direct child of the reverted SHA expected below.
151+
across_ref="2d5f3b0a61fb171617012a2c3ba05fd31fb3bb1d",
152+
# This SHA is the direct child of the revert mentioned in
153+
# `assertEqual` below.
154+
root="2c01b278580212914ec037bb5dd9b73702dfe7f1",
155+
max_pr_lookback=50,
156+
# This SHA is the direct parent of the revert mentioned in
157+
# `assertEqual` below.
158+
stop_at_sha="b96ebee1fab2b281c97deb54f3d61c469fe07d01",
159+
)
160+
self.assertEqual(
161+
reverts,
162+
[
163+
revert_checker.Revert(
164+
# This SHA is a `Reverts ${PR}` for #111004.
165+
sha="50866e84d1da8462aeb96607bf6d9e5bbd5869c5",
166+
# ...And this was the commit for #111004.
167+
reverted_sha="67160c5ab5f5b7fd5fa7851abcfde367c8a9f91b",
168+
),
169+
],
170+
)
171+
133172
def test_pr_based_revert_works(self) -> None:
134173
reverts = revert_checker.find_reverts(
135174
git_dir=get_llvm_project_path(),

0 commit comments

Comments
 (0)