Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions pygit2/_libgit2/ffi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ NULL: NULL_TYPE = ...
char = NewType('char', object)
char_pointer = NewType('char_pointer', object)

class size_t:
def __getitem__(self, item: Literal[0]) -> int: ...

class _Pointer(Generic[T]):
def __setitem__(self, item: Literal[0], a: T) -> None: ...
@overload
def __getitem__(self, item: Literal[0]) -> T: ...
@overload
def __getitem__(self, item: slice[None, None, None]) -> bytes: ...

class _MultiPointer(Generic[T]):
def __getitem__(self, item: int) -> T: ...

class ArrayC(Generic[T]):
# incomplete!
# def _len(self, ?) -> ?: ...
Expand Down Expand Up @@ -83,6 +89,13 @@ class GitSubmoduleC:
class GitSubmoduleUpdateOptionsC:
fetch_opts: GitFetchOptionsC

class GitRemoteHeadC:
local: int
oid: GitOidC
loid: GitOidC
name: char_pointer
symref_target: char_pointer

class UnsignedIntC:
def __getitem__(self, item: Literal[0]) -> int: ...

Expand Down Expand Up @@ -284,6 +297,12 @@ def new(a: Literal['git_signature *']) -> GitSignatureC: ...
@overload
def new(a: Literal['git_signature **']) -> _Pointer[GitSignatureC]: ...
@overload
def new(
a: Literal['git_remote_head ***'],
) -> _Pointer[_MultiPointer[GitRemoteHeadC]]: ...
@overload
def new(a: Literal['size_t *']) -> size_t: ...
@overload
def new(a: Literal['git_stash_save_options *']) -> GitStashSaveOptionsC: ...
@overload
def new(a: Literal['git_strarray *']) -> GitStrrayC: ...
Expand Down
78 changes: 69 additions & 9 deletions pygit2/_pygit2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ from typing import (
overload,
)

from . import Index
from . import Index, IndexEntry
from ._libgit2.ffi import (
GitCommitC,
GitMergeOptionsC,
Expand All @@ -28,7 +28,7 @@ from ._libgit2.ffi import (
_Pointer,
)
from .blame import Blame
from .callbacks import CheckoutCallbacks
from .callbacks import CheckoutCallbacks, StashApplyCallbacks
from .config import Config
from .enums import (
ApplyLocation,
Expand Down Expand Up @@ -59,6 +59,7 @@ from .enums import (
SortMode,
)
from .filter import Filter
from .index import MergeFileResult
from .packbuilder import PackBuilder
from .remotes import Remote
from .repository import BaseRepository
Expand Down Expand Up @@ -643,7 +644,9 @@ class Refdb:
class RefdbBackend:
def __init__(self, *args, **kwargs) -> None: ...
def compress(self) -> None: ...
def delete(self, ref_name: str, old_id: _OidArg, old_target: str) -> None: ...
def delete(
self, ref_name: str, old_id: _OidArg, old_target: str | None
) -> None: ...
def ensure_log(self, ref_name: str) -> bool: ...
def exists(self, refname: str) -> bool: ...
def has_log(self, ref_name: str) -> bool: ...
Expand All @@ -657,9 +660,10 @@ class RefdbBackend:
force: bool,
who: Signature,
message: str,
old: _OidArg,
old_target: str,
old: None | _OidArg,
old_target: None | str,
) -> None: ...
def __iter__(self) -> Iterator[Reference]: ...

class RefdbFsBackend(RefdbBackend):
def __init__(self, *args, **kwargs) -> None: ...
Expand Down Expand Up @@ -738,6 +742,7 @@ class Branches:
class Repository:
_pointer: GitRepositoryC
_repo: GitRepositoryC
backend: RefdbBackend
default_signature: Signature
head: Reference
head_is_detached: bool
Expand All @@ -761,7 +766,9 @@ class Repository:
def _from_c(cls, ptr: 'GitRepositoryC', owned: bool) -> 'Repository': ...
def __iter__(self) -> Iterator[Oid]: ...
def __getitem__(self, key: str | Oid) -> Object: ...
def add_worktree(self, name: str, path: str, ref: Reference = ...) -> Worktree: ...
def add_worktree(
self, name: str, path: str | Path, ref: Reference = ...
) -> Worktree: ...
def amend_commit(
self,
commit: Commit | Oid | str,
Expand Down Expand Up @@ -793,13 +800,14 @@ class Repository:
) -> Blame: ...
def checkout(
self,
refname: Optional[_OidArg],
refname: _OidArg | None | Reference = None,
*,
strategy: CheckoutStrategy | None = None,
directory: str | None = None,
directory: str | Path | None = None,
paths: list[str] | None = None,
callbacks: CheckoutCallbacks | None = None,
) -> None: ...
def ahead_behind(self, local: _OidArg, upstream: _OidArg) -> tuple[int, int]: ...
def cherrypick(self, id: _OidArg) -> None: ...
def compress_references(self) -> None: ...
@property
Expand Down Expand Up @@ -841,7 +849,11 @@ class Repository:
force: bool = False,
) -> Oid: ...
def create_reference(
self, name: str, target: _OidArg, force: bool = False
self,
name: str,
target: _OidArg,
force: bool = False,
message: str | None = None,
) -> Reference: ...
def create_reference_direct(
self, name: str, target: _OidArg, force: bool, message: Optional[str] = None
Expand Down Expand Up @@ -885,10 +897,17 @@ class Repository:
commit: _OidArg | None = None,
) -> bool | None | str: ...
def git_object_lookup_prefix(self, oid: _OidArg) -> Object: ...
def hashfile(
self,
path: str,
object_type: ObjectType = ObjectType.BLOB,
as_path: str | None = None,
) -> Oid: ...
def list_worktrees(self) -> list[str]: ...
def listall_branches(self, flag: BranchType = BranchType.LOCAL) -> list[str]: ...
def listall_mergeheads(self) -> list[Oid]: ...
def listall_references(self) -> list[str]: ...
def listall_reference_objects(self) -> list[Reference]: ...
def listall_stashes(self) -> list[Stash]: ...
def listall_submodules(self) -> list[str]: ...
def lookup_branch(
Expand Down Expand Up @@ -921,6 +940,13 @@ class Repository:
flags: MergeFlag = MergeFlag.FIND_RENAMES,
file_flags: MergeFileFlag = MergeFileFlag.DEFAULT,
) -> Index: ...
def merge_file_from_index(
self,
ancestor: IndexEntry | None,
ours: IndexEntry | None,
theirs: IndexEntry | None,
use_deprecated: bool = True,
) -> str | MergeFileResult | None: ...
@staticmethod
def _merge_options(
favor: int | MergeFavor, flags: int | MergeFlag, file_flags: int | MergeFileFlag
Expand Down Expand Up @@ -958,15 +984,49 @@ class Repository:
references_return_type: ReferenceFilter = ReferenceFilter.ALL,
) -> Reference: ...
def reset(self, oid: _OidArg, reset_type: ResetMode) -> None: ...
def resolve_refish(self, refresh: str) -> tuple[Commit, Reference]: ...
def revparse(self, revspec: str) -> RevSpec: ...
def revparse_ext(self, revision: str) -> tuple[Object, Reference]: ...
def revparse_single(self, revision: str) -> Object: ...
def revert(self, commit: Commit) -> None: ...
def revert_commit(
self, revert_commit: Commit, our_commit: Commit, mainline: int = 0
) -> Index: ...
def set_ident(self, name: str, email: str) -> None: ...
def set_odb(self, odb: Odb) -> None: ...
def set_refdb(self, refdb: Refdb) -> None: ...
def status(
self, untracked_files: str = 'all', ignored: bool = False
) -> dict[str, int]: ...
def stash(
self,
stasher: Signature,
message: str | None = None,
keep_index: bool = False,
include_untracked: bool = False,
include_ignored: bool = False,
keep_all: bool = False,
paths: list[str] | None = None,
) -> None: ...
def stash_apply(
self,
index: int = 0,
reinstate_index: bool | None = None,
include_untracked: bool | None = None,
message: str | None = None,
strategy: CheckoutStrategy | None = None,
callbacks: StashApplyCallbacks | None = None,
) -> None: ...
def stash_pop(
self,
index: int = 0,
reinstate_index: bool | None = None,
include_untracked: bool | None = None,
message: str | None = None,
strategy: CheckoutStrategy | None = None,
callbacks: StashApplyCallbacks | None = None,
) -> None: ...
def stash_drop(self, index: int = 0) -> None: ...
def status_file(self, path: str) -> int: ...
def state(self) -> RepositoryState: ...
def state_cleanup(self) -> None: ...
Expand Down
4 changes: 2 additions & 2 deletions pygit2/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __contains__(self, path) -> bool:
check_error(err)
return True

def __getitem__(self, key):
def __getitem__(self, key: str | int | PathLike[str]) -> 'IndexEntry':
centry = ffi.NULL
if isinstance(key, str) or hasattr(key, '__fspath__'):
centry = C.git_index_get_bypath(self._index, to_bytes(key), 0)
Expand Down Expand Up @@ -389,7 +389,7 @@ class MergeFileResult:
automergeable: bool
'True if the output was automerged, false if the output contains conflict markers'

path: typing.Union[str, None]
path: typing.Union[str, None, PathLike[str]]
'The path that the resultant merge file should use, or None if a filename conflict would occur'

mode: FileMode
Expand Down
48 changes: 35 additions & 13 deletions pygit2/remotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from . import utils
from typing import TYPE_CHECKING, Any, TypedDict

# Import from pygit2
from pygit2 import RemoteCallbacks

from . import utils
from ._pygit2 import Oid
from .callbacks import (
git_fetch_options,
Expand All @@ -48,6 +49,14 @@
from .repository import BaseRepository


class LsRemotesDict(TypedDict):
local: bool
loid: None | Oid
name: str | None
symref_target: str | None
oid: Oid


class TransferProgress:
"""Progress downloading and indexing data during a fetch."""

Expand Down Expand Up @@ -180,7 +189,9 @@ def fetch(

return TransferProgress(C.git_remote_stats(self._remote))

def ls_remotes(self, callbacks=None, proxy=None):
def ls_remotes(
self, callbacks: RemoteCallbacks | None = None, proxy: str | None | bool = None
) -> list[LsRemotesDict]:
"""
Return a list of dicts that maps to `git_remote_head` from a
`ls_remotes` call.
Expand Down Expand Up @@ -209,19 +220,21 @@ def ls_remotes(self, callbacks=None, proxy=None):
else:
loid = None

remote = {
'local': local,
'loid': loid,
'name': maybe_string(ref.name),
'symref_target': maybe_string(ref.symref_target),
'oid': Oid(raw=bytes(ffi.buffer(ref.oid.id)[:])),
}
remote = LsRemotesDict(
{
'local': local,
'loid': loid,
'name': maybe_string(ref.name),
'symref_target': maybe_string(ref.symref_target),
'oid': Oid(raw=bytes(ffi.buffer(ref.oid.id)[:])),
}
)

results.append(remote)

return results

def prune(self, callbacks=None):
def prune(self, callbacks: RemoteCallbacks | None = None) -> None:
"""Perform a prune against this remote."""
with git_remote_callbacks(callbacks) as payload:
err = C.git_remote_prune(self._remote, payload.remote_callbacks)
Expand Down Expand Up @@ -256,7 +269,14 @@ def push_refspecs(self):
check_error(err)
return strarray_to_strings(specs)

def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1):
def push(
self,
specs: list[str],
callbacks: RemoteCallbacks | None = None,
proxy: None | bool | str = None,
push_options: None | list[str] = None,
threads: int = 1,
) -> None:
"""
Push the given refspec to the remote. Raises ``GitError`` on protocol
error or unpack failure.
Expand All @@ -272,6 +292,8 @@ def push(self, specs, callbacks=None, proxy=None, push_options=None, threads=1):
specs : [str]
Push refspecs to use.

callbacks :

proxy : None or True or str
Proxy configuration. Can be one of:

Expand Down
4 changes: 2 additions & 2 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def test_read(testrepo: Repository) -> None:
assert len(index) == 2

with pytest.raises(TypeError):
index[()]
utils.assertRaisesWithArg(ValueError, -4, lambda: index[-4])
index[()] # type: ignore
utils.assertRaisesWithArg(ValueError, -4, lambda: index[-4]) # type: ignore
utils.assertRaisesWithArg(KeyError, 'abc', lambda: index['abc'])

sha = 'a520c24d85fbfc815d385957eed41406ca5a860b'
Expand Down
Loading
Loading