Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8a30861

Browse files
committed
Fixes test_blob and improved commit writing/reading
1 parent3d0556a commit8a30861

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

‎git/compat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
ifPY3:
2828
importio
2929
FileType=io.IOBase
30+
defbyte_ord(b):
31+
returnb
3032
else:
3133
FileType=file
3234
# usually, this is just ascii, which might not enough for our encoding needs
3335
# Unless it's set specifically, we override it to be utf-8
3436
ifdefenc=='ascii':
3537
defenc='utf-8'
38+
byte_ord=ord
3639

3740

3841
defwith_metaclass(meta,*bases):
@@ -54,4 +57,3 @@ def __new__(cls, name, nbases, d):
5457
# end metaclass
5558
returnmetaclass(meta.__name__+'Helper',None, {})
5659
# end handle py2
57-

‎git/objects/commit.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,25 @@ def _deserialize(self, stream):
419419
next_line=None
420420
whileTrue:
421421
parent_line=readline()
422-
ifnotparent_line.startswith('parent'):
422+
ifnotparent_line.startswith(b'parent'):
423423
next_line=parent_line
424424
break
425425
# END abort reading parents
426-
self.parents.append(type(self)(self.repo,hex_to_bin(parent_line.split()[-1])))
426+
self.parents.append(type(self)(self.repo,hex_to_bin(parent_line.split()[-1].decode('ascii'))))
427427
# END for each parent line
428428
self.parents=tuple(self.parents)
429429

430-
self.author,self.authored_date,self.author_tz_offset=parse_actor_and_date(next_line)
431-
self.committer,self.committed_date,self.committer_tz_offset=parse_actor_and_date(readline())
430+
# we don't know actual author encoding before we have parsed it, so keep the lines around
431+
author_line=next_line
432+
committer_line=readline()
432433

433434
# we might run into one or more mergetag blocks, skip those for now
434435
next_line=readline()
435-
whilenext_line.startswith('mergetag '):
436+
whilenext_line.startswith(b'mergetag '):
436437
next_line=readline()
437438
whilenext_line.startswith(' '):
438439
next_line=readline()
440+
# end skip mergetags
439441

440442
# now we can have the encoding line, or an empty line followed by the optional
441443
# message.
@@ -444,39 +446,40 @@ def _deserialize(self, stream):
444446
# read headers
445447
enc=next_line
446448
buf=enc.strip()
447-
whilebuf!="":
448-
ifbuf[0:10]=="encoding ":
449-
self.encoding=buf[buf.find(' ')+1:]
450-
elifbuf[0:7]=="gpgsig ":
451-
sig=buf[buf.find(' ')+1:]+"\n"
449+
whilebuf:
450+
ifbuf[0:10]==b"encoding ":
451+
self.encoding=buf[buf.find(' ')+1:].decode('ascii')
452+
elifbuf[0:7]==b"gpgsig ":
453+
sig=buf[buf.find(b' ')+1:]+b"\n"
452454
is_next_header=False
453455
whileTrue:
454456
sigbuf=readline()
455-
ifsigbuf=="":
457+
ifnotsigbuf:
456458
break
457-
ifsigbuf[0:1]!=" ":
459+
ifsigbuf[0:1]!=b" ":
458460
buf=sigbuf.strip()
459461
is_next_header=True
460462
break
461463
sig+=sigbuf[1:]
462-
self.gpgsig=sig.rstrip("\n")
464+
# end read all signature
465+
self.gpgsig=sig.rstrip(b"\n").decode('ascii')
463466
ifis_next_header:
464467
continue
465468
buf=readline().strip()
466-
467469
# decode the authors name
470+
468471
try:
469-
self.author.name=self.author.name.decode(self.encoding)
472+
self.author,self.authored_date,self.author_tz_offset= \
473+
parse_actor_and_date(author_line.decode(self.encoding))
470474
exceptUnicodeDecodeError:
471-
log.error("Failed to decode authorname '%s' using encoding %s",self.author.name,self.encoding,
475+
log.error("Failed to decode authorline '%s' using encoding %s",author_line,self.encoding,
472476
exc_info=True)
473-
# END handle author's encoding
474477

475-
# decode committer name
476478
try:
477-
self.committer.name=self.committer.name.decode(self.encoding)
479+
self.committer,self.committed_date,self.committer_tz_offset= \
480+
parse_actor_and_date(committer_line.decode(self.encoding))
478481
exceptUnicodeDecodeError:
479-
log.error("Failed to decode committername '%s' using encoding %s",self.committer.name,self.encoding,
482+
log.error("Failed to decode committerline '%s' using encoding %s",committer_line,self.encoding,
480483
exc_info=True)
481484
# END handle author's encoding
482485

@@ -488,6 +491,7 @@ def _deserialize(self, stream):
488491
exceptUnicodeDecodeError:
489492
log.error("Failed to decode message '%s' using encoding %s",self.message,self.encoding,exc_info=True)
490493
# END exception handling
494+
491495
returnself
492496

493497
#} END serializable implementation

‎git/objects/fun.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
"""Module with functions which are supposed to be as fast as possible"""
22
fromstatimportS_ISDIR
33
fromgit.compatimport (
4+
byte_ord,
5+
force_bytes,
6+
defenc,
47
xrange,
58
text_type
69
)
@@ -17,13 +20,13 @@ def tree_to_stream(entries, write):
1720
bit_mask=7# 3 bits set
1821

1922
forbinsha,mode,nameinentries:
20-
mode_str=''
23+
mode_str=b''
2124
foriinxrange(6):
2225
mode_str=chr(((mode>> (i*3))&bit_mask)+ord_zero)+mode_str
2326
# END for each 8 octal value
2427

2528
# git slices away the first octal if its zero
26-
ifmode_str[0]=='0':
29+
ifbyte_ord(mode_str[0])==ord_zero:
2730
mode_str=mode_str[1:]
2831
# END save a byte
2932

@@ -33,16 +36,16 @@ def tree_to_stream(entries, write):
3336
# According to my tests, this is exactly what git does, that is it just
3437
# takes the input literally, which appears to be utf8 on linux.
3538
ifisinstance(name,text_type):
36-
name=name.encode("utf8")
37-
write("%s %s\0%s"%(mode_str,name,binsha))
39+
name=name.encode(defenc)
40+
write(b''.join(mode_str,b' ',name,b'\0',binsha))
3841
# END for each item
3942

40-
4143
deftree_entries_from_data(data):
4244
"""Reads the binary representation of a tree and returns tuples of Tree items
43-
:param data: data block with tree data
45+
:param data: data block with tree data (as bytes)
4446
:return: list(tuple(binsha, mode, tree_relative_path), ...)"""
4547
ord_zero=ord('0')
48+
space_ord=ord(' ')
4649
len_data=len(data)
4750
i=0
4851
out=list()
@@ -52,10 +55,10 @@ def tree_entries_from_data(data):
5255
# read mode
5356
# Some git versions truncate the leading 0, some don't
5457
# The type will be extracted from the mode later
55-
whiledata[i]!=' ':
58+
whilebyte_ord(data[i])!=space_ord:
5659
# move existing mode integer up one level being 3 bits
5760
# and add the actual ordinal value of the character
58-
mode= (mode<<3)+ (ord(data[i])-ord_zero)
61+
mode= (mode<<3)+ (byte_ord(data[i])-ord_zero)
5962
i+=1
6063
# END while reading mode
6164

@@ -65,20 +68,17 @@ def tree_entries_from_data(data):
6568
# parse name, it is NULL separated
6669

6770
ns=i
68-
whiledata[i]!='\0':
71+
whilebyte_ord(data[i])!=0:
6972
i+=1
7073
# END while not reached NULL
7174

7275
# default encoding for strings in git is utf8
7376
# Only use the respective unicode object if the byte stream was encoded
7477
name=data[ns:i]
7578
try:
76-
name_enc=name.decode("utf-8")
79+
name=name.decode(defenc)
7780
exceptUnicodeDecodeError:
7881
pass
79-
else:
80-
iflen(name)>len(name_enc):
81-
name=name_enc
8282
# END handle encoding
8383

8484
# byte is NULL, get next 20

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp