def parse_tree(blob, path=b''):
"""Parses a git tree"""
res = []
leng, _, tree = blob.partition(b'\0')
if path is str:
path = path.encode('utf-8')
if not tree:
return Result.Err('Invalid tree')
while len(tree) > 0:
mode, _, tree = tree.partition(b' ')
name, _, tree = tree.partition(b'\0')
sha = b16encode(tree[0:20]).decode('utf-8').lower()
tree = tree[20:]
if not name or not sha:
return Result.Err()
is_dir = mode[0:1] != b'1'
res.append((is_dir, sha, path + b'/' + name))
return Result.Ok(res)
评论列表
文章目录