Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit afc60ab

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent db9032d commit afc60ab

File tree

19 files changed

+282
-417
lines changed

19 files changed

+282
-417
lines changed

git/cmd.py

Lines changed: 65 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -258,28 +258,32 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
258258
# revert to whatever the old_git was
259259
cls.GIT_PYTHON_GIT_EXECUTABLE = old_git
260260

261-
if old_git is None:
262-
# on the first refresh (when GIT_PYTHON_GIT_EXECUTABLE is
263-
# None) we only are quiet, warn, or error depending on the
264-
# GIT_PYTHON_REFRESH value
265-
266-
# determine what the user wants to happen during the initial
267-
# refresh we expect GIT_PYTHON_REFRESH to either be unset or
268-
# be one of the following values:
269-
# 0|q|quiet|s|silence
270-
# 1|w|warn|warning
271-
# 2|r|raise|e|error
272-
273-
mode = os.environ.get(cls._refresh_env_var, "raise").lower()
274-
275-
quiet = ["quiet", "q", "silence", "s", "none", "n", "0"]
276-
warn = ["warn", "w", "warning", "1"]
277-
error = ["error", "e", "raise", "r", "2"]
278-
279-
if mode in quiet:
280-
pass
281-
elif mode in warn or mode in error:
282-
err = dedent("""\
261+
if old_git is not None:
262+
# after the first refresh (when GIT_PYTHON_GIT_EXECUTABLE
263+
# is no longer None) we raise an exception
264+
raise GitCommandNotFound("git", err)
265+
266+
# on the first refresh (when GIT_PYTHON_GIT_EXECUTABLE is
267+
# None) we only are quiet, warn, or error depending on the
268+
# GIT_PYTHON_REFRESH value
269+
270+
# determine what the user wants to happen during the initial
271+
# refresh we expect GIT_PYTHON_REFRESH to either be unset or
272+
# be one of the following values:
273+
# 0|q|quiet|s|silence
274+
# 1|w|warn|warning
275+
# 2|r|raise|e|error
276+
277+
mode = os.environ.get(cls._refresh_env_var, "raise").lower()
278+
279+
quiet = ["quiet", "q", "silence", "s", "none", "n", "0"]
280+
warn = ["warn", "w", "warning", "1"]
281+
error = ["error", "e", "raise", "r", "2"]
282+
283+
if mode in quiet:
284+
pass
285+
elif mode in warn or mode in error:
286+
err = dedent("""\
283287
%s
284288
All git commands will error until this is rectified.
285289
@@ -292,43 +296,38 @@ def refresh(cls, path: Union[None, PathLike] = None) -> bool:
292296
Example:
293297
export %s=%s
294298
""") % (
295-
err,
296-
cls._refresh_env_var,
297-
"|".join(quiet),
298-
"|".join(warn),
299-
"|".join(error),
300-
cls._refresh_env_var,
301-
quiet[0])
302-
303-
if mode in warn:
304-
print("WARNING: %s" % err)
305-
else:
306-
raise ImportError(err)
299+
err,
300+
cls._refresh_env_var,
301+
"|".join(quiet),
302+
"|".join(warn),
303+
"|".join(error),
304+
cls._refresh_env_var,
305+
quiet[0])
306+
307+
if mode in warn:
308+
print("WARNING: %s" % err)
307309
else:
308-
err = dedent("""\
310+
raise ImportError(err)
311+
else:
312+
err = dedent("""\
309313
%s environment variable has been set but it has been set with an invalid value.
310314
311315
Use only the following values:
312316
- %s: for no warning or exception
313317
- %s: for a printed warning
314318
- %s: for a raised exception
315319
""") % (
316-
cls._refresh_env_var,
317-
"|".join(quiet),
318-
"|".join(warn),
319-
"|".join(error))
320-
raise ImportError(err)
321-
322-
# we get here if this was the init refresh and the refresh mode
323-
# was not error, go ahead and set the GIT_PYTHON_GIT_EXECUTABLE
324-
# such that we discern the difference between a first import
325-
# and a second import
326-
cls.GIT_PYTHON_GIT_EXECUTABLE = cls.git_exec_name
327-
else:
328-
# after the first refresh (when GIT_PYTHON_GIT_EXECUTABLE
329-
# is no longer None) we raise an exception
330-
raise GitCommandNotFound("git", err)
331-
320+
cls._refresh_env_var,
321+
"|".join(quiet),
322+
"|".join(warn),
323+
"|".join(error))
324+
raise ImportError(err)
325+
326+
# we get here if this was the init refresh and the refresh mode
327+
# was not error, go ahead and set the GIT_PYTHON_GIT_EXECUTABLE
328+
# such that we discern the difference between a first import
329+
# and a second import
330+
cls.GIT_PYTHON_GIT_EXECUTABLE = cls.git_exec_name
332331
return has_git
333332

334333
@classmethod
@@ -476,12 +475,7 @@ def read(self, size: int = -1) -> bytes:
476475
bytes_left = self._size - self._nbr
477476
if bytes_left == 0:
478477
return b''
479-
if size > -1:
480-
# assure we don't try to read past our limit
481-
size = min(bytes_left, size)
482-
else:
483-
# they try to read all, make sure its not more than what remains
484-
size = bytes_left
478+
size = min(bytes_left, size) if size > -1 else bytes_left
485479
# END check early depletion
486480
data = self._stream.read(size)
487481
self._nbr += len(data)
@@ -498,10 +492,7 @@ def readline(self, size: int = -1) -> bytes:
498492

499493
# clamp size to lowest allowed value
500494
bytes_left = self._size - self._nbr
501-
if size > -1:
502-
size = min(bytes_left, size)
503-
else:
504-
size = bytes_left
495+
size = min(bytes_left, size) if size > -1 else bytes_left
505496
# END handle size
506497

507498
data = self._stream.readline(size)
@@ -985,19 +976,16 @@ def custom_environment(self, **kwargs):
985976
self.update_environment(**old_env)
986977

987978
def transform_kwarg(self, name: str, value: Any, split_single_char_options: bool) -> List[str]:
988-
if len(name) == 1:
989-
if value is True:
979+
if value is True:
980+
if len(name) == 1:
990981
return ["-%s" % name]
991-
elif value not in (False, None):
992-
if split_single_char_options:
993-
return ["-%s" % name, "%s" % value]
994-
else:
995-
return ["-%s%s" % (name, value)]
996-
else:
997-
if value is True:
982+
else:
998983
return ["--%s" % dashify(name)]
999-
elif value is not False and value is not None:
1000-
return ["--%s=%s" % (dashify(name), value)]
984+
elif value not in (False, None):
985+
if split_single_char_options:
986+
return ["-%s" % name, "%s" % value]
987+
else:
988+
return ["-%s%s" % (name, value)]
1001989
return []
1002990

1003991
def transform_kwargs(self, split_single_char_options: bool = True, **kwargs: Any) -> List[str]:
@@ -1108,14 +1096,13 @@ def _call_process(self, method: str, *args: Any, **kwargs: Any
11081096
args_list = ext_args[:index + 1] + opt_args + ext_args[index + 1:]
11091097
# end handle opts_kwargs
11101098

1111-
call = [self.GIT_PYTHON_GIT_EXECUTABLE]
1099+
call = [
1100+
self.GIT_PYTHON_GIT_EXECUTABLE,
1101+
*self._persistent_git_options,
1102+
*self._git_options,
1103+
]
11121104

1113-
# add persistent git options
1114-
call.extend(self._persistent_git_options)
11151105

1116-
# add the git options, then reset to empty
1117-
# to avoid side_effects
1118-
call.extend(self._git_options)
11191106
self._git_options = ()
11201107

11211108
call.append(dashify(method))

git/config.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ def __new__(cls, name: str, bases: TBD, clsdict: Dict[str, Any]) -> TBD:
8181
clsdict[name] = method_with_values
8282
# END for each name/method pair
8383
# END for each base
84-
# END if mutating methods configuration is set
85-
86-
new_type = super(MetaParserBuilder, cls).__new__(cls, name, bases, clsdict)
87-
return new_type
84+
return super(MetaParserBuilder, cls).__new__(cls, name, bases, clsdict)
8885

8986

9087
def needs_values(func: Callable) -> Callable:
@@ -303,16 +300,15 @@ def __init__(self, file_or_files: Union[None, PathLike, IO, Sequence[Union[PathL
303300

304301
if file_or_files is not None:
305302
self._file_or_files = file_or_files # type: Union[PathLike, IO, Sequence[Union[PathLike, IO]]]
306-
else:
307-
if config_level is None:
308-
if read_only:
309-
self._file_or_files = [get_config_path(f) # type: ignore
310-
for f in CONFIG_LEVELS # Can type f properly when 3.5 dropped
311-
if f != 'repository']
312-
else:
313-
raise ValueError("No configuration level or configuration files specified")
303+
elif config_level is None:
304+
if read_only:
305+
self._file_or_files = [get_config_path(f) # type: ignore
306+
for f in CONFIG_LEVELS # Can type f properly when 3.5 dropped
307+
if f != 'repository']
314308
else:
315-
self._file_or_files = [get_config_path(config_level)]
309+
raise ValueError("No configuration level or configuration files specified")
310+
else:
311+
self._file_or_files = [get_config_path(config_level)]
316312

317313
self._read_only = read_only
318314
self._dirty = False
@@ -365,15 +361,14 @@ def release(self) -> None:
365361
return
366362

367363
try:
368-
try:
369-
self.write()
370-
except IOError:
371-
log.error("Exception during destruction of GitConfigParser", exc_info=True)
372-
except ReferenceError:
373-
# This happens in PY3 ... and usually means that some state cannot be written
374-
# as the sections dict cannot be iterated
375-
# Usually when shutting down the interpreter, don'y know how to fix this
376-
pass
364+
self.write()
365+
except IOError:
366+
log.error("Exception during destruction of GitConfigParser", exc_info=True)
367+
except ReferenceError:
368+
# This happens in PY3 ... and usually means that some state cannot be written
369+
# as the sections dict cannot be iterated
370+
# Usually when shutting down the interpreter, don'y know how to fix this
371+
pass
377372
finally:
378373
if self._lock is not None:
379374
self._lock._release_lock()
@@ -518,9 +513,10 @@ def _included_paths(self) -> List[Tuple[str, str]]:
518513
),
519514
value
520515
)
521-
if self._repo.git_dir:
522-
if fnmatch.fnmatchcase(str(self._repo.git_dir), value):
523-
paths += self.items(section)
516+
if self._repo.git_dir and fnmatch.fnmatchcase(
517+
str(self._repo.git_dir), value
518+
):
519+
paths += self.items(section)
524520

525521
elif keyword == "onbranch":
526522
try:

git/diff.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,7 @@ def diff(self, other: Union[Type[Index], Type['Tree'], object, None, str] = Inde
116116
:note:
117117
On a bare repository, 'other' needs to be provided as Index or as
118118
as Tree/Commit, or a git command error will occur"""
119-
args = [] # type: List[Union[str, Diffable, object]]
120-
args.append("--abbrev=40") # we need full shas
121-
args.append("--full-index") # get full index paths, not only filenames
122-
123-
args.append("-M") # check for renames, in both formats
119+
args = ["--abbrev=40", "--full-index", "-M"]
124120
if create_patch:
125121
args.append("-p")
126122
else:
@@ -325,11 +321,9 @@ def __init__(self, repo: 'Repo',
325321
self.score = score
326322

327323
def __eq__(self, other: object) -> bool:
328-
for name in self.__slots__:
329-
if getattr(self, name) != getattr(other, name):
330-
return False
331-
# END for each name
332-
return True
324+
return all(
325+
getattr(self, name) == getattr(other, name) for name in self.__slots__
326+
)
333327

334328
def __ne__(self, other: object) -> bool:
335329
return not (self == other)
@@ -348,10 +342,7 @@ def __str__(self) -> str:
348342
line = None # temp line
349343
line_length = 0 # line length
350344
for b, n in zip((self.a_blob, self.b_blob), ('lhs', 'rhs')):
351-
if b:
352-
line = "\n%s: %o | %s" % (n, b.mode, b.hexsha)
353-
else:
354-
line = "\n%s: None" % n
345+
line = "\n%s: %o | %s" % (n, b.mode, b.hexsha) if b else "\n%s: None" % n
355346
# END if blob is not None
356347
line_length = max(len(line), line_length)
357348
msg += line
@@ -378,13 +369,8 @@ def __str__(self) -> str:
378369
msg += 'OMITTED BINARY DATA'
379370
# end handle encoding
380371
msg += '\n---'
381-
# END diff info
382-
383-
# Python2 silliness: have to assure we convert our likely to be unicode object to a string with the
384-
# right encoding. Otherwise it tries to convert it using ascii, which may fail ungracefully
385-
res = h + msg
386372
# end
387-
return res
373+
return h + msg
388374

389375
@property
390376
def a_path(self) -> Optional[str]:
@@ -533,9 +519,6 @@ def _handle_diff_line(lines_bytes: bytes, repo: 'Repo', index: DiffIndex) -> Non
533519
a_path = a_path_str.encode(defenc)
534520
b_path = b_path_str.encode(defenc)
535521
rename_from, rename_to = a_path, b_path
536-
elif change_type == 'T':
537-
# Nothing to do
538-
pass
539522
# END add/remove handling
540523

541524
diff = Diff(repo, a_path, b_path, a_blob_id, b_blob_id, old_mode, new_mode,

0 commit comments

Comments
 (0)