Skip to content

Commit

Permalink
fix: push tags for remote-build
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Schvezov <[email protected]>
  • Loading branch information
sergiusens committed Dec 1, 2023
1 parent 3069141 commit 36f2cb5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
23 changes: 19 additions & 4 deletions snapcraft/remote/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ def add_all(self) -> None:
f"Could not add changes for the git repository in {str(self.path)!r}."
) from error

def commit(self, message: str = "auto commit") -> None:
def commit(self, message: str = "auto commit") -> str:
"""Commit changes to the repo.
:param message: the commit message
:returns: object ID of the commit as str
:raises GitError: if the commit could not be created
"""
logger.debug("Committing changes.")
Expand All @@ -110,7 +112,9 @@ def commit(self, message: str = "auto commit") -> None:
target = [] if self._repo.head_is_unborn else [self._repo.head.target]

try:
self._repo.create_commit("HEAD", author, author, message, tree, target)
return str(
self._repo.create_commit("HEAD", author, author, message, tree, target)
)
except pygit2.GitError as error:
raise GitError(
"Could not create a commit for the git repository "
Expand Down Expand Up @@ -152,13 +156,15 @@ def push_url(
remote_branch: str,
ref: str = "HEAD",
token: Optional[str] = None,
push_tags: bool = False,
) -> None:
"""Push a reference to a branch on a remote url.
:param remote_url: the remote repo URL to push to
:param remote_branch: the branch on the remote to push to
:param ref: name of shorthand ref to push (i.e. a branch, tag, or `HEAD`)
:param token: token in the url to hide in logs and errors
:param push_tags: if true, push all tags to URL (similar to `git push --tags`)
:raises GitError: if the ref cannot be resolved or pushed
"""
Expand All @@ -175,12 +181,21 @@ def push_url(
"Pushing %r to remote %r with refspec %r.", ref, stripped_url, refspec
)

# Create a list of tags to push
if push_tags:
tag_refs = [
t.name
for t in self._repo.references.iterator(pygit2.GIT_REFERENCES_TAGS)
]
else:
tag_refs = []
try:
self._repo.remotes.create_anonymous(remote_url).push([refspec])
self._repo.remotes.create_anonymous(remote_url).push([refspec] + tag_refs)
except pygit2.GitError as error:
raise GitError(
f"Could not push {ref!r} to {stripped_url!r} with refspec {refspec!r} "
f"for the git repository in {str(self.path)!r}."
f"for the git repository in {str(self.path)!r}: "
f"{error!s}"
) from error

def _resolve_ref(self, ref: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion snapcraft/remote/launchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,4 +484,4 @@ def push_source_tree(self, repo_dir: Path) -> None:
logger.info("Sending build data to Launchpad: %s", stripped_url)

repo = GitRepo(repo_dir)
repo.push_url(url, "main", "HEAD", token)
repo.push_url(url, "main", "HEAD", token, push_tags=True)
36 changes: 36 additions & 0 deletions tests/unit/remote/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,42 @@ def test_push_url_branch(new_dir):
assert blob.name == "test-file"


def test_push_tags(new_dir):
"""Verify that tags are push by trying to ref them from the remote."""
# create a local repo and make a commit
Path("local-repo").mkdir()
repo = GitRepo(Path("local-repo"))
(repo.path / "test-file").touch()
repo.add_all()
commit = repo.commit()
tag = "tag1"
repo._repo.create_reference(f"refs/tags/{tag}", commit)
# create a bare remote repo
Path("remote-repo").mkdir()
remote = pygit2.init_repository(Path("remote-repo"), True)

repo.push_url(
remote_url=f"file://{str(Path('remote-repo').absolute())}",
remote_branch="test-branch",
push_tags=True,
)

# verify commit through tag in remote (the `isinstance` checks are to satsify pyright)
commit = remote.revparse_single(tag)
assert isinstance(commit, pygit2.Commit)
assert commit.message == "auto commit"
assert commit.committer.name == "auto commit"
assert commit.committer.email == "auto commit"
# verify tree in remote
tree = commit.tree
assert isinstance(tree, pygit2.Tree)
assert len(tree) == 1
# verify contents of tree in remote
blob = tree[0]
assert isinstance(blob, pygit2.Blob)
assert blob.name == "test-file"


def test_push_url_refspec_unknown_ref(new_dir):
"""Raise an error for an unknown refspec."""
repo = GitRepo(new_dir)
Expand Down
1 change: 1 addition & 0 deletions tests/unit/remote/test_launchpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ def test_push_source_tree(new_dir, mock_git_repo, launchpad_client):
"main",
"HEAD",
"access-token",
push_tags=True,
),
]
)
Expand Down

0 comments on commit 36f2cb5

Please sign in to comment.