1919 stream_copy
2020)
2121from .exc import GitCommandError
22- from git .compat import text_type
22+ from git .compat import (
23+ text_type ,
24+ string_types ,
25+ defenc
26+ )
2327
2428execute_kwargs = ('istream' , 'with_keep_cwd' , 'with_extended_output' ,
2529 'with_exceptions' , 'as_process' ,
@@ -373,9 +377,9 @@ def execute(self, command,
373377 if output_stream is None :
374378 stdout_value , stderr_value = proc .communicate ()
375379 # strip trailing "\n"
376- if stdout_value .endswith ("\n " ):
380+ if stdout_value .endswith (b "\n " ):
377381 stdout_value = stdout_value [:- 1 ]
378- if stderr_value .endswith ("\n " ):
382+ if stderr_value .endswith (b "\n " ):
379383 stderr_value = stderr_value [:- 1 ]
380384 status = proc .returncode
381385 else :
@@ -394,9 +398,9 @@ def execute(self, command,
394398 if self .GIT_PYTHON_TRACE == 'full' :
395399 cmdstr = " " .join (command )
396400 if stderr_value :
397- log .info ("%s -> %d; stdout: '%s'; stderr: '%s'" , cmdstr , status , stdout_value , stderr_value )
401+ log .info ("%s -> %d; stdout: '%s'; stderr: '%s'" , cmdstr , status , stdout_value . decode ( defenc ) , stderr_value . decode ( defenc ) )
398402 elif stdout_value :
399- log .info ("%s -> %d; stdout: '%s'" , cmdstr , status , stdout_value )
403+ log .info ("%s -> %d; stdout: '%s'" , cmdstr , status , stdout_value . decode ( defenc ) )
400404 else :
401405 log .info ("%s -> %d" , cmdstr , status )
402406 # END handle debug printing
@@ -436,15 +440,15 @@ def transform_kwargs(self, split_single_char_options=False, **kwargs):
436440 def __unpack_args (cls , arg_list ):
437441 if not isinstance (arg_list , (list , tuple )):
438442 if isinstance (arg_list , text_type ):
439- return [arg_list .encode ('utf-8' )]
443+ return [arg_list .encode (defenc )]
440444 return [str (arg_list )]
441445
442446 outlist = list ()
443447 for arg in arg_list :
444448 if isinstance (arg_list , (list , tuple )):
445449 outlist .extend (cls .__unpack_args (arg ))
446450 elif isinstance (arg_list , text_type ):
447- outlist .append (arg_list .encode ('utf-8' ))
451+ outlist .append (arg_list .encode (defenc ))
448452 # END recursion
449453 else :
450454 outlist .append (str (arg ))
@@ -569,14 +573,20 @@ def _parse_object_header(self, header_line):
569573 raise ValueError ("Failed to parse header: %r" % header_line )
570574 return (tokens [0 ], tokens [1 ], int (tokens [2 ]))
571575
572- def __prepare_ref (self , ref ):
573- # required for command to separate refs on stdin
574- refstr = str (ref ) # could be ref-object
575- if refstr .endswith ("\n " ):
576- return refstr
577- return refstr + "\n "
576+ def _prepare_ref (self , ref ):
577+ # required for command to separate refs on stdin, as bytes
578+ refstr = ref
579+ if isinstance (ref , bytes ):
580+ # Assume 40 bytes hexsha - bin-to-ascii for some reason returns bytes, not text
581+ refstr = ref .decode ('ascii' )
582+ elif not isinstance (ref , string_types ):
583+ refstr = str (ref ) # could be ref-object
584+
585+ if not refstr .endswith ("\n " ):
586+ refstr += "\n "
587+ return refstr .encode (defenc )
578588
579- def __get_persistent_cmd (self , attr_name , cmd_name , * args , ** kwargs ):
589+ def _get_persistent_cmd (self , attr_name , cmd_name , * args , ** kwargs ):
580590 cur_val = getattr (self , attr_name )
581591 if cur_val is not None :
582592 return cur_val
@@ -589,7 +599,7 @@ def __get_persistent_cmd(self, attr_name, cmd_name, *args, **kwargs):
589599 return cmd
590600
591601 def __get_object_header (self , cmd , ref ):
592- cmd .stdin .write (self .__prepare_ref (ref ))
602+ cmd .stdin .write (self ._prepare_ref (ref ))
593603 cmd .stdin .flush ()
594604 return self ._parse_object_header (cmd .stdout .readline ())
595605
@@ -601,7 +611,7 @@ def get_object_header(self, ref):
601611 once and reuses the command in subsequent calls.
602612
603613 :return: (hexsha, type_string, size_as_int)"""
604- cmd = self .__get_persistent_cmd ("cat_file_header" , "cat_file" , batch_check = True )
614+ cmd = self ._get_persistent_cmd ("cat_file_header" , "cat_file" , batch_check = True )
605615 return self .__get_object_header (cmd , ref )
606616
607617 def get_object_data (self , ref ):
@@ -618,7 +628,7 @@ def stream_object_data(self, ref):
618628 :return: (hexsha, type_string, size_as_int, stream)
619629 :note: This method is not threadsafe, you need one independent Command instance
620630 per thread to be safe !"""
621- cmd = self .__get_persistent_cmd ("cat_file_all" , "cat_file" , batch = True )
631+ cmd = self ._get_persistent_cmd ("cat_file_all" , "cat_file" , batch = True )
622632 hexsha , typename , size = self .__get_object_header (cmd , ref )
623633 return (hexsha , typename , size , self .CatFileContentStream (size , cmd .stdout ))
624634
0 commit comments