9 type URLConfig struct {
13 func NewURLConfig(git Environment) *URLConfig {
15 git = EnvironmentOf(make(mapFetcher))
23 // Get retrieves a `http.{url}.{key}` for the given key and urls, following the
24 // rules in https://git-scm.com/docs/git-config#git-config-httplturlgt.
25 // The value for `http.{key}` is returned as a fallback if no config keys are
26 // set for the given urls.
27 func (c *URLConfig) Get(prefix, rawurl, key string) (string, bool) {
32 key = strings.ToLower(key)
33 prefix = strings.ToLower(prefix)
34 if v := c.getAll(prefix, rawurl, key); len(v) > 0 {
35 return v[len(v)-1], true
37 return c.git.Get(strings.Join([]string{prefix, key}, "."))
40 func (c *URLConfig) GetAll(prefix, rawurl, key string) []string {
45 key = strings.ToLower(key)
46 prefix = strings.ToLower(prefix)
47 if v := c.getAll(prefix, rawurl, key); len(v) > 0 {
50 return c.git.GetAll(strings.Join([]string{prefix, key}, "."))
53 func (c *URLConfig) getAll(prefix, rawurl, key string) []string {
54 hosts, paths := c.hostsAndPaths(rawurl)
56 for i := len(paths); i > 0; i-- {
57 for _, host := range hosts {
58 path := strings.Join(paths[:i], slash)
59 if v := c.git.GetAll(fmt.Sprintf("%s.%s/%s.%s", prefix, host, path, key)); len(v) > 0 {
62 if v := c.git.GetAll(fmt.Sprintf("%s.%s/%s/.%s", prefix, host, path, key)); len(v) > 0 {
66 if isDefaultLFSUrl(path, paths, i) {
67 path = path[0 : len(path)-4]
68 if v := c.git.GetAll(fmt.Sprintf("%s.%s/%s.%s", prefix, host, path, key)); len(v) > 0 {
75 for _, host := range hosts {
76 if v := c.git.GetAll(fmt.Sprintf("%s.%s.%s", prefix, host, key)); len(v) > 0 {
79 if v := c.git.GetAll(fmt.Sprintf("%s.%s/.%s", prefix, host, key)); len(v) > 0 {
86 func (c *URLConfig) hostsAndPaths(rawurl string) (hosts, paths []string) {
87 u, err := url.Parse(rawurl)
92 return c.hosts(u), c.paths(u.Path)
95 func (c *URLConfig) hosts(u *url.URL) []string {
96 hosts := make([]string, 0, 1)
99 hosts = append(hosts, fmt.Sprintf("%s://%s@%s", u.Scheme, u.User.Username(), u.Host))
101 hosts = append(hosts, fmt.Sprintf("%s://%s", u.Scheme, u.Host))
106 func (c *URLConfig) paths(path string) []string {
113 if strings.HasSuffix(path, slash) {
116 return strings.Split(path[1:end], slash)
126 func isDefaultLFSUrl(path string, parts []string, index int) bool {
128 return false // shorter than ".git"
131 if !strings.HasSuffix(path, gitExt) {
135 if index > len(parts)-2 {
139 return parts[index] == infoPart && parts[index+1] == lfsPart