Skip to content

Commit 3e14ab5

Browse files
committed
git.Tree: Fixed critical issue when reading trees from binary data. The previous version was making assumptions that would only be true for old git repositories it sesms. The new version of the algorithm deals with this gracefully.
1 parent 4375386 commit 3e14ab5

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/git/objects/tree.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class Tree(base.IndexObject, diff.Diffable, utils.Traversable):
4444
commit_id = 016
4545
blob_id = 010
4646
symlink_id = 012
47-
tree_id = 040
47+
tree_id = 004
4848

4949

5050
def __init__(self, repo, sha, mode=0, path=None):
@@ -97,18 +97,17 @@ def _iter_from_data(self):
9797
i = 0
9898
while i < len_data:
9999
mode = 0
100-
mode_boundary = i + 6
101-
102-
# read type
103-
type_id = ((ord(data[i])-ord_zero)<<3) + (ord(data[i+1])-ord_zero)
104-
i += 2
105100

101+
# read mode
102+
# Some git versions truncate the leading 0, some don't
103+
# The type will be extracted from the mode later
106104
while data[i] != ' ':
107105
# move existing mode integer up one level being 3 bits
108106
# and add the actual ordinal value of the character
109107
mode = (mode << 3) + (ord(data[i]) - ord_zero)
110108
i += 1
111109
# END while reading mode
110+
type_id = mode >> 12
112111

113112
# byte is space now, skip it
114113
i += 1
@@ -127,7 +126,6 @@ def _iter_from_data(self):
127126
sha = data[i:i+20]
128127
i = i + 20
129128

130-
mode |= type_id<<12
131129
hexsha = sha_to_hex(sha)
132130
if type_id == self.blob_id or type_id == self.symlink_id:
133131
yield blob.Blob(self.repo, hexsha, mode, path)
@@ -137,7 +135,7 @@ def _iter_from_data(self):
137135
# todo
138136
yield None
139137
else:
140-
raise TypeError( "Unknown type found in tree data: %i" % type_id )
138+
raise TypeError( "Unknown type found in tree data %i for path '%s'" % (type_id, path))
141139
# END for each byte in data stream
142140

143141

0 commit comments

Comments
 (0)