Repository navigation
Expand file tree
/
Copy pathload.go
More file actions
467 lines (432 loc) · 13 KB
/
Copy pathload.go
File metadata and controls
467 lines (432 loc) · 13 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package gitconfig
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
)
// Limits of one load (requirement X3 of docs/REQUIREMENTS.md): git's
// include depth, and bounds on the work and memory a set of files built to
// multiply them can cause. Parse and edits apply the byte and entry limits
// to their one file.
//
// Memory follows the entries, not the bytes: one entry ("k\n") can cost
// a hundred times its size. Distinct section headers count as entries, as
// the parser keeps each one. A parsed File also keeps every piece of the
// file (blank lines, comments, ...), at most maxFileItems of them: four per
// entry, as a line holds indentation, an entry, a comment and a newline.
//
// A typical configuration has tens to hundreds of entries; the limits leave
// room for one that tools have appended to for years.
const (
maxIncludeDepth = 10
maxLoadFiles = 1000
maxLoadBytes = 4 << 20
maxLoadEntries = 1 << 16
maxFileItems = 4 * maxLoadEntries
)
// repo is the repository of an Env.
type repo struct {
gitDir string // as given: the worktree's git directory
commonDir string // the repository's git directory
}
// repo returns the repository of the Env, or nil outside a repository.
func (e Env) repo() (*repo, error) {
if e.GitDir == "" {
return nil, nil
}
if !filepath.IsAbs(e.GitDir) {
return nil, &FileError{Path: e.GitDir, Err: ErrNotAbsolute}
}
r := &repo{gitDir: e.GitDir, commonDir: e.GitDir}
data, err := readSmall(filepath.Join(e.GitDir, "commondir"))
switch {
case err == nil:
dir := strings.TrimRight(string(data), "\r\n")
if !filepath.IsAbs(dir) {
if dir, err = joinReal(e.GitDir, dir); err != nil {
return nil, &FileError{Path: e.GitDir, Err: err}
}
}
r.commonDir = filepath.Clean(dir)
case !errors.Is(err, fs.ErrNotExist):
return nil, &FileError{Path: filepath.Join(e.GitDir, "commondir"), Err: err}
}
return r, nil
}
// joinReal joins the relative path rel to the directory dir as the system
// resolves it: the links of dir are followed before the ".." of rel apply,
// where filepath.Join would apply them first.
func joinReal(dir, rel string) (string, error) {
real, err := filepath.EvalSymlinks(dir)
if err != nil {
return "", err
}
return filepath.Join(real, rel), nil
}
func (r *repo) localConfig() string {
return filepath.Join(r.commonDir, "config")
}
func (r *repo) worktreeConfig() string {
return filepath.Join(r.gitDir, "config.worktree")
}
// worktreeConfigEnabled reports whether extensions.worktreeConfig is set
// in the repository's config.
func (r *repo) worktreeConfigEnabled() (bool, error) {
data, err := readConfigFile(r.localConfig(), maxLoadBytes)
if isMissing(err) {
return false, nil
}
if err != nil {
return false, &FileError{Path: r.localConfig(), Err: err}
}
var entries []Entry
err = parseEntries(r.localConfig(), data, ScopeLocal, new(maxLoadEntries), func(e Entry) error {
if e.Key == worktreeConfigKey {
entries = append(entries, e)
}
return nil
})
if err != nil {
return false, &FileError{Path: r.localConfig(), Err: err}
}
return worktreeConfigIn(entries, r.localConfig())
}
var worktreeConfigKey = Key{Section: "extensions", Name: "worktreeconfig"}
// worktreeConfigIn returns the last value of extensions.worktreeConfig in
// the entries of file path: git does not take it from included files.
func worktreeConfigIn(entries []Entry, path string) (bool, error) {
enabled := false
for _, e := range entries {
if e.Key != worktreeConfigKey || e.Origin.File != path {
continue
}
b, err := e.Bool()
if err != nil {
return false, &FileError{Path: path, Err: err}
}
enabled = b
}
return enabled, nil
}
// severalWorktrees reports whether the repository has linked worktrees.
func (r *repo) severalWorktrees() bool {
entries, err := os.ReadDir(filepath.Join(r.commonDir, "worktrees"))
return err == nil && len(entries) > 0
}
// branch returns the branch HEAD points to, or "" if it is detached or
// cannot be read.
func (r *repo) branch() string {
data, err := readSmall(filepath.Join(r.gitDir, "HEAD"))
if err != nil {
return ""
}
s, ok := strings.CutPrefix(string(data), "ref:")
if !ok {
return ""
}
s = strings.TrimSpace(s)
name, ok := strings.CutPrefix(s, "refs/heads/")
if !ok {
return ""
}
return name
}
// readSmall reads a small file of the repository, such as HEAD.
func readSmall(path string) ([]byte, error) {
return readConfigFile(path, 4096)
}
// readConfigFile reads a regular file, at most limit bytes. A file that is
// not regular is ErrNotRegular, read without blocking: a FIFO would block
// until someone writes to it. os.DevNull, which git documents to skip a
// scope, reads as empty.
func readConfigFile(path string, limit int64) ([]byte, error) {
if path == os.DevNull {
return nil, nil
}
var data []byte
err := retryTransient(func() error {
f, err := openRegular(path)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
if !fi.Mode().IsRegular() {
return ErrNotRegular
}
if fi.Size() > limit {
return ErrLoadLimit
}
data, err = io.ReadAll(io.LimitReader(f, limit+1))
if err == nil && int64(len(data)) > limit {
err = ErrLoadLimit
}
return err
})
return data, err
}
// isMissing reports whether err means that a file does not exist: also
// when a directory of its path is a file, as git takes it.
func isMissing(err error) bool {
return errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR)
}
// loader loads files into a list of entries, following includes, within
// the limits of one load.
type loader struct {
env Env
repo *repo
files int
bytes int64
budget int // entries and distinct headers that may still be read
entries []Entry
branch string
branchRead bool
realGitDir string // with '/', "" if it cannot be resolved
realDirRead bool
conds map[condKey]bool // includeIf conditions evaluated so far
}
// condKey is an includeIf condition and the file that holds it, which
// "gitdir:./" depends on.
type condKey struct{ cond, from string }
func (e Env) newLoader() (*loader, error) {
r, err := e.repo()
if err != nil {
return nil, err
}
return &loader{env: e, repo: r, budget: maxLoadEntries}, nil
}
func (l *loader) config() *Config {
return newConfig(l.entries)
}
// loadFile loads the file at path for scope. A missing file is empty. If
// skipUnreadable, a file that cannot be read for lack of permission is
// skipped, as git does for global files.
func (l *loader) loadFile(path string, scope Scope, skipUnreadable bool) error {
err := l.load(path, scope, 0)
// Only path itself is skipped: an unreadable include of it still fails.
if skipUnreadable {
if fe, ok := errors.AsType[*FileError](err); ok && fe.Path == path && errors.Is(fe.Err, fs.ErrPermission) {
return nil
}
}
return err
}
func (l *loader) load(path string, scope Scope, depth int) error {
l.files++
if l.files > maxLoadFiles {
return &FileError{Path: path, Err: ErrLoadLimit}
}
// Read at most what remains of the byte budget of the load.
data, err := readConfigFile(path, maxLoadBytes-l.bytes)
if isMissing(err) {
return nil
}
if err != nil {
return &FileError{Path: path, Err: err}
}
l.bytes += int64(len(data))
// The entries of an include go right after its include.path entry, so
// that later entries of this file take precedence over them: includes
// are followed as the file is parsed. As in git, an error in an include
// is reported before a syntax error further down this file.
var includeErr error
err = parseEntries(path, data, scope, &l.budget, func(e Entry) error {
l.entries = append(l.entries, e)
includeErr = l.include(e, path, scope, depth)
return includeErr
})
if includeErr != nil {
return includeErr
}
if err != nil {
return &FileError{Path: path, Err: err}
}
return nil
}
// include follows e if it is an include directive. from is the file that
// holds it, "" for the command line.
func (l *loader) include(e Entry, from string, scope Scope, depth int) error {
if e.Key.Name != "path" {
return nil
}
switch {
case e.Key.Section == "include" && !e.Key.HasSubsection:
case e.Key.Section == "includeif" && e.Key.HasSubsection:
ok, err := l.condition(e.Key.Subsection, from)
if err != nil || !ok {
return err
}
default:
return nil
}
if e.Implicit {
return l.fail(from, e, "missing value for include.path")
}
if e.Value == "" {
// An empty path includes nothing.
return nil
}
path, err := e.Path(l.env.Home)
if err != nil {
return &FileError{Path: from, Err: err}
}
if !filepath.IsAbs(path) {
if from == "" {
return &FileError{Path: path, Err: ErrNotAbsolute}
}
path = dirPrefix(from) + path
}
if depth+1 > maxIncludeDepth {
return &FileError{Path: path, Err: fmt.Errorf("%w while including %s from %s; this might be due to circular includes", ErrIncludeDepth, path, from)}
}
return l.load(path, scope, depth+1)
}
// fail reports an invalid include directive, as git does: a bad line of
// the file, or a bad command-line entry.
func (l *loader) fail(from string, e Entry, msg string) error {
if from == "" {
return &KeyError{Key: e.Key.String(), Err: errors.New("gitconfig: " + msg)}
}
return &FileError{Path: from, Err: &SyntaxError{File: from, Line: e.Origin.Line, Msg: msg}}
}
// dirPrefix returns path up to and including its last separator: an
// include's relative path is appended to it as is, not cleaned, as git
// does.
func dirPrefix(path string) string {
i := strings.LastIndexByte(path, '/')
if runtime.GOOS == "windows" {
i = max(i, strings.LastIndexByte(path, '\\'))
}
return path[:i+1]
}
// condition evaluates the condition of an includeIf, once per condition
// and file: a file can repeat path under one costly gitdir: pattern.
func (l *loader) condition(cond, from string) (bool, error) {
k := condKey{cond, from}
if ok, found := l.conds[k]; found {
return ok, nil
}
ok, err := l.evalCondition(cond, from)
if err != nil {
return false, err
}
if l.conds == nil {
l.conds = make(map[condKey]bool)
}
l.conds[k] = ok
return ok, nil
}
// evalCondition evaluates the condition of an includeIf. Unknown
// conditions, and those this package does not support (hasconfig:), are
// false.
func (l *loader) evalCondition(cond, from string) (bool, error) {
switch {
case strings.HasPrefix(cond, "gitdir:"):
return l.gitdir(cond[len("gitdir:"):], from, false)
case strings.HasPrefix(cond, "gitdir/i:"):
return l.gitdir(cond[len("gitdir/i:"):], from, true)
case strings.HasPrefix(cond, "onbranch:"):
if l.repo == nil {
return false, nil
}
if !l.branchRead {
l.branch, l.branchRead = l.repo.branch(), true
}
if l.branch == "" {
return false, nil
}
pattern := cond[len("onbranch:"):]
if strings.HasSuffix(pattern, "/") {
pattern += "**"
}
return globMatch(pattern, l.branch, false), nil
}
return false, nil
}
// gitdir evaluates includeIf "gitdir:<pattern>": "~/" is the home
// directory, "./" the absolute directory of the including file, a relative
// pattern matches at any depth ("**/" prepended) and a trailing '/' matches
// everything below ("**" appended). The pattern is matched against the git
// directory as given and its real path.
func (l *loader) gitdir(pattern, from string, fold bool) (bool, error) {
switch {
case strings.HasPrefix(pattern, "./"):
if from == "" {
return false, &FileError{Path: pattern, Err: ErrNotAbsolute}
}
dir := dirPrefix(from)
if !filepath.IsAbs(from) {
abs, err := filepath.Abs(from)
if err != nil {
return false, &FileError{Path: from, Err: err}
}
dir = dirPrefix(abs)
}
pattern = filepath.ToSlash(dir) + pattern[2:]
case strings.HasPrefix(pattern, "~/"):
if l.env.Home == "" {
return false, nil
}
pattern = strings.TrimSuffix(filepath.ToSlash(l.env.Home), "/") + pattern[1:]
}
// Checked only now: a "./" pattern on the command line is an error even
// outside a repository.
if l.repo == nil {
return false, nil
}
if !isAbsPattern(pattern) {
pattern = "**/" + pattern
}
if strings.HasSuffix(pattern, "/") {
pattern += "**"
}
dir := filepath.ToSlash(l.repo.gitDir)
if globMatch(pattern, dir, fold) {
return true, nil
}
if !l.realDirRead {
if real, err := filepath.EvalSymlinks(l.repo.gitDir); err == nil {
l.realGitDir = filepath.ToSlash(real)
}
l.realDirRead = true
}
if l.realGitDir != "" && l.realGitDir != dir {
return globMatch(pattern, l.realGitDir, fold), nil
}
return false, nil
}
// isAbsPattern reports whether a gitdir pattern, written with '/', is
// absolute.
func isAbsPattern(p string) bool {
if strings.HasPrefix(p, "/") {
return true
}
return runtime.GOOS == "windows" && filepath.IsAbs(filepath.FromSlash(p))
}
// command loads the entries of the command line.
func (l *loader) command() error {
for _, c := range l.env.Command {
k, err := ParseKey(c.Key)
if err != nil {
return err
}
e := Entry{Key: k, Value: c.Value, Implicit: c.Implicit, Origin: Origin{Scope: ScopeCommand}}
if c.Implicit {
e.Value = ""
}
l.entries = append(l.entries, e)
if err := l.include(e, "", ScopeCommand, 0); err != nil {
return err
}
}
return nil
}