-
Notifications
You must be signed in to change notification settings - Fork 375
/
repo_tree.go
89 lines (78 loc) · 1.74 KB
/
repo_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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.
// +build !codeanalysis
package gitmodule
import (
"path"
git "github.com/libgit2/git2go/v30"
)
// Find the tree object in the repository.
func (repo *Repository) GetTree(idStr string) (*Tree, error) {
rawRepo, err := repo.GetRawRepo()
if err != nil {
return nil, err
}
oid, err := git.NewOid(idStr)
if err != nil {
return nil, err
}
tree, err := rawRepo.LookupTree(oid)
if err != nil {
return nil, err
}
return &Tree{
ID: tree.Id().String(),
Repo: repo,
}, nil
}
// Find the tree object in the repository.
func (repo *Repository) GetTreeEntryByPath(idStr string, path string) (*TreeEntry, error) {
commit, err := repo.GetCommit(idStr)
if err != nil {
return nil, err
}
rootTreeId := commit.TreeSha
tree, err := repo.GetTree(rootTreeId)
if err != nil {
return nil, err
}
return tree.GetTreeEntryByPath(path)
}
func (repo *Repository) FillTreeEntriesCommitInfo(id string, replath string, entries ...*TreeEntry) error {
if len(entries) == 0 {
return nil
}
maxConcurrency := 5
revChan := make(chan commitInfo, maxConcurrency)
doneChan := make(chan error)
go func() {
i := 0
for info := range revChan {
if info.err != nil {
doneChan <- info.err
return
}
i++
if i == len(entries) {
break
}
}
doneChan <- nil
}()
for _, entry := range entries {
go func(entry *TreeEntry) {
commit, err := repo.getCommitByPathWithID(id, path.Join(replath, entry.Name))
if err == nil {
entry.Commit = commit
}
revChan <- commitInfo{
err: err,
}
}(entry)
}
if err := <-doneChan; err != nil {
return err
}
return nil
}