Repository navigation
Expand file tree
/
Copy pathsubmodule_update.go
More file actions
199 lines (180 loc) · 5.72 KB
/
Copy pathsubmodule_update.go
File metadata and controls
199 lines (180 loc) · 5.72 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package clone
import (
"context"
"fmt"
"net/url"
"regexp"
"slices"
"strings"
)
// SubmoduleIssueReason says why a declared submodule is missing after
// EnsureWithOptions.
type SubmoduleIssueReason string
const (
SubmoduleIssueFetchFailed SubmoduleIssueReason = "fetch_failed"
SubmoduleIssueDepthLimit SubmoduleIssueReason = "depth_limit"
SubmoduleIssueModuleLimit SubmoduleIssueReason = "module_limit"
)
// SubmoduleIssue describes a submodule the best-effort submodule step left
// out. Path is relative to the top-level checkout. URL omits userinfo and is
// empty when it could not be read. Output is Git's output for a failed fetch,
// with URL userinfo redacted. Submodules nested beneath a skipped submodule
// are not reported.
type SubmoduleIssue struct {
Path string
URL string
Reason SubmoduleIssueReason
Output string
}
type credentialPrefix struct {
host string
path string
}
// ValidateSubmoduleCredentialURLs reports the first entry EnsureOptions would
// reject in SubmoduleCredentialURLs, so callers can check configuration
// before cloning.
func ValidateSubmoduleCredentialURLs(urls []string) error {
_, err := parseCredentialPrefixes(urls)
return err
}
func parseCredentialPrefixes(raw []string) ([]credentialPrefix, error) {
prefixes := make([]credentialPrefix, 0, len(raw))
for i, entry := range raw {
u, ok := strictHTTPSURL(entry)
if !ok {
// The entry itself is not echoed: it may hold a credential that
// RedactURL cannot find in an unparseable URL.
return nil, fmt.Errorf("invalid submodule credential URL at index %d: must be https with no userinfo, query, fragment, percent escapes, or dot segments", i)
}
prefixes = append(prefixes, credentialPrefix{
host: strings.ToLower(u.Host),
path: strings.TrimSuffix(u.Path, "/"),
})
}
return prefixes, nil
}
// strictHTTPSURL parses raw and rejects anything Git might resolve to a
// different repository than the literal path suggests.
func strictHTTPSURL(raw string) (*url.URL, bool) {
// url.Parse decodes canonical escapes without recording them, so a %
// anywhere could hide a segment the server decodes differently.
if ValidateURL(raw) != nil || strings.ContainsAny(raw, "\\ %") {
return nil, false
}
u, err := url.Parse(raw)
if err != nil || u.User != nil || u.Opaque != "" || u.RawPath != "" ||
u.ForceQuery || u.RawQuery != "" || u.Fragment != "" {
return nil, false
}
segments := strings.Split(strings.TrimSuffix(u.Path, "/"), "/")
for i, segment := range segments {
if segment == "." || segment == ".." || (segment == "" && i > 0) {
return nil, false
}
}
return u, true
}
func credentialAllowed(prefixes []credentialPrefix, raw string) bool {
u, ok := strictHTTPSURL(raw)
if !ok {
return false
}
host := strings.ToLower(u.Host)
return slices.ContainsFunc(prefixes, func(p credentialPrefix) bool {
return host == p.host && (u.Path == p.path || strings.HasPrefix(u.Path, p.path+"/"))
})
}
type submoduleUpdate struct {
budget submoduleBudget
trusted []credentialPrefix
onIssue func(SubmoduleIssue)
}
type pendingSubmodules struct {
dir string
prefix string // slash path of dir relative to the top-level checkout
depth int // depth of the submodules declared in dir
}
func (u submoduleUpdate) reportLinks(ctx context.Context, policy Retry, p pendingSubmodules, links []gitlink, reason SubmoduleIssueReason, output string) {
if u.onIssue == nil {
return
}
for _, link := range links {
u.onIssue(SubmoduleIssue{
Path: joinSlash(p.prefix, link.path),
URL: submoduleIssueURL(ctx, policy, p.dir, link.name),
Reason: reason,
Output: redactOutput(output),
})
}
}
// reportSkipped reports links the budget left unfetched, except those a
// previous update already checked out at their gitlink.
func (u submoduleUpdate) reportSkipped(ctx context.Context, policy Retry, p pendingSubmodules, links []gitlink, reason SubmoduleIssueReason) error {
if u.onIssue == nil {
return nil
}
var missing []gitlink
for _, link := range links {
ok, err := checkedOutAt(ctx, policy, pathFromGit(p.dir, link.path), link.commit)
if err != nil {
return err
}
if !ok {
missing = append(missing, link)
}
}
u.reportLinks(ctx, policy, p, missing, reason, "")
return ctx.Err()
}
// reportPending reports the submodules declared by each pending directory as
// skipped for reason.
func (u submoduleUpdate) reportPending(ctx context.Context, policy Retry, queue []pendingSubmodules, reason SubmoduleIssueReason) error {
if u.onIssue == nil {
return nil
}
for _, p := range queue {
links, err := submoduleGitlinks(ctx, policy, p.dir)
if err != nil {
return err
}
if err := u.reportSkipped(ctx, policy, p, links, reason); err != nil {
return err
}
}
return nil
}
func submoduleURLConfig(ctx context.Context, policy Retry, dir, name string) string {
out, err := policy.Run(ctx, dir, nil, "config", "-z", "--get", "submodule."+name+".url")
if err != nil {
return ""
}
return strings.TrimSuffix(out, "\x00")
}
func submoduleIssueURL(ctx context.Context, policy Retry, dir, name string) string {
raw := submoduleURLConfig(ctx, policy, dir, name)
if raw == "" {
out, err := policy.Run(ctx, dir, nil, "config", "-z", "--blob", gitmodulesBlob, "--get", "submodule."+name+".url")
if err != nil {
return ""
}
raw = strings.TrimSuffix(out, "\x00")
}
u, err := url.Parse(raw)
if err != nil {
return ""
}
u.User = nil
return u.String()
}
// Greedy so a password containing @ is consumed through the last @ before
// the host.
var outputUserinfo = regexp.MustCompile(`://[^/\s]*@`)
func redactOutput(out string) string {
return outputUserinfo.ReplaceAllString(strings.TrimSpace(out), "://REDACTED@")
}
func joinSlash(prefix, p string) string {
if prefix == "" {
return p
}
return prefix + "/" + p
}