Skip to content

Commit

Permalink
Merge pull request #430 from aryan9600/osfs
Browse files Browse the repository at this point in the history
git/gogit: fix chroot filesystem and add submodule test
  • Loading branch information
Paulo Gomes authored Dec 12, 2022
2 parents e7417c9 + bf8628c commit 0b80dfb
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
84 changes: 84 additions & 0 deletions git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"context"
"errors"
"fmt"
iofs "io/fs"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -475,6 +477,88 @@ func TestClone_cloneSemVer(t *testing.T) {
}
}

func Test_cloneSubmodule(t *testing.T) {
g := NewWithT(t)

server, err := gittestserver.NewTempGitServer()
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(server.Root())

err = server.StartHTTP()
g.Expect(err).ToNot(HaveOccurred())
defer server.StopHTTP()

baseRepoPath := "base.git"
err = server.InitRepo("../testdata/git/repo", git.DefaultBranch, baseRepoPath)
g.Expect(err).ToNot(HaveOccurred())

icingRepoPath := "icing.git"
err = server.InitRepo("../testdata/git/repo2", git.DefaultBranch, icingRepoPath)
g.Expect(err).ToNot(HaveOccurred())

tmp := t.TempDir()
icingRepo, err := extgogit.PlainClone(tmp, false, &extgogit.CloneOptions{
URL: server.HTTPAddress() + "/" + icingRepoPath,
ReferenceName: plumbing.NewBranchReferenceName(git.DefaultBranch),
Tags: extgogit.NoTags,
})
g.Expect(err).ToNot(HaveOccurred())

cmd := exec.Command("git", "submodule", "add", fmt.Sprintf("%s/%s", server.HTTPAddress(), baseRepoPath))
cmd.Dir = tmp
_, err = cmd.Output()
g.Expect(err).ToNot(HaveOccurred())

wt, err := icingRepo.Worktree()
g.Expect(err).ToNot(HaveOccurred())
_, err = wt.Add(".gitmodules")
g.Expect(err).ToNot(HaveOccurred())
_, err = wt.Commit("submod", &extgogit.CommitOptions{
Author: &object.Signature{
Name: "test user",
},
})
g.Expect(err).ToNot(HaveOccurred())
err = icingRepo.Push(&extgogit.PushOptions{})
g.Expect(err).ToNot(HaveOccurred())

tmpDir := t.TempDir()
ggc, err := NewClient(tmpDir, &git.AuthOptions{
Transport: git.HTTP,
})
g.Expect(err).ToNot(HaveOccurred())

_, err = ggc.Clone(context.TODO(), server.HTTPAddress()+"/"+icingRepoPath, repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: "master",
},
ShallowClone: true,
RecurseSubmodules: true,
})

expectedPaths := []string{"base", "base/foo.txt", "bar.txt", "."}
var c int
filepath.Walk(tmpDir, func(path string, d iofs.FileInfo, err error) error {
if err != nil {
return err
}
if strings.Contains(path, ".git") {
return nil
}
rel, err := filepath.Rel(tmpDir, path)
if err != nil {
return err
}
for _, expectedPath := range expectedPaths {
if rel == expectedPath {
c += 1
}
}
return nil
})
g.Expect(c).To(Equal(len(expectedPaths)))
}

// Test_ssh_KeyTypes assures support for the different types of keys
// for SSH Authentication supported by Flux.
func Test_ssh_KeyTypes(t *testing.T) {
Expand Down
7 changes: 6 additions & 1 deletion git/gogit/fs/osfs_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"strings"
"sync"

securejoin "github.com/cyphar/filepath-securejoin"
"github.com/go-git/go-billy/v5"
)

Expand Down Expand Up @@ -215,7 +216,11 @@ func (fs *OS) Readlink(link string) (string, error) {

// Chroot returns a new OS filesystem, with working directory set to path.
func (fs *OS) Chroot(path string) (billy.Filesystem, error) {
return New(path), nil
joined, err := securejoin.SecureJoin(fs.workingDir, path)
if err != nil {
return nil, err
}
return New(joined), nil
}

// Root returns the current working dir of the billy.Filesystem.
Expand Down
4 changes: 3 additions & 1 deletion git/gogit/fs/osfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,13 @@ func TestTempFile(t *testing.T) {

func TestChroot(t *testing.T) {
g := NewWithT(t)
fs := New(t.TempDir())
tmp := t.TempDir()
fs := New(tmp)

f, err := fs.Chroot("test")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(f).ToNot(BeNil())
g.Expect(f.Root()).To(Equal(filepath.Join(tmp, "test")))
}

func TestRoot(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion git/gogit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ replace (
require (
github.com/Masterminds/semver/v3 v3.1.1
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
github.com/cyphar/filepath-securejoin v0.2.3
github.com/elazarl/goproxy v0.0.0-20221015165544-a0805db90819
github.com/fluxcd/gitkit v0.6.0
github.com/fluxcd/go-git/v5 v5.0.0-20221206140629-ec778c2c37df
Expand All @@ -30,7 +31,6 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20221026131551-cf6655e29de4 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/cloudflare/circl v1.3.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions git/testdata/git/repo2/bar.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
another test file
another test file

0 comments on commit 0b80dfb

Please sign in to comment.