From 81d429bc0f13d05770fa5a824202e07f6f80c001 Mon Sep 17 00:00:00 2001 From: Axel Christ Date: Wed, 28 Oct 2020 16:52:52 +0100 Subject: Fix relative submodule resolution With the current behavior, the config will always hold the resolved, absolute URL, leavin the user of go-git no choice to determine whether the original URL is relative or not. This changes to employ relative URL resolution only when resolving a submodule to a repository to keep the correct configuration 'unresolved' and intact. Change relative resolution using `filepath.Dir` to `path.Join` while parsing both the 'root' and the relative URL with `net/url.URL`. Adapt test to verify the new behavior. Re-fixes #184 (see comments). --- submodule.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'submodule.go') diff --git a/submodule.go b/submodule.go index dff26b0..b6bef46 100644 --- a/submodule.go +++ b/submodule.go @@ -5,6 +5,8 @@ import ( "context" "errors" "fmt" + "net/url" + "path" "github.com/go-git/go-billy/v5" "github.com/go-git/go-git/v5/config" @@ -131,9 +133,29 @@ func (s *Submodule) Repository() (*Repository, error) { return nil, err } + moduleURL, err := url.Parse(s.c.URL) + if err != nil { + return nil, err + } + + if !path.IsAbs(moduleURL.Path) { + remotes, err := s.w.r.Remotes() + if err != nil { + return nil, err + } + + rootURL, err := url.Parse(remotes[0].c.URLs[0]) + if err != nil { + return nil, err + } + + rootURL.Path = path.Join(rootURL.Path, moduleURL.Path) + *moduleURL = *rootURL + } + _, err = r.CreateRemote(&config.RemoteConfig{ Name: DefaultRemoteName, - URLs: []string{s.c.URL}, + URLs: []string{moduleURL.String()}, }) return r, err -- cgit