Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints for submodule functions in Repository #1247

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions pygit2/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from ._pygit2 import Reference, Tree, Commit, Blob, Signature
from ._pygit2 import InvalidSpecError

from .callbacks import git_checkout_options, git_fetch_options, git_stash_apply_options
from .callbacks import git_checkout_options, git_fetch_options, git_stash_apply_options, RemoteCallbacks
from .config import Config
from .errors import check_error
from .ffi import ffi, C
Expand Down Expand Up @@ -123,8 +123,20 @@ def pack_all_objects(pack_builder):
def __iter__(self):
return iter(self.odb)

def add_submodule(self, url, path, link=True, callbacks=None):
"""Add a submodule to the index.
#
# Submodules
#

def add_submodule(
self,
url: str,
path: str,
link: bool = True,
callbacks: typing.Optional[RemoteCallbacks] = None
) -> Submodule:
"""
Add a submodule to the index.
The submodule is automatically cloned.

Returns: the submodule that was added.

Expand All @@ -138,6 +150,9 @@ def add_submodule(self, url, path, link=True, callbacks=None):

link
Should workdir contain a gitlink to the repo in `.git/modules` vs. repo directly in workdir.

callbacks
Optional RemoteCallbacks to clone the submodule.
"""
csub = ffi.new('git_submodule **')
curl = ffi.new('char[]', to_bytes(url))
Expand Down Expand Up @@ -165,7 +180,7 @@ def add_submodule(self, url, path, link=True, callbacks=None):
check_error(err)
return submodule_instance

def lookup_submodule(self, path):
def lookup_submodule(self, path: str) -> Submodule:
"""
Look up submodule information by name or path.
"""
Expand All @@ -176,13 +191,32 @@ def lookup_submodule(self, path):
check_error(err)
return Submodule._from_c(self, csub[0])

def update_submodules(self, submodules=None, init=False, callbacks=None):
def update_submodules(
self,
submodules: typing.Optional[typing.Iterable[str]] = None,
init: bool = False,
callbacks: typing.Optional[RemoteCallbacks] = None):
"""
Update a submodule. This will clone a missing submodule and checkout
Update submodules. This will clone a missing submodule and checkout
the subrepository to the commit specified in the index of the
containing repository. If the submodule repository doesn't contain the
target commit (e.g. because fetchRecurseSubmodules isn't set), then the
submodule is fetched using the fetch options supplied in options.

Parameters:

submodules
Optional list of submodule paths or names (the submodule is ultimately
resolved with Repository.lookup_submodule()). If you omit this parameter
or pass None, all submodules will be updated.

init
If the submodule is not initialized, setting this flag to True will
initialize the submodule before updating. Otherwise, this will raise
an error if attempting to update an uninitialized repository.

callbacks
Optional RemoteCallbacks to clone or fetch the submodule.
"""
if submodules is None:
submodules = self.listall_submodules()
Expand Down
Loading