-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit_info.go
86 lines (75 loc) · 2.14 KB
/
commit_info.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
package git
import (
"fmt"
"path"
"path/filepath"
)
type commitInfo struct {
entryName string
infos []interface{}
err error
}
// GetCommitsInfo takes advantages of concurrey to speed up getting information
// of all commits that are corresponding to these entries.
// TODO: limit max goroutines at same time
// NOTE: basically this is a nonsense stuff because public method returns all-private data structure
// we should trace it back to gogs and investigate the path of those return values
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) {
if len(tes) == 0 {
return nil, nil
}
revChan := make(chan commitInfo, 10)
infoMap := make(map[string][]interface{}, len(tes))
for i := range tes {
if tes[i].Type != OBJECT_COMMIT {
go func(i int) {
cinfo := commitInfo{entryName: tes[i].Name()}
c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name()))
if err != nil {
cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err)
} else {
cinfo.infos = []interface{}{tes[i], c}
}
revChan <- cinfo
}(i)
continue
}
// Handle submodule
go func(i int) {
cinfo := commitInfo{entryName: tes[i].Name()}
sm, err := commit.GetSubModule(path.Join(treePath, tes[i].Name()))
if err != nil && !IsErrNotExist(err) {
cinfo.err = fmt.Errorf("GetSubModule (%s/%s): %v", treePath, tes[i].Name(), err)
revChan <- cinfo
return
}
smUrl := ""
if sm != nil {
smUrl = sm.Url
}
c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name()))
if err != nil {
cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err)
} else {
cinfo.infos = []interface{}{tes[i], NewSubModuleFile(c, smUrl, tes[i].ID.String())}
}
revChan <- cinfo
}(i)
}
i := 0
for info := range revChan {
if info.err != nil {
return nil, info.err
}
infoMap[info.entryName] = info.infos
i++
if i == len(tes) {
break
}
}
commitsInfo := make([][]interface{}, len(tes))
for i := 0; i < len(tes); i++ {
commitsInfo[i] = infoMap[tes[i].Name()]
}
return commitsInfo, nil
}