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-03-19 19:30:55 +00:00
|
|
|
"github.com/github/git-lfs/git"
|
2015-01-12 00:53:10 +00:00
|
|
|
"net/http"
|
2015-02-12 23:25:18 +00:00
|
|
|
"net/url"
|
2014-03-12 15:31:41 +00:00
|
|
|
"os"
|
2014-03-19 21:00:28 +00:00
|
|
|
"path"
|
2014-03-19 21:21:52 +00:00
|
|
|
"regexp"
|
2014-02-01 20:38:29 +00:00
|
|
|
"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
|
2015-02-13 23:51:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-27 22:36:33 +00:00
|
|
|
type Endpoint struct {
|
|
|
|
Url string
|
|
|
|
SshUserAndHost string
|
|
|
|
SshPath string
|
|
|
|
}
|
|
|
|
|
2014-05-22 23:02:56 +00:00
|
|
|
var (
|
2015-03-05 19:59:52 +00:00
|
|
|
Config = NewConfig()
|
2015-02-02 22:39:03 +00:00
|
|
|
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 {
|
2015-03-05 20:04:00 +00:00
|
|
|
c := &Configuration{
|
|
|
|
CurrentRemote: defaultRemote,
|
2015-03-05 21:10:17 +00:00
|
|
|
isTracingHttp: len(os.Getenv("GIT_CURL_VERBOSE")) > 0,
|
2015-03-05 19:59:52 +00:00
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2015-03-29 19:16:28 +00:00
|
|
|
func ObjectUrl(endpoint Endpoint, oid string) (*url.URL, error) {
|
|
|
|
u, err := url.Parse(endpoint.Url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Path = path.Join(u.Path, "objects")
|
|
|
|
if len(oid) > 0 {
|
|
|
|
u.Path = path.Join(u.Path, oid)
|
|
|
|
}
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
|
2015-03-27 22:36:33 +00:00
|
|
|
func (c *Configuration) Endpoint() Endpoint {
|
2015-03-19 19:30:55 +00:00
|
|
|
if url, ok := c.GitConfig("lfs.url"); ok {
|
2015-03-27 22:36:33 +00:00
|
|
|
return Endpoint{Url: url}
|
2013-11-05 17:07:03 +00:00
|
|
|
}
|
|
|
|
|
2015-02-02 22:39:03 +00:00
|
|
|
if len(c.CurrentRemote) > 0 && c.CurrentRemote != defaultRemote {
|
2015-03-27 22:36:33 +00:00
|
|
|
if endpoint := c.RemoteEndpoint(c.CurrentRemote); len(endpoint.Url) > 0 {
|
2015-02-02 22:39:03 +00:00
|
|
|
return endpoint
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.RemoteEndpoint(defaultRemote)
|
2013-11-05 17:07:03 +00:00
|
|
|
}
|
2013-11-05 17:45:01 +00:00
|
|
|
|
2015-03-27 22:36:33 +00:00
|
|
|
func (c *Configuration) RemoteEndpoint(remote string) Endpoint {
|
2015-02-02 22:39:03 +00:00
|
|
|
if len(remote) == 0 {
|
|
|
|
remote = defaultRemote
|
|
|
|
}
|
|
|
|
|
2015-04-15 19:24:44 +00:00
|
|
|
if url, ok := c.GitConfig("remote." + remote + ".lfsurl"); ok {
|
2015-03-27 22:36:33 +00:00
|
|
|
return Endpoint{Url: url}
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if url, ok := c.GitConfig("remote." + remote + ".url"); ok {
|
2015-03-27 22:36:33 +00:00
|
|
|
endpoint := Endpoint{Url: url}
|
|
|
|
|
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 {
|
2015-03-27 22:36:33 +00:00
|
|
|
endpoint.Url = "<unknown>"
|
|
|
|
return endpoint
|
2014-05-28 20:48:43 +00:00
|
|
|
}
|
2015-03-27 22:36:33 +00:00
|
|
|
|
|
|
|
endpoint.SshUserAndHost = pieces[0]
|
|
|
|
endpoint.SshPath = pieces[1]
|
|
|
|
endpoint.Url = fmt.Sprintf("https://%s/%s", hostPieces[1], pieces[1])
|
2014-03-19 21:21:52 +00:00
|
|
|
}
|
|
|
|
|
2014-03-19 21:00:28 +00:00
|
|
|
if path.Ext(url) == ".git" {
|
2015-03-27 22:36:33 +00:00
|
|
|
endpoint.Url += "/info/lfs"
|
|
|
|
} else {
|
|
|
|
endpoint.Url += ".git/info/lfs"
|
2014-03-19 21:00:28 +00:00
|
|
|
}
|
2015-03-27 22:36:33 +00:00
|
|
|
|
|
|
|
return endpoint
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 22:36:33 +00:00
|
|
|
return Endpoint{}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-02-01 20:46:58 +00:00
|
|
|
func (c *Configuration) SetConfig(key, value string) {
|
2015-03-05 19:49:15 +00:00
|
|
|
c.loadGitConfig()
|
2014-02-01 20:46:58 +00:00
|
|
|
c.gitConfig[key] = value
|
|
|
|
}
|
|
|
|
|
2015-03-19 20:39:44 +00:00
|
|
|
func (c *Configuration) ObjectUrl(oid string) (*url.URL, error) {
|
2015-03-29 19:16:28 +00:00
|
|
|
return ObjectUrl(c.Endpoint(), oid)
|
2015-02-12 23:25:18 +00:00
|
|
|
}
|
|
|
|
|
2014-03-12 15:31:41 +00:00
|
|
|
type AltConfig struct {
|
|
|
|
Remote map[string]*struct {
|
|
|
|
Media string
|
|
|
|
}
|
|
|
|
|
|
|
|
Media struct {
|
|
|
|
Url string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
func (c *Configuration) loadGitConfig() {
|
2015-03-05 19:49:15 +00:00
|
|
|
if c.gitConfig != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-01 20:38:29 +00:00
|
|
|
uniqRemotes := make(map[string]bool)
|
|
|
|
|
|
|
|
c.gitConfig = make(map[string]string)
|
2014-03-12 15:31:41 +00:00
|
|
|
|
|
|
|
var output string
|
2014-09-23 15:40:23 +00:00
|
|
|
listOutput, err := git.Config.List()
|
2014-06-03 22:03:43 +00:00
|
|
|
if err != nil {
|
2014-06-05 18:48:23 +00:00
|
|
|
panic(fmt.Errorf("Error listing git config: %s", err))
|
2014-06-03 22:03:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 15:40:23 +00:00
|
|
|
fileOutput, err := git.Config.ListFromFile()
|
2014-06-03 22:03:43 +00:00
|
|
|
if err != nil {
|
2014-06-05 18:48:23 +00:00
|
|
|
panic(fmt.Errorf("Error listing git config from file: %s", err))
|
2014-06-03 22:03:43 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 16:38:17 +00:00
|
|
|
output = fileOutput + "\n" + listOutput
|
2014-03-12 15:31:41 +00:00
|
|
|
|
|
|
|
lines := strings.Split(output, "\n")
|
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-01-12 00:53:10 +00:00
|
|
|
key := strings.ToLower(pieces[0])
|
2014-02-01 20:38:29 +00:00
|
|
|
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 20:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2014-02-01 20:38:29 +00:00
|
|
|
}
|
|
|
|
}
|