git-lfs/config.go

112 lines
2.0 KiB
Go
Raw Normal View History

2013-11-05 17:07:03 +00:00
package gitmedia
2013-11-05 17:45:01 +00:00
import (
"os"
"path"
"strings"
2013-11-05 17:45:01 +00:00
)
2013-11-05 17:07:03 +00:00
type Configuration struct {
gitConfig map[string]string
remotes []string
2013-11-05 17:07:03 +00:00
}
var Config = &Configuration{}
2013-11-05 17:07:03 +00:00
func (c *Configuration) Endpoint() string {
if url, ok := c.GitConfig("media.url"); ok {
return url
2013-11-05 17:07:03 +00:00
}
2014-02-01 21:04:40 +00:00
return c.RemoteEndpoint("origin")
2013-11-05 17:07:03 +00:00
}
2013-11-05 17:45:01 +00:00
func (c *Configuration) RemoteEndpoint(remote string) string {
2014-02-01 21:09:52 +00:00
if url, ok := c.GitConfig("remote." + remote + ".media"); ok {
return url
}
if url, ok := c.GitConfig("remote." + remote + ".url"); ok {
if path.Ext(url) == ".git" {
return url + "/info/media"
}
return url + ".git/info/media"
}
return ""
}
func (c *Configuration) Remotes() []string {
if c.remotes == nil {
c.loadGitConfig()
}
return c.remotes
}
func (c *Configuration) GitConfig(key string) (string, bool) {
if c.gitConfig == nil {
c.loadGitConfig()
}
value, ok := c.gitConfig[key]
return value, ok
}
func (c *Configuration) SetConfig(key, value string) {
if c.gitConfig == nil {
c.loadGitConfig()
}
c.gitConfig[key] = value
}
type AltConfig struct {
Remote map[string]*struct {
Media string
}
Media struct {
Url string
}
}
func (c *Configuration) loadGitConfig() {
uniqRemotes := make(map[string]bool)
c.gitConfig = make(map[string]string)
var output string
output = SimpleExec("git", "config", "-l")
output += "\n"
output += SimpleExec("git", "config", "-l", "-f", ".gitconfig")
lines := strings.Split(output, "\n")
for _, line := range lines {
pieces := strings.SplitN(line, "=", 2)
if len(pieces) < 2 {
continue
}
key := pieces[0]
c.gitConfig[key] = pieces[1]
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"
}
}
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)
}
}
func configFileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}