Skip to content

Commit

Permalink
Merge pull request #1279 from Yobmod/main
Browse files Browse the repository at this point in the history
Finish typing object, improve verious other types.
  • Loading branch information
Byron authored Jun 26, 2021
2 parents 703280b + 5d7b8ba commit 2d2ff03
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 148 deletions.
2 changes: 1 addition & 1 deletion git/index/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def write_tree(self) -> Tree:
# note: additional deserialization could be saved if write_tree_from_cache
# would return sorted tree entries
root_tree = Tree(self.repo, binsha, path='')
root_tree._cache = tree_items
root_tree._cache = tree_items # type: ignore
return root_tree

def _process_diff_args(self, args: List[Union[str, diff.Diffable, object]]
Expand Down
14 changes: 10 additions & 4 deletions git/index/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

from typing import (Dict, IO, List, Sequence, TYPE_CHECKING, Tuple, Type, Union, cast)

from git.types import PathLike
from git.types import PathLike, TypeGuard

if TYPE_CHECKING:
from .base import IndexFile
Expand Down Expand Up @@ -185,11 +185,17 @@ def read_header(stream: IO[bytes]) -> Tuple[int, int]:
def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, int]:
""":return: Key suitable to be used for the index.entries dictionary
:param entry: One instance of type BaseIndexEntry or the path and the stage"""

def is_entry_tuple(entry: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
return isinstance(entry, tuple) and len(entry) == 2

if len(entry) == 1:
entry_first = cast(BaseIndexEntry, entry[0]) # type: BaseIndexEntry
entry_first = entry[0]
assert isinstance(entry_first, BaseIndexEntry)
return (entry_first.path, entry_first.stage)
else:
entry = cast(Tuple[PathLike, int], tuple(entry))
# entry = tuple(entry)
assert is_entry_tuple(entry)
return entry
# END handle entry

Expand Down Expand Up @@ -293,7 +299,7 @@ def write_tree_from_cache(entries: List[IndexEntry], odb, sl: slice, si: int = 0
# finally create the tree
sio = BytesIO()
tree_to_stream(tree_items, sio.write) # converts bytes of each item[0] to str
tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items) # type: List[Tuple[str, int, str]]
tree_items_stringified = cast(List[Tuple[str, int, str]], tree_items)
sio.seek(0)

istream = odb.store(IStream(str_tree_type, len(sio.getvalue()), sio))
Expand Down
4 changes: 2 additions & 2 deletions git/objects/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from git.util import (
hex_to_bin,
Actor,
Iterable,
IterableObj,
Stats,
finalize_process
)
Expand Down Expand Up @@ -47,7 +47,7 @@
__all__ = ('Commit', )


class Commit(base.Object, Iterable, Diffable, Traversable, Serializable):
class Commit(base.Object, IterableObj, Diffable, Traversable, Serializable):

"""Wraps a git Commit object.
Expand Down
15 changes: 12 additions & 3 deletions git/objects/fun.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
"""Module with functions which are supposed to be as fast as possible"""
from stat import S_ISDIR

from git.compat import (
safe_decode,
defenc
)

# typing ----------------------------------------------

from typing import List, Tuple


# ---------------------------------------------------


__all__ = ('tree_to_stream', 'tree_entries_from_data', 'traverse_trees_recursive',
'traverse_tree_recursive')

Expand Down Expand Up @@ -38,7 +47,7 @@ def tree_to_stream(entries, write):
# END for each item


def tree_entries_from_data(data):
def tree_entries_from_data(data: bytes) -> List[Tuple[bytes, int, str]]:
"""Reads the binary representation of a tree and returns tuples of Tree items
:param data: data block with tree data (as bytes)
:return: list(tuple(binsha, mode, tree_relative_path), ...)"""
Expand Down Expand Up @@ -72,8 +81,8 @@ def tree_entries_from_data(data):

# default encoding for strings in git is utf8
# Only use the respective unicode object if the byte stream was encoded
name = data[ns:i]
name = safe_decode(name)
name_bytes = data[ns:i]
name = safe_decode(name_bytes)

# byte is NULL, get next 20
i += 1
Expand Down
21 changes: 13 additions & 8 deletions git/objects/submodule/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import os
import stat
from typing import List
from unittest import SkipTest
import uuid

Expand All @@ -27,12 +26,13 @@
from git.objects.base import IndexObject, Object
from git.objects.util import Traversable
from git.util import (
Iterable,
IterableObj,
join_path_native,
to_native_path_linux,
RemoteProgress,
rmtree,
unbare_repo
unbare_repo,
IterableList
)
from git.util import HIDE_WINDOWS_KNOWN_ERRORS

Expand All @@ -47,6 +47,11 @@
)


# typing ----------------------------------------------------------------------


# -----------------------------------------------------------------------------

__all__ = ["Submodule", "UpdateProgress"]


Expand Down Expand Up @@ -74,7 +79,7 @@ class UpdateProgress(RemoteProgress):
# IndexObject comes via util module, its a 'hacky' fix thanks to pythons import
# mechanism which cause plenty of trouble of the only reason for packages and
# modules is refactoring - subpackages shouldn't depend on parent packages
class Submodule(IndexObject, Iterable, Traversable):
class Submodule(IndexObject, IterableObj, Traversable):

"""Implements access to a git submodule. They are special in that their sha
represents a commit in the submodule's repository which is to be checked out
Expand Down Expand Up @@ -136,12 +141,12 @@ def _set_cache_(self, attr):
# END handle attribute name

@classmethod
def _get_intermediate_items(cls, item: 'Submodule') -> List['Submodule']: # type: ignore
def _get_intermediate_items(cls, item: 'Submodule') -> IterableList['Submodule']:
""":return: all the submodules of our module repository"""
try:
return cls.list_items(item.module())
except InvalidGitRepositoryError:
return []
return IterableList('')
# END handle intermediate items

@classmethod
Expand Down Expand Up @@ -1153,7 +1158,7 @@ def name(self):
"""
return self._name

def config_reader(self):
def config_reader(self) -> SectionConstraint:
"""
:return: ConfigReader instance which allows you to qurey the configuration values
of this submodule, as provided by the .gitmodules file
Expand All @@ -1163,7 +1168,7 @@ def config_reader(self):
:raise IOError: If the .gitmodules file/blob could not be read"""
return self._config_parser_constrained(read_only=True)

def children(self):
def children(self) -> IterableList['Submodule']:
"""
:return: IterableList(Submodule, ...) an iterable list of submodules instances
which are children of this submodule or 0 if the submodule is not checked out"""
Expand Down
9 changes: 7 additions & 2 deletions git/objects/submodule/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
from io import BytesIO
import weakref

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .base import Submodule

__all__ = ('sm_section', 'sm_name', 'mkhead', 'find_first_remote_branch',
'SubmoduleConfigParser')

Expand Down Expand Up @@ -60,12 +65,12 @@ def __init__(self, *args, **kwargs):
super(SubmoduleConfigParser, self).__init__(*args, **kwargs)

#{ Interface
def set_submodule(self, submodule):
def set_submodule(self, submodule: 'Submodule') -> None:
"""Set this instance's submodule. It must be called before
the first write operation begins"""
self._smref = weakref.ref(submodule)

def flush_to_index(self):
def flush_to_index(self) -> None:
"""Flush changes in our configuration file to the index"""
assert self._smref is not None
# should always have a file here
Expand Down
Loading

0 comments on commit 2d2ff03

Please sign in to comment.