-
Notifications
You must be signed in to change notification settings - Fork 128
/
tree.go
69 lines (58 loc) · 1.2 KB
/
tree.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
// 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 (
"strings"
"sync"
)
// Tree represents a flat directory listing in Git.
type Tree struct {
id *SHA1
parent *Tree
repo *Repository
entries Entries
entriesOnce sync.Once
entriesErr error
}
// Subtree returns a subtree by given subpath of the tree.
func (t *Tree) Subtree(subpath string, opts ...LsTreeOptions) (*Tree, error) {
if len(subpath) == 0 {
return t, nil
}
paths := strings.Split(subpath, "/")
var (
err error
g = t
p = t
e *TreeEntry
)
for _, name := range paths {
e, err = p.TreeEntry(name, opts...)
if err != nil {
return nil, err
}
g = &Tree{
id: e.id,
parent: p,
repo: t.repo,
}
p = g
}
return g, nil
}
// Entries returns all entries of the tree.
func (t *Tree) Entries(opts ...LsTreeOptions) (Entries, error) {
t.entriesOnce.Do(func() {
if t.entries != nil {
return
}
var tt *Tree
tt, t.entriesErr = t.repo.LsTree(t.id.String(), opts...)
if t.entriesErr != nil {
return
}
t.entries = tt.entries
})
return t.entries, t.entriesErr
}