2016-05-13 16:38:06 +00:00
|
|
|
package config
|
2013-11-05 17:07:03 +00:00
|
|
|
|
2013-11-05 17:45:01 +00:00
|
|
|
import (
|
2014-05-22 23:02:56 +00:00
|
|
|
"fmt"
|
2014-03-12 15:31:41 +00:00
|
|
|
"os"
|
2015-06-01 18:53:08 +00:00
|
|
|
"path/filepath"
|
2015-04-23 14:48:44 +00:00
|
|
|
"strconv"
|
2014-02-01 20:38:29 +00:00
|
|
|
"strings"
|
2015-04-23 01:07:52 +00:00
|
|
|
"sync"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
|
|
|
"github.com/github/git-lfs/git"
|
2015-10-07 20:54:23 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/ThomsonReutersEikon/go-ntlm/ntlm"
|
2015-11-18 17:23:36 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/bgentry/go-netrc/netrc"
|
2015-07-09 13:45:18 +00:00
|
|
|
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
|
2013-11-05 17:45:01 +00:00
|
|
|
)
|
|
|
|
|
2015-06-17 20:38:09 +00:00
|
|
|
var (
|
2015-11-24 18:58:52 +00:00
|
|
|
Config = NewConfig()
|
|
|
|
ShowConfigWarnings = false
|
|
|
|
defaultRemote = "origin"
|
|
|
|
gitConfigWarningPrefix = "lfs."
|
2015-06-17 20:38:09 +00:00
|
|
|
)
|
|
|
|
|
2015-08-04 16:46:51 +00:00
|
|
|
// FetchPruneConfig collects together the config options that control fetching and pruning
|
|
|
|
type FetchPruneConfig struct {
|
|
|
|
// The number of days prior to current date for which (local) refs other than HEAD
|
|
|
|
// will be fetched with --recent (default 7, 0 = only fetch HEAD)
|
|
|
|
FetchRecentRefsDays int
|
2015-09-01 11:06:30 +00:00
|
|
|
// Makes the FetchRecentRefsDays option apply to remote refs from fetch source as well (default true)
|
2015-08-04 16:46:51 +00:00
|
|
|
FetchRecentRefsIncludeRemotes bool
|
|
|
|
// number of days prior to latest commit on a ref that we'll fetch previous
|
2015-08-17 11:04:43 +00:00
|
|
|
// LFS changes too (default 0 = only fetch at ref)
|
2015-08-04 16:46:51 +00:00
|
|
|
FetchRecentCommitsDays int
|
2015-08-17 11:03:13 +00:00
|
|
|
// Whether to always fetch recent even without --recent
|
|
|
|
FetchRecentAlways bool
|
2015-08-04 16:46:51 +00:00
|
|
|
// Number of days added to FetchRecent*; data outside combined window will be
|
|
|
|
// deleted when prune is run. (default 3)
|
|
|
|
PruneOffsetDays int
|
2015-09-08 14:00:28 +00:00
|
|
|
// Always verify with remote before pruning
|
|
|
|
PruneVerifyRemoteAlways bool
|
|
|
|
// Name of remote to check for unpushed and verify checks
|
|
|
|
PruneRemoteName string
|
2015-08-04 16:46:51 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 17:07:03 +00:00
|
|
|
type Configuration struct {
|
2016-05-13 16:38:06 +00:00
|
|
|
CurrentRemote string
|
|
|
|
NtlmSession ntlm.ClientSession
|
|
|
|
envVars map[string]string
|
|
|
|
envVarsMutex sync.Mutex
|
|
|
|
IsTracingHttp bool
|
|
|
|
IsDebuggingHttp bool
|
|
|
|
IsLoggingStats bool
|
2015-06-13 23:00:36 +00:00
|
|
|
|
2015-08-06 13:56:57 +00:00
|
|
|
loading sync.Mutex // guards initialization of gitConfig and remotes
|
|
|
|
gitConfig map[string]string
|
2015-08-28 21:19:52 +00:00
|
|
|
origConfig map[string]string
|
2015-08-06 13:56:57 +00:00
|
|
|
remotes []string
|
|
|
|
extensions map[string]Extension
|
|
|
|
fetchIncludePaths []string
|
|
|
|
fetchExcludePaths []string
|
2015-08-04 16:46:51 +00:00
|
|
|
fetchPruneConfig *FetchPruneConfig
|
2015-11-24 13:05:47 +00:00
|
|
|
manualEndpoint *Endpoint
|
2015-11-18 17:23:36 +00:00
|
|
|
parsedNetrc netrcfinder
|
2013-11-05 17:07:03 +00:00
|
|
|
}
|
|
|
|
|
2015-03-05 19:59:52 +00:00
|
|
|
func NewConfig() *Configuration {
|
2015-03-05 20:04:00 +00:00
|
|
|
c := &Configuration{
|
|
|
|
CurrentRemote: defaultRemote,
|
2015-06-13 23:13:58 +00:00
|
|
|
envVars: make(map[string]string),
|
2015-03-05 19:59:52 +00:00
|
|
|
}
|
2016-05-13 16:38:06 +00:00
|
|
|
c.IsTracingHttp = c.GetenvBool("GIT_CURL_VERBOSE", false)
|
|
|
|
c.IsDebuggingHttp = c.GetenvBool("LFS_DEBUG_HTTP", false)
|
|
|
|
c.IsLoggingStats = c.GetenvBool("GIT_LOG_STATS", false)
|
2015-03-05 19:59:52 +00:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2015-06-13 23:13:58 +00:00
|
|
|
func (c *Configuration) Getenv(key string) string {
|
2016-04-05 13:29:45 +00:00
|
|
|
c.envVarsMutex.Lock()
|
|
|
|
defer c.envVarsMutex.Unlock()
|
|
|
|
|
2015-06-13 23:13:58 +00:00
|
|
|
if i, ok := c.envVars[key]; ok {
|
|
|
|
return i
|
2015-03-29 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 23:13:58 +00:00
|
|
|
v := os.Getenv(key)
|
|
|
|
c.envVars[key] = v
|
|
|
|
return v
|
2015-03-29 19:16:28 +00:00
|
|
|
}
|
|
|
|
|
2015-06-22 09:46:40 +00:00
|
|
|
func (c *Configuration) Setenv(key, value string) error {
|
2016-04-05 13:29:45 +00:00
|
|
|
c.envVarsMutex.Lock()
|
|
|
|
defer c.envVarsMutex.Unlock()
|
|
|
|
|
2015-06-22 09:46:40 +00:00
|
|
|
// Check see if we have this in our cache, if so update it
|
|
|
|
if _, ok := c.envVars[key]; ok {
|
|
|
|
c.envVars[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now set in process
|
|
|
|
return os.Setenv(key, value)
|
|
|
|
}
|
|
|
|
|
2016-05-17 13:40:05 +00:00
|
|
|
func (c *Configuration) GetAllEnv() map[string]string {
|
|
|
|
c.envVarsMutex.Lock()
|
|
|
|
defer c.envVarsMutex.Unlock()
|
|
|
|
|
|
|
|
ret := make(map[string]string)
|
|
|
|
for k, v := range c.envVars {
|
|
|
|
ret[k] = v
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Configuration) SetAllEnv(env map[string]string) {
|
|
|
|
c.envVarsMutex.Lock()
|
|
|
|
defer c.envVarsMutex.Unlock()
|
|
|
|
|
|
|
|
c.envVars = make(map[string]string)
|
|
|
|
for k, v := range env {
|
|
|
|
c.envVars[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 23:30:16 +00:00
|
|
|
// GetenvBool parses a boolean environment variable and returns the result as a bool.
|
|
|
|
// If the environment variable is unset, empty, or if the parsing fails,
|
|
|
|
// the value of def (default) is returned instead.
|
|
|
|
func (c *Configuration) GetenvBool(key string, def bool) bool {
|
|
|
|
s := c.Getenv(key)
|
|
|
|
if len(s) == 0 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
2015-09-02 17:52:38 +00:00
|
|
|
b, err := parseConfigBool(s)
|
2015-06-16 23:30:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
// GitRemoteUrl returns the git clone/push url for a given remote (blank if not found)
|
|
|
|
// the forpush argument is to cater for separate remote.name.pushurl settings
|
|
|
|
func (c *Configuration) GitRemoteUrl(remote string, forpush bool) string {
|
|
|
|
if forpush {
|
|
|
|
if u, ok := c.GitConfig("remote." + remote + ".pushurl"); ok {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if u, ok := c.GitConfig("remote." + remote + ".url"); ok {
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-24 13:05:47 +00:00
|
|
|
// Manually set an Endpoint to use instead of deriving from Git config
|
|
|
|
func (c *Configuration) SetManualEndpoint(e Endpoint) {
|
|
|
|
c.manualEndpoint = &e
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
func (c *Configuration) Endpoint(operation string) Endpoint {
|
2015-11-24 13:05:47 +00:00
|
|
|
if c.manualEndpoint != nil {
|
|
|
|
return *c.manualEndpoint
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
if operation == "upload" {
|
|
|
|
if url, ok := c.GitConfig("lfs.pushurl"); ok {
|
2016-02-02 17:42:20 +00:00
|
|
|
return NewEndpointWithConfig(url, c)
|
2016-01-25 15:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:30:55 +00:00
|
|
|
if url, ok := c.GitConfig("lfs.url"); ok {
|
2016-02-02 17:42:20 +00:00
|
|
|
return NewEndpointWithConfig(url, c)
|
2013-11-05 17:07:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 22:39:03 +00:00
|
|
|
if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
|
2016-01-25 15:09:55 +00:00
|
|
|
if endpoint := c.RemoteEndpoint(c.CurrentRemote, operation); len(endpoint.Url) > 0 {
|
2015-02-02 22:39:03 +00:00
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
return c.RemoteEndpoint(defaultRemote, operation)
|
2013-11-05 17:07:03 +00:00
|
|
|
}
|
2013-11-05 17:45:01 +00:00
|
|
|
|
2015-05-13 15:27:06 +00:00
|
|
|
func (c *Configuration) ConcurrentTransfers() int {
|
2016-01-25 15:09:55 +00:00
|
|
|
if c.NtlmAccess("download") {
|
2015-11-06 18:08:44 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2015-04-23 14:48:44 +00:00
|
|
|
uploads := 3
|
|
|
|
|
2015-05-13 15:27:06 +00:00
|
|
|
if v, ok := c.GitConfig("lfs.concurrenttransfers"); ok {
|
2015-04-23 14:48:44 +00:00
|
|
|
n, err := strconv.Atoi(v)
|
2015-05-26 19:42:37 +00:00
|
|
|
if err == nil && n > 0 {
|
2015-04-23 14:48:44 +00:00
|
|
|
uploads = n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return uploads
|
|
|
|
}
|
|
|
|
|
2015-05-07 18:37:30 +00:00
|
|
|
func (c *Configuration) BatchTransfer() bool {
|
2015-09-02 17:52:38 +00:00
|
|
|
value, ok := c.GitConfig("lfs.batch")
|
|
|
|
if !ok || len(value) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
useBatch, err := parseConfigBool(value)
|
|
|
|
if err != nil {
|
2015-09-02 17:37:19 +00:00
|
|
|
return false
|
2015-05-07 18:37:30 +00:00
|
|
|
}
|
2015-09-02 17:52:38 +00:00
|
|
|
|
|
|
|
return useBatch
|
2015-05-07 18:37:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
func (c *Configuration) NtlmAccess(operation string) bool {
|
|
|
|
return c.Access(operation) == "ntlm"
|
2015-10-05 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 20:18:41 +00:00
|
|
|
// PrivateAccess will retrieve the access value and return true if
|
|
|
|
// the value is set to private. When a repo is marked as having private
|
|
|
|
// access, the http requests for the batch api will fetch the credentials
|
|
|
|
// before running, otherwise the request will run without credentials.
|
2016-01-25 15:09:55 +00:00
|
|
|
func (c *Configuration) PrivateAccess(operation string) bool {
|
|
|
|
return c.Access(operation) != "none"
|
2015-09-01 17:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Access returns the access auth type.
|
2016-01-25 15:09:55 +00:00
|
|
|
func (c *Configuration) Access(operation string) string {
|
|
|
|
return c.EndpointAccess(c.Endpoint(operation))
|
2015-09-01 21:47:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAccess will set the private access flag in .git/config.
|
2016-01-25 15:09:55 +00:00
|
|
|
func (c *Configuration) SetAccess(operation string, authType string) {
|
|
|
|
c.SetEndpointAccess(c.Endpoint(operation), authType)
|
2015-09-01 21:47:47 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:23:36 +00:00
|
|
|
func (c *Configuration) FindNetrcHost(host string) (*netrc.Machine, error) {
|
|
|
|
c.loading.Lock()
|
|
|
|
defer c.loading.Unlock()
|
|
|
|
if c.parsedNetrc == nil {
|
|
|
|
n, err := c.parseNetrc()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c.parsedNetrc = n
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.parsedNetrc.FindMachine(host), nil
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:38:06 +00:00
|
|
|
// Manually override the netrc config
|
|
|
|
func (c *Configuration) SetNetrc(n netrcfinder) {
|
|
|
|
c.parsedNetrc = n
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:53:43 +00:00
|
|
|
func (c *Configuration) EndpointAccess(e Endpoint) string {
|
2015-09-01 21:47:47 +00:00
|
|
|
key := fmt.Sprintf("lfs.%s.access", e.Url)
|
2015-09-01 21:48:47 +00:00
|
|
|
if v, ok := c.GitConfig(key); ok && len(v) > 0 {
|
2015-09-02 19:03:37 +00:00
|
|
|
lower := strings.ToLower(v)
|
|
|
|
if lower == "private" {
|
|
|
|
return "basic"
|
|
|
|
}
|
|
|
|
return lower
|
2015-06-18 16:46:27 +00:00
|
|
|
}
|
2015-09-02 18:53:43 +00:00
|
|
|
return "none"
|
2015-06-18 16:46:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 18:53:43 +00:00
|
|
|
func (c *Configuration) SetEndpointAccess(e Endpoint, authType string) {
|
2015-09-01 17:24:10 +00:00
|
|
|
tracerx.Printf("setting repository access to %s", authType)
|
2015-09-01 21:47:47 +00:00
|
|
|
key := fmt.Sprintf("lfs.%s.access", e.Url)
|
2015-06-23 21:27:04 +00:00
|
|
|
|
|
|
|
// Modify the config cache because it's checked again in this process
|
|
|
|
// without being reloaded.
|
2015-09-02 18:53:43 +00:00
|
|
|
switch authType {
|
|
|
|
case "", "none":
|
2015-09-02 19:28:30 +00:00
|
|
|
git.Config.UnsetLocalKey("", key)
|
2015-09-01 17:24:10 +00:00
|
|
|
|
|
|
|
c.loading.Lock()
|
2015-10-01 20:18:37 +00:00
|
|
|
delete(c.gitConfig, strings.ToLower(key))
|
2015-09-01 17:24:10 +00:00
|
|
|
c.loading.Unlock()
|
2015-09-02 18:53:43 +00:00
|
|
|
default:
|
2015-09-02 19:28:30 +00:00
|
|
|
git.Config.SetLocal("", key, authType)
|
2015-09-01 17:24:10 +00:00
|
|
|
|
|
|
|
c.loading.Lock()
|
2015-10-01 20:18:37 +00:00
|
|
|
c.gitConfig[strings.ToLower(key)] = authType
|
2015-09-01 17:24:10 +00:00
|
|
|
c.loading.Unlock()
|
|
|
|
}
|
2015-06-23 21:27:04 +00:00
|
|
|
}
|
|
|
|
|
2015-08-06 13:56:57 +00:00
|
|
|
func (c *Configuration) FetchIncludePaths() []string {
|
|
|
|
c.loadGitConfig()
|
|
|
|
return c.fetchIncludePaths
|
|
|
|
}
|
|
|
|
func (c *Configuration) FetchExcludePaths() []string {
|
|
|
|
c.loadGitConfig()
|
|
|
|
return c.fetchExcludePaths
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
func (c *Configuration) RemoteEndpoint(remote, operation string) Endpoint {
|
2015-02-02 22:39:03 +00:00
|
|
|
if len(remote) == 0 {
|
|
|
|
remote = defaultRemote
|
|
|
|
}
|
2016-01-25 15:09:55 +00:00
|
|
|
|
|
|
|
// Support separate push URL if specified and pushing
|
|
|
|
if operation == "upload" {
|
|
|
|
if url, ok := c.GitConfig("remote." + remote + ".lfspushurl"); ok {
|
2016-02-02 17:42:20 +00:00
|
|
|
return NewEndpointWithConfig(url, c)
|
2016-01-25 15:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-15 19:24:44 +00:00
|
|
|
if url, ok := c.GitConfig("remote." + remote + ".lfsurl"); ok {
|
2016-02-02 17:42:20 +00:00
|
|
|
return NewEndpointWithConfig(url, c)
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 15:09:55 +00:00
|
|
|
// finally fall back on git remote url (also supports pushurl)
|
|
|
|
if url := c.GitRemoteUrl(remote, operation == "upload"); url != "" {
|
2016-02-02 17:42:20 +00:00
|
|
|
return NewEndpointFromCloneURLWithConfig(url, c)
|
2015-06-13 22:55:23 +00:00
|
|
|
}
|
2014-03-19 21:21:52 +00:00
|
|
|
|
2015-06-13 22:55:23 +00:00
|
|
|
return Endpoint{}
|
|
|
|
}
|
2014-03-19 21:21:52 +00:00
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
func (c *Configuration) Remotes() []string {
|
2015-03-05 19:49:15 +00:00
|
|
|
c.loadGitConfig()
|
2014-02-01 20:38:29 +00:00
|
|
|
return c.remotes
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:42:20 +00:00
|
|
|
// GitProtocol returns the protocol for the LFS API when converting from a
|
|
|
|
// git:// remote url.
|
|
|
|
func (c *Configuration) GitProtocol() string {
|
|
|
|
if value, ok := c.GitConfig("lfs.gitprotocol"); ok {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return "https"
|
|
|
|
}
|
|
|
|
|
2015-07-10 20:54:06 +00:00
|
|
|
func (c *Configuration) Extensions() map[string]Extension {
|
|
|
|
c.loadGitConfig()
|
|
|
|
return c.extensions
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:38:06 +00:00
|
|
|
// SortedExtensions gets the list of extensions ordered by Priority
|
|
|
|
func (c *Configuration) SortedExtensions() ([]Extension, error) {
|
|
|
|
return SortExtensions(c.Extensions())
|
|
|
|
}
|
|
|
|
|
2016-02-04 16:50:25 +00:00
|
|
|
// GitConfigInt parses a git config value and returns it as an integer.
|
|
|
|
func (c *Configuration) GitConfigInt(key string, def int) int {
|
|
|
|
s, _ := c.GitConfig(key)
|
|
|
|
if len(s) == 0 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
i, _ := strconv.Atoi(s)
|
|
|
|
if i < 1 {
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
2016-05-10 10:38:17 +00:00
|
|
|
// GitConfigBool parses a git config value and returns true if defined as
|
2016-05-11 09:07:34 +00:00
|
|
|
// true, 1, on, yes
|
2016-05-10 10:38:17 +00:00
|
|
|
func (c *Configuration) GitConfigBool(key string) bool {
|
|
|
|
s, _ := c.GitConfig(key)
|
|
|
|
if len(s) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-05-11 09:07:34 +00:00
|
|
|
ret, err := parseConfigBool(s)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return ret
|
2016-05-10 10:38:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
func (c *Configuration) GitConfig(key string) (string, bool) {
|
2015-03-05 19:49:15 +00:00
|
|
|
c.loadGitConfig()
|
2015-01-12 00:53:10 +00:00
|
|
|
value, ok := c.gitConfig[strings.ToLower(key)]
|
2014-02-01 20:38:29 +00:00
|
|
|
return value, ok
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:25:34 +00:00
|
|
|
func (c *Configuration) AllGitConfig() map[string]string {
|
|
|
|
c.loadGitConfig()
|
|
|
|
return c.gitConfig
|
|
|
|
}
|
|
|
|
|
2015-08-04 16:46:51 +00:00
|
|
|
func (c *Configuration) FetchPruneConfig() *FetchPruneConfig {
|
|
|
|
if c.fetchPruneConfig == nil {
|
|
|
|
c.fetchPruneConfig = &FetchPruneConfig{
|
|
|
|
FetchRecentRefsDays: 7,
|
2015-09-01 11:06:30 +00:00
|
|
|
FetchRecentRefsIncludeRemotes: true,
|
2015-08-17 11:04:43 +00:00
|
|
|
FetchRecentCommitsDays: 0,
|
2015-08-04 16:46:51 +00:00
|
|
|
PruneOffsetDays: 3,
|
2015-09-08 14:00:28 +00:00
|
|
|
PruneVerifyRemoteAlways: false,
|
|
|
|
PruneRemoteName: "origin",
|
2015-08-04 16:46:51 +00:00
|
|
|
}
|
|
|
|
if v, ok := c.GitConfig("lfs.fetchrecentrefsdays"); ok {
|
|
|
|
n, err := strconv.Atoi(v)
|
2015-08-27 14:17:40 +00:00
|
|
|
if err == nil && n >= 0 {
|
2015-08-04 16:46:51 +00:00
|
|
|
c.fetchPruneConfig.FetchRecentRefsDays = n
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 11:03:55 +00:00
|
|
|
if v, ok := c.GitConfig("lfs.fetchrecentremoterefs"); ok {
|
2015-09-02 17:52:38 +00:00
|
|
|
if b, err := parseConfigBool(v); err == nil {
|
|
|
|
c.fetchPruneConfig.FetchRecentRefsIncludeRemotes = b
|
2015-08-04 16:46:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := c.GitConfig("lfs.fetchrecentcommitsdays"); ok {
|
|
|
|
n, err := strconv.Atoi(v)
|
2015-08-27 14:17:40 +00:00
|
|
|
if err == nil && n >= 0 {
|
2015-08-04 16:46:51 +00:00
|
|
|
c.fetchPruneConfig.FetchRecentCommitsDays = n
|
|
|
|
}
|
|
|
|
}
|
2015-08-17 11:03:13 +00:00
|
|
|
if v, ok := c.GitConfig("lfs.fetchrecentalways"); ok {
|
2015-09-02 17:52:38 +00:00
|
|
|
if b, err := parseConfigBool(v); err == nil {
|
|
|
|
c.fetchPruneConfig.FetchRecentAlways = b
|
2015-08-17 11:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-04 16:46:51 +00:00
|
|
|
if v, ok := c.GitConfig("lfs.pruneoffsetdays"); ok {
|
|
|
|
n, err := strconv.Atoi(v)
|
2015-08-27 14:17:40 +00:00
|
|
|
if err == nil && n >= 0 {
|
2015-08-04 16:46:51 +00:00
|
|
|
c.fetchPruneConfig.PruneOffsetDays = n
|
|
|
|
}
|
|
|
|
}
|
2015-09-08 14:00:28 +00:00
|
|
|
if v, ok := c.GitConfig("lfs.pruneverifyremotealways"); ok {
|
|
|
|
if b, err := parseConfigBool(v); err == nil {
|
|
|
|
c.fetchPruneConfig.PruneVerifyRemoteAlways = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if v, ok := c.GitConfig("lfs.pruneremotetocheck"); ok {
|
|
|
|
c.fetchPruneConfig.PruneRemoteName = v
|
|
|
|
}
|
2015-08-04 16:46:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return c.fetchPruneConfig
|
|
|
|
}
|
|
|
|
|
2016-05-10 10:38:17 +00:00
|
|
|
func (c *Configuration) SkipDownloadErrors() bool {
|
|
|
|
return c.GetenvBool("GIT_LFS_SKIP_DOWNLOAD_ERRORS", false) || c.GitConfigBool("lfs.skipdownloaderrors")
|
|
|
|
}
|
|
|
|
|
2015-09-02 17:52:38 +00:00
|
|
|
func parseConfigBool(str string) (bool, error) {
|
|
|
|
switch strings.ToLower(str) {
|
|
|
|
case "true", "1", "on", "yes", "t":
|
|
|
|
return true, nil
|
|
|
|
case "false", "0", "off", "no", "f":
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, fmt.Errorf("Unable to parse %q as a boolean", str)
|
|
|
|
}
|
|
|
|
|
2015-08-28 21:19:52 +00:00
|
|
|
func (c *Configuration) loadGitConfig() bool {
|
2015-04-23 01:07:52 +00:00
|
|
|
c.loading.Lock()
|
|
|
|
defer c.loading.Unlock()
|
|
|
|
|
2015-03-05 19:49:15 +00:00
|
|
|
if c.gitConfig != nil {
|
2015-08-28 21:19:52 +00:00
|
|
|
return false
|
2015-03-05 19:49:15 +00:00
|
|
|
}
|
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
c.gitConfig = make(map[string]string)
|
2015-07-10 20:54:06 +00:00
|
|
|
c.extensions = make(map[string]Extension)
|
2015-11-16 19:53:01 +00:00
|
|
|
uniqRemotes := make(map[string]bool)
|
2014-03-12 15:31:41 +00:00
|
|
|
|
2015-11-16 19:53:01 +00:00
|
|
|
configFiles := []string{
|
|
|
|
filepath.Join(LocalWorkingDir, ".lfsconfig"),
|
2015-11-16 20:07:36 +00:00
|
|
|
|
2015-11-16 23:39:49 +00:00
|
|
|
// TODO: remove .gitconfig support for Git LFS v2.0 https://github.com/github/git-lfs/issues/839
|
2015-11-16 19:53:01 +00:00
|
|
|
filepath.Join(LocalWorkingDir, ".gitconfig"),
|
2014-06-03 22:03:43 +00:00
|
|
|
}
|
2015-11-16 19:53:01 +00:00
|
|
|
c.readGitConfigFromFiles(configFiles, 0, uniqRemotes)
|
2014-06-03 22:03:43 +00:00
|
|
|
|
2015-11-16 19:53:01 +00:00
|
|
|
listOutput, err := git.Config.List()
|
2015-06-23 21:27:04 +00:00
|
|
|
if err != nil {
|
2015-11-16 19:53:01 +00:00
|
|
|
panic(fmt.Errorf("Error listing git config: %s", err))
|
2015-06-23 21:27:04 +00:00
|
|
|
}
|
|
|
|
|
2015-10-20 16:31:56 +00:00
|
|
|
c.readGitConfig(listOutput, uniqRemotes, false)
|
|
|
|
|
|
|
|
c.remotes = make([]string, 0, len(uniqRemotes))
|
|
|
|
for remote, isOrigin := range uniqRemotes {
|
|
|
|
if isOrigin {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c.remotes = append(c.remotes, remote)
|
|
|
|
}
|
2014-03-12 15:31:41 +00:00
|
|
|
|
2015-10-20 16:31:56 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:53:01 +00:00
|
|
|
func (c *Configuration) readGitConfigFromFiles(filenames []string, filenameIndex int, uniqRemotes map[string]bool) {
|
|
|
|
filename := filenames[filenameIndex]
|
|
|
|
_, err := os.Stat(filename)
|
|
|
|
if err == nil {
|
2015-11-16 21:57:44 +00:00
|
|
|
if filenameIndex > 0 && ShowConfigWarnings {
|
|
|
|
expected := ".lfsconfig"
|
|
|
|
fmt.Fprintf(os.Stderr, "WARNING: Reading LFS config from %q, not %q. Rename to %q before Git LFS v2.0 to remove this warning.\n",
|
|
|
|
filepath.Base(filename), expected, expected)
|
|
|
|
}
|
|
|
|
|
2015-11-16 19:53:01 +00:00
|
|
|
fileOutput, err := git.Config.ListFromFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("Error listing git config from %s: %s", filename, err))
|
|
|
|
}
|
|
|
|
c.readGitConfig(fileOutput, uniqRemotes, true)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
newIndex := filenameIndex + 1
|
|
|
|
if len(filenames) > newIndex {
|
|
|
|
c.readGitConfigFromFiles(filenames, newIndex, uniqRemotes)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
panic(fmt.Errorf("Error listing git config from %s: %s", filename, err))
|
|
|
|
}
|
|
|
|
|
2015-10-23 16:14:13 +00:00
|
|
|
func (c *Configuration) readGitConfig(output string, uniqRemotes map[string]bool, onlySafe bool) {
|
2014-03-12 15:31:41 +00:00
|
|
|
lines := strings.Split(output, "\n")
|
2015-11-16 21:47:28 +00:00
|
|
|
uniqKeys := make(map[string]string)
|
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
for _, line := range lines {
|
|
|
|
pieces := strings.SplitN(line, "=", 2)
|
2014-03-12 15:31:41 +00:00
|
|
|
if len(pieces) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
2015-10-20 16:31:56 +00:00
|
|
|
|
2015-10-23 16:14:13 +00:00
|
|
|
allowed := !onlySafe
|
2015-01-12 00:53:10 +00:00
|
|
|
key := strings.ToLower(pieces[0])
|
2015-07-10 20:54:06 +00:00
|
|
|
value := pieces[1]
|
2014-02-01 20:38:29 +00:00
|
|
|
|
2015-11-24 18:58:52 +00:00
|
|
|
if origKey, ok := uniqKeys[key]; ok {
|
|
|
|
if ShowConfigWarnings && c.gitConfig[key] != value && strings.HasPrefix(key, gitConfigWarningPrefix) {
|
|
|
|
fmt.Fprintf(os.Stderr, "WARNING: These git config values clash:\n")
|
|
|
|
fmt.Fprintf(os.Stderr, " git config %q = %q\n", origKey, c.gitConfig[key])
|
|
|
|
fmt.Fprintf(os.Stderr, " git config %q = %q\n", pieces[0], value)
|
|
|
|
}
|
2015-11-16 21:47:28 +00:00
|
|
|
} else {
|
|
|
|
uniqKeys[key] = pieces[0]
|
|
|
|
}
|
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
keyParts := strings.Split(key, ".")
|
2015-10-20 16:31:56 +00:00
|
|
|
if len(keyParts) == 4 && keyParts[0] == "lfs" && keyParts[1] == "extension" {
|
2015-08-02 01:09:06 +00:00
|
|
|
name := keyParts[2]
|
2015-07-10 20:54:06 +00:00
|
|
|
ext := c.extensions[name]
|
2015-08-02 01:09:06 +00:00
|
|
|
switch keyParts[3] {
|
2015-07-10 20:54:06 +00:00
|
|
|
case "clean":
|
2015-10-23 16:14:13 +00:00
|
|
|
if onlySafe {
|
2015-10-20 16:31:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-07-10 20:54:06 +00:00
|
|
|
ext.Clean = value
|
|
|
|
case "smudge":
|
2015-10-23 16:14:13 +00:00
|
|
|
if onlySafe {
|
2015-10-20 16:31:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-07-10 20:54:06 +00:00
|
|
|
ext.Smudge = value
|
|
|
|
case "priority":
|
2015-10-20 16:31:56 +00:00
|
|
|
allowed = true
|
2015-07-10 20:54:06 +00:00
|
|
|
p, err := strconv.Atoi(value)
|
|
|
|
if err == nil && p >= 0 {
|
|
|
|
ext.Priority = p
|
|
|
|
}
|
|
|
|
}
|
2015-10-20 16:31:56 +00:00
|
|
|
|
2015-07-10 20:54:06 +00:00
|
|
|
ext.Name = name
|
|
|
|
c.extensions[name] = ext
|
2015-10-20 16:31:56 +00:00
|
|
|
} else if len(keyParts) > 1 && keyParts[0] == "remote" {
|
2015-10-23 16:14:13 +00:00
|
|
|
if onlySafe && (len(keyParts) == 3 && keyParts[2] != "lfsurl") {
|
2015-10-20 16:31:56 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
allowed = true
|
|
|
|
remote := keyParts[1]
|
|
|
|
uniqRemotes[remote] = remote == "origin"
|
2016-04-04 22:38:17 +00:00
|
|
|
} else if len(keyParts) > 2 && keyParts[len(keyParts)-1] == "access" {
|
|
|
|
allowed = true
|
2015-10-20 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
2015-10-26 16:46:58 +00:00
|
|
|
if !allowed && keyIsUnsafe(key) {
|
2015-10-20 16:31:56 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
c.gitConfig[key] = value
|
|
|
|
|
|
|
|
if len(keyParts) == 2 && keyParts[0] == "lfs" && keyParts[1] == "fetchinclude" {
|
2015-08-06 13:56:57 +00:00
|
|
|
for _, inc := range strings.Split(value, ",") {
|
|
|
|
inc = strings.TrimSpace(inc)
|
|
|
|
c.fetchIncludePaths = append(c.fetchIncludePaths, inc)
|
|
|
|
}
|
|
|
|
} else if len(keyParts) == 2 && keyParts[0] == "lfs" && keyParts[1] == "fetchexclude" {
|
|
|
|
for _, ex := range strings.Split(value, ",") {
|
|
|
|
ex = strings.TrimSpace(ex)
|
|
|
|
c.fetchExcludePaths = append(c.fetchExcludePaths, ex)
|
|
|
|
}
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 16:46:58 +00:00
|
|
|
func keyIsUnsafe(key string) bool {
|
|
|
|
for _, safe := range safeKeys {
|
|
|
|
if safe == key {
|
|
|
|
return false
|
2014-02-01 21:04:40 +00:00
|
|
|
}
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
2015-08-28 21:19:52 +00:00
|
|
|
return true
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
2015-10-26 16:46:58 +00:00
|
|
|
|
|
|
|
var safeKeys = []string{
|
|
|
|
"lfs.fetchexclude",
|
2016-02-02 17:45:49 +00:00
|
|
|
"lfs.fetchinclude",
|
|
|
|
"lfs.gitprotocol",
|
|
|
|
"lfs.url",
|
2015-10-26 16:46:58 +00:00
|
|
|
}
|
2016-05-13 16:38:06 +00:00
|
|
|
|
|
|
|
// only used for tests
|
|
|
|
func (c *Configuration) SetConfig(key, value string) {
|
|
|
|
if c.loadGitConfig() {
|
|
|
|
c.loading.Lock()
|
|
|
|
c.origConfig = make(map[string]string)
|
|
|
|
for k, v := range c.gitConfig {
|
|
|
|
c.origConfig[k] = v
|
|
|
|
}
|
|
|
|
c.loading.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.gitConfig[key] = value
|
|
|
|
}
|
|
|
|
|
2016-05-17 13:40:05 +00:00
|
|
|
func (c *Configuration) ClearConfig() {
|
|
|
|
if c.loadGitConfig() {
|
|
|
|
c.loading.Lock()
|
|
|
|
c.origConfig = make(map[string]string)
|
|
|
|
for k, v := range c.gitConfig {
|
|
|
|
c.origConfig[k] = v
|
|
|
|
}
|
|
|
|
c.loading.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.gitConfig = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2016-05-13 16:38:06 +00:00
|
|
|
func (c *Configuration) ResetConfig() {
|
|
|
|
c.loading.Lock()
|
|
|
|
c.gitConfig = make(map[string]string)
|
|
|
|
for k, v := range c.origConfig {
|
|
|
|
c.gitConfig[k] = v
|
|
|
|
}
|
|
|
|
c.loading.Unlock()
|
|
|
|
}
|