Skip to content

Commit 719dd66

Browse files
committed
Move SubmoduleCollection to submodule.py
1 parent 236a040 commit 719dd66

File tree

2 files changed

+205
-198
lines changed

2 files changed

+205
-198
lines changed

pygit2/repository.py

+3-197
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@
4343
from ._pygit2 import Reference, Tree, Commit, Blob, Signature
4444
from ._pygit2 import InvalidSpecError
4545

46-
from .callbacks import git_checkout_options, git_fetch_options, git_stash_apply_options, RemoteCallbacks
46+
from .callbacks import git_checkout_options, git_stash_apply_options
4747
from .config import Config
4848
from .errors import check_error
49-
from .enums import SubmoduleIgnore, SubmoduleStatus
5049
from .ffi import ffi, C
5150
from .index import Index
5251
from .remote import RemoteCollection
5352
from .blame import Blame
5453
from .utils import to_bytes, StrArray
55-
from .submodule import Submodule
54+
from .submodule import Submodule, SubmoduleCollection
5655
from .packbuilder import PackBuilder
5756

5857

@@ -65,7 +64,7 @@ def _common_init(self):
6564
self.branches = Branches(self)
6665
self.references = References(self)
6766
self.remotes = RemoteCollection(self)
68-
self.submodules = Submodules(self)
67+
self.submodules = SubmoduleCollection(self)
6968

7069
# Get the pointer as the contents of a buffer and store it for
7170
# later access
@@ -1682,199 +1681,6 @@ def compress(self):
16821681
return self._repository.compress_references()
16831682

16841683

1685-
class Submodules:
1686-
""" Collection of submodules in a repository. """
1687-
1688-
def __init__(self, repository: BaseRepository):
1689-
self._repository = repository
1690-
assert isinstance(self._repository, BaseRepository)
1691-
1692-
def __getitem__(self, name: str) -> Submodule:
1693-
"""
1694-
Look up submodule information by name or path.
1695-
Raises KeyError if there is no such submodule.
1696-
"""
1697-
csub = ffi.new('git_submodule **')
1698-
cpath = ffi.new('char[]', to_bytes(name))
1699-
1700-
err = C.git_submodule_lookup(csub, self._repository._repo, cpath)
1701-
check_error(err)
1702-
return Submodule._from_c(self._repository, csub[0])
1703-
1704-
def __contains__(self, name: str) -> bool:
1705-
return self.get(name) is not None
1706-
1707-
def __iter__(self) -> typing.Iterator[Submodule]:
1708-
for s in self._repository.listall_submodules():
1709-
yield self[s]
1710-
1711-
def get(self, name: str) -> typing.Union[Submodule, None]:
1712-
"""
1713-
Look up submodule information by name or path.
1714-
Unlike __getitem__, this returns None if the submodule is not found.
1715-
"""
1716-
try:
1717-
return self[name]
1718-
except KeyError:
1719-
return None
1720-
1721-
def add(
1722-
self,
1723-
url: str,
1724-
path: str,
1725-
link: bool = True,
1726-
callbacks: typing.Optional[RemoteCallbacks] = None
1727-
) -> Submodule:
1728-
"""
1729-
Add a submodule to the index.
1730-
The submodule is automatically cloned.
1731-
1732-
Returns: the submodule that was added.
1733-
1734-
Parameters:
1735-
1736-
url
1737-
The URL of the submodule.
1738-
1739-
path
1740-
The path within the parent repository to add the submodule
1741-
1742-
link
1743-
Should workdir contain a gitlink to the repo in `.git/modules` vs. repo directly in workdir.
1744-
1745-
callbacks
1746-
Optional RemoteCallbacks to clone the submodule.
1747-
"""
1748-
csub = ffi.new('git_submodule **')
1749-
curl = ffi.new('char[]', to_bytes(url))
1750-
cpath = ffi.new('char[]', to_bytes(path))
1751-
gitlink = 1 if link else 0
1752-
1753-
err = C.git_submodule_add_setup(csub, self._repository._repo, curl, cpath, gitlink)
1754-
check_error(err)
1755-
1756-
submodule_instance = Submodule._from_c(self._repository, csub[0])
1757-
1758-
# prepare options
1759-
opts = ffi.new('git_submodule_update_options *')
1760-
C.git_submodule_update_options_init(opts, C.GIT_SUBMODULE_UPDATE_OPTIONS_VERSION)
1761-
1762-
with git_fetch_options(callbacks, opts=opts.fetch_opts) as payload:
1763-
crepo = ffi.new('git_repository **')
1764-
err = C.git_submodule_clone(crepo, submodule_instance._subm, opts)
1765-
payload.check_error(err)
1766-
1767-
# clean-up the submodule repository
1768-
Repository._from_c(crepo[0], True)
1769-
1770-
err = C.git_submodule_add_finalize(submodule_instance._subm)
1771-
check_error(err)
1772-
return submodule_instance
1773-
1774-
def init(
1775-
self,
1776-
submodules: typing.Optional[typing.Iterable[str]] = None,
1777-
overwrite: bool = False):
1778-
"""
1779-
Initialize submodules in the repository. Just like "git submodule init",
1780-
this copies information about the submodules into ".git/config".
1781-
1782-
Parameters:
1783-
1784-
submodules
1785-
Optional list of submodule paths or names to initialize.
1786-
Default argument initializes all submodules.
1787-
1788-
overwrite
1789-
Flag indicating if initialization should overwrite submodule entries.
1790-
"""
1791-
if submodules is None:
1792-
submodules = self._repository.listall_submodules()
1793-
1794-
instances = [self[s] for s in submodules]
1795-
1796-
for submodule in instances:
1797-
submodule.init(overwrite)
1798-
1799-
def update(
1800-
self,
1801-
submodules: typing.Optional[typing.Iterable[str]] = None,
1802-
init: bool = False,
1803-
callbacks: typing.Optional[RemoteCallbacks] = None):
1804-
"""
1805-
Update submodules. This will clone a missing submodule and checkout
1806-
the subrepository to the commit specified in the index of the
1807-
containing repository. If the submodule repository doesn't contain the
1808-
target commit (e.g. because fetchRecurseSubmodules isn't set), then the
1809-
submodule is fetched using the fetch options supplied in options.
1810-
1811-
Parameters:
1812-
1813-
submodules
1814-
Optional list of submodule paths or names. If you omit this parameter
1815-
or pass None, all submodules will be updated.
1816-
1817-
init
1818-
If the submodule is not initialized, setting this flag to True will
1819-
initialize the submodule before updating. Otherwise, this will raise
1820-
an error if attempting to update an uninitialized repository.
1821-
1822-
callbacks
1823-
Optional RemoteCallbacks to clone or fetch the submodule.
1824-
"""
1825-
if submodules is None:
1826-
submodules = self._repository.listall_submodules()
1827-
1828-
instances = [self[s] for s in submodules]
1829-
1830-
for submodule in instances:
1831-
submodule.update(init, callbacks)
1832-
1833-
def status(self, name: str, ignore: SubmoduleIgnore = SubmoduleIgnore.UNSPECIFIED) -> SubmoduleStatus:
1834-
"""
1835-
Get the status of a submodule.
1836-
1837-
Returns: A combination of SubmoduleStatus flags.
1838-
1839-
Parameters:
1840-
1841-
name
1842-
Submodule name or path.
1843-
1844-
ignore
1845-
A SubmoduleIgnore value indicating how deeply to examine the working directory.
1846-
"""
1847-
cstatus = ffi.new('unsigned int *')
1848-
err = C.git_submodule_status(cstatus, self._repository._repo, to_bytes(name), ignore)
1849-
check_error(err)
1850-
return SubmoduleStatus(cstatus[0])
1851-
1852-
def cache_all(self):
1853-
"""
1854-
Load and cache all submodules in the repository.
1855-
1856-
Because the `.gitmodules` file is unstructured, loading submodules is an
1857-
O(N) operation. Any operation that requires accessing all submodules is O(N^2)
1858-
in the number of submodules, if it has to look each one up individually.
1859-
This function loads all submodules and caches them so that subsequent
1860-
submodule lookups by name are O(1).
1861-
"""
1862-
err = C.git_repository_submodule_cache_all(self._repository._repo)
1863-
check_error(err)
1864-
1865-
def cache_clear(self):
1866-
"""
1867-
Clear the submodule cache populated by `submodule_cache_all`.
1868-
If there is no cache, do nothing.
1869-
1870-
The cache incorporates data from the repository's configuration, as well
1871-
as the state of the working tree, the index, and HEAD. So any time any
1872-
of these has changed, the cache might become invalid.
1873-
"""
1874-
err = C.git_repository_submodule_cache_clear(self._repository._repo)
1875-
check_error(err)
1876-
1877-
18781684
class Repository(BaseRepository):
18791685
def __init__(self, path=None, flags=0):
18801686
"""

0 commit comments

Comments
 (0)