-
Notifications
You must be signed in to change notification settings - Fork 128
/
tree_blob.go
87 lines (75 loc) · 1.69 KB
/
tree_blob.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
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
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package git
import (
"path"
"strings"
)
// TreeEntry returns the TreeEntry by given subpath of the tree.
func (t *Tree) TreeEntry(subpath string, opts ...LsTreeOptions) (*TreeEntry, error) {
if len(subpath) == 0 {
return &TreeEntry{
id: t.id,
typ: ObjectTree,
mode: EntryTree,
}, nil
}
subpath = path.Clean(subpath)
paths := strings.Split(subpath, "/")
var err error
tree := t
for i, name := range paths {
// Reached end of the loop
if i == len(paths)-1 {
entries, err := tree.Entries(opts...)
if err != nil {
return nil, err
}
for _, v := range entries {
if v.name == name {
return v, nil
}
}
} else {
tree, err = tree.Subtree(name, opts...)
if err != nil {
return nil, err
}
}
}
return nil, ErrRevisionNotExist
}
// Blob returns the blob object by given subpath of the tree.
func (t *Tree) Blob(subpath string, opts ...LsTreeOptions) (*Blob, error) {
e, err := t.TreeEntry(subpath, opts...)
if err != nil {
return nil, err
}
if e.IsBlob() || e.IsExec() {
return e.Blob(), nil
}
return nil, ErrNotBlob
}
// BlobByIndex returns blob object by given index.
func (t *Tree) BlobByIndex(index string) (*Blob, error) {
typ, err := t.repo.CatFileType(index)
if err != nil {
return nil, err
}
if typ != ObjectBlob {
return nil, ErrNotBlob
}
id, err := t.repo.RevParse(index)
if err != nil {
return nil, err
}
return &Blob{
TreeEntry: &TreeEntry{
mode: EntryBlob,
typ: ObjectBlob,
id: MustIDFromString(id),
parent: t,
},
}, nil
}