2017-10-18 18:22:45 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2018-10-25 21:24:58 +00:00
|
|
|
"errors"
|
config: optionally read .lfsconfig from the repository
Currently we only read .lfsconfig from the working tree. This is better
than nothing, but it means that if there's an LFS file that starts with
a name earlier than .lfsconfig, we won't read the proper config file,
and we won't use any LFS remote URLs that are located in that file.
If the file is missing, let's additionally read from the index, if
there's a working tree, and then from HEAD. If the repository is bare,
let's just read from HEAD. This is very similar to what Git does for
.gitmodules.
It does, however, differ in one significant way: Git will read from the
index first if it's doing a checkout, since that might have newer
information. We don't do that here, since it's not totally clear that
we can articulate all the cases where that occurs in Git LFS, but we
explicitly allow for the possibility of changing the behavior in the
future and document the feature accordingly.
We no longer read the file .lfsconfig from a bare repository, but that
we did this in the first place was very questionable and pretty clearly
a bug, so it's intentional that we no longer do.
2020-07-28 19:58:15 +00:00
|
|
|
"fmt"
|
2017-10-18 18:22:45 +00:00
|
|
|
"os"
|
2017-10-25 19:47:13 +00:00
|
|
|
"path/filepath"
|
2017-10-18 18:22:45 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2021-09-01 19:41:10 +00:00
|
|
|
"github.com/git-lfs/git-lfs/v3/subprocess"
|
2017-10-18 18:22:45 +00:00
|
|
|
)
|
|
|
|
|
2018-10-25 21:24:58 +00:00
|
|
|
var (
|
|
|
|
ErrReadOnly = errors.New("configuration is read-only")
|
|
|
|
)
|
|
|
|
|
2019-08-09 15:25:23 +00:00
|
|
|
// Environment is a restricted version of config.Environment that only provides
|
|
|
|
// a single method.
|
|
|
|
type Environment interface {
|
|
|
|
// Get is shorthand for calling `e.Fetcher.Get(key)`.
|
|
|
|
Get(key string) (val string, ok bool)
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:22:45 +00:00
|
|
|
// Configuration can fetch or modify the current Git config and track the Git
|
|
|
|
// version.
|
|
|
|
type Configuration struct {
|
2018-10-25 21:24:58 +00:00
|
|
|
WorkDir string
|
|
|
|
GitDir string
|
|
|
|
version *string
|
|
|
|
readOnly bool
|
|
|
|
mu sync.Mutex
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-25 19:47:13 +00:00
|
|
|
func NewConfig(workdir, gitdir string) *Configuration {
|
|
|
|
if len(gitdir) == 0 && len(workdir) > 0 {
|
|
|
|
gitdir = filepath.Join(workdir, ".git")
|
|
|
|
}
|
|
|
|
return &Configuration{WorkDir: workdir, GitDir: gitdir}
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:24:58 +00:00
|
|
|
// NewReadOnlyConfig creates a new confguration that returns an error if an
|
|
|
|
// attempt to write to the configuration is made.
|
|
|
|
func NewReadOnlyConfig(workdir, gitdir string) *Configuration {
|
|
|
|
cfg := NewConfig(workdir, gitdir)
|
|
|
|
cfg.readOnly = true
|
|
|
|
return cfg
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:22:45 +00:00
|
|
|
func ParseConfigLines(lines string, onlySafeKeys bool) *ConfigurationSource {
|
|
|
|
return &ConfigurationSource{
|
|
|
|
Lines: strings.Split(lines, "\n"),
|
|
|
|
OnlySafeKeys: onlySafeKeys,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigurationSource struct {
|
|
|
|
Lines []string
|
|
|
|
OnlySafeKeys bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find returns the git config value for the key
|
|
|
|
func (c *Configuration) Find(val string) string {
|
2017-10-26 01:46:36 +00:00
|
|
|
output, _ := c.gitConfig(val)
|
2017-10-18 18:22:45 +00:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-06-26 08:57:09 +00:00
|
|
|
// FindGlobal returns the git config value in global scope for the key
|
2017-10-18 18:52:24 +00:00
|
|
|
func (c *Configuration) FindGlobal(key string) string {
|
2017-10-26 01:46:36 +00:00
|
|
|
output, _ := c.gitConfig("--global", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindSystem returns the git config value in system scope for the key
|
2017-10-18 18:52:24 +00:00
|
|
|
func (c *Configuration) FindSystem(key string) string {
|
2017-10-26 01:46:36 +00:00
|
|
|
output, _ := c.gitConfig("--system", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-06-26 08:57:09 +00:00
|
|
|
// FindLocal returns the git config value in local scope for the key
|
2017-10-18 18:52:24 +00:00
|
|
|
func (c *Configuration) FindLocal(key string) string {
|
2017-10-26 01:46:36 +00:00
|
|
|
output, _ := c.gitConfig("--local", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:10:28 +00:00
|
|
|
// FindWorktree returns the git config value in worktree or local scope for the key, depending on whether multiple worktrees are in use
|
|
|
|
func (c *Configuration) FindWorktree(key string) string {
|
|
|
|
output, _ := c.gitConfig("--worktree", key)
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:22:45 +00:00
|
|
|
// SetGlobal sets the git config value for the key in the global config
|
|
|
|
func (c *Configuration) SetGlobal(key, val string) (string, error) {
|
2018-10-25 21:24:58 +00:00
|
|
|
return c.gitConfigWrite("--global", "--replace-all", key, val)
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSystem sets the git config value for the key in the system config
|
|
|
|
func (c *Configuration) SetSystem(key, val string) (string, error) {
|
2018-10-25 21:24:58 +00:00
|
|
|
return c.gitConfigWrite("--system", "--replace-all", key, val)
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 08:57:09 +00:00
|
|
|
// SetLocal sets the git config value for the key in the specified config file
|
|
|
|
func (c *Configuration) SetLocal(key, val string) (string, error) {
|
|
|
|
return c.gitConfigWrite("--replace-all", key, val)
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:10:28 +00:00
|
|
|
// SetWorktree sets the git config value for the key in the worktree or local config, depending on whether multiple worktrees are in use
|
|
|
|
func (c *Configuration) SetWorktree(key, val string) (string, error) {
|
|
|
|
return c.gitConfigWrite("--worktree", "--replace-all", key, val)
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:22:45 +00:00
|
|
|
// UnsetGlobalSection removes the entire named section from the global config
|
|
|
|
func (c *Configuration) UnsetGlobalSection(key string) (string, error) {
|
2018-10-25 21:24:58 +00:00
|
|
|
return c.gitConfigWrite("--global", "--remove-section", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnsetSystemSection removes the entire named section from the system config
|
|
|
|
func (c *Configuration) UnsetSystemSection(key string) (string, error) {
|
2018-10-25 21:24:58 +00:00
|
|
|
return c.gitConfigWrite("--system", "--remove-section", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 08:57:09 +00:00
|
|
|
// UnsetLocalSection removes the entire named section from the local config
|
2017-10-18 18:22:45 +00:00
|
|
|
func (c *Configuration) UnsetLocalSection(key string) (string, error) {
|
2018-10-25 21:24:58 +00:00
|
|
|
return c.gitConfigWrite("--local", "--remove-section", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 22:10:28 +00:00
|
|
|
// UnsetWorktreeSection removes the entire named section from the worktree or local config, depending on whether multiple worktrees are in use
|
|
|
|
func (c *Configuration) UnsetWorktreeSection(key string) (string, error) {
|
|
|
|
return c.gitConfigWrite("--worktree", "--remove-section", key)
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:22:45 +00:00
|
|
|
// UnsetLocalKey removes the git config value for the key from the specified config file
|
2017-10-26 01:20:35 +00:00
|
|
|
func (c *Configuration) UnsetLocalKey(key string) (string, error) {
|
2018-10-25 21:24:58 +00:00
|
|
|
return c.gitConfigWrite("--unset", key)
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
config: optionally read .lfsconfig from the repository
Currently we only read .lfsconfig from the working tree. This is better
than nothing, but it means that if there's an LFS file that starts with
a name earlier than .lfsconfig, we won't read the proper config file,
and we won't use any LFS remote URLs that are located in that file.
If the file is missing, let's additionally read from the index, if
there's a working tree, and then from HEAD. If the repository is bare,
let's just read from HEAD. This is very similar to what Git does for
.gitmodules.
It does, however, differ in one significant way: Git will read from the
index first if it's doing a checkout, since that might have newer
information. We don't do that here, since it's not totally clear that
we can articulate all the cases where that occurs in Git LFS, but we
explicitly allow for the possibility of changing the behavior in the
future and document the feature accordingly.
We no longer read the file .lfsconfig from a bare repository, but that
we did this in the first place was very questionable and pretty clearly
a bug, so it's intentional that we no longer do.
2020-07-28 19:58:15 +00:00
|
|
|
func (c *Configuration) Sources(dir string, optionalFilename string) ([]*ConfigurationSource, error) {
|
2017-10-18 18:22:45 +00:00
|
|
|
gitconfig, err := c.Source()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-29 17:01:34 +00:00
|
|
|
configs := make([]*ConfigurationSource, 0, 2)
|
2017-10-18 18:22:45 +00:00
|
|
|
|
config: optionally read .lfsconfig from the repository
Currently we only read .lfsconfig from the working tree. This is better
than nothing, but it means that if there's an LFS file that starts with
a name earlier than .lfsconfig, we won't read the proper config file,
and we won't use any LFS remote URLs that are located in that file.
If the file is missing, let's additionally read from the index, if
there's a working tree, and then from HEAD. If the repository is bare,
let's just read from HEAD. This is very similar to what Git does for
.gitmodules.
It does, however, differ in one significant way: Git will read from the
index first if it's doing a checkout, since that might have newer
information. We don't do that here, since it's not totally clear that
we can articulate all the cases where that occurs in Git LFS, but we
explicitly allow for the possibility of changing the behavior in the
future and document the feature accordingly.
We no longer read the file .lfsconfig from a bare repository, but that
we did this in the first place was very questionable and pretty clearly
a bug, so it's intentional that we no longer do.
2020-07-28 19:58:15 +00:00
|
|
|
bare, err := IsBare()
|
2021-10-29 17:01:34 +00:00
|
|
|
if err == nil {
|
|
|
|
// First try to read from the working directory and then the index if
|
|
|
|
// the file is missing from the working directory.
|
|
|
|
var fileconfig *ConfigurationSource
|
|
|
|
if !bare {
|
|
|
|
fileconfig, err = c.FileSource(filepath.Join(dir, optionalFilename))
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fileconfig, _ = c.RevisionSource(fmt.Sprintf(":%s", optionalFilename))
|
config: optionally read .lfsconfig from the repository
Currently we only read .lfsconfig from the working tree. This is better
than nothing, but it means that if there's an LFS file that starts with
a name earlier than .lfsconfig, we won't read the proper config file,
and we won't use any LFS remote URLs that are located in that file.
If the file is missing, let's additionally read from the index, if
there's a working tree, and then from HEAD. If the repository is bare,
let's just read from HEAD. This is very similar to what Git does for
.gitmodules.
It does, however, differ in one significant way: Git will read from the
index first if it's doing a checkout, since that might have newer
information. We don't do that here, since it's not totally clear that
we can articulate all the cases where that occurs in Git LFS, but we
explicitly allow for the possibility of changing the behavior in the
future and document the feature accordingly.
We no longer read the file .lfsconfig from a bare repository, but that
we did this in the first place was very questionable and pretty clearly
a bug, so it's intentional that we no longer do.
2020-07-28 19:58:15 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 17:01:34 +00:00
|
|
|
if fileconfig == nil {
|
|
|
|
fileconfig, _ = c.RevisionSource(fmt.Sprintf("HEAD:%s", optionalFilename))
|
|
|
|
}
|
config: optionally read .lfsconfig from the repository
Currently we only read .lfsconfig from the working tree. This is better
than nothing, but it means that if there's an LFS file that starts with
a name earlier than .lfsconfig, we won't read the proper config file,
and we won't use any LFS remote URLs that are located in that file.
If the file is missing, let's additionally read from the index, if
there's a working tree, and then from HEAD. If the repository is bare,
let's just read from HEAD. This is very similar to what Git does for
.gitmodules.
It does, however, differ in one significant way: Git will read from the
index first if it's doing a checkout, since that might have newer
information. We don't do that here, since it's not totally clear that
we can articulate all the cases where that occurs in Git LFS, but we
explicitly allow for the possibility of changing the behavior in the
future and document the feature accordingly.
We no longer read the file .lfsconfig from a bare repository, but that
we did this in the first place was very questionable and pretty clearly
a bug, so it's intentional that we no longer do.
2020-07-28 19:58:15 +00:00
|
|
|
|
2021-10-29 17:01:34 +00:00
|
|
|
if fileconfig != nil {
|
|
|
|
configs = append(configs, fileconfig)
|
|
|
|
}
|
2017-10-18 18:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return append(configs, gitconfig), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) FileSource(filename string) (*ConfigurationSource, error) {
|
|
|
|
if _, err := os.Stat(filename); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:46:36 +00:00
|
|
|
out, err := c.gitConfig("-l", "-f", filename)
|
2017-10-18 18:22:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ParseConfigLines(out, true), nil
|
|
|
|
}
|
|
|
|
|
config: optionally read .lfsconfig from the repository
Currently we only read .lfsconfig from the working tree. This is better
than nothing, but it means that if there's an LFS file that starts with
a name earlier than .lfsconfig, we won't read the proper config file,
and we won't use any LFS remote URLs that are located in that file.
If the file is missing, let's additionally read from the index, if
there's a working tree, and then from HEAD. If the repository is bare,
let's just read from HEAD. This is very similar to what Git does for
.gitmodules.
It does, however, differ in one significant way: Git will read from the
index first if it's doing a checkout, since that might have newer
information. We don't do that here, since it's not totally clear that
we can articulate all the cases where that occurs in Git LFS, but we
explicitly allow for the possibility of changing the behavior in the
future and document the feature accordingly.
We no longer read the file .lfsconfig from a bare repository, but that
we did this in the first place was very questionable and pretty clearly
a bug, so it's intentional that we no longer do.
2020-07-28 19:58:15 +00:00
|
|
|
func (c *Configuration) RevisionSource(revision string) (*ConfigurationSource, error) {
|
|
|
|
out, err := c.gitConfig("-l", "--blob", revision)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ParseConfigLines(out, true), nil
|
|
|
|
}
|
|
|
|
|
2017-10-18 18:22:45 +00:00
|
|
|
func (c *Configuration) Source() (*ConfigurationSource, error) {
|
2017-10-26 01:46:36 +00:00
|
|
|
out, err := c.gitConfig("-l")
|
2017-10-18 18:22:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ParseConfigLines(out, false), nil
|
|
|
|
}
|
|
|
|
|
2017-10-26 01:46:36 +00:00
|
|
|
func (c *Configuration) gitConfig(args ...string) (string, error) {
|
2021-03-03 19:25:04 +00:00
|
|
|
args = append([]string{"config", "--includes"}, args...)
|
2017-10-26 01:46:36 +00:00
|
|
|
cmd := subprocess.ExecCommand("git", args...)
|
2017-10-25 19:47:13 +00:00
|
|
|
if len(c.GitDir) > 0 {
|
2017-10-26 01:46:36 +00:00
|
|
|
cmd.Dir = c.GitDir
|
2017-10-25 19:47:13 +00:00
|
|
|
}
|
2017-10-26 01:46:36 +00:00
|
|
|
return subprocess.Output(cmd)
|
2017-10-25 19:47:13 +00:00
|
|
|
}
|
2018-10-25 21:24:58 +00:00
|
|
|
|
|
|
|
func (c *Configuration) gitConfigWrite(args ...string) (string, error) {
|
|
|
|
if c.readOnly {
|
|
|
|
return "", ErrReadOnly
|
|
|
|
}
|
|
|
|
return c.gitConfig(args...)
|
|
|
|
}
|