Skip to content

Instantly share code, notes, and snippets.

@Kobzol
Created November 10, 2024 20:40
Show Gist options
  • Select an option

  • Save Kobzol/ba9f56e01dd2d21446eba933c55297f6 to your computer and use it in GitHub Desktop.

Select an option

Save Kobzol/ba9f56e01dd2d21446eba933c55297f6 to your computer and use it in GitHub Desktop.
# Requires $ pip install tqdm gitpython
import datetime
from collections import defaultdict
from os import path
from git import Repo
from tqdm import tqdm
def iter_merge_commits(days: int):
repo = Repo("../")
# Generate the cutoff date for one year ago
cutoff_date = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=days)
for commit in tqdm(repo.iter_commits('master', first_parent=True)):
# Check if the commit date is older than the cutoff
if commit.committed_datetime < cutoff_date:
break
# Check if the commit author is 'bors' and the message starts with "Auto merge" or "Rollup merge"
if commit.author.name != 'bors' or (
not commit.message.startswith("Auto merge") and not commit.message.startswith(
"Rollup merge")):
continue
yield commit
def cacheable_changes():
allowlist = [
".clang-format",
".editorconfig",
".git-blame-ignore-revs",
".gitattributes",
".gitignore",
".gitmodules",
".ignore",
".mailmap",
"CODE_OF_CONDUCT.md",
"CONTRIBUTING.md",
"COPYRIGHT",
"INSTALL.md",
"LICENSE-APACHE",
"LICENSE-MIT",
"LICENSES",
"README.md",
"RELEASES.md",
"REUSE.toml",
"config.example.toml",
"rust-bors.toml",
"rustfmt.toml",
"tests",
"triagebot.toml",
"src/ci/cpu-usage-over-time.py",
"src/ci/publish_toolstate.sh",
"src/doc",
"src/etc",
"src/librustdoc",
"src/rustdoc-json-types",
"src/README.md",
]
total_commits = 0
cacheable_commits = 0
for commit in iter_merge_commits(days=365):
total_commits += 1
cacheable = True
for changed_file in commit.stats.files:
is_ok = False
for allowed in allowlist:
if changed_file.startswith(allowed):
is_ok = True
break
if not is_ok:
cacheable = False
break
if cacheable:
cacheable_commits += 1
print(f"Cacheable: {cacheable_commits}/{total_commits} ({cacheable_commits / total_commits * 100:.2f}%)")
if __name__ == "__main__":
cacheable_changes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment