@@ -97,12 +97,8 @@ def handle_process_output(
97
97
Callable [[List [AnyStr ]], None ],
98
98
Callable [[bytes , "Repo" , "DiffIndex" ], None ],
99
99
],
100
- stderr_handler : Union [
101
- None , Callable [[AnyStr ], None ], Callable [[List [AnyStr ]], None ]
102
- ],
103
- finalizer : Union [
104
- None , Callable [[Union [subprocess .Popen , "Git.AutoInterrupt" ]], None ]
105
- ] = None ,
100
+ stderr_handler : Union [None , Callable [[AnyStr ], None ], Callable [[List [AnyStr ]], None ]],
101
+ finalizer : Union [None , Callable [[Union [subprocess .Popen , "Git.AutoInterrupt" ]], None ]] = None ,
106
102
decode_streams : bool = True ,
107
103
kill_after_timeout : Union [None , float ] = None ,
108
104
) -> None :
@@ -144,14 +140,10 @@ def pump_stream(
144
140
handler (line )
145
141
146
142
except Exception as ex :
147
- log .error (
148
- f"Pumping { name !r} of cmd({ remove_password_if_present (cmdline )} ) failed due to: { ex !r} "
149
- )
143
+ log .error (f"Pumping { name !r} of cmd({ remove_password_if_present (cmdline )} ) failed due to: { ex !r} " )
150
144
if "I/O operation on closed file" not in str (ex ):
151
145
# Only reraise if the error was not due to the stream closing
152
- raise CommandError (
153
- [f"<{ name } -pump>" ] + remove_password_if_present (cmdline ), ex
154
- ) from ex
146
+ raise CommandError ([f"<{ name } -pump>" ] + remove_password_if_present (cmdline ), ex ) from ex
155
147
finally :
156
148
stream .close ()
157
149
@@ -178,9 +170,7 @@ def pump_stream(
178
170
threads : List [threading .Thread ] = []
179
171
180
172
for name , stream , handler in pumps :
181
- t = threading .Thread (
182
- target = pump_stream , args = (cmdline , name , stream , decode_streams , handler )
183
- )
173
+ t = threading .Thread (target = pump_stream , args = (cmdline , name , stream , decode_streams , handler ))
184
174
t .daemon = True
185
175
t .start ()
186
176
threads .append (t )
@@ -199,8 +189,7 @@ def pump_stream(
199
189
)
200
190
if stderr_handler :
201
191
error_str : Union [str , bytes ] = (
202
- "error: process killed because it timed out."
203
- f" kill_after_timeout={ kill_after_timeout } seconds"
192
+ "error: process killed because it timed out." f" kill_after_timeout={ kill_after_timeout } seconds"
204
193
)
205
194
if not decode_streams and isinstance (p_stderr , BinaryIO ):
206
195
# Assume stderr_handler needs binary input
@@ -224,9 +213,7 @@ def slots_to_dict(self: object, exclude: Sequence[str] = ()) -> Dict[str, Any]:
224
213
return {s : getattr (self , s ) for s in self .__slots__ if s not in exclude }
225
214
226
215
227
- def dict_to_slots_and__excluded_are_none (
228
- self : object , d : Mapping [str , Any ], excluded : Sequence [str ] = ()
229
- ) -> None :
216
+ def dict_to_slots_and__excluded_are_none (self : object , d : Mapping [str , Any ], excluded : Sequence [str ] = ()) -> None :
230
217
for k , v in d .items ():
231
218
setattr (self , k , v )
232
219
for k in excluded :
@@ -242,9 +229,7 @@ def dict_to_slots_and__excluded_are_none(
242
229
## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards,
243
230
# see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
244
231
PROC_CREATIONFLAGS = (
245
- CREATE_NO_WINDOW | subprocess .CREATE_NEW_PROCESS_GROUP # type: ignore[attr-defined]
246
- if is_win
247
- else 0
232
+ CREATE_NO_WINDOW | subprocess .CREATE_NEW_PROCESS_GROUP if is_win else 0 # type: ignore[attr-defined]
248
233
) # mypy error if not windows
249
234
250
235
@@ -557,9 +542,7 @@ def wait(self, stderr: Union[None, str, bytes] = b"") -> int:
557
542
status = self .status
558
543
p_stderr = None
559
544
560
- def read_all_from_possibly_closed_stream (
561
- stream : Union [IO [bytes ], None ]
562
- ) -> bytes :
545
+ def read_all_from_possibly_closed_stream (stream : Union [IO [bytes ], None ]) -> bytes :
563
546
if stream :
564
547
try :
565
548
return stderr_b + force_bytes (stream .read ())
@@ -573,9 +556,7 @@ def read_all_from_possibly_closed_stream(
573
556
if status != 0 :
574
557
errstr = read_all_from_possibly_closed_stream (p_stderr )
575
558
log .debug ("AutoInterrupt wait stderr: %r" % (errstr ,))
576
- raise GitCommandError (
577
- remove_password_if_present (self .args ), status , errstr
578
- )
559
+ raise GitCommandError (remove_password_if_present (self .args ), status , errstr )
579
560
return status
580
561
581
562
# END auto interrupt
@@ -725,16 +706,12 @@ def set_persistent_git_options(self, **kwargs: Any) -> None:
725
706
the subcommand.
726
707
"""
727
708
728
- self ._persistent_git_options = self .transform_kwargs (
729
- split_single_char_options = True , ** kwargs
730
- )
709
+ self ._persistent_git_options = self .transform_kwargs (split_single_char_options = True , ** kwargs )
731
710
732
711
def _set_cache_ (self , attr : str ) -> None :
733
712
if attr == "_version_info" :
734
713
# We only use the first 4 numbers, as everything else could be strings in fact (on windows)
735
- process_version = self ._call_process (
736
- "version"
737
- ) # should be as default *args and **kwargs used
714
+ process_version = self ._call_process ("version" ) # should be as default *args and **kwargs used
738
715
version_numbers = process_version .split (" " )[2 ]
739
716
740
717
self ._version_info = cast (
@@ -759,9 +736,7 @@ def version_info(self) -> Tuple[int, int, int, int]:
759
736
return self ._version_info
760
737
761
738
@overload
762
- def execute (
763
- self , command : Union [str , Sequence [Any ]], * , as_process : Literal [True ]
764
- ) -> "AutoInterrupt" :
739
+ def execute (self , command : Union [str , Sequence [Any ]], * , as_process : Literal [True ]) -> "AutoInterrupt" :
765
740
...
766
741
767
742
@overload
@@ -946,16 +921,10 @@ def execute(
946
921
'"kill_after_timeout" feature is not supported on Windows.' ,
947
922
)
948
923
else :
949
- cmd_not_found_exception = (
950
- FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable
951
- )
924
+ cmd_not_found_exception = FileNotFoundError # NOQA # exists, flake8 unknown @UndefinedVariable
952
925
# end handle
953
926
954
- stdout_sink = (
955
- PIPE
956
- if with_stdout
957
- else getattr (subprocess , "DEVNULL" , None ) or open (os .devnull , "wb" )
958
- )
927
+ stdout_sink = PIPE if with_stdout else getattr (subprocess , "DEVNULL" , None ) or open (os .devnull , "wb" )
959
928
istream_ok = "None"
960
929
if istream :
961
930
istream_ok = "<valid stream>"
@@ -1027,9 +996,7 @@ def _kill_process(pid: int) -> None:
1027
996
1028
997
if kill_after_timeout is not None :
1029
998
kill_check = threading .Event ()
1030
- watchdog = threading .Timer (
1031
- kill_after_timeout , _kill_process , args = (proc .pid ,)
1032
- )
999
+ watchdog = threading .Timer (kill_after_timeout , _kill_process , args = (proc .pid ,))
1033
1000
1034
1001
# Wait for the process to return
1035
1002
status = 0
@@ -1044,9 +1011,9 @@ def _kill_process(pid: int) -> None:
1044
1011
if kill_after_timeout is not None :
1045
1012
watchdog .cancel ()
1046
1013
if kill_check .is_set ():
1047
- stderr_value = (
1048
- 'Timeout: the command "%s" did not complete in %d '
1049
- "secs." % ( " " . join ( redacted_command ), kill_after_timeout )
1014
+ stderr_value = 'Timeout: the command "%s" did not complete in %d ' "secs." % (
1015
+ " " . join ( redacted_command ),
1016
+ kill_after_timeout ,
1050
1017
)
1051
1018
if not universal_newlines :
1052
1019
stderr_value = stderr_value .encode (defenc )
@@ -1058,11 +1025,7 @@ def _kill_process(pid: int) -> None:
1058
1025
1059
1026
status = proc .returncode
1060
1027
else :
1061
- max_chunk_size = (
1062
- max_chunk_size
1063
- if max_chunk_size and max_chunk_size > 0
1064
- else io .DEFAULT_BUFFER_SIZE
1065
- )
1028
+ max_chunk_size = max_chunk_size if max_chunk_size and max_chunk_size > 0 else io .DEFAULT_BUFFER_SIZE
1066
1029
stream_copy (proc .stdout , output_stream , max_chunk_size )
1067
1030
stdout_value = proc .stdout .read ()
1068
1031
stderr_value = proc .stderr .read ()
@@ -1079,9 +1042,7 @@ def _kill_process(pid: int) -> None:
1079
1042
cmdstr = " " .join (redacted_command )
1080
1043
1081
1044
def as_text (stdout_value : Union [bytes , str ]) -> str :
1082
- return (
1083
- not output_stream and safe_decode (stdout_value ) or "<OUTPUT_STREAM>"
1084
- )
1045
+ return not output_stream and safe_decode (stdout_value ) or "<OUTPUT_STREAM>"
1085
1046
1086
1047
# end
1087
1048
@@ -1094,19 +1055,15 @@ def as_text(stdout_value: Union[bytes, str]) -> str:
1094
1055
safe_decode (stderr_value ),
1095
1056
)
1096
1057
elif stdout_value :
1097
- log .info (
1098
- "%s -> %d; stdout: '%s'" , cmdstr , status , as_text (stdout_value )
1099
- )
1058
+ log .info ("%s -> %d; stdout: '%s'" , cmdstr , status , as_text (stdout_value ))
1100
1059
else :
1101
1060
log .info ("%s -> %d" , cmdstr , status )
1102
1061
# END handle debug printing
1103
1062
1104
1063
if with_exceptions and status != 0 :
1105
1064
raise GitCommandError (redacted_command , status , stderr_value , stdout_value )
1106
1065
1107
- if (
1108
- isinstance (stdout_value , bytes ) and stdout_as_string
1109
- ): # could also be output_stream
1066
+ if isinstance (stdout_value , bytes ) and stdout_as_string : # could also be output_stream
1110
1067
stdout_value = safe_decode (stdout_value )
1111
1068
1112
1069
# Allow access to the command's status code
@@ -1163,9 +1120,7 @@ def custom_environment(self, **kwargs: Any) -> Iterator[None]:
1163
1120
finally :
1164
1121
self .update_environment (** old_env )
1165
1122
1166
- def transform_kwarg (
1167
- self , name : str , value : Any , split_single_char_options : bool
1168
- ) -> List [str ]:
1123
+ def transform_kwarg (self , name : str , value : Any , split_single_char_options : bool ) -> List [str ]:
1169
1124
if len (name ) == 1 :
1170
1125
if value is True :
1171
1126
return ["-%s" % name ]
@@ -1181,9 +1136,7 @@ def transform_kwarg(
1181
1136
return ["--%s=%s" % (dashify (name ), value )]
1182
1137
return []
1183
1138
1184
- def transform_kwargs (
1185
- self , split_single_char_options : bool = True , ** kwargs : Any
1186
- ) -> List [str ]:
1139
+ def transform_kwargs (self , split_single_char_options : bool = True , ** kwargs : Any ) -> List [str ]:
1187
1140
"""Transforms Python style kwargs into git command line options."""
1188
1141
args = []
1189
1142
for k , v in kwargs .items ():
@@ -1218,9 +1171,7 @@ def __call__(self, **kwargs: Any) -> "Git":
1218
1171
1219
1172
``Examples``::
1220
1173
git(work_tree='/tmp').difftool()"""
1221
- self ._git_options = self .transform_kwargs (
1222
- split_single_char_options = True , ** kwargs
1223
- )
1174
+ self ._git_options = self .transform_kwargs (split_single_char_options = True , ** kwargs )
1224
1175
return self
1225
1176
1226
1177
@overload
@@ -1330,15 +1281,9 @@ def _parse_object_header(self, header_line: str) -> Tuple[str, str, int]:
1330
1281
tokens = header_line .split ()
1331
1282
if len (tokens ) != 3 :
1332
1283
if not tokens :
1333
- raise ValueError (
1334
- "SHA could not be resolved, git returned: %r"
1335
- % (header_line .strip ())
1336
- )
1284
+ raise ValueError ("SHA could not be resolved, git returned: %r" % (header_line .strip ()))
1337
1285
else :
1338
- raise ValueError (
1339
- "SHA %s could not be resolved, git returned: %r"
1340
- % (tokens [0 ], header_line .strip ())
1341
- )
1286
+ raise ValueError ("SHA %s could not be resolved, git returned: %r" % (tokens [0 ], header_line .strip ()))
1342
1287
# END handle actual return value
1343
1288
# END error handling
1344
1289
@@ -1360,9 +1305,7 @@ def _prepare_ref(self, ref: AnyStr) -> bytes:
1360
1305
refstr += "\n "
1361
1306
return refstr .encode (defenc )
1362
1307
1363
- def _get_persistent_cmd (
1364
- self , attr_name : str , cmd_name : str , * args : Any , ** kwargs : Any
1365
- ) -> "Git.AutoInterrupt" :
1308
+ def _get_persistent_cmd (self , attr_name : str , cmd_name : str , * args : Any , ** kwargs : Any ) -> "Git.AutoInterrupt" :
1366
1309
cur_val = getattr (self , attr_name )
1367
1310
if cur_val is not None :
1368
1311
return cur_val
@@ -1375,9 +1318,7 @@ def _get_persistent_cmd(
1375
1318
cmd = cast ("Git.AutoInterrupt" , cmd )
1376
1319
return cmd
1377
1320
1378
- def __get_object_header (
1379
- self , cmd : "Git.AutoInterrupt" , ref : AnyStr
1380
- ) -> Tuple [str , str , int ]:
1321
+ def __get_object_header (self , cmd : "Git.AutoInterrupt" , ref : AnyStr ) -> Tuple [str , str , int ]:
1381
1322
if cmd .stdin and cmd .stdout :
1382
1323
cmd .stdin .write (self ._prepare_ref (ref ))
1383
1324
cmd .stdin .flush ()
@@ -1405,9 +1346,7 @@ def get_object_data(self, ref: str) -> Tuple[str, str, int, bytes]:
1405
1346
del stream
1406
1347
return (hexsha , typename , size , data )
1407
1348
1408
- def stream_object_data (
1409
- self , ref : str
1410
- ) -> Tuple [str , str , int , "Git.CatFileContentStream" ]:
1349
+ def stream_object_data (self , ref : str ) -> Tuple [str , str , int , "Git.CatFileContentStream" ]:
1411
1350
"""As get_object_header, but returns the data as a stream
1412
1351
1413
1352
:return: (hexsha, type_string, size_as_int, stream)
0 commit comments