19
19
stream_copy
20
20
)
21
21
from .exc import GitCommandError
22
- from git .compat import text_type
22
+ from git .compat import (
23
+ text_type ,
24
+ string_types ,
25
+ defenc
26
+ )
23
27
24
28
execute_kwargs = ('istream' , 'with_keep_cwd' , 'with_extended_output' ,
25
29
'with_exceptions' , 'as_process' ,
@@ -373,9 +377,9 @@ def execute(self, command,
373
377
if output_stream is None :
374
378
stdout_value , stderr_value = proc .communicate ()
375
379
# strip trailing "\n"
376
- if stdout_value .endswith ("\n " ):
380
+ if stdout_value .endswith (b "\n " ):
377
381
stdout_value = stdout_value [:- 1 ]
378
- if stderr_value .endswith ("\n " ):
382
+ if stderr_value .endswith (b "\n " ):
379
383
stderr_value = stderr_value [:- 1 ]
380
384
status = proc .returncode
381
385
else :
@@ -394,9 +398,9 @@ def execute(self, command,
394
398
if self .GIT_PYTHON_TRACE == 'full' :
395
399
cmdstr = " " .join (command )
396
400
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 ) )
398
402
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 ) )
400
404
else :
401
405
log .info ("%s -> %d" , cmdstr , status )
402
406
# END handle debug printing
@@ -436,15 +440,15 @@ def transform_kwargs(self, split_single_char_options=False, **kwargs):
436
440
def __unpack_args (cls , arg_list ):
437
441
if not isinstance (arg_list , (list , tuple )):
438
442
if isinstance (arg_list , text_type ):
439
- return [arg_list .encode ('utf-8' )]
443
+ return [arg_list .encode (defenc )]
440
444
return [str (arg_list )]
441
445
442
446
outlist = list ()
443
447
for arg in arg_list :
444
448
if isinstance (arg_list , (list , tuple )):
445
449
outlist .extend (cls .__unpack_args (arg ))
446
450
elif isinstance (arg_list , text_type ):
447
- outlist .append (arg_list .encode ('utf-8' ))
451
+ outlist .append (arg_list .encode (defenc ))
448
452
# END recursion
449
453
else :
450
454
outlist .append (str (arg ))
@@ -569,14 +573,20 @@ def _parse_object_header(self, header_line):
569
573
raise ValueError ("Failed to parse header: %r" % header_line )
570
574
return (tokens [0 ], tokens [1 ], int (tokens [2 ]))
571
575
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 )
578
588
579
- def __get_persistent_cmd (self , attr_name , cmd_name , * args , ** kwargs ):
589
+ def _get_persistent_cmd (self , attr_name , cmd_name , * args , ** kwargs ):
580
590
cur_val = getattr (self , attr_name )
581
591
if cur_val is not None :
582
592
return cur_val
@@ -589,7 +599,7 @@ def __get_persistent_cmd(self, attr_name, cmd_name, *args, **kwargs):
589
599
return cmd
590
600
591
601
def __get_object_header (self , cmd , ref ):
592
- cmd .stdin .write (self .__prepare_ref (ref ))
602
+ cmd .stdin .write (self ._prepare_ref (ref ))
593
603
cmd .stdin .flush ()
594
604
return self ._parse_object_header (cmd .stdout .readline ())
595
605
@@ -601,7 +611,7 @@ def get_object_header(self, ref):
601
611
once and reuses the command in subsequent calls.
602
612
603
613
: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 )
605
615
return self .__get_object_header (cmd , ref )
606
616
607
617
def get_object_data (self , ref ):
@@ -618,7 +628,7 @@ def stream_object_data(self, ref):
618
628
:return: (hexsha, type_string, size_as_int, stream)
619
629
:note: This method is not threadsafe, you need one independent Command instance
620
630
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 )
622
632
hexsha , typename , size = self .__get_object_header (cmd , ref )
623
633
return (hexsha , typename , size , self .CatFileContentStream (size , cmd .stdout ))
624
634
0 commit comments