Skip to content

Commit df0bc27

Browse files
committed
Correctly handle component versions which are tags
Tags show up as heads in the remote, but without the remote name as prefix. Previously the code would then try to checkout a branch with the name of the tag. This commit makes the assumption that any heads in a remote which don't have the remote name as a prefix are tags, and handles checkout accordingly. To keep the things simple, tags are checked out in detached HEAD state.
1 parent b8bdad8 commit df0bc27

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
### Fixed
2323

2424
* Values in `parameters.components` always have precedence over values in `parameters.component_versions` ([#289])
25+
* Correctly handle tags as component versions ([#290])
2526

2627
## [v0.4.2] - 2021-01-14
2728

@@ -339,3 +340,4 @@ Initial implementation
339340
[#286]: https://github.com/projectsyn/commodore/pull/286
340341
[#287]: https://github.com/projectsyn/commodore/pull/287
341342
[#289]: https://github.com/projectsyn/commodore/pull/289
343+
[#290]: https://github.com/projectsyn/commodore/pull/290

commodore/component/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def parameters_key(self):
114114
return component_parameters_key(self.name)
115115

116116
def checkout(self):
117-
remote_heads = self._repo.remote().fetch(prune=True)
117+
remote_heads = self._repo.remote().fetch(prune=True, tags=True)
118118
remote_prefix = self._repo.remote().name + "/"
119119
version = self._version
120120
if self._version is None:
@@ -127,17 +127,23 @@ def checkout(self):
127127

128128
version = version.replace(remote_prefix, "", 1)
129129
for head in remote_heads:
130-
branch = head.name
131-
if branch.startswith(remote_prefix):
132-
branch = branch.replace(remote_prefix, "", 1)
133-
if branch == version:
130+
tag = None
131+
branch = None
132+
headname = head.name
133+
if headname.startswith(remote_prefix):
134+
branch = headname.replace(remote_prefix, "", 1)
135+
else:
136+
tag = headname
137+
138+
if headname == version:
134139
commit = head.commit
135140
break
136141
else:
137-
# If we haven't found a branch matching the requested version,
142+
# If we haven't found a branch or tag matching the requested version,
138143
# assume the version is a commit sha.
139144
commit = version
140145
branch = None
146+
tag = None
141147

142148
try:
143149
if branch:
@@ -152,6 +158,9 @@ def checkout(self):
152158

153159
head.set_tracking_branch(self.repo.remote().refs[branch])
154160
self._repo.head.reference = head
161+
elif tag:
162+
# Simply create detached head pointing to tag
163+
self._repo.head.reference = tag
155164
else:
156165
# Create detached head by setting repo.head.reference as
157166
# direct ref to commit object.

tests/test_component.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ def _setup_component(
3737
tmp_path: P,
3838
version="master",
3939
repo_url=REPO_URL,
40+
name="argocd",
4041
):
4142
return Component(
42-
"argocd",
43+
name,
4344
repo_url=repo_url,
44-
directory=tmp_path / "argocd",
45+
directory=tmp_path / name,
4546
version=version,
4647
)
4748

@@ -128,6 +129,20 @@ def test_component_checkout_sha1version(tmp_path: P):
128129
assert c.repo.head.commit.hexsha == commit
129130

130131

132+
def test_component_checkout_tag(tmp_path: P):
133+
c = _setup_component(
134+
tmp_path,
135+
version="v1.0.0",
136+
repo_url="https://github.com/projectsyn/component-backup-k8up.git",
137+
name="backup-k8up",
138+
)
139+
140+
c.checkout()
141+
142+
assert c.repo.head.is_detached
143+
assert c.repo.head.commit.hexsha == c.repo.tags["v1.0.0"].commit.hexsha
144+
145+
131146
def test_component_checkout_nonexisting_version(tmp_path: P):
132147
c = _setup_component(tmp_path, version="does-not-exist")
133148

0 commit comments

Comments
 (0)