| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,12 +27,15 @@ | |||
| 27 | 27 | if PY3: | |
| 28 | 28 | import io | |
| 29 | 29 | FileType = io.IOBase | |
| 30 | + def byte_ord(b): | ||
| 31 | + return b | ||
| 30 | 32 | else: | |
| 31 | 33 | FileType = file | |
| 32 | 34 | # usually, this is just ascii, which might not enough for our encoding needs | |
| 33 | 35 | # Unless it's set specifically, we override it to be utf-8 | |
| 34 | 36 | if defenc == 'ascii': | |
| 35 | 37 | defenc = 'utf-8' | |
| 38 | + byte_ord = ord | ||
| 36 | 39 | ||
| 37 | 40 | ||
| 38 | 41 | def with_metaclass(meta, *bases): | |
@@ -54,4 +57,3 @@ def __new__(cls, name, nbases, d): | |||
| 54 | 57 | # end metaclass | |
| 55 | 58 | return metaclass(meta.__name__ + 'Helper', None, {}) | |
| 56 | 59 | # end handle py2 | |
| 57 | - | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -419,23 +419,25 @@ def _deserialize(self, stream): | |||
| 419 | 419 | next_line = None | |
| 420 | 420 | while True: | |
| 421 | 421 | parent_line = readline() | |
| 422 | - if not parent_line.startswith('parent'): | ||
| 422 | + if not parent_line.startswith(b'parent'): | ||
| 423 | 423 | next_line = parent_line | |
| 424 | 424 | break | |
| 425 | 425 | # 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')))) | ||
| 427 | 427 | # END for each parent line | |
| 428 | 428 | self.parents = tuple(self.parents) | |
| 429 | 429 | ||
| 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() | ||
| 432 | 433 | ||
| 433 | 434 | # we might run into one or more mergetag blocks, skip those for now | |
| 434 | 435 | next_line = readline() | |
| 435 | - while next_line.startswith('mergetag '): | ||
| 436 | + while next_line.startswith(b'mergetag '): | ||
| 436 | 437 | next_line = readline() | |
| 437 | 438 | while next_line.startswith(' '): | |
| 438 | 439 | next_line = readline() | |
| 440 | + # end skip mergetags | ||
| 439 | 441 | ||
| 440 | 442 | # now we can have the encoding line, or an empty line followed by the optional | |
| 441 | 443 | # message. | |
@@ -444,39 +446,40 @@ def _deserialize(self, stream): | |||
| 444 | 446 | # read headers | |
| 445 | 447 | enc = next_line | |
| 446 | 448 | buf = enc.strip() | |
| 447 | - while buf != "": | ||
| 448 | - if buf[0:10] == "encoding ": | ||
| 449 | - self.encoding = buf[buf.find(' ') + 1:] | ||
| 450 | - elif buf[0:7] == "gpgsig ": | ||
| 451 | - sig = buf[buf.find(' ') + 1:] + "\n" | ||
| 449 | + while buf: | ||
| 450 | + if buf[0:10] == b"encoding ": | ||
| 451 | + self.encoding = buf[buf.find(' ') + 1:].decode('ascii') | ||
| 452 | + elif buf[0:7] == b"gpgsig ": | ||
| 453 | + sig = buf[buf.find(b' ') + 1:] + b"\n" | ||
| 452 | 454 | is_next_header = False | |
| 453 | 455 | while True: | |
| 454 | 456 | sigbuf = readline() | |
| 455 | - if sigbuf == "": | ||
| 457 | + if not sigbuf: | ||
| 456 | 458 | break | |
| 457 | - if sigbuf[0:1] != " ": | ||
| 459 | + if sigbuf[0:1] != b" ": | ||
| 458 | 460 | buf = sigbuf.strip() | |
| 459 | 461 | is_next_header = True | |
| 460 | 462 | break | |
| 461 | 463 | sig += sigbuf[1:] | |
| 462 | - self.gpgsig = sig.rstrip("\n") | ||
| 464 | + # end read all signature | ||
| 465 | + self.gpgsig = sig.rstrip(b"\n").decode('ascii') | ||
| 463 | 466 | if is_next_header: | |
| 464 | 467 | continue | |
| 465 | 468 | buf = readline().strip() | |
| 466 | - | ||
| 467 | 469 | # decode the authors name | |
| 470 | + | ||
| 468 | 471 | 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)) | ||
| 470 | 474 | except UnicodeDecodeError: | |
| 471 | - log.error("Failed to decode author name '%s' using encoding %s", self.author.name, self.encoding, | ||
| 475 | + log.error("Failed to decode author line '%s' using encoding %s", author_line, self.encoding, | ||
| 472 | 476 | exc_info=True) | |
| 473 | - # END handle author's encoding | ||
| 474 | 477 | ||
| 475 | - # decode committer name | ||
| 476 | 478 | 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)) | ||
| 478 | 481 | except UnicodeDecodeError: | |
| 479 | - log.error("Failed to decode committer name '%s' using encoding %s", self.committer.name, self.encoding, | ||
| 482 | + log.error("Failed to decode committer line '%s' using encoding %s", committer_line, self.encoding, | ||
| 480 | 483 | exc_info=True) | |
| 481 | 484 | # END handle author's encoding | |
| 482 | 485 | ||
@@ -488,6 +491,7 @@ def _deserialize(self, stream): | |||
| 488 | 491 | except UnicodeDecodeError: | |
| 489 | 492 | log.error("Failed to decode message '%s' using encoding %s", self.message, self.encoding, exc_info=True) | |
| 490 | 493 | # END exception handling | |
| 494 | + | ||
| 491 | 495 | return self | |
| 492 | 496 | ||
| 493 | 497 | #} END serializable implementation | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,9 @@ | |||
| 1 | 1 | """Module with functions which are supposed to be as fast as possible""" | |
| 2 | 2 | from stat import S_ISDIR | |
| 3 | 3 | from git.compat import ( | |
| 4 | + byte_ord, | ||
| 5 | + force_bytes, | ||
| 6 | + defenc, | ||
| 4 | 7 | xrange, | |
| 5 | 8 | text_type | |
| 6 | 9 | ) | |
@@ -17,13 +20,13 @@ def tree_to_stream(entries, write): | |||
| 17 | 20 | bit_mask = 7 # 3 bits set | |
| 18 | 21 | ||
| 19 | 22 | for binsha, mode, name in entries: | |
| 20 | - mode_str = '' | ||
| 23 | + mode_str = b'' | ||
| 21 | 24 | for i in xrange(6): | |
| 22 | 25 | mode_str = chr(((mode >> (i * 3)) & bit_mask) + ord_zero) + mode_str | |
| 23 | 26 | # END for each 8 octal value | |
| 24 | 27 | ||
| 25 | 28 | # git slices away the first octal if its zero | |
| 26 | - if mode_str[0] == '0': | ||
| 29 | + if byte_ord(mode_str[0]) == ord_zero: | ||
| 27 | 30 | mode_str = mode_str[1:] | |
| 28 | 31 | # END save a byte | |
| 29 | 32 | ||
@@ -33,16 +36,16 @@ def tree_to_stream(entries, write): | |||
| 33 | 36 | # According to my tests, this is exactly what git does, that is it just | |
| 34 | 37 | # takes the input literally, which appears to be utf8 on linux. | |
| 35 | 38 | if isinstance(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)) | ||
| 38 | 41 | # END for each item | |
| 39 | 42 | ||
| 40 | - | ||
| 41 | 43 | def tree_entries_from_data(data): | |
| 42 | 44 | """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) | ||
| 44 | 46 | :return: list(tuple(binsha, mode, tree_relative_path), ...)""" | |
| 45 | 47 | ord_zero = ord('0') | |
| 48 | + space_ord = ord(' ') | ||
| 46 | 49 | len_data = len(data) | |
| 47 | 50 | i = 0 | |
| 48 | 51 | out = list() | |
@@ -52,10 +55,10 @@ def tree_entries_from_data(data): | |||
| 52 | 55 | # read mode | |
| 53 | 56 | # Some git versions truncate the leading 0, some don't | |
| 54 | 57 | # The type will be extracted from the mode later | |
| 55 | - while data[i] != ' ': | ||
| 58 | + while byte_ord(data[i]) != space_ord: | ||
| 56 | 59 | # move existing mode integer up one level being 3 bits | |
| 57 | 60 | # 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) | ||
| 59 | 62 | i += 1 | |
| 60 | 63 | # END while reading mode | |
| 61 | 64 | ||
@@ -65,20 +68,17 @@ def tree_entries_from_data(data): | |||
| 65 | 68 | # parse name, it is NULL separated | |
| 66 | 69 | ||
| 67 | 70 | ns = i | |
| 68 | - while data[i] != '\0': | ||
| 71 | + while byte_ord(data[i]) != 0: | ||
| 69 | 72 | i += 1 | |
| 70 | 73 | # END while not reached NULL | |
| 71 | 74 | ||
| 72 | 75 | # default encoding for strings in git is utf8 | |
| 73 | 76 | # Only use the respective unicode object if the byte stream was encoded | |
| 74 | 77 | name = data[ns:i] | |
| 75 | 78 | try: | |
| 76 | - name_enc = name.decode("utf-8") | ||
| 79 | + name = name.decode(defenc) | ||
| 77 | 80 | except UnicodeDecodeError: | |
| 78 | 81 | pass | |
| 79 | - else: | ||
| 80 | - if len(name) > len(name_enc): | ||
| 81 | - name = name_enc | ||
| 82 | 82 | # END handle encoding | |
| 83 | 83 | ||
| 84 | 84 | # byte is NULL, get next 20 | |
| Back | FazBrowse Home | New Git URL |
0 commit comments