aboutsummaryrefslogtreecommitdiff
path: root/obj_tree.go
blob: 7b04b231b118545f4a5590c05f3ffb59d3d65d93 (about) (plain) (blame)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package furgit

import (
	"bytes"
	"errors"
	"fmt"
	"strconv"
)

// Tree represents a Git tree object.
type Tree struct {
	Entries []TreeEntry
}

// StoredTree represents a tree stored in the object database.
type StoredTree struct {
	Tree
	hash Hash
}

// Hash returns the hash of the stored tree.
func (sTree *StoredTree) Hash() Hash {
	return sTree.hash
}

// TreeEntry represents a single entry in a Git tree.
type TreeEntry struct {
	Mode uint32
	Name []byte
	ID   Hash
}

// ObjectType returns the object type of the tree.
//
// It always returns ObjectTypeTree.
func (tree *Tree) ObjectType() ObjectType {
	_ = tree
	return ObjectTypeTree
}

// parseTree decodes a tree body.
func parseTree(id Hash, body []byte, repo *Repository) (*StoredTree, error) {
	var entries []TreeEntry
	i := 0
	for i < len(body) {
		space := bytes.IndexByte(body[i:], ' ')
		if space < 0 {
			return nil, errors.New("furgit: tree: missing mode terminator")
		}
		modeBytes := body[i : i+space]
		i += space + 1

		nul := bytes.IndexByte(body[i:], 0)
		if nul < 0 {
			return nil, errors.New("furgit: tree: missing name terminator")
		}
		nameBytes := body[i : i+nul]
		i += nul + 1

		if i+repo.hashSize > len(body) {
			return nil, errors.New("furgit: tree: truncated child hash")
		}
		var child Hash
		copy(child.data[:], body[i:i+repo.hashSize])
		child.size = repo.hashSize
		i += repo.hashSize

		mode, err := strconv.ParseUint(string(modeBytes), 8, 32)
		if err != nil {
			return nil, fmt.Errorf("furgit: tree: parse mode: %w", err)
		}

		entry := TreeEntry{
			Mode: uint32(mode),
			Name: append([]byte(nil), nameBytes...),
			ID:   child,
		}
		entries = append(entries, entry)
	}

	return &StoredTree{
		hash: id,
		Tree: Tree{
			Entries: entries,
		},
	}, nil
}

// treeBody builds the entry list for a tree without the Git header.
func treeBody(t *Tree) []byte {
	var bodyLen int
	for _, e := range t.Entries {
		mode := strconv.FormatUint(uint64(e.Mode), 8)
		bodyLen += len(mode) + 1 + len(e.Name) + 1 + e.ID.size
	}

	body := make([]byte, bodyLen)
	pos := 0
	for _, e := range t.Entries {
		mode := strconv.FormatUint(uint64(e.Mode), 8)
		pos += copy(body[pos:], []byte(mode))
		body[pos] = ' '
		pos++
		pos += copy(body[pos:], e.Name)
		body[pos] = 0
		pos++
		pos += copy(body[pos:], e.ID.data[:e.ID.size])
	}

	return body
}

// Serialize renders the tree into its raw byte representation,
// including the header (i.e., "type size\0").
func (tree *Tree) Serialize() ([]byte, error) {
	body := treeBody(tree)
	header, err := headerForType(ObjectTypeTree, body)
	if err != nil {
		return nil, err
	}

	raw := make([]byte, len(header)+len(body))
	copy(raw, header)
	copy(raw[len(header):], body)
	return raw, nil
}

// Entry looks up a tree entry by name.
//
// Lookups are not recursive.
// It returns nil if no such entry exists.
func (tree *Tree) Entry(name []byte) *TreeEntry {
	low, high := 0, len(tree.Entries)-1
	for low <= high {
		mid := (low + high) / 2
		cmp := bytes.Compare(tree.Entries[mid].Name, name)
		if cmp == 0 {
			return &tree.Entries[mid]
		} else if cmp < 0 {
			low = mid + 1
		} else {
			high = mid - 1
		}
	}
	return nil
}

// EntryRecursive looks up a tree entry by path.
//
// Lookups are recursive.
// It returns nil if no such entry exists.
func (tree *Tree) EntryRecursive(repo *Repository, path [][]byte) (*TreeEntry, error) {
	if len(path) == 0 {
		return nil, errors.New("furgit: tree: empty path")
	}

	currentTree := tree
	for i, part := range path {
		entry := currentTree.Entry(part)
		if entry == nil {
			return nil, nil
		}
		if i == len(path)-1 {
			return entry, nil
		}
		obj, err := repo.ReadObject(entry.ID)
		if err != nil {
			return nil, err
		}
		nextTree, ok := obj.(*Tree)
		if !ok {
			return nil, fmt.Errorf("furgit: tree: expected tree object at %s, got %T", part, obj)
			// TODO: It may be useful to check the mode instead of reporting
			// an object type error.
		}
		currentTree = nextTree
	}

	return nil, nil
}