@@ -57,13 +57,13 @@ def touch(filename: str) -> str:
57
57
58
58
59
59
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.
62
61
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
+ """
67
67
if osp .isdir (d ):
68
68
if (osp .isdir (osp .join (d , "objects" )) or "GIT_OBJECT_DIRECTORY" in os .environ ) and osp .isdir (
69
69
osp .join (d , "refs" )
@@ -107,15 +107,15 @@ def find_submodule_git_dir(d: "PathLike") -> Optional["PathLike"]:
107
107
with open (d ) as fp :
108
108
content = fp .read ().rstrip ()
109
109
except IOError :
110
- # it 's probably not a file
110
+ # It 's probably not a file.
111
111
pass
112
112
else :
113
113
if content .startswith ("gitdir: " ):
114
114
path = content [8 :]
115
115
116
116
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.
119
119
# Also the Cygwin tests are assuming Cygwin paths.
120
120
path = cygpath (path )
121
121
if not osp .isabs (path ):
@@ -126,9 +126,14 @@ def find_submodule_git_dir(d: "PathLike") -> Optional["PathLike"]:
126
126
127
127
128
128
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
+ """
132
137
try :
133
138
return bin_to_hex (odb .partial_to_complete_sha_hex (hexsha ))
134
139
except BadObject :
@@ -140,25 +145,29 @@ def name_to_object(
140
145
repo : "Repo" , name : str , return_ref : bool = False
141
146
) -> Union [SymbolicReference , "Commit" , "TagObject" , "Blob" , "Tree" ]:
142
147
"""
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`.
147
156
"""
148
157
hexsha : Union [None , str , bytes ] = None
149
158
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.
151
160
if repo .re_hexsha_shortened .match (name ):
152
161
if len (name ) != 40 :
153
- # find long sha for short sha
162
+ # Find long sha for short sha.
154
163
hexsha = short_to_long (repo .odb , name )
155
164
else :
156
165
hexsha = name
157
166
# END handle short shas
158
167
# END find sha if it matches
159
168
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.
162
171
if hexsha is None :
163
172
for base in (
164
173
"%s" ,
@@ -179,12 +188,12 @@ def name_to_object(
179
188
# END for each base
180
189
# END handle hexsha
181
190
182
- # didn 't find any ref, this is an error
191
+ # Didn 't find any ref, this is an error.
183
192
if return_ref :
184
193
raise BadObject ("Couldn't find reference named %r" % name )
185
194
# END handle return ref
186
195
187
- # tried everything ? fail
196
+ # Tried everything ? fail.
188
197
if hexsha is None :
189
198
raise BadName (name )
190
199
# END assert hexsha was found
@@ -216,17 +225,27 @@ def to_commit(obj: Object) -> Union["Commit", "TagObject"]:
216
225
217
226
def rev_parse (repo : "Repo" , rev : str ) -> Union ["Commit" , "Tag" , "Tree" , "Blob" ]:
218
227
"""
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?
228
247
if rev .startswith (":/" ):
229
- # colon search mode
248
+ # Colon search mode
230
249
raise NotImplementedError ("commit by message search ( regex )" )
231
250
# END handle search
232
251
@@ -245,7 +264,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
245
264
token = rev [start ]
246
265
247
266
if obj is None :
248
- # token is a rev name
267
+ # token is a rev name.
249
268
if start == 0 :
250
269
ref = repo .head .ref
251
270
else :
@@ -265,41 +284,40 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
265
284
266
285
start += 1
267
286
268
- # try to parse {type}
287
+ # Try to parse {type}.
269
288
if start < lr and rev [start ] == "{" :
270
289
end = rev .find ("}" , start )
271
290
if end == - 1 :
272
291
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.
274
293
275
- # handle type
294
+ # Handle type.
276
295
if output_type == "commit" :
277
- pass # default
296
+ pass # Default.
278
297
elif output_type == "tree" :
279
298
try :
280
299
obj = cast (Commit_ish , obj )
281
300
obj = to_commit (obj ).tree
282
301
except (AttributeError , ValueError ):
283
- pass # error raised later
302
+ pass # Error raised later.
284
303
# END exception handling
285
304
elif output_type in ("" , "blob" ):
286
305
obj = cast ("TagObject" , obj )
287
306
if obj and obj .type == "tag" :
288
307
obj = deref_tag (obj )
289
308
else :
290
- # cannot do anything for non-tags
309
+ # Cannot do anything for non-tags.
291
310
pass
292
311
# END handle tag
293
312
elif token == "@" :
294
313
# try single int
295
314
assert ref is not None , "Require Reference to access reflog"
296
315
revlog_index = None
297
316
try :
298
- # transform reversed index into the format of our revlog
317
+ # Transform reversed index into the format of our revlog.
299
318
revlog_index = - (int (output_type ) + 1 )
300
319
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.
303
321
raise NotImplementedError ("Support for additional @{...} modes not implemented" ) from e
304
322
# END handle revlog index
305
323
@@ -311,23 +329,24 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
311
329
312
330
obj = Object .new_from_sha (repo , hex_to_bin (entry .newhexsha ))
313
331
314
- # make it pass the following checks
332
+ # Make it pass the following checks.
315
333
output_type = ""
316
334
else :
317
335
raise ValueError ("Invalid output type: %s ( in %s )" % (output_type , rev ))
318
336
# END handle output type
319
337
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.
321
340
if output_type and obj and obj .type != output_type :
322
341
raise ValueError ("Could not accommodate requested object type %r, got %s" % (output_type , obj .type ))
323
342
# END verify output type
324
343
325
- start = end + 1 # skip brace
344
+ start = end + 1 # Skip brace.
326
345
parsed_to = start
327
346
continue
328
347
# END parse type
329
348
330
- # try to parse a number
349
+ # Try to parse a number.
331
350
num = 0
332
351
if token != ":" :
333
352
found_digit = False
@@ -341,15 +360,14 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
341
360
# END handle number
342
361
# END number parse loop
343
362
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.
346
364
if not found_digit :
347
365
num = 1
348
366
# END set default num
349
367
# END number parsing only if non-blob mode
350
368
351
369
parsed_to = start
352
- # handle hierarchy walk
370
+ # Handle hierarchy walk.
353
371
try :
354
372
obj = cast (Commit_ish , obj )
355
373
if token == "~" :
@@ -359,7 +377,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
359
377
# END for each history item to walk
360
378
elif token == "^" :
361
379
obj = to_commit (obj )
362
- # must be n'th parent
380
+ # Must be n'th parent.
363
381
if num :
364
382
obj = obj .parents [num - 1 ]
365
383
elif token == ":" :
@@ -378,7 +396,7 @@ def rev_parse(repo: "Repo", rev: str) -> Union["Commit", "Tag", "Tree", "Blob"]:
378
396
# END exception handling
379
397
# END parse loop
380
398
381
- # still no obj ? Its probably a simple name
399
+ # Still no obj? It's probably a simple name.
382
400
if obj is None :
383
401
obj = cast (Commit_ish , name_to_object (repo , rev ))
384
402
parsed_to = lr
0 commit comments