Skip to content

Commit bda18e0

Browse files
authored
Merge pull request #2261 from SashaMIT/decode-byte-mode
objects: decode a byte mode before reading its digits
2 parents 2d7f566 + 62266da commit bda18e0

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

‎git/objects/util.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,11 @@ def mode_str_to_int(modestr: Union[bytes, str]) -> int:
100100
module regarding the rwx permissions for user, group and other, special flags
101101
and file system flags, such as whether it is a symlink.
102102
"""
103+
if isinstance(modestr, bytes):
104+
# A byte is an int. int(b"4"[0]) is 52, so b"100644" became 0o6767524.
105+
modestr = modestr.decode("ascii")
103106
mode = 0
104107
for iteration, char in enumerate(reversed(modestr[-6:])):
105-
char = cast(Union[str, int], char)
106108
mode += int(char) << iteration * 3
107109
# END for each char
108110
return mode

‎test/test_util.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from git.objects.util import (
2323
altz_to_utctz_str,
2424
from_timestamp,
25+
mode_str_to_int,
2526
parse_actor_and_date,
2627
parse_date,
2728
tzoffset,
@@ -760,3 +761,10 @@ def test_remove_password_from_command_line(self):
760761
redacted_cmd_6 = remove_password_if_present(cmd_6)
761762
assert authorization not in " ".join(redacted_cmd_6)
762763
assert "http.extraHeader=Authorization: *****" in redacted_cmd_6
764+
765+
766+
def test_mode_str_to_int_accepts_bytes():
767+
assert mode_str_to_int("100644") == 0o100644
768+
assert mode_str_to_int(b"100644") == 0o100644
769+
assert mode_str_to_int("644") == 0o644
770+
assert mode_str_to_int(b"120000") == 0o120000

0 commit comments

Comments
 (0)