Skip to content

Commit 8ec7e32

Browse files
committedFeb 26, 2024
Revise docstrings within git.index
Along the same lines as e08066c and 1cd73ba.
1 parent cd8a312 commit 8ec7e32

File tree

4 files changed

+378
-280
lines changed

4 files changed

+378
-280
lines changed
 

‎git/index/base.py

Lines changed: 298 additions & 233 deletions
Large diffs are not rendered by default.

‎git/index/fun.py

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# This module is part of GitPython and is released under the
22
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
33

4-
"""Standalone functions to accompany the index implementation and make it more versatile."""
4+
"""Standalone functions to accompany the index implementation and make it more
5+
versatile."""
56

67
from io import BytesIO
78
import os
@@ -78,9 +79,15 @@ def _has_file_extension(path: str) -> str:
7879
def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
7980
"""Run the commit hook of the given name. Silently ignore hooks that do not exist.
8081
81-
:param name: name of hook, like 'pre-commit'
82-
:param index: IndexFile instance
83-
:param args: Arguments passed to hook file
82+
:param name:
83+
Name of hook, like ``pre-commit``.
84+
85+
:param index:
86+
:class:`~git.index.base.IndexFile` instance.
87+
88+
:param args:
89+
Arguments passed to hook file.
90+
8491
:raises HookExecutionError:
8592
"""
8693
hp = hook_path(name, index.repo.git_dir)
@@ -121,8 +128,8 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
121128

122129

123130
def stat_mode_to_index_mode(mode: int) -> int:
124-
"""Convert the given mode from a stat call to the corresponding index mode
125-
and return it."""
131+
"""Convert the given mode from a stat call to the corresponding index mode and
132+
return it."""
126133
if S_ISLNK(mode): # symlinks
127134
return S_IFLNK
128135
if S_ISDIR(mode) or S_IFMT(mode) == S_IFGITLINK: # submodules
@@ -138,16 +145,19 @@ def write_cache(
138145
) -> None:
139146
"""Write the cache represented by entries to a stream.
140147
141-
:param entries: **sorted** list of entries
148+
:param entries:
149+
**Sorted** list of entries.
142150
143-
:param stream: stream to wrap into the AdapterStreamCls - it is used for
144-
final output.
151+
:param stream:
152+
Stream to wrap into the AdapterStreamCls - it is used for final output.
145153
146-
:param ShaStreamCls: Type to use when writing to the stream. It produces a sha
147-
while writing to it, before the data is passed on to the wrapped stream
154+
:param ShaStreamCls:
155+
Type to use when writing to the stream. It produces a sha while writing to it,
156+
before the data is passed on to the wrapped stream.
148157
149-
:param extension_data: any kind of data to write as a trailer, it must begin
150-
a 4 byte identifier, followed by its size (4 bytes).
158+
:param extension_data:
159+
Any kind of data to write as a trailer, it must begin a 4 byte identifier,
160+
followed by its size (4 bytes).
151161
"""
152162
# Wrap the stream into a compatible writer.
153163
stream_sha = ShaStreamCls(stream)
@@ -197,7 +207,7 @@ def write_cache(
197207

198208

199209
def read_header(stream: IO[bytes]) -> Tuple[int, int]:
200-
"""Return tuple(version_long, num_entries) from the given stream"""
210+
"""Return tuple(version_long, num_entries) from the given stream."""
201211
type_id = stream.read(4)
202212
if type_id != b"DIRC":
203213
raise AssertionError("Invalid index file header: %r" % type_id)
@@ -210,9 +220,13 @@ def read_header(stream: IO[bytes]) -> Tuple[int, int]:
210220

211221

212222
def entry_key(*entry: Union[BaseIndexEntry, PathLike, int]) -> Tuple[PathLike, int]:
213-
""":return: Key suitable to be used for the index.entries dictionary
223+
"""
224+
:return:
225+
Key suitable to be used for the :attr:`index.entries
226+
<git.index.base.IndexFile.entries>` dictionary.
214227
215-
:param entry: One instance of type BaseIndexEntry or the path and the stage
228+
:param entry:
229+
One instance of type BaseIndexEntry or the path and the stage.
216230
"""
217231

218232
# def is_entry_key_tup(entry_key: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
@@ -234,12 +248,14 @@ def read_cache(
234248
) -> Tuple[int, Dict[Tuple[PathLike, int], "IndexEntry"], bytes, bytes]:
235249
"""Read a cache file from the given stream.
236250
237-
:return: tuple(version, entries_dict, extension_data, content_sha)
251+
:return:
252+
tuple(version, entries_dict, extension_data, content_sha)
238253
239-
* version is the integer version number.
240-
* entries dict is a dictionary which maps IndexEntry instances to a path at a stage.
241-
* extension_data is '' or 4 bytes of type + 4 bytes of size + size bytes.
242-
* content_sha is a 20 byte sha on all cache file contents.
254+
* *version* is the integer version number.
255+
* *entries_dict* is a dictionary which maps IndexEntry instances to a path at a
256+
stage.
257+
* *extension_data* is ``""`` or 4 bytes of type + 4 bytes of size + size bytes.
258+
* *content_sha* is a 20 byte sha on all cache file contents.
243259
"""
244260
version, num_entries = read_header(stream)
245261
count = 0
@@ -285,15 +301,25 @@ def read_cache(
285301
def write_tree_from_cache(
286302
entries: List[IndexEntry], odb: "GitCmdObjectDB", sl: slice, si: int = 0
287303
) -> Tuple[bytes, List["TreeCacheTup"]]:
288-
"""Create a tree from the given sorted list of entries and put the respective
304+
R"""Create a tree from the given sorted list of entries and put the respective
289305
trees into the given object database.
290306
291-
:param entries: **Sorted** list of IndexEntries
292-
:param odb: Object database to store the trees in
293-
:param si: Start index at which we should start creating subtrees
294-
:param sl: Slice indicating the range we should process on the entries list
295-
:return: tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of
296-
tree entries being a tuple of hexsha, mode, name
307+
:param entries:
308+
**Sorted** list of :class:`~git.index.typ.IndexEntry`\s.
309+
310+
:param odb:
311+
Object database to store the trees in.
312+
313+
:param si:
314+
Start index at which we should start creating subtrees.
315+
316+
:param sl:
317+
Slice indicating the range we should process on the entries list.
318+
319+
:return:
320+
tuple(binsha, list(tree_entry, ...))
321+
322+
A tuple of a sha and a list of tree entries being a tuple of hexsha, mode, name.
297323
"""
298324
tree_items: List["TreeCacheTup"] = []
299325

@@ -346,15 +372,17 @@ def _tree_entry_to_baseindexentry(tree_entry: "TreeCacheTup", stage: int) -> Bas
346372

347373

348374
def aggressive_tree_merge(odb: "GitCmdObjectDB", tree_shas: Sequence[bytes]) -> List[BaseIndexEntry]:
349-
"""
350-
:return: List of BaseIndexEntries representing the aggressive merge of the given
351-
trees. All valid entries are on stage 0, whereas the conflicting ones are left
352-
on stage 1, 2 or 3, whereas stage 1 corresponds to the common ancestor tree,
353-
2 to our tree and 3 to 'their' tree.
354-
355-
:param tree_shas: 1, 2 or 3 trees as identified by their binary 20 byte shas.
356-
If 1 or two, the entries will effectively correspond to the last given tree.
357-
If 3 are given, a 3 way merge is performed.
375+
R"""
376+
:return:
377+
List of :class:`~git.index.typ.BaseIndexEntry`\s representing the aggressive
378+
merge of the given trees. All valid entries are on stage 0, whereas the
379+
conflicting ones are left on stage 1, 2 or 3, whereas stage 1 corresponds to the
380+
common ancestor tree, 2 to our tree and 3 to 'their' tree.
381+
382+
:param tree_shas:
383+
1, 2 or 3 trees as identified by their binary 20 byte shas. If 1 or two, the
384+
entries will effectively correspond to the last given tree. If 3 are given, a 3
385+
way merge is performed.
358386
"""
359387
out: List[BaseIndexEntry] = []
360388

‎git/index/typ.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool:
5858
blob_path: Path = blob_pathlike if isinstance(blob_pathlike, Path) else Path(blob_pathlike)
5959
for pathlike in self.paths:
6060
path: Path = pathlike if isinstance(pathlike, Path) else Path(pathlike)
61-
# TODO: Change to use `PosixPath.is_relative_to` once Python 3.8 is no longer supported.
61+
# TODO: Change to use `PosixPath.is_relative_to` once Python 3.8 is no
62+
# longer supported.
6263
filter_parts: List[str] = path.parts
6364
blob_parts: List[str] = blob_path.parts
6465
if len(filter_parts) > len(blob_parts):
@@ -69,8 +70,11 @@ def __call__(self, stage_blob: Tuple[StageType, Blob]) -> bool:
6970

7071

7172
class BaseIndexEntryHelper(NamedTuple):
72-
"""Typed namedtuple to provide named attribute access for BaseIndexEntry.
73-
Needed to allow overriding __new__ in child class to preserve backwards compat."""
73+
"""Typed named tuple to provide named attribute access for BaseIndexEntry.
74+
75+
This is needed to allow overriding ``__new__`` in child class to preserve backwards
76+
compatibility.
77+
"""
7478

7579
mode: int
7680
binsha: bytes
@@ -101,7 +105,8 @@ def __new__(
101105
Tuple[int, bytes, int, PathLike, bytes, bytes, int, int, int, int, int],
102106
],
103107
) -> "BaseIndexEntry":
104-
"""Override __new__ to allow construction from a tuple for backwards compatibility"""
108+
"""Override ``__new__`` to allow construction from a tuple for backwards
109+
compatibility."""
105110
return super().__new__(cls, *inp_tuple)
106111

107112
def __str__(self) -> str:

‎git/index/util.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434

3535

3636
class TemporaryFileSwap:
37-
"""Utility class moving a file to a temporary location within the same directory
38-
and moving it back on to where on object deletion."""
37+
"""Utility class moving a file to a temporary location within the same directory and
38+
moving it back on to where on object deletion."""
3939

4040
__slots__ = ("file_path", "tmp_file_path")
4141

@@ -66,12 +66,12 @@ def __exit__(
6666

6767
def post_clear_cache(func: Callable[..., _T]) -> Callable[..., _T]:
6868
"""Decorator for functions that alter the index using the git command. This would
69-
invalidate our possibly existing entries dictionary which is why it must be
70-
deleted to allow it to be lazily reread later.
69+
invalidate our possibly existing entries dictionary which is why it must be deleted
70+
to allow it to be lazily reread later.
7171
7272
:note:
73-
This decorator will not be required once all functions are implemented
74-
natively which in fact is possible, but probably not feasible performance wise.
73+
This decorator will not be required once all functions are implemented natively
74+
which in fact is possible, but probably not feasible performance wise.
7575
"""
7676

7777
@wraps(func)

0 commit comments

Comments
 (0)
Please sign in to comment.