git-lfs/hawser/config.go

158 lines
3.2 KiB
Go
Raw Normal View History

2015-01-30 18:13:15 +00:00
package hawser
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-30 19:08:47 +00:00
"github.com/hawser/git-hawser/git"
2015-01-12 00:53:10 +00:00
"net/http"
"net/url"
"os"
"path"
2014-03-19 21:21:52 +00:00
"regexp"
"strings"
2013-11-05 17:45:01 +00:00
)
2013-11-05 17:07:03 +00:00
type Configuration struct {
2015-02-17 16:39:15 +00:00
CurrentRemote string
gitConfig map[string]string
remotes []string
httpClient *http.Client
redirectingHttpClient *http.Client
2015-03-05 19:49:15 +00:00
isTracingHttp bool
2013-11-05 17:07:03 +00:00
}
2014-05-22 23:02:56 +00:00
var (
2015-03-05 19:59:52 +00:00
Config = NewConfig()
RedirectError = fmt.Errorf("Unexpected redirection")
httpPrefixRe = regexp.MustCompile("\\Ahttps?://")
defaultRemote = "origin"
2014-05-22 23:02:56 +00:00
)
2013-11-05 17:07:03 +00:00
2015-03-05 19:59:52 +00:00
func NewConfig() *Configuration {
c := &Configuration{
CurrentRemote: defaultRemote,
isTracingHttp: len(os.Getenv("GIT_CURL_VERBOSE")) > 0,
2015-03-05 19:59:52 +00:00
}
return c
}
func (c *Configuration) Endpoint() string {
2015-01-30 20:04:52 +00:00
if url, ok := c.GitConfig("hawser.url"); ok {
return 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) > 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) RemoteEndpoint(remote string) string {
if len(remote) == 0 {
remote = defaultRemote
}
2015-01-30 20:04:52 +00:00
if url, ok := c.GitConfig("remote." + remote + ".hawser"); ok {
return url
}
if url, ok := c.GitConfig("remote." + remote + ".url"); ok {
2014-05-22 23:02:56 +00:00
if !httpPrefixRe.MatchString(url) {
pieces := strings.SplitN(url, ":", 2)
hostPieces := strings.SplitN(pieces[0], "@", 2)
2014-05-28 20:48:43 +00:00
if len(hostPieces) < 2 {
return "unknown"
}
2014-05-22 23:02:56 +00:00
url = fmt.Sprintf("https://%s/%s", hostPieces[1], pieces[1])
2014-03-19 21:21:52 +00:00
}
if path.Ext(url) == ".git" {
return url + "/info/media"
}
return url + ".git/info/media"
}
return ""
}
func (c *Configuration) Remotes() []string {
2015-03-05 19:49:15 +00:00
c.loadGitConfig()
return c.remotes
}
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
}
func (c *Configuration) SetConfig(key, value string) {
2015-03-05 19:49:15 +00:00
c.loadGitConfig()
c.gitConfig[key] = value
}
func (c *Configuration) ObjectUrl(oid string) *url.URL {
u, _ := url.Parse(c.Endpoint())
u.Path = path.Join(u.Path, "objects", oid)
return u
}
type AltConfig struct {
Remote map[string]*struct {
Media string
}
Media struct {
Url string
}
}
func (c *Configuration) loadGitConfig() {
2015-03-05 19:49:15 +00:00
if c.gitConfig != nil {
return
}
uniqRemotes := make(map[string]bool)
c.gitConfig = make(map[string]string)
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))
}
fileOutput, err := git.Config.ListFromFile()
if err != nil {
2014-06-05 18:48:23 +00:00
panic(fmt.Errorf("Error listing git config from file: %s", err))
}
output = listOutput + "\n" + fileOutput
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])
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)
}
}