Skip to content

Commit

Permalink
get_ref: Fix annotated tags (#10)
Browse files Browse the repository at this point in the history
* dulwich: Use `get_peeled` for Tags

Fixes `get_ref` for annotated tags.

Closes iterative/dvc#6383

* pygit2: Use `peel` for annotted tags in `get_ref`.

Fixes `get_ref` for annotated tags.
  • Loading branch information
daavoo authored Dec 22, 2021
1 parent 9dcd906 commit dbfdb67
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions scmrepo/git/backend/dulwich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def set_ref(
raise SCMError(f"Failed to set '{name}'")

def get_ref(self, name, follow: bool = True) -> Optional[str]:
from dulwich.objects import Tag
from dulwich.refs import parse_symref_value

name_b = os.fsencode(name)
Expand All @@ -350,6 +351,8 @@ def get_ref(self, name, follow: bool = True) -> Optional[str]:
except ValueError:
pass
if ref:
if ref in self.repo and isinstance(self.repo[ref], Tag):
ref = self.repo.get_peeled(name_b)
return os.fsdecode(ref)
return None

Expand Down
9 changes: 8 additions & 1 deletion scmrepo/git/backend/pygit2.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,20 @@ def set_ref(
)

def get_ref(self, name, follow: bool = True) -> Optional[str]:
from pygit2 import GIT_REF_SYMBOLIC
from pygit2 import GIT_OBJ_COMMIT, GIT_REF_SYMBOLIC, Tag

ref = self.repo.references.get(name)
if not ref:
return None
if follow and ref.type == GIT_REF_SYMBOLIC:
ref = ref.resolve()
try:
obj = self.repo[ref.target]
if isinstance(obj, Tag):
return str(obj.peel(GIT_OBJ_COMMIT).id)
except ValueError:
pass

return str(ref.target)

def remove_ref(self, name: str, old_ref: Optional[str] = None):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,12 @@ def test_get_ref(tmp_dir: TmpDir, scm: Git, git: Git):
): "ref: refs/heads/master",
}
)
scm.tag(["-a", "annotated", "-m", "Annotated Tag"])

assert init_rev == git.get_ref("refs/foo/bar")
assert init_rev == git.get_ref("refs/foo/baz")
assert init_rev == git.get_ref("refs/tags/annotated", follow=True)
assert init_rev == git.get_ref("refs/tags/annotated", follow=False)
assert git.get_ref("refs/foo/baz", follow=False) == "refs/heads/master"
assert git.get_ref("refs/foo/qux") is None

Expand Down

0 comments on commit dbfdb67

Please sign in to comment.