43
43
# typing ---------------------------------------------------------------------------
44
44
45
45
from typing import (Any ,AnyStr ,BinaryIO ,Callable ,Dict ,IO ,List ,Mapping ,
46
- Sequence ,TYPE_CHECKING ,Tuple ,Union ,cast ,overload )
46
+ Sequence ,TYPE_CHECKING ,TextIO , Tuple ,Union ,cast ,overload )
47
47
48
48
from git .types import PathLike ,Literal ,TBD
49
49
@@ -98,14 +98,17 @@ def handle_process_output(process: subprocess.Popen,
98
98
or if decoding must happen later (i.e. for Diffs).
99
99
"""
100
100
# Use 2 "pump" threads and wait for both to finish.
101
- def pump_stream (cmdline :str ,name :str ,stream :BinaryIO ,is_decode :bool ,
102
- handler :Union [None ,Callable [[str ],None ]])-> None :
101
+ def pump_stream (cmdline :str ,name :str ,stream :Union [ BinaryIO , TextIO ] ,is_decode :bool ,
102
+ handler :Union [None ,Callable [[Union [ bytes , str ] ],None ]])-> None :
103
103
try :
104
104
for line in stream :
105
105
if handler :
106
106
if is_decode :
107
+ assert isinstance (line ,bytes )
107
108
line_str = line .decode (defenc )
108
- handler (line_str )
109
+ handler (line_str )
110
+ else :
111
+ handler (line )
109
112
except Exception as ex :
110
113
log .error ("Pumping %r of cmd(%s) failed due to: %r" ,name ,remove_password_if_present (cmdline ),ex )
111
114
raise CommandError (['<%s-pump>' % name ]+ remove_password_if_present (cmdline ),ex )from ex
@@ -337,12 +340,12 @@ def is_cygwin(cls) -> bool:
337
340
338
341
@overload
339
342
@classmethod
340
- def polish_url (cls ,url :str ,is_cygwin :Union [ None , bool ]= None )-> str :
343
+ def polish_url (cls ,url :str ,is_cygwin :Literal [ False ]= ... )-> str :
341
344
...
342
345
343
346
@overload
344
347
@classmethod
345
- def polish_url (cls ,url :PathLike ,is_cygwin :Union [None ,bool ]= None )-> PathLike :
348
+ def polish_url (cls ,url :PathLike ,is_cygwin :Union [None ,bool ]= None )-> str :
346
349
...
347
350
348
351
@classmethod
@@ -628,16 +631,16 @@ def version_info(self) -> Tuple[int, int, int, int]:
628
631
def execute (self ,
629
632
command :Union [str ,Sequence [Any ]],
630
633
* ,
631
- as_process :Literal [True ],
632
- )-> AutoInterrupt :
634
+ as_process :Literal [True ]
635
+ )-> ' AutoInterrupt' :
633
636
...
634
637
635
638
@overload
636
639
def execute (self ,
637
640
command :Union [str ,Sequence [Any ]],
638
641
* ,
639
642
as_process :Literal [False ]= False ,
640
- stdout_as_string :Literal [True ],
643
+ stdout_as_string :Literal [True ]
641
644
)-> Union [str ,Tuple [int ,str ,str ]]:
642
645
...
643
646
@@ -646,7 +649,7 @@ def execute(self,
646
649
command :Union [str ,Sequence [Any ]],
647
650
* ,
648
651
as_process :Literal [False ]= False ,
649
- stdout_as_string :Literal [False ]= False ,
652
+ stdout_as_string :Literal [False ]= False
650
653
)-> Union [bytes ,Tuple [int ,bytes ,str ]]:
651
654
...
652
655
@@ -656,8 +659,7 @@ def execute(self,
656
659
* ,
657
660
with_extended_output :Literal [False ],
658
661
as_process :Literal [False ],
659
- stdout_as_string :Literal [True ],
660
-
662
+ stdout_as_string :Literal [True ]
661
663
)-> str :
662
664
...
663
665
@@ -667,8 +669,7 @@ def execute(self,
667
669
* ,
668
670
with_extended_output :Literal [False ],
669
671
as_process :Literal [False ],
670
- stdout_as_string :Literal [False ],
671
-
672
+ stdout_as_string :Literal [False ]
672
673
)-> bytes :
673
674
...
674
675
@@ -829,16 +830,13 @@ def execute(self,
829
830
creationflags = PROC_CREATIONFLAGS ,
830
831
** subprocess_kwargs
831
832
)
832
- proc = cast (Popen [bytes ],proc )
833
833
834
- proc .stdout = cast (BinaryIO ,proc .stdout )
835
834
except cmd_not_found_exception as err :
836
835
raise GitCommandNotFound (redacted_command ,err )from err
837
836
else :
838
- assert isinstance (proc .stdout ,BinaryIO )
839
- assert isinstance (proc .stderr ,BinaryIO )
840
- # proc.stdout = cast(BinaryIO, proc.stdout)
841
- # proc.stderr = cast(BinaryIO, proc.stderr)
837
+ proc = cast (Popen ,proc )
838
+ proc .stdout = cast (BinaryIO ,proc .stdout )
839
+ proc .stderr = cast (BinaryIO ,proc .stderr )
842
840
843
841
if as_process :
844
842
return self .AutoInterrupt (proc ,command )
@@ -1164,6 +1162,8 @@ def _prepare_ref(self, ref: AnyStr) -> bytes:
1164
1162
refstr = ref .decode ('ascii' )# type: str
1165
1163
elif not isinstance (ref ,str ):
1166
1164
refstr = str (ref )# could be ref-object
1165
+ else :
1166
+ refstr = ref
1167
1167
1168
1168
if not refstr .endswith ("\n " ):
1169
1169
refstr += "\n "