/
tree_utils.go
50 lines (48 loc) · 1.13 KB
/
tree_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package git
import (
"bytes"
"errors"
)
// Parse tree information from the (uncompressed) raw
// data from the tree object.
func parseTreeData(tree *Tree, data []byte) ([]*TreeEntry, error) {
entries := make([]*TreeEntry, 0, 10)
l := len(data)
pos := 0
for pos < l {
entry := new(TreeEntry)
entry.ptree = tree
spacepos := bytes.IndexByte(data[pos:], ' ')
switch string(data[pos : pos+spacepos]) {
case "100644":
entry.mode = ModeBlob
entry.Type = ObjectBlob
case "100755":
entry.mode = ModeExec
entry.Type = ObjectBlob
case "120000":
entry.mode = ModeSymlink
entry.Type = ObjectBlob
case "160000":
entry.mode = ModeCommit
entry.Type = ObjectCommit
case "40000":
entry.mode = ModeTree
entry.Type = ObjectTree
default:
return nil, errors.New("unknown type: " + string(data[pos:pos+spacepos]))
}
pos += spacepos + 1
zero := bytes.IndexByte(data[pos:], 0)
entry.name = string(data[pos : pos+zero])
pos += zero + 1
id, err := NewId(data[pos : pos+20])
if err != nil {
return nil, err
}
entry.Id = id
pos = pos + 20
entries = append(entries, entry)
}
return entries, nil
}