1
1
# This module is part of GitPython and is released under the
2
2
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
3
3
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."""
5
6
6
7
from io import BytesIO
7
8
import os
@@ -78,9 +79,15 @@ def _has_file_extension(path: str) -> str:
78
79
def run_commit_hook (name : str , index : "IndexFile" , * args : str ) -> None :
79
80
"""Run the commit hook of the given name. Silently ignore hooks that do not exist.
80
81
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
+
84
91
:raises HookExecutionError:
85
92
"""
86
93
hp = hook_path (name , index .repo .git_dir )
@@ -121,8 +128,8 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
121
128
122
129
123
130
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."""
126
133
if S_ISLNK (mode ): # symlinks
127
134
return S_IFLNK
128
135
if S_ISDIR (mode ) or S_IFMT (mode ) == S_IFGITLINK : # submodules
@@ -138,16 +145,19 @@ def write_cache(
138
145
) -> None :
139
146
"""Write the cache represented by entries to a stream.
140
147
141
- :param entries: **sorted** list of entries
148
+ :param entries:
149
+ **Sorted** list of entries.
142
150
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.
145
153
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.
148
157
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).
151
161
"""
152
162
# Wrap the stream into a compatible writer.
153
163
stream_sha = ShaStreamCls (stream )
@@ -197,7 +207,7 @@ def write_cache(
197
207
198
208
199
209
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. """
201
211
type_id = stream .read (4 )
202
212
if type_id != b"DIRC" :
203
213
raise AssertionError ("Invalid index file header: %r" % type_id )
@@ -210,9 +220,13 @@ def read_header(stream: IO[bytes]) -> Tuple[int, int]:
210
220
211
221
212
222
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.
214
227
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.
216
230
"""
217
231
218
232
# def is_entry_key_tup(entry_key: Tuple) -> TypeGuard[Tuple[PathLike, int]]:
@@ -234,12 +248,14 @@ def read_cache(
234
248
) -> Tuple [int , Dict [Tuple [PathLike , int ], "IndexEntry" ], bytes , bytes ]:
235
249
"""Read a cache file from the given stream.
236
250
237
- :return: tuple(version, entries_dict, extension_data, content_sha)
251
+ :return:
252
+ tuple(version, entries_dict, extension_data, content_sha)
238
253
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.
243
259
"""
244
260
version , num_entries = read_header (stream )
245
261
count = 0
@@ -285,15 +301,25 @@ def read_cache(
285
301
def write_tree_from_cache (
286
302
entries : List [IndexEntry ], odb : "GitCmdObjectDB" , sl : slice , si : int = 0
287
303
) -> 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
289
305
trees into the given object database.
290
306
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.
297
323
"""
298
324
tree_items : List ["TreeCacheTup" ] = []
299
325
@@ -346,15 +372,17 @@ def _tree_entry_to_baseindexentry(tree_entry: "TreeCacheTup", stage: int) -> Bas
346
372
347
373
348
374
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.
358
386
"""
359
387
out : List [BaseIndexEntry ] = []
360
388
0 commit comments