Closed
Description
Using pygit2-0.28.1, I have an issue when using for example repo.branches.with_commit()
and one of the branches is in the packed-refs file.
Reproduce with tests/submodulerepo.tar
:
subrepo = pygit2.Repository(r"path\pygit2\test\data\submodulerepo")
# SHA1 for refs/remotes/origin/master from submodulerepo/.git/packed-refs
branches = subrepo.branches.with_commit('215fc695515c4093e2beffb21225ce6e1b3d55bc')
list(branches.remote) # This raises ValueError
You'll get:
File "<input>", line 1, in <module>
File "path\lib\site-packages\pygit2\repository.py", line 1181, in __iter__
if self._commit is None or self.get(branch_name) is not None:
File "path\lib\site-packages\pygit2\repository.py", line 1175, in get
return self[key]
File "path\lib\site-packages\pygit2\repository.py", line 1168, in __getitem__
if branch is None or not self._valid(branch):
File "path\lib\site-packages\pygit2\repository.py", line 1197, in _valid
self._repository.descendant_of(branch.target, self._commit))
ValueError: refs/remotes/origin/master
I'm not 100% sure that it's related to packed-ref, but I tried reproducing when the ref actually exists as a file and didn't get the same issue.
In my ignorance I came up with a workaround,
# pygit2/repository.py :: L1190
def _valid(self, branch):
try:
target = self._repository.expand_id(branch.target)
except ValueError:
target = self._repository.expand_id(self._repository.lookup_reference(branch.target).target)
return (self._commit is None or target == self._commit or # target was branch.target
self._repository.descendant_of(target, self._commit)) # target was branch.target