Skip to content
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
22 changes: 16 additions & 6 deletions tagbot/action/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,25 +591,35 @@ def _build_tree_to_commit_cache(self) -> Dict[str, str]:

Uses git log to get all commit:tree pairs in one command,
enabling O(1) lookups instead of iterating through commits.

With a configured release branch, its history is read first, so that
where several commits share a tree, such as a release's merge commit
and a later merge of it into another branch, the commit on the
release branch is chosen, as the `branch` input documents.
"""
if self.__tree_to_commit_cache is not None:
return self.__tree_to_commit_cache

logger.debug("Building tree→commit cache")
cache: Dict[str, str] = {}
try:
# Get all commit:tree pairs in one git command
output = self._git.command("log", "--all", "--format=%H %T")
revisions = ["--all"]
if self.__release_branch:
revisions.insert(0, f"origin/{self.__release_branch}")
for revision in revisions:
try:
# Get all commit:tree pairs in one git command
output = self._git.command("log", revision, "--format=%H %T")
except Exception as e:
logger.warning(f"Failed to read {revision} for tree→commit cache: {e}")
continue
for line in output.splitlines():
parts = line.split()
if len(parts) == 2:
commit_sha, tree_sha = parts
# Only keep first occurrence (most recent commit for that tree)
if tree_sha not in cache:
cache[tree_sha] = commit_sha
logger.debug(f"Tree→commit cache built with {len(cache)} entries")
except Exception as e:
logger.warning(f"Failed to build tree→commit cache: {e}")
logger.debug(f"Tree→commit cache built with {len(cache)} entries")

self.__tree_to_commit_cache = cache
return cache
Expand Down
51 changes: 51 additions & 0 deletions test/action/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,57 @@ def test_commit_sha_of_tree():
assert r._commit_sha_of_tree("nonexistent") is None


def test_commit_sha_of_tree_prefers_release_branch():
"""With a release branch, a tree that several commits share resolves to the
commit on that branch, such as a release's merge commit rather than a later
merge of it into another branch."""
r = _repo(branch="master")

def log(*args):
if args[1] == "origin/master":
return "merge_sha shared_tree\nold_sha old_tree"
return "backmerge_sha shared_tree\nmerge_sha shared_tree\nold_sha old_tree"

r._git.command = Mock(side_effect=log)
assert r._commit_sha_of_tree("shared_tree") == "merge_sha"
assert r._git.command.call_args_list == [
call("log", "origin/master", "--format=%H %T"),
call("log", "--all", "--format=%H %T"),
]
# Commits on other branches still resolve, from the full history
r = _repo(branch="master")
r._git.command = Mock(side_effect=log)
assert r._commit_sha_of_tree("old_tree") == "old_sha"


def test_commit_sha_of_tree_subdir_prefers_release_branch():
"""The subdirectory cache, derived from the full cache, also prefers the
commit on the release branch."""
r = _repo(branch="master", subdir="path/to/package")

def log(*args):
if args[1] == "origin/master":
return "merge_sha root_tree"
return "backmerge_sha root_tree\nmerge_sha root_tree"

r._git.command = Mock(side_effect=log)
with patch.object(r, "_subdir_tree_hash", return_value="dir_tree"):
assert r._commit_sha_of_tree("dir_tree") == "merge_sha"


def test_commit_sha_of_tree_missing_release_branch():
"""A release branch that can't be read leaves the full history."""
r = _repo(branch="gone")

def log(*args):
if args[1] == "origin/gone":
raise Abort("unknown revision")
return "sha1 tree1"

r._git.command = Mock(side_effect=log)
assert r._commit_sha_of_tree("tree1") == "sha1"


def test_commit_sha_of_tree_subdir_fallback():
"""Test subdirectory tree→commit cache."""
r = _repo(subdir="path/to/package")
Expand Down
Loading