Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b2b6f7c

Browse files
committedFeb 28, 2024
Revise docstrings within git.repo
1 parent 0c8ca1a commit b2b6f7c

File tree

3 files changed

+366
-235
lines changed

3 files changed

+366
-235
lines changed
 

‎git/repo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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-
"""Initialize the Repo package."""
4+
"""Initialize the repo package."""
55

66
from .base import Repo as Repo # noqa: F401

‎git/repo/base.py

Lines changed: 296 additions & 183 deletions
Large diffs are not rendered by default.

‎git/repo/fun.py

Lines changed: 69 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def touch(filename: str) -> str:
5757

5858

5959
def is_git_dir(d: "PathLike") -> bool:
60-
"""This is taken from the git setup.c:is_git_directory
61-
function.
60+
"""This is taken from the git setup.c:is_git_directory function.
6261
63-
@throws WorkTreeRepositoryUnsupported if it sees a worktree directory. It's quite hacky to do that here,
64-
but at least clearly indicates that we don't support it.
65-
There is the unlikely danger to throw if we see directories which just look like a worktree dir,
66-
but are none."""
62+
:raises WorkTreeRepositoryUnsupported:
63+
If it sees a worktree directory. It's quite hacky to do that here, but at least
64+
clearly indicates that we don't support it. There is the unlikely danger to
65+
throw if we see directories which just look like a worktree dir, but are none.
66+
"""
6767
if osp.isdir(d):
6868
if (osp.isdir(osp.join(d, "objects")) or "GIT_OBJECT_DIRECTORY" in os.environ) and osp.isdir(
6969
osp.join(d, "refs")
@@ -107,15 +107,15 @@ def find_submodule_git_dir(d: "PathLike") -> Optional["PathLike"]:
107107
with open(d) as fp:
108108
content = fp.read().rstrip()
109109
except IOError:
110-
# it's probably not a file
110+
# It's probably not a file.
111111
pass
112112
else:
113113
if content.startswith("gitdir: "):
114114
path = content[8:]
115115

116116
if Git.is_cygwin():
117-
## Cygwin creates submodules prefixed with `/cygdrive/...` suffixes.
118-
# Cygwin git understands Cygwin paths much better than Windows ones
117+
# Cygwin creates submodules prefixed with `/cygdrive/...` suffixes.
118+
# Cygwin git understands Cygwin paths much better than Windows ones.
119119
# Also the Cygwin tests are assuming Cygwin paths.
120120
path = cygpath(path)
121121
if not osp.isabs(path):
@@ -126,9 +126,14 @@ def find_submodule_git_dir(d: "PathLike") -> Optional["PathLike"]:
126126

127127

128128
def short_to_long(odb: "GitCmdObjectDB", hexsha: str) -> Optional[bytes]:
129-
""":return: long hexadecimal sha1 from the given less-than-40 byte hexsha
130-
or None if no candidate could be found.
131-
:param hexsha: hexsha with less than 40 byte"""
129+
"""
130+
:return:
131+
Long hexadecimal sha1 from the given less than 40 byte hexsha or None if no
132+
candidate could be found.
133+
134+
:param hexsha:
135+
hexsha with less than 40 bytes.
136+
"""
132137
try:
133138
return bin_to_hex(odb.partial_to_complete_sha_hex(hexsha))
134139
except BadObject:
@@ -140,25 +145,29 @@ def name_to_object(
140145
repo: "Repo", name: str, return_ref: bool = False
141146
) -> Union[SymbolicReference, "Commit", "TagObject", "Blob", "Tree"]:
142147
"""
143-
:return: object specified by the given name, hexshas ( short and long )
144-
as well as references are supported
145-
:param return_ref: if name specifies a reference, we will return the reference
146-
instead of the object. Otherwise it will raise BadObject or BadName
148+
:return:
149+
Object specified by the given name - hexshas (short and long) as well as
150+
references are supported.
151+
152+
:param return_ref:
153+
If True, and name specifies a reference, we will return the reference
154+
instead of the object. Otherwise it will raise `~gitdb.exc.BadObject` o
155+
`~gitdb.exc.BadName`.
147156
"""
148157
hexsha: Union[None, str, bytes] = None
149158

150-
# is it a hexsha ? Try the most common ones, which is 7 to 40
159+
# Is it a hexsha? Try the most common ones, which is 7 to 40.
151160
if repo.re_hexsha_shortened.match(name):
152161
if len(name) != 40:
153-
# find long sha for short sha
162+
# Find long sha for short sha.
154163
hexsha = short_to_long(repo.odb, name)
155164
else:
156165
hexsha = name
157166
# END handle short shas
158167
# END find sha if it matches
159168

160-
# if we couldn't find an object for what seemed to be a short hexsha
161-
# try to find it as reference anyway, it could be named 'aaa' for instance
169+
# If we couldn't find an object for what seemed to be a short hexsha, try to find it
170+
# as reference anyway, it could be named 'aaa' for instance.
162171
if hexsha is None:
163172
for base in (
164173
"%s",
@@ -179,12 +188,12 @@ def name_to_object(
179188
# END for each base
180189
# END handle hexsha
181190

182-
# didn't find any ref, this is an error
191+
# Didn't find any ref, this is an error.
183192
if return_ref:
184193
raise BadObject("Couldn't find reference named %r" % name)
185194
# END handle return ref
186195

187-
# tried everything ? fail
196+
# Tried everything ? fail.
188197
if hexsha is None:
189198
raise BadName(name)
190199
# END assert hexsha was found
@@ -216,17 +225,27 @@ def to_commit(obj: Object) -> Union["Commit", "TagObject"]:
216225

217226
def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
218227
"""
219-
:return: Object at the given revision, either Commit, Tag, Tree or Blob
220-
:param rev: git-rev-parse compatible revision specification as string, please see
221-
http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html
222-
for details
223-
:raise BadObject: if the given revision could not be found
224-
:raise ValueError: If rev couldn't be parsed
225-
:raise IndexError: If invalid reflog index is specified"""
226-
227-
# colon search mode ?
228+
:return:
229+
`~git.objects.base.Object` at the given revision, either
230+
`~git.objects.commit.Commit`, `~git.refs.tag.Tag`, `~git.objects.tree.Tree` or
231+
`~git.objects.blob.Blob`.
232+
233+
:param rev:
234+
``git rev-parse``-compatible revision specification as string. Please see
235+
http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html for details.
236+
237+
:raise BadObject:
238+
If the given revision could not be found.
239+
240+
:raise ValueError:
241+
If `rev` couldn't be parsed.
242+
243+
:raise IndexError:
244+
If an invalid reflog index is specified.
245+
"""
246+
# Are we in colon search mode?
228247
if rev.startswith(":/"):
229-
# colon search mode
248+
# Colon search mode
230249
raise NotImplementedError("commit by message search ( regex )")
231250
# END handle search
232251

@@ -245,7 +264,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
245264
token = rev[start]
246265

247266
if obj is None:
248-
# token is a rev name
267+
# token is a rev name.
249268
if start == 0:
250269
ref = repo.head.ref
251270
else:
@@ -265,41 +284,40 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
265284

266285
start += 1
267286

268-
# try to parse {type}
287+
# Try to parse {type}.
269288
if start < lr and rev[start] == "{":
270289
end = rev.find("}", start)
271290
if end == -1:
272291
raise ValueError("Missing closing brace to define type in %s" % rev)
273-
output_type = rev[start + 1 : end] # exclude brace
292+
output_type = rev[start + 1 : end] # Exclude brace.
274293

275-
# handle type
294+
# Handle type.
276295
if output_type == "commit":
277-
pass # default
296+
pass # Default.
278297
elif output_type == "tree":
279298
try:
280299
obj = cast(Commit_ish, obj)
281300
obj = to_commit(obj).tree
282301
except (AttributeError, ValueError):
283-
pass # error raised later
302+
pass # Error raised later.
284303
# END exception handling
285304
elif output_type in ("", "blob"):
286305
obj = cast("TagObject", obj)
287306
if obj and obj.type == "tag":
288307
obj = deref_tag(obj)
289308
else:
290-
# cannot do anything for non-tags
309+
# Cannot do anything for non-tags.
291310
pass
292311
# END handle tag
293312
elif token == "@":
294313
# try single int
295314
assert ref is not None, "Require Reference to access reflog"
296315
revlog_index = None
297316
try:
298-
# transform reversed index into the format of our revlog
317+
# Transform reversed index into the format of our revlog.
299318
revlog_index = -(int(output_type) + 1)
300319
except ValueError as e:
301-
# TODO: Try to parse the other date options, using parse_date
302-
# maybe
320+
# TODO: Try to parse the other date options, using parse_date maybe.
303321
raise NotImplementedError("Support for additional @{...} modes not implemented") from e
304322
# END handle revlog index
305323

@@ -311,23 +329,24 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
311329

312330
obj = Object.new_from_sha(repo, hex_to_bin(entry.newhexsha))
313331

314-
# make it pass the following checks
332+
# Make it pass the following checks.
315333
output_type = ""
316334
else:
317335
raise ValueError("Invalid output type: %s ( in %s )" % (output_type, rev))
318336
# END handle output type
319337

320-
# empty output types don't require any specific type, its just about dereferencing tags
338+
# Empty output types don't require any specific type, its just about
339+
# dereferencing tags.
321340
if output_type and obj and obj.type != output_type:
322341
raise ValueError("Could not accommodate requested object type %r, got %s" % (output_type, obj.type))
323342
# END verify output type
324343

325-
start = end + 1 # skip brace
344+
start = end + 1 # Skip brace.
326345
parsed_to = start
327346
continue
328347
# END parse type
329348

330-
# try to parse a number
349+
# Try to parse a number.
331350
num = 0
332351
if token != ":":
333352
found_digit = False
@@ -341,15 +360,14 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
341360
# END handle number
342361
# END number parse loop
343362

344-
# no explicit number given, 1 is the default
345-
# It could be 0 though
363+
# No explicit number given, 1 is the default. It could be 0 though.
346364
if not found_digit:
347365
num = 1
348366
# END set default num
349367
# END number parsing only if non-blob mode
350368

351369
parsed_to = start
352-
# handle hierarchy walk
370+
# Handle hierarchy walk.
353371
try:
354372
obj = cast(Commit_ish, obj)
355373
if token == "~":
@@ -359,7 +377,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
359377
# END for each history item to walk
360378
elif token == "^":
361379
obj = to_commit(obj)
362-
# must be n'th parent
380+
# Must be n'th parent.
363381
if num:
364382
obj = obj.parents[num - 1]
365383
elif token == ":":
@@ -378,7 +396,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
378396
# END exception handling
379397
# END parse loop
380398

381-
# still no obj ? Its probably a simple name
399+
# Still no obj? It's probably a simple name.
382400
if obj is None:
383401
obj = cast(Commit_ish, name_to_object(repo, rev))
384402
parsed_to = lr

0 commit comments

Comments
 (0)
Please sign in to comment.