Debian Codegraph @

git/config.go

raw symbols SCIP
1 package git
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
8 "strings"
9 "sync"
10
11 "github.com/git-lfs/git-lfs/v3/subprocess"
12 "github.com/git-lfs/git-lfs/v3/tr"
13 )
14
15 var (
16 ErrReadOnly = errors.New(tr.Tr.Get("configuration is read-only"))
17 )
18
19 // Environment is a restricted version of config.Environment that only provides
20 // a single method.
21 type Environment interface {
22 // Get is shorthand for calling `e.Fetcher.Get(key)`.
23 Get(key string) (val string, ok bool)
24 }
25
26 // Configuration can fetch or modify the current Git config and track the Git
27 // version.
28 type Configuration struct {
29 WorkDir string
30 GitDir string
31 version *string
32 readOnly bool
33 mu sync.Mutex
34 }
35
36 func NewConfig(workdir, gitdir string) *Configuration {
37 if len(gitdir) == 0 && len(workdir) > 0 {
38 gitdir = filepath.Join(workdir, ".git")
39 }
40 return &Configuration{WorkDir: workdir, GitDir: gitdir}
41 }
42
43 // NewReadOnlyConfig creates a new configuration that returns an error if an
44 // attempt to write to the configuration is made.
45 func NewReadOnlyConfig(workdir, gitdir string) *Configuration {
46 cfg := NewConfig(workdir, gitdir)
47 cfg.readOnly = true
48 return cfg
49
50 }
51
52 func ParseConfigLines(lines string, onlySafeKeys bool) *ConfigurationSource {
53 return &ConfigurationSource{
54 Lines: strings.Split(lines, "\n"),
55 OnlySafeKeys: onlySafeKeys,
56 }
57 }
58
59 type ConfigurationSource struct {
60 Lines []string
61 OnlySafeKeys bool
62 }
63
64 // Find returns the git config value for the key
65 func (c *Configuration) Find(val string) string {
66 output, _ := c.gitConfig(val)
67 return output
68 }
69
70 // FindGlobal returns the git config value in global scope for the key
71 func (c *Configuration) FindGlobal(key string) string {
72 output, _ := c.gitConfig("--global", key)
73 return output
74 }
75
76 // FindSystem returns the git config value in system scope for the key
77 func (c *Configuration) FindSystem(key string) string {
78 output, _ := c.gitConfig("--system", key)
79 return output
80 }
81
82 // FindLocal returns the git config value in local scope for the key
83 func (c *Configuration) FindLocal(key string) string {
84 output, _ := c.gitConfig("--local", key)
85 return output
86 }
87
88 // FindWorktree returns the git config value in worktree or local scope for the key, depending on whether multiple worktrees are in use
89 func (c *Configuration) FindWorktree(key string) string {
90 output, _ := c.gitConfig("--worktree", key)
91 return output
92 }
93
94 // FindWorktree returns the git config value for the key in the given configuration file
95 func (c *Configuration) FindFile(file, key string) string {
96 output, _ := c.gitConfig("--file", file, key)
97 return output
98 }
99
100 // SetGlobal sets the git config value for the key in the global config
101 func (c *Configuration) SetGlobal(key, val string) (string, error) {
102 return c.gitConfigWrite("--global", "--replace-all", key, val)
103 }
104
105 // SetSystem sets the git config value for the key in the system config
106 func (c *Configuration) SetSystem(key, val string) (string, error) {
107 return c.gitConfigWrite("--system", "--replace-all", key, val)
108 }
109
110 // SetLocal sets the git config value for the key in the specified config file
111 func (c *Configuration) SetLocal(key, val string) (string, error) {
112 return c.gitConfigWrite("--replace-all", key, val)
113 }
114
115 // SetWorktree sets the git config value for the key in the worktree or local config, depending on whether multiple worktrees are in use
116 func (c *Configuration) SetWorktree(key, val string) (string, error) {
117 return c.gitConfigWrite("--worktree", "--replace-all", key, val)
118 }
119
120 // SetFile sets the git config value for the key in the given configuration file
121 func (c *Configuration) SetFile(file, key, val string) (string, error) {
122 return c.gitConfigWrite("--file", file, "--replace-all", key, val)
123 }
124
125 // UnsetGlobalSection removes the entire named section from the global config
126 func (c *Configuration) UnsetGlobalSection(key string) (string, error) {
127 return c.gitConfigWrite("--global", "--remove-section", key)
128 }
129
130 // UnsetSystemSection removes the entire named section from the system config
131 func (c *Configuration) UnsetSystemSection(key string) (string, error) {
132 return c.gitConfigWrite("--system", "--remove-section", key)
133 }
134
135 // UnsetLocalSection removes the entire named section from the local config
136 func (c *Configuration) UnsetLocalSection(key string) (string, error) {
137 return c.gitConfigWrite("--local", "--remove-section", key)
138 }
139
140 // UnsetWorktreeSection removes the entire named section from the worktree or local config, depending on whether multiple worktrees are in use
141 func (c *Configuration) UnsetWorktreeSection(key string) (string, error) {
142 return c.gitConfigWrite("--worktree", "--remove-section", key)
143 }
144
145 // UnsetFileSection removes the entire named section from the given configuration file
146 func (c *Configuration) UnsetFileSection(file, key string) (string, error) {
147 return c.gitConfigWrite("--file", file, "--remove-section", key)
148 }
149
150 // UnsetLocalKey removes the git config value for the key from the specified config file
151 func (c *Configuration) UnsetLocalKey(key string) (string, error) {
152 return c.gitConfigWrite("--unset", key)
153 }
154
155 func (c *Configuration) Sources(dir string, optionalFilename string) ([]*ConfigurationSource, error) {
156 gitconfig, err := c.Source()
157 if err != nil {
158 return nil, err
159 }
160 configs := make([]*ConfigurationSource, 0, 2)
161
162 bare, err := IsBare()
163 if err == nil {
164 // First try to read from the working directory and then the index if
165 // the file is missing from the working directory.
166 var fileconfig *ConfigurationSource
167 if !bare {
168 fileconfig, err = c.FileSource(filepath.Join(dir, optionalFilename))
169 if err != nil {
170 if !os.IsNotExist(err) {
171 return nil, err
172 }
173 fileconfig, _ = c.RevisionSource(fmt.Sprintf(":%s", optionalFilename))
174 }
175 }
176 if fileconfig == nil {
177 fileconfig, _ = c.RevisionSource(fmt.Sprintf("HEAD:%s", optionalFilename))
178 }
179
180 if fileconfig != nil {
181 configs = append(configs, fileconfig)
182 }
183 }
184
185 return append(configs, gitconfig), nil
186 }
187
188 func (c *Configuration) FileSource(filename string) (*ConfigurationSource, error) {
189 if _, err := os.Stat(filename); err != nil {
190 return nil, err
191 }
192
193 out, err := c.gitConfig("-l", "-f", filename)
194 if err != nil {
195 return nil, err
196 }
197 return ParseConfigLines(out, true), nil
198 }
199
200 func (c *Configuration) RevisionSource(revision string) (*ConfigurationSource, error) {
201 out, err := c.gitConfig("-l", "--blob", revision)
202 if err != nil {
203 return nil, err
204 }
205 return ParseConfigLines(out, true), nil
206 }
207
208 func (c *Configuration) Source() (*ConfigurationSource, error) {
209 out, err := c.gitConfig("-l")
210 if err != nil {
211 return nil, err
212 }
213 return ParseConfigLines(out, false), nil
214 }
215
216 func (c *Configuration) gitConfig(args ...string) (string, error) {
217 args = append([]string{"config", "--includes"}, args...)
218 cmd, err := subprocess.ExecCommand("git", args...)
219 if err != nil {
220 return "", err
221 }
222 if len(c.GitDir) > 0 {
223 cmd.Dir = c.GitDir
224 }
225 return subprocess.Output(cmd)
226 }
227
228 func (c *Configuration) gitConfigWrite(args ...string) (string, error) {
229 if c.readOnly {
230 return "", ErrReadOnly
231 }
232 return c.gitConfig(args...)
233 }