Repository navigation
Conversation
… pass The conventional-monorepo parser triggered one git diff subprocess per commit through GitPython's Commit.stats for every non-merge commit in the full-history walk of ReleaseHistory.from_git_history(), making release runs minutes-long on monorepos with long histories (python-semantic-release#1521: 13.6 ms/commit, ~32k non-merge commits, ~10 min per release on a 44,996-commit monorepo). Add ConventionalCommitMonorepoParser.prepare_changed_paths(repo), which builds a commit-sha to path-filter-match index from a single git log HEAD --topo-order --no-renames --name-only --format=%x1e%H pass, and have ReleaseHistory.from_git_history() invoke it via a capability check when the parser provides it (changelog command included). _has_relevant_changed_files() consults the index and falls back to the original per-commit stats lookup for commits missing from the index (merge and empty commits, which git log lists without file entries, so ignore_merge_commits = false configurations still diff merges against their first parent). --no-renames --name-only yields the same repo-relative path keys as the numstat output parsed by Commit.stats, including git's C-quoting of unusual paths, so filter verdicts are identical. If the batch fails, the parser logs a warning, disables the index, and falls back per-commit: the optimization is fail-open. Measured on the 44,996-commit monorepo from python-semantic-release#1521: - ReleaseHistory.from_git_history(): 515 s -> 6.4 s (81x) - per-commit classifications identical for all 44,996 commits - rendered CHANGELOG.md byte-identical against stock v10.7.0 Refs python-semantic-release#1521 Assisted-by: opencode (openrouter/z-ai/glm-5.3-flash)
|
AI disclosure: OpenAI Codex performed this investigation, prepared this comment and patch, and ran the checks below. Posted at the contributor's request. I found a filename-quoting compatibility edge when testing this PR at That GitPython source unquotes the paths returned by Both this PR and the GitPython main behavior above are unreleased. The published GitPython 3.2.1 wheel still retains quoted stats paths, even though it reports the same version number as that main checkout. The reproduction therefore needs the exact GitPython source above. A small conservative follow-up is to leave commits containing any quoted path out of the prepared index. The existing stats fallback then preserves the installed GitPython implementation's semantics. Ordinary paths retain batching; quoted-path commits retain their existing per-commit lookup cost. I added six real-repository parity cases: plain, accented Latin, and Arabic filenames, each with
Environment: Windows 11, Python 3.13.17, Git 2.56.0.windows.4, CLI backend. Ruff 0.6.1 lint and To run the six cases, set Separately, an eight-commit synthetic process-count check confirmed this PR's ordinary-path batching benefit: 8 diff processes without preparation, versus 1 log process and 0 diffs with preparation, with identical relevance results. This was a process-count check, not a timing benchmark. No large-repository timing, Gix backend comparison, full-suite pass, or end-to-end release is claimed; Gix had no compatible prebuilt Windows wheel, and the broader parser run was stopped before completion. The quoted-path follow-up preserves the published wheel's existing per-commit behavior. Proposed follow-up patch against da03e68 (implementation and regression tests)diff --git a/src/semantic_release/commit_parser/conventional/parser_monorepo.py b/src/semantic_release/commit_parser/conventional/parser_monorepo.py
index 1149e23..68d03b1 100644
--- a/src/semantic_release/commit_parser/conventional/parser_monorepo.py
+++ b/src/semantic_release/commit_parser/conventional/parser_monorepo.py
@@ -516,10 +516,10 @@ class ConventionalCommitMonorepoParser(
every commit in one ``git log --name-only`` invocation and stores only
the filter-match result for each commit.
- Commits missing from the index (merge commits, empty commits, or commits
- created after the batch ran) transparently fall back to the per-commit
- stats lookup. On any failure the index stays disabled and the original
- behavior is used, so this can never fail a release.
+ Commits missing from the index (merge commits, empty commits, commits with
+ quoted paths, or commits created after the batch ran) transparently fall
+ back to the per-commit stats lookup. On any failure the index stays disabled
+ and the original behavior is used, so this can never fail a release.
"""
git_root = (
Path(repo.working_tree_dir or repo.working_dir).absolute().resolve()
@@ -557,6 +557,11 @@ class ConventionalCommitMonorepoParser(
# which can diff a merge against its first parent, is used.
continue
+ # GitPython versions differ in whether Commit.stats unquotes paths.
+ # Keep their per-commit behavior whenever Git quotes a filename.
+ if any(path.startswith('"') for path in changed_paths):
+ continue
+
index[lines[0]] = self._changed_paths_match(
(str(git_root / path) for path in changed_paths),
selection_filters,
diff --git a/tests/unit/semantic_release/commit_parser/test_conventional_monorepo.py b/tests/unit/semantic_release/commit_parser/test_conventional_monorepo.py
index 76bba53..61d5833 100644
--- a/tests/unit/semantic_release/commit_parser/test_conventional_monorepo.py
+++ b/tests/unit/semantic_release/commit_parser/test_conventional_monorepo.py
@@ -186,6 +186,29 @@ def test_rename_out_of_package_is_relevant(git_repo: Repo):
assert parser._changed_paths_index[rename_commit.hexsha] is True
+@pytest.mark.parametrize("quote_paths", [True, False])
+@pytest.mark.parametrize("filename", ["plain.py", "café.py", "مهمة.py"])
+def test_quoted_paths_preserve_per_commit_results(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch, quote_paths: bool, filename: str
+):
+ with Repo.init(tmp_path, initial_branch="main") as repo:
+ with repo.config_writer() as config:
+ config.set_value("user", "name", "pytest")
+ config.set_value("user", "email", "pytest@example.com")
+ config.set_value("core", "quotePath", quote_paths)
+ commit = _commit_files(
+ repo, "fix: update package", {f"{PKG_A}/{filename}": "pass\n"}
+ )
+ monkeypatch.chdir(tmp_path / PKG_A)
+ unindexed_parser = _make_parser(ignore_merge_commits=True)
+ expected = _classify(unindexed_parser, commit)
+
+ indexed_parser = _make_parser(ignore_merge_commits=True)
+ indexed_parser.prepare_changed_paths(repo)
+
+ assert _classify(indexed_parser, commit) == expected
+
+
def test_merge_commit_falls_back_to_stats(git_repo: Repo):
"""Merge commits are absent from the index and resolved via Commit.stats."""
parser = _make_parser(ignore_merge_commits=False) |
Purpose
Resolves the performance half of #1521: the
conventional-monorepoparser pays onegit diffsubprocess per commit whileReleaseHistory.from_git_history()walks therepository's full history, which makes every release run minutes-long in monorepos
with long histories.
Rationale
Commit.stats(GitPython) spawns onegit diff <parent> <sha> --numstat --no-renames --rawsubprocess per commit, and the monorepo parser triggers it for every non-mergecommit via
_has_relevant_changed_files(). On a monorepo with 44,996 commits(12,910 merge commits, ~32k non-merge commits) this measured 13.6 ms/commit — about
7.5 minutes of subprocess spawns per release, on top of the walk itself.
This change replaces the per-commit lookups with a single batched pass:
ConventionalCommitMonorepoParser.prepare_changed_paths(repo)runs onegit log HEAD --topo-order --no-renames --name-only --format=%x1e%Hinvocationand builds a
commit-sha -> filter-matchindex. The%x1erecord separator and--name-onlyoutput make the parse unambiguous (paths are C-quoted by git whenthey contain special characters, so they cannot collide with record boundaries).
ReleaseHistory.from_git_history()callsprepare_changed_paths()when theparser provides it (capability check via
getattr, no isinstance coupling), sothe
changelogcommand benefits as well._has_relevant_changed_files()consults the index first and falls back to theoriginal per-commit
Commit.statslookup for any commit missing from the index(merge and empty commits, which produce no file entries in
git log, plus anycommit the batch could not classify).
Behavior is preserved exactly:
--name-only --no-renamesyields the same repo-relative path set as thefilename column of the
git diff --numstat --no-renamesoutput thatCommit.statsparses (including git's C-quoting of unusual paths), so thefnmatchverdicts are identical.git logprints no entries for them by default, so they areabsent from the index and still resolved through
Commit.stats, which diffs amerge against its first parent. This keeps
ignore_merge_commits = falseconfigurations behaving as before.
git loglists the root commit's files as additions, matchingthe
root=Truehandling inCommit.stats.index disabled, and every commit goes through the original per-commit path. The
optimization is fail-open and cannot fail a release.
Measured on the 44,996-commit monorepo (same repo as reported in #1521):
git diffspawn per commit)git log)ReleaseHistory.from_git_history()semantic-release --noop changelogend to endHow to test?
New unit tests in
tests/unit/semantic_release/commit_parser/test_conventional_monorepo.py:ignore_merge_commitssettings, across a fixture repo covering renames insideand outside the package (the
--no-renamesparity case), a merge commit, anempty commit, a squashed multi-commit message, and a non-conventional message;
Commit.statsverdict;Commit.statsand produce the sameverdict as an unindexed parser;
unindexed behavior;
git diff/git diff-treesubprocess.Additionally, against a real 44,996-commit monorepo: per-commit parse
classifications (type, bump, changelog inclusion) were compared for all commits
between stock v10.7.0 and this change and are identical, the resulting
ReleaseHistorysummaries are identical, and the renderedCHANGELOG.mdoutputis byte-identical.
PR Completion Checklist
and are separated into the proper commit type and scope (recommended order: test, build, feat/fix, docs)
Refs: #1521 (proposes the same batching fix plus a bounded walk for
mode = "update"; the bounded walk is left as a potential follow-up since itneeds a closer look at the changelog render path).