git-lfs/lfs/config.go

373 lines
9.4 KiB
Go
Raw Normal View History

2015-03-19 19:30:55 +00:00
package lfs
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"
2015-01-12 00:53:10 +00:00
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"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-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
)
var (
Config = NewConfig()
defaultRemote = "origin"
)
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
// 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
// LFS changes too (default 0 = only fetch at ref)
2015-08-04 16:46:51 +00:00
FetchRecentCommitsDays int
// 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
}
2013-11-05 17:07:03 +00:00
type Configuration struct {
2015-02-17 16:39:15 +00:00
CurrentRemote string
httpClient *HttpClient
redirectingHttpClient *http.Client
envVars map[string]string
2015-03-05 19:49:15 +00:00
isTracingHttp bool
isLoggingStats bool
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
remotes []string
extensions map[string]Extension
fetchIncludePaths []string
fetchExcludePaths []string
2015-08-04 16:46:51 +00:00
fetchPruneConfig *FetchPruneConfig
2013-11-05 17:07:03 +00:00
}
2015-03-05 19:59:52 +00:00
func NewConfig() *Configuration {
c := &Configuration{
CurrentRemote: defaultRemote,
envVars: make(map[string]string),
2015-03-05 19:59:52 +00:00
}
c.isTracingHttp = c.GetenvBool("GIT_CURL_VERBOSE", false)
c.isLoggingStats = c.GetenvBool("GIT_LOG_STATS", false)
2015-03-05 19:59:52 +00:00
return c
}
func (c *Configuration) Getenv(key string) string {
if i, ok := c.envVars[key]; ok {
return i
}
v := os.Getenv(key)
c.envVars[key] = v
return v
}
func (c *Configuration) Setenv(key, value string) error {
// 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)
}
// 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
}
b, err := strconv.ParseBool(s)
if err != nil {
return def
}
return b
}
func (c *Configuration) Endpoint() Endpoint {
2015-03-19 19:30:55 +00:00
if url, ok := c.GitConfig("lfs.url"); ok {
return NewEndpoint(url)
2013-11-05 17:07:03 +00:00
}
if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
if endpoint := c.RemoteEndpoint(c.CurrentRemote); len(endpoint.Url) > 0 {
return endpoint
}
}
return c.RemoteEndpoint(defaultRemote)
2013-11-05 17:07:03 +00:00
}
2013-11-05 17:45:01 +00:00
func (c *Configuration) ConcurrentTransfers() int {
uploads := 3
if v, ok := c.GitConfig("lfs.concurrenttransfers"); ok {
n, err := strconv.Atoi(v)
if err == nil && n > 0 {
uploads = n
}
}
return uploads
}
2015-05-07 18:37:30 +00:00
func (c *Configuration) BatchTransfer() bool {
if v, ok := c.GitConfig("lfs.batch"); ok {
if v == "true" || v == "" {
return true
}
// Any numeric value except 0 is considered true
n, _ := strconv.Atoi(v)
return n != 0
2015-05-07 18:37:30 +00:00
}
return true
2015-05-07 18:37:30 +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.
func (c *Configuration) PrivateAccess() bool {
2015-09-02 18:53:43 +00:00
return c.Access() != "none"
}
// Access returns the access auth type.
2015-09-02 18:53:43 +00:00
func (c *Configuration) Access() string {
return c.EndpointAccess(c.Endpoint())
}
// SetAccess will set the private access flag in .git/config.
2015-09-02 18:53:43 +00:00
func (c *Configuration) SetAccess(authType string) {
c.SetEndpointAccess(c.Endpoint(), authType)
}
2015-09-02 18:53:43 +00:00
func (c *Configuration) EndpointAccess(e Endpoint) string {
key := fmt.Sprintf("lfs.%s.access", e.Url)
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-09-02 18:53:43 +00:00
return "none"
}
2015-09-02 18:53:43 +00:00
func (c *Configuration) SetEndpointAccess(e Endpoint, authType string) {
tracerx.Printf("setting repository access to %s", authType)
key := fmt.Sprintf("lfs.%s.access", e.Url)
configFile := filepath.Join(LocalGitDir, "config")
// 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":
git.Config.UnsetLocalKey(configFile, key)
c.loading.Lock()
delete(c.gitConfig, key)
c.loading.Unlock()
2015-09-02 18:53:43 +00:00
default:
git.Config.SetLocal(configFile, key, authType)
c.loading.Lock()
2015-09-02 18:53:43 +00:00
c.gitConfig[key] = authType
c.loading.Unlock()
}
}
func (c *Configuration) FetchIncludePaths() []string {
c.loadGitConfig()
return c.fetchIncludePaths
}
func (c *Configuration) FetchExcludePaths() []string {
c.loadGitConfig()
return c.fetchExcludePaths
}
func (c *Configuration) RemoteEndpoint(remote string) Endpoint {
if len(remote) == 0 {
remote = defaultRemote
}
if url, ok := c.GitConfig("remote." + remote + ".lfsurl"); ok {
return NewEndpoint(url)
}
if url, ok := c.GitConfig("remote." + remote + ".url"); ok {
return NewEndpointFromCloneURL(url)
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
func (c *Configuration) Remotes() []string {
2015-03-05 19:49:15 +00:00
c.loadGitConfig()
return c.remotes
}
2015-07-10 20:54:06 +00:00
func (c *Configuration) Extensions() map[string]Extension {
c.loadGitConfig()
return c.extensions
}
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)]
return value, ok
}
2015-03-19 20:39:44 +00:00
func (c *Configuration) ObjectUrl(oid string) (*url.URL, error) {
return ObjectUrl(c.Endpoint(), oid)
}
2015-08-04 16:46:51 +00:00
func (c *Configuration) FetchPruneConfig() *FetchPruneConfig {
if c.fetchPruneConfig == nil {
c.fetchPruneConfig = &FetchPruneConfig{
FetchRecentRefsDays: 7,
FetchRecentRefsIncludeRemotes: true,
FetchRecentCommitsDays: 0,
2015-08-04 16:46:51 +00:00
PruneOffsetDays: 3,
}
if v, ok := c.GitConfig("lfs.fetchrecentrefsdays"); ok {
n, err := strconv.Atoi(v)
if err == nil && n >= 0 {
2015-08-04 16:46:51 +00:00
c.fetchPruneConfig.FetchRecentRefsDays = n
}
}
if v, ok := c.GitConfig("lfs.fetchrecentremoterefs"); ok {
2015-08-04 16:46:51 +00:00
if v == "false" {
c.fetchPruneConfig.FetchRecentRefsIncludeRemotes = false
2015-08-04 16:46:51 +00:00
}
// Any numeric value except 0 is considered true
if n, err := strconv.Atoi(v); err == nil && n == 0 {
c.fetchPruneConfig.FetchRecentRefsIncludeRemotes = false
2015-08-04 16:46:51 +00:00
}
}
if v, ok := c.GitConfig("lfs.fetchrecentcommitsdays"); ok {
n, err := strconv.Atoi(v)
if err == nil && n >= 0 {
2015-08-04 16:46:51 +00:00
c.fetchPruneConfig.FetchRecentCommitsDays = n
}
}
if v, ok := c.GitConfig("lfs.fetchrecentalways"); ok {
if v == "true" || v == "" {
c.fetchPruneConfig.FetchRecentAlways = true
}
}
2015-08-04 16:46:51 +00:00
if v, ok := c.GitConfig("lfs.pruneoffsetdays"); ok {
n, err := strconv.Atoi(v)
if err == nil && n >= 0 {
2015-08-04 16:46:51 +00:00
c.fetchPruneConfig.PruneOffsetDays = n
}
}
}
return c.fetchPruneConfig
}
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
}
uniqRemotes := make(map[string]bool)
c.gitConfig = make(map[string]string)
2015-07-10 20:54:06 +00:00
c.extensions = make(map[string]Extension)
var output string
listOutput, err := git.Config.List()
if err != nil {
2014-06-05 18:48:23 +00:00
panic(fmt.Errorf("Error listing git config: %s", err))
}
configFile := filepath.Join(LocalWorkingDir, ".gitconfig")
fileOutput, err := git.Config.ListFromFile(configFile)
if err != nil {
2014-06-05 18:48:23 +00:00
panic(fmt.Errorf("Error listing git config from file: %s", err))
}
localConfig := filepath.Join(LocalGitDir, "config")
localOutput, err := git.Config.ListFromFile(localConfig)
if err != nil {
panic(fmt.Errorf("Error listing git config from file %s", err))
}
output = fileOutput + "\n" + listOutput + "\n" + localOutput
lines := strings.Split(output, "\n")
for _, line := range lines {
pieces := strings.SplitN(line, "=", 2)
if len(pieces) < 2 {
continue
}
2015-01-12 00:53:10 +00:00
key := strings.ToLower(pieces[0])
2015-07-10 20:54:06 +00:00
value := pieces[1]
c.gitConfig[key] = value
keyParts := strings.Split(key, ".")
if len(keyParts) > 1 && keyParts[0] == "remote" {
2014-02-01 21:04:40 +00:00
remote := keyParts[1]
uniqRemotes[remote] = remote == "origin"
} else if len(keyParts) == 4 && keyParts[0] == "lfs" && keyParts[1] == "extension" {
name := keyParts[2]
2015-07-10 20:54:06 +00:00
ext := c.extensions[name]
switch keyParts[3] {
2015-07-10 20:54:06 +00:00
case "clean":
ext.Clean = value
case "smudge":
ext.Smudge = value
case "priority":
p, err := strconv.Atoi(value)
if err == nil && p >= 0 {
ext.Priority = p
}
}
ext.Name = name
c.extensions[name] = ext
} else if len(keyParts) == 2 && keyParts[0] == "lfs" && keyParts[1] == "fetchinclude" {
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 21:04:40 +00:00
c.remotes = make([]string, 0, len(uniqRemotes))
for remote, isOrigin := range uniqRemotes {
if isOrigin {
continue
}
c.remotes = append(c.remotes, remote)
}
2015-08-28 21:19:52 +00:00
return true
}