Repository navigation
Expand file tree
/
Copy pathdiscover.go
More file actions
332 lines (311 loc) · 9.31 KB
/
Copy pathdiscover.go
File metadata and controls
332 lines (311 loc) · 9.31 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
package gitconfig
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
)
// environ is the process environment, as Discover reads it.
type environ interface {
Lookup(key string) (string, bool)
}
type osEnviron struct{}
func (osEnviron) Lookup(key string) (string, bool) { return os.LookupEnv(key) }
func get(env environ, key string) string {
v, _ := env.Lookup(key)
return v
}
func discover(dir string, env environ) (Env, error) {
var e Env
e.Home = get(env, "HOME")
if e.Home == "" && runtime.GOOS == "windows" {
e.Home = get(env, "USERPROFILE")
}
if g, ok := env.Lookup("GIT_CONFIG_GLOBAL"); ok {
// Set but empty: no global file at all.
if g != "" {
e.GlobalConfig = []string{g}
}
} else {
if xdg := get(env, "XDG_CONFIG_HOME"); xdg != "" {
e.GlobalConfig = append(e.GlobalConfig, filepath.Join(xdg, "git", "config"))
} else if e.Home != "" {
e.GlobalConfig = append(e.GlobalConfig, filepath.Join(e.Home, ".config", "git", "config"))
}
if e.Home != "" {
e.GlobalConfig = append(e.GlobalConfig, filepath.Join(e.Home, ".gitconfig"))
}
}
noSystem := false
if v, ok := env.Lookup("GIT_CONFIG_NOSYSTEM"); ok {
b, err := parseBool(v)
if err != nil {
return Env{}, &EnvError{Var: "GIT_CONFIG_NOSYSTEM", Err: fmt.Errorf("bad boolean value %q", v)}
}
noSystem = b
}
switch s, ok := env.Lookup("GIT_CONFIG_SYSTEM"); {
case noSystem:
case ok:
e.SystemConfig = s
case runtime.GOOS != "windows":
e.SystemConfig = "/etc/gitconfig"
}
// On Windows the system file is under git's installation directory,
// which this package cannot know: there is none unless
// GIT_CONFIG_SYSTEM names it.
cmd, err := countEntries(env)
if err != nil {
return Env{}, err
}
params, err := parseParameters(get(env, "GIT_CONFIG_PARAMETERS"))
if err != nil {
return Env{}, &EnvError{Var: "GIT_CONFIG_PARAMETERS", Err: err}
}
e.Command = append(cmd, params...)
if dir != "" {
var owned []string
if e.GitDir, owned, err = findRepo(dir); err != nil {
return Env{}, err
}
if err := e.checkTrust(owned, get(env, "SUDO_UID")); err != nil {
return Env{}, err
}
}
return e, nil
}
// findRepo returns the git directory of the repository at dir, as git finds
// it when started there, and the paths whose owner git checks. In a working
// tree (dir holds .git, or is a "gitdir:" file), they are the working tree,
// its .git and the git directory; elsewhere, dir alone. The first is the
// path safe.directory lists. A git directory that checkGitDir refuses fails
// with ErrNotRepository.
func findRepo(dir string) (gitDir string, owned []string, err error) {
if dir, err = filepath.Abs(dir); err != nil {
return "", nil, err
}
dot := filepath.Join(dir, ".git")
if fi, err := os.Stat(dir); err == nil && fi.Mode().IsRegular() {
dir, dot = filepath.Dir(dir), dir
} else if _, err := os.Stat(dot); isMissing(err) {
if err := checkGitDir(dir); err != nil {
return "", nil, err
}
return dir, []string{dir}, nil
} else if err != nil {
return "", nil, &FileError{Path: dot, Err: err}
}
if gitDir, err = resolveGitDir(dot); err != nil {
return "", nil, err
}
if err := checkGitDir(gitDir); err != nil {
return "", nil, err
}
return gitDir, []string{dir, dot, gitDir}, nil
}
// checkGitDir fails with ErrNotRepository unless gitDir is a git directory
// as git recognizes one: it holds a HEAD file, and its common directory
// (gitDir itself, or the one its commondir file names in a linked worktree)
// holds objects and refs directories. Any other directory would have its
// config read and written as the local scope. HEAD may also be a symbolic
// link to a branch (core.preferSymlinkRefs), whose target does not exist
// while the branch is unborn or packed: as for git, the link's text must
// start with "refs/". What HEAD holds is not checked, where git also
// requires a ref or an object name.
func checkGitDir(gitDir string) error {
head := filepath.Join(gitDir, "HEAD")
fi, err := os.Lstat(head)
if err != nil {
return notRepository(gitDir, err)
}
switch {
case fi.Mode().IsRegular():
case fi.Mode()&fs.ModeSymlink != 0:
target, err := os.Readlink(head)
if err != nil {
return &FileError{Path: head, Err: err}
}
if !strings.HasPrefix(filepath.ToSlash(target), "refs/") {
return &FileError{Path: gitDir, Err: ErrNotRepository}
}
default:
return &FileError{Path: gitDir, Err: ErrNotRepository}
}
r, err := Env{GitDir: gitDir}.repo()
if err != nil {
return err
}
for _, name := range []string{"objects", "refs"} {
fi, err := os.Stat(filepath.Join(r.commonDir, name))
if err != nil {
return notRepository(gitDir, err)
}
if !fi.IsDir() {
return &FileError{Path: gitDir, Err: ErrNotRepository}
}
}
return nil
}
// notRepository wraps the error met checking the git directory gitDir: a
// missing path means it is not a repository.
func notRepository(gitDir string, err error) error {
if isMissing(err) {
err = ErrNotRepository
}
return &FileError{Path: gitDir, Err: err}
}
// checkTrust fails with ErrUntrusted unless the current user owns every
// path of owned that exists, or safe.directory lists the first.
func (e Env) checkTrust(owned []string, sudoUID string) error {
for _, p := range owned {
ok, err := ownedByCurrentUser(p, sudoUID)
if errors.Is(err, fs.ErrNotExist) {
continue
}
if err != nil {
return &FileError{Path: p, Err: err}
}
if ok {
continue
}
// Without the repository, as git: its HEAD and path must not
// match an includeIf that lists it.
p := e
p.GitDir = ""
c, err := p.LoadProtected()
if err != nil {
return err
}
if !safeDirectory(c, owned[0], e.Home) {
return &FileError{Path: owned[0], Err: ErrUntrusted}
}
return nil
}
return nil
}
// resolveGitDir follows a "gitdir: <path>" file; any other path is the git
// directory itself. gitDir is absolute.
func resolveGitDir(gitDir string) (string, error) {
fi, err := os.Stat(gitDir)
if err != nil || !fi.Mode().IsRegular() {
return gitDir, nil
}
data, err := readSmall(gitDir)
if err != nil {
return "", &FileError{Path: gitDir, Err: err}
}
path, ok := strings.CutPrefix(strings.TrimRight(string(data), "\r\n"), "gitdir: ")
if !ok || path == "" {
return "", &FileError{Path: gitDir, Err: errors.New("invalid gitfile format")}
}
if !filepath.IsAbs(path) {
if path, err = joinReal(filepath.Dir(gitDir), path); err != nil {
return "", &FileError{Path: gitDir, Err: err}
}
}
return path, nil
}
// countEntries reads GIT_CONFIG_COUNT, GIT_CONFIG_KEY_<n> and
// GIT_CONFIG_VALUE_<n>. The count is digits only, where git also accepts a
// sign and leading whitespace.
func countEntries(env environ) ([]CommandEntry, error) {
count := get(env, "GIT_CONFIG_COUNT")
if count == "" {
return nil, nil
}
n, err := strconv.ParseInt(count, 10, 32)
if err != nil || count[0] < '0' || count[0] > '9' {
return nil, &EnvError{Var: "GIT_CONFIG_COUNT", Err: fmt.Errorf("bogus count %q", count)}
}
var entries []CommandEntry
for i := range int(n) {
keyVar, valueVar := "GIT_CONFIG_KEY_"+strconv.Itoa(i), "GIT_CONFIG_VALUE_"+strconv.Itoa(i)
key, ok := env.Lookup(keyVar)
if !ok {
return nil, &EnvError{Var: keyVar, Err: errMissingVar}
}
value, ok := env.Lookup(valueVar)
if !ok {
return nil, &EnvError{Var: valueVar, Err: errMissingVar}
}
entries = append(entries, CommandEntry{Key: key, Value: value})
}
return entries, nil
}
var (
errMissingVar = errors.New("missing")
errBogusParameters = errors.New("bogus format")
)
// parseParameters parses GIT_CONFIG_PARAMETERS, where git passes its -c
// options to the commands it runs: items separated by whitespace, each a
// single-quoted key, then "=" and a single-quoted value, or "=" alone for
// an option without a value. Inside a quoted string, a quote is written as
// a backslash and a quote between two quoted strings, and so may be an
// exclamation mark:
//
// 'e.f'='x'\''y' 'g.h'=''\!'z'
//
// The older form of an item, a single-quoted "key=value", is accepted too.
func parseParameters(s string) ([]CommandEntry, error) {
var entries []CommandEntry
for s != "" {
key, rest, ok := unquote(s)
if !ok {
return nil, errBogusParameters
}
var c CommandEntry
if v, ok := strings.CutPrefix(rest, "="); ok {
// The current form: 'key'='value', or 'key'= for no value.
c.Key = key
if v == "" || isParamSpace(v[0]) {
c.Implicit = true
rest = v
} else {
c.Value, rest, ok = unquote(v)
if !ok {
return nil, errBogusParameters
}
}
} else if k, v, ok := strings.Cut(key, "="); ok {
// The older form: 'key=value'.
c.Key, c.Value = k, v
} else {
c.Key, c.Implicit = key, true
}
if rest != "" && !isParamSpace(rest[0]) {
return nil, errBogusParameters
}
entries = append(entries, c)
s = strings.TrimLeft(rest, " \t\n\v\f\r")
}
return entries, nil
}
func isParamSpace(c byte) bool {
return strings.IndexByte(" \t\n\v\f\r", c) >= 0
}
// unquote reads a single-quoted string at the start of s.
func unquote(s string) (value, rest string, ok bool) {
if s == "" || s[0] != '\'' {
return "", "", false
}
var sb strings.Builder
s = s[1:]
for {
i := strings.IndexByte(s, '\'')
if i < 0 {
return "", "", false
}
sb.WriteString(s[:i])
s = s[i+1:]
if len(s) >= 3 && s[0] == '\\' && (s[1] == '\'' || s[1] == '!') && s[2] == '\'' {
sb.WriteByte(s[1])
s = s[3:]
continue
}
return sb.String(), s, true
}
}