|
28 | 28 | from string import hexdigits
|
29 | 29 | import tarfile
|
30 | 30 | from time import time
|
| 31 | +import typing |
31 | 32 | import warnings
|
32 | 33 |
|
33 | 34 | # Import from pygit2
|
34 | 35 | from ._pygit2 import Repository as _Repository, init_file_backend
|
35 | 36 | from ._pygit2 import Oid, GIT_OID_HEXSZ, GIT_OID_MINPREFIXLEN
|
36 |
| -from ._pygit2 import GIT_CHECKOUT_SAFE, GIT_CHECKOUT_RECREATE_MISSING, GIT_DIFF_NORMAL |
| 37 | +from ._pygit2 import GIT_DIFF_NORMAL |
37 | 38 | from ._pygit2 import GIT_FILEMODE_LINK
|
38 | 39 | from ._pygit2 import GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE, GIT_BRANCH_ALL
|
39 | 40 | from ._pygit2 import GIT_REF_SYMBOLIC
|
| 41 | +from ._pygit2 import GIT_REFERENCES_ALL, GIT_REFERENCES_BRANCHES, GIT_REFERENCES_TAGS |
40 | 42 | from ._pygit2 import Reference, Tree, Commit, Blob, Signature
|
41 | 43 | from ._pygit2 import InvalidSpecError
|
42 | 44 |
|
|
47 | 49 | from .index import Index
|
48 | 50 | from .remote import RemoteCollection
|
49 | 51 | from .blame import Blame
|
50 |
| -from .utils import to_bytes, StrArray |
| 52 | +from .utils import to_bytes |
51 | 53 | from .submodule import Submodule
|
52 | 54 | from .packbuilder import PackBuilder
|
53 | 55 |
|
@@ -291,6 +293,16 @@ def create_reference(self, name, target, force=False, message=None):
|
291 | 293 | return self.create_reference_symbolic(name, target, force,
|
292 | 294 | message=message)
|
293 | 295 |
|
| 296 | + def listall_references(self) -> typing.List[str]: |
| 297 | + """Return a list with all the references in the repository. |
| 298 | + """ |
| 299 | + return list(x.name for x in self.references.iterator()) |
| 300 | + |
| 301 | + def listall_reference_objects(self) -> typing.List[Reference]: |
| 302 | + """Return a list with all the reference objects in the repository. |
| 303 | + """ |
| 304 | + return list(x for x in self.references.iterator()) |
| 305 | + |
294 | 306 | def resolve_refish(self, refish):
|
295 | 307 | """Convert a reference-like short name "ref-ish" to a valid
|
296 | 308 | (commit, reference) pair.
|
@@ -1624,8 +1636,42 @@ def get(self, key: str):
|
1624 | 1636 | return None
|
1625 | 1637 |
|
1626 | 1638 | def __iter__(self):
|
1627 |
| - for ref_name in self._repository.listall_references(): |
1628 |
| - yield ref_name |
| 1639 | + iter = self._repository.references_iterator_init() |
| 1640 | + while True: |
| 1641 | + ref = self._repository.references_iterator_next(iter) |
| 1642 | + if ref: |
| 1643 | + yield ref.name |
| 1644 | + else: |
| 1645 | + return |
| 1646 | + |
| 1647 | + def iterator(self, references_return_type:int = GIT_REFERENCES_ALL): |
| 1648 | + """ Creates a new iterator and fetches references for a given repository. |
| 1649 | +
|
| 1650 | + Can also filter and pass all refs or only branches or only tags. |
| 1651 | +
|
| 1652 | + Parameters: |
| 1653 | +
|
| 1654 | + references_return_type: int |
| 1655 | + Optional specifier to filter references. By default, all references are |
| 1656 | + returned. |
| 1657 | +
|
| 1658 | + The following values are accepted: |
| 1659 | + 0 -> GIT_REFERENCES_ALL, fetches all refs, this is the default |
| 1660 | + 1 -> GIT_REFERENCES_BRANCHES, fetches only branches |
| 1661 | + 2 -> GIT_REFERENCES_TAGS, fetches only tags |
| 1662 | +
|
| 1663 | + TODO: Add support for filtering by reference types notes and remotes. |
| 1664 | + """ |
| 1665 | + |
| 1666 | + if references_return_type not in (GIT_REFERENCES_ALL, GIT_REFERENCES_BRANCHES, GIT_REFERENCES_TAGS): |
| 1667 | + raise ValueError("Parameter references_return_type is invalid") |
| 1668 | + iter = self._repository.references_iterator_init() |
| 1669 | + while True: |
| 1670 | + ref = self._repository.references_iterator_next(iter, references_return_type) |
| 1671 | + if ref: |
| 1672 | + yield ref |
| 1673 | + else: |
| 1674 | + return |
1629 | 1675 |
|
1630 | 1676 | def create(self, name, target, force=False):
|
1631 | 1677 | return self._repository.create_reference(name, target, force)
|
|
0 commit comments