-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_reader.go
More file actions
273 lines (219 loc) · 5.42 KB
/
tree_reader.go
File metadata and controls
273 lines (219 loc) · 5.42 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package gitlib
import (
"fmt"
"os"
"sort"
"strings"
)
type rawTreeEntry struct {
Mode string
Name string
Hash Hash
}
func GetTreeEntries(repoPath string, branch string, treePath string) ([]TreeEntry, error) {
st, treeHash, err := resolveTreeHashForPath(repoPath, branch, treePath)
if err != nil {
return nil, err
}
entries, err := readTreeEntries(st, treeHash)
if err != nil {
return nil, err
}
out := make([]TreeEntry, 0, len(entries))
for _, e := range entries {
typ := "file"
if e.Mode == treeModeDir {
typ = "dir"
}
out = append(out, TreeEntry{
Name: e.Name,
Type: typ,
})
}
sort.Slice(out, func(i, j int) bool {
return out[i].Name < out[j].Name
})
return out, nil
}
func GetTreeNames(repoPath string, branch string) ([]string, error) {
entries, err := GetTreeEntries(repoPath, branch, "")
if err != nil {
return nil, err
}
out := make([]string, 0, len(entries))
for _, e := range entries {
out = append(out, e.Name)
}
return out, nil
}
func ShowFile(repoPath string, branch string, path string) ([]byte, error) {
st, treeHash, err := openRepoStorageWithRootTree(repoPath, branch)
if err != nil {
return nil, err
}
cleanPath, err := normalizePath(path)
if err != nil {
return nil, err
}
entry, err := resolveTreeEntryForPath(st, treeHash, cleanPath)
if err != nil {
return nil, err
}
if entry.Mode == treeModeDir {
return nil, fmt.Errorf("%s: это директория", cleanPath)
}
obj, err := st.ReadLoose(entry.Hash)
if err != nil {
return nil, err
}
if obj.Type != ObjectBlob {
return nil, fmt.Errorf("%s: объект не является блобом", cleanPath)
}
return obj.Data, nil
}
func resolveRootTreeHash(repo *Repository, branch string) (Hash, error) {
st := repo.Storage()
commitHash, err := resolveCommitHash(repo, branch)
if err != nil {
return Hash{}, err
}
obj, err := st.ReadLoose(commitHash)
if err != nil {
return Hash{}, err
}
if obj.Type != ObjectCommit {
return Hash{}, fmt.Errorf("ссылка не указывает на коммит")
}
info := parseCommitData(obj.Data)
return ParseHash(info.Tree)
}
func openRepoStorageWithRootTree(repoPath, branch string) (*Storage, Hash, error) {
repo, err := Open(repoPath)
if err != nil {
return nil, Hash{}, err
}
st := repo.Storage()
treeHash, err := resolveRootTreeHash(repo, branch)
if err != nil {
return nil, Hash{}, err
}
return st, treeHash, nil
}
func resolveTreeHashForPath(repoPath, branch, treePath string) (*Storage, Hash, error) {
st, treeHash, err := openRepoStorageWithRootTree(repoPath, branch)
if err != nil {
return nil, Hash{}, err
}
cleanPath := strings.Trim(strings.TrimSpace(treePath), "/")
if cleanPath == "" {
return st, treeHash, nil
}
parts := strings.SplitSeq(cleanPath, "/")
for p := range parts {
entries, err := readTreeEntries(st, treeHash)
if err != nil {
return nil, Hash{}, err
}
found := false
for _, entry := range entries {
if entry.Name != p {
continue
}
if entry.Mode != treeModeDir {
return nil, Hash{}, os.ErrNotExist
}
treeHash = entry.Hash
found = true
break
}
if !found {
return nil, Hash{}, os.ErrNotExist
}
}
return st, treeHash, nil
}
func resolveTreeEntryForPath(st *Storage, rootTreeHash Hash, path string) (rawTreeEntry, error) {
cleanPath, err := normalizePath(path)
if err != nil {
return rawTreeEntry{}, err
}
treeHash := rootTreeHash
parts := strings.Split(cleanPath, "/")
for i, p := range parts {
entries, err := readTreeEntries(st, treeHash)
if err != nil {
return rawTreeEntry{}, err
}
matched, ok := findTreeEntryByName(entries, p)
if !ok {
return rawTreeEntry{}, os.ErrNotExist
}
if i == len(parts)-1 {
return matched, nil
}
if matched.Mode != treeModeDir {
return rawTreeEntry{}, os.ErrNotExist
}
treeHash = matched.Hash
}
return rawTreeEntry{}, os.ErrNotExist
}
func findTreeEntryByName(entries []rawTreeEntry, name string) (rawTreeEntry, bool) {
for _, entry := range entries {
if entry.Name == name {
return entry, true
}
}
return rawTreeEntry{}, false
}
func normalizePath(path string) (string, error) {
cleanPath := strings.Trim(strings.TrimSpace(path), "/")
if cleanPath == "" {
return "", fmt.Errorf("пустой путь")
}
return cleanPath, nil
}
func readTreeEntries(st *Storage, treeHash Hash) ([]rawTreeEntry, error) {
obj, err := st.ReadLoose(treeHash)
if err != nil {
return nil, err
}
if obj.Type != ObjectTree {
return nil, fmt.Errorf("объект %s не является деревом", treeHash.String())
}
data := obj.Data
entries := make([]rawTreeEntry, 0)
i := 0
for i < len(data) {
modeStart := i
for i < len(data) && data[i] != ' ' {
i++
}
if i >= len(data) {
return nil, fmt.Errorf("некорректное дерево: нет разделителя режима")
}
mode := string(data[modeStart:i])
i++
nameStart := i
for i < len(data) && data[i] != 0 {
i++
}
if i >= len(data) {
return nil, fmt.Errorf("некорректное дерево: нет терминатора имени")
}
name := string(data[nameStart:i])
i++
if i+HashSize > len(data) {
return nil, fmt.Errorf("некорректное дерево: обрезанный хеш")
}
var h Hash
copy(h[:], data[i:i+HashSize])
i += HashSize
entries = append(entries, rawTreeEntry{
Mode: mode,
Name: name,
Hash: h,
})
}
return entries, nil
}