Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit ef441ca
Merge: dda3fd1 1dbd729
Author: matej.risek <matej.risek@hashicorp.com>
Date:   Mon May 8 14:56:31 2023 +0200

    Merge branch 'master' into fix/scp-style-submodule-url

    # Conflicts:
    #	submodule_test.go

commit dda3fd1
Merge: 6785da7 da73c5f
Author: matej.risek <matej.risek@hashicorp.com>
Date:   Mon May 8 14:55:31 2023 +0200

    Merge branch 'master' into fix/scp-style-submodule-url

commit 6785da7
Author: matej.risek <matej.risek@hashicorp.com>
Date:   Wed May 3 14:56:32 2023 +0200

    git: fix the issue with submodules having the SCP style URL fail due to the wrong URL parsing
  • Loading branch information
matejrisek committed Jun 5, 2023
1 parent 1dbd729 commit 240acb6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
14 changes: 7 additions & 7 deletions submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"context"
"errors"
"fmt"
"net/url"
"path"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/index"
"github.com/go-git/go-git/v5/plumbing/transport"
)

var (
Expand Down Expand Up @@ -133,29 +133,29 @@ func (s *Submodule) Repository() (*Repository, error) {
return nil, err
}

moduleURL, err := url.Parse(s.c.URL)
moduleEndpoint, err := transport.NewEndpoint(s.c.URL)
if err != nil {
return nil, err
}

if !path.IsAbs(moduleURL.Path) {
if !path.IsAbs(moduleEndpoint.Path) && moduleEndpoint.Protocol == "file" {
remotes, err := s.w.r.Remotes()
if err != nil {
return nil, err
}

rootURL, err := url.Parse(remotes[0].c.URLs[0])
rootEndpoint, err := transport.NewEndpoint(remotes[0].c.URLs[0])
if err != nil {
return nil, err
}

rootURL.Path = path.Join(rootURL.Path, moduleURL.Path)
*moduleURL = *rootURL
rootEndpoint.Path = path.Join(rootEndpoint.Path, moduleEndpoint.Path)
*moduleEndpoint = *rootEndpoint
}

_, err = r.CreateRemote(&config.RemoteConfig{
Name: DefaultRemoteName,
URLs: []string{moduleURL.String()},
URLs: []string{moduleEndpoint.String()},
})

return r, err
Expand Down
26 changes: 26 additions & 0 deletions submodule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"path/filepath"
"testing"

"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/storage/memory"

fixtures "github.com/go-git/go-git-fixtures/v4"
. "gopkg.in/check.v1"
Expand Down Expand Up @@ -262,3 +265,26 @@ func (s *SubmoduleSuite) TestSubmodulesFetchDepth(c *C) {

c.Assert(commitCount, Equals, 1)
}

func (s *SubmoduleSuite) TestSubmoduleParseScp(c *C) {
repo := &Repository{
Storer: memory.NewStorage(),
wt: memfs.New(),
}
worktree := &Worktree{
Filesystem: memfs.New(),
r: repo,
}
submodule := &Submodule{
initialized: true,
c: nil,
w: worktree,
}

submodule.c = &config.Submodule{
URL: "git@github.com:username/submodule_repo",
}

_, err := submodule.Repository()
c.Assert(err, IsNil)
}

0 comments on commit 240acb6

Please sign in to comment.