-
Notifications
You must be signed in to change notification settings - Fork 375
/
repo_submodule.go
98 lines (92 loc) · 2.41 KB
/
repo_submodule.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
90
91
92
93
94
95
96
97
98
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gitmodule
import (
"bufio"
"errors"
"fmt"
"strings"
)
type SubModuleData struct {
UrlMap map[string]string
Exist bool
}
func (repo *Repository) GetSubmodules(commitID string) (map[string]string, error) {
key := fmt.Sprintf("%v-%v", repo.ID, commitID)
var submoduleData SubModuleData
err := Setting.RepoSubmoduleCache.Get(key, &submoduleData)
//有缓存但是为空,直接返回
if err == nil && submoduleData.Exist == false {
if submoduleData.Exist {
return submoduleData.UrlMap, nil
} else {
return nil, errors.New("submodule not found")
}
}
treeEntry, err := repo.GetTreeEntryByPath(commitID, ".gitmodules")
if err == ERROR_PATH_NOT_FOUND {
Setting.RepoSubmoduleCache.Set(key, SubModuleData{
UrlMap: nil,
Exist: false,
})
return nil, err
}
if err != nil {
return nil, err
}
rd, err := treeEntry.Blob().Data()
if err != nil {
return nil, err
}
submoduleMap := map[string]string{}
scanner := bufio.NewScanner(rd)
var ismodule bool
var path string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "[submodule") {
ismodule = true
continue
}
if ismodule {
fields := strings.Split(scanner.Text(), "=")
k := strings.TrimSpace(fields[0])
if k == "path" {
path = strings.TrimSpace(fields[1])
} else if k == "url" {
submoduleMap[path] = strings.TrimSpace(fields[1])
ismodule = false
}
}
}
Setting.RepoSubmoduleCache.Set(key, SubModuleData{
UrlMap: submoduleMap,
Exist: true,
})
return submoduleMap, nil
}
func (repo *Repository) FillSubmoduleInfo(commitID string, entries ...*TreeEntry) {
submodules, err := repo.GetSubmodules(commitID)
if err != nil {
return
}
for _, entry := range entries {
if entry.Type == OBJECT_COMMIT {
if err == nil {
url, ok := submodules[entry.Name]
if ok {
entry.Url = url
}
}
}
}
}