Skip to content

[utils] add stop_at_sha to revert_checker's API #152011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions llvm/utils/revert_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import argparse
import collections
import itertools
import logging
import re
import subprocess
Expand Down Expand Up @@ -246,7 +247,11 @@ def _load_pr_commit_mappings(
# enough for the 99% case of reverts: rarely should someone land a cleanish
# revert of a >6 month old change...
def find_reverts(
git_dir: str, across_ref: str, root: str, max_pr_lookback: int = 20000
git_dir: str,
across_ref: str,
root: str,
max_pr_lookback: int = 20000,
stop_at_sha: Optional[str] = None,
) -> List[Revert]:
"""Finds reverts across `across_ref` in `git_dir`, starting from `root`.

Expand All @@ -260,6 +265,9 @@ def find_reverts(
SHAs. These heuristics require that commit history from `root` to
`some_parent_of_root` is loaded in memory. `max_pr_lookback` is how
many commits behind `across_ref` should be loaded in memory.
stop_at_sha: If non-None and `stop_at_sha` is encountered while walking
to `across_ref` from `root`, stop checking for reverts. This allows for
faster incremental checking between `find_reverts` calls.
"""
across_sha = _rev_parse(git_dir, across_ref)
root_sha = _rev_parse(git_dir, root)
Expand All @@ -281,10 +289,16 @@ def find_reverts(
root_sha,
)

commit_log_stream: Iterable[_LogEntry] = _log_stream(git_dir, root_sha, across_sha)
if stop_at_sha:
commit_log_stream = itertools.takewhile(
lambda x: x.sha != stop_at_sha, commit_log_stream
)

all_reverts = []
# Lazily load PR <-> commit mappings, since it can be expensive.
pr_commit_mappings = None
for sha, commit_message in _log_stream(git_dir, root_sha, across_sha):
for sha, commit_message in commit_log_stream:
reverts, pr_reverts = _try_parse_reverts_from_commit_message(
commit_message,
)
Expand Down
14 changes: 14 additions & 0 deletions llvm/utils/revert_checker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ def test_known_reverts_across_arbitrary_llvm_rev(self) -> None:
],
)

def test_stop_at_sha_works(self) -> None:
reverts = revert_checker.find_reverts(
git_dir=get_llvm_project_path(),
# This SHA is a direct child of the reverted SHA expected below.
across_ref="2d5f3b0a61fb171617012a2c3ba05fd31fb3bb1d",
# This SHA is a direct child of the revert SHA listed below.
root="2c01b278580212914ec037bb5dd9b73702dfe7f1",
max_pr_lookback=50,
# This SHA is the first revert that would be returned, if not for
# `stop_at_sha`.
stop_at_sha="50866e84d1da8462aeb96607bf6d9e5bbd5869c5",
)
self.assertEqual(reverts, [])

def test_pr_based_revert_works(self) -> None:
reverts = revert_checker.find_reverts(
git_dir=get_llvm_project_path(),
Expand Down