From db27c47e91abeb31fe09e6345fd0afe97f40da18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ciar=C3=A1n=20O=E2=80=99Mara?= Date: Tue, 27 Oct 2026 17:06:37 +1100 Subject: [PATCH] Search the release branch first for a commit to tag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The README says that the branch set by the `branch` input is the first one searched for a commit to tag, but the tree→commit cache read `git log --all` alone, keeping the most recent commit for each tree. Where several commits share a registered tree, such as a release's merge commit on the release branch and a later merge of it into a development branch under gitflow, TagBot tagged the latter, which the registry didn't register and which isn't on the release branch. With `branch` set, the cache now reads that branch's history first, then the full history, so the commit on the release branch wins. The subdirectory cache, derived from the full cache, follows. Without `branch`, the cache is built as before. --- tagbot/action/repo.py | 22 ++++++++++++----- test/action/test_repo.py | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/tagbot/action/repo.py b/tagbot/action/repo.py index db3f84cb..84346b58 100644 --- a/tagbot/action/repo.py +++ b/tagbot/action/repo.py @@ -591,15 +591,27 @@ 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: @@ -607,9 +619,7 @@ def _build_tree_to_commit_cache(self) -> Dict[str, str]: # 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 diff --git a/test/action/test_repo.py b/test/action/test_repo.py index a4fae68a..78f48820 100644 --- a/test/action/test_repo.py +++ b/test/action/test_repo.py @@ -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")