Skip to content

Commit

Permalink
Fall back to repo clone when tree SHA can't be found within lookback
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-dG committed Mar 3, 2020
1 parent ef74598 commit 8115dfc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions tagbot/action/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ def check(self, *argv: str, repo: Optional[str] = "") -> bool:
except Abort:
return False

def commit_sha_of_tree(self, tree: str) -> Optional[str]:
"""Get the commit SHA of a corresponding tree SHA."""
# We need --all in case the registered commit isn't on the default branch.
for line in self.command("log", "--all", "--format=%H %T").splitlines():
# The format of each line is "<commit sha> <tree sha>".
c, t = line.split()
if t == tree:
return c
return None

def commit_sha_of_default(self) -> str:
"""Get the commit SHA of the default branch."""
return self.command("rev-parse", self._default_branch)
Expand Down
5 changes: 4 additions & 1 deletion tagbot/action/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def _commit_sha_of_tree(self, tree: str) -> Optional[str]:
sha = self._commit_sha_of_tree_from_branch(branch.name, tree, since)
if sha:
return sha
return None
# For a valid tree SHA, the only time that we reach here is when a release
# has been made long after the commit was made, which is reasonably rare.
# Fall back to cloning the repo in that case.
return self._git.commit_sha_of_tree(tree)

def _commit_sha_of_tag(self, version: str) -> Optional[str]:
"""Look up the commit SHA of a given tag."""
Expand Down
8 changes: 8 additions & 0 deletions test/action/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def test_default_branch():
assert g._default_branch == "master"


def test_commit_sha_of_tree():
g = _git(command="a b\n c d\n d e\n")
assert g.commit_sha_of_tree("b") == "a"
g.command.assert_called_with("log", "--all", "--format=%H %T")
assert g.commit_sha_of_tree("e") == "d"
assert g.commit_sha_of_tree("c") is None


def test_commit_sha_of_default():
g = _git(command="abcdef")
g._Git__default_branch = "branch"
Expand Down
5 changes: 5 additions & 0 deletions test/action/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ def test_commit_sha_of_tree():
r._commit_sha_of_tree_from_branch.assert_called_once_with("master", "tree", now)
assert r._commit_sha_of_tree("tree") == "sha2"
r._commit_sha_of_tree_from_branch.assert_called_with("foo", "tree", now)
r._commit_sha_of_tree_from_branch.side_effect = None
r._commit_sha_of_tree_from_branch.return_value = None
r._git.commit_sha_of_tree = Mock(side_effect=["sha", None])
assert r._commit_sha_of_tree("tree") == "sha"
assert r._commit_sha_of_tree("tree") is None


def test_commit_sha_of_tag():
Expand Down

0 comments on commit 8115dfc

Please sign in to comment.