Skip to content

Commit

Permalink
git: Move repository options to git/repository
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <paulo.gomes@weave.works>
  • Loading branch information
Paulo Gomes committed Nov 15, 2022
1 parent 6db7f53 commit 1337974
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 151 deletions.
6 changes: 3 additions & 3 deletions git/gogit/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (g *Client) Init(ctx context.Context, url, branch string) error {
return nil
}

func (g *Client) Clone(ctx context.Context, url string, cloneOpts git.CloneOptions) (*git.Commit, error) {
func (g *Client) Clone(ctx context.Context, url string, cloneOpts repository.CloneOptions) (*git.Commit, error) {
if err := g.validateUrl(url); err != nil {
return nil, err
}
Expand Down Expand Up @@ -245,12 +245,12 @@ func (g *Client) writeFile(path string, reader io.Reader) error {
return err
}

func (g *Client) Commit(info git.Commit, commitOpts ...git.CommitOption) (string, error) {
func (g *Client) Commit(info git.Commit, commitOpts ...repository.CommitOption) (string, error) {
if g.repository == nil {
return "", git.ErrNoGitRepository
}

options := &git.CommitOptions{}
options := &repository.CommitOptions{}
for _, o := range commitOpts {
o(options)
}
Expand Down
3 changes: 2 additions & 1 deletion git/gogit/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
. "github.com/onsi/gomega"

"github.com/fluxcd/pkg/git"
"github.com/fluxcd/pkg/git/repository"
"github.com/fluxcd/pkg/gittestserver"
)

Expand Down Expand Up @@ -163,7 +164,7 @@ func TestCommit(t *testing.T) {
},
Message: "testing",
},
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader("testing gogit commit"),
}),
)
Expand Down
9 changes: 5 additions & 4 deletions git/gogit/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import (
"github.com/fluxcd/go-git/v5/storage/memory"

"github.com/fluxcd/pkg/git"
"github.com/fluxcd/pkg/git/repository"
"github.com/fluxcd/pkg/version"
)

func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts git.CloneOptions) (*git.Commit, error) {
func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts repository.CloneOptions) (*git.Commit, error) {
if g.authOpts == nil {
return nil, fmt.Errorf("unable to checkout repo with an empty set of auth options")
}
Expand Down Expand Up @@ -126,7 +127,7 @@ func (g *Client) cloneBranch(ctx context.Context, url, branch string, opts git.C
return buildCommitWithRef(cc, ref)
}

func (g *Client) cloneTag(ctx context.Context, url, tag string, opts git.CloneOptions) (*git.Commit, error) {
func (g *Client) cloneTag(ctx context.Context, url, tag string, opts repository.CloneOptions) (*git.Commit, error) {
if g.authOpts == nil {
return nil, fmt.Errorf("unable to checkout repo with an empty set of auth options")
}
Expand Down Expand Up @@ -204,7 +205,7 @@ func (g *Client) cloneTag(ctx context.Context, url, tag string, opts git.CloneOp
return buildCommitWithRef(cc, ref)
}

func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts git.CloneOptions) (*git.Commit, error) {
func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts repository.CloneOptions) (*git.Commit, error) {
authMethod, err := transportAuth(g.authOpts)
if err != nil {
return nil, fmt.Errorf("unable to construct auth method with options: %w", err)
Expand Down Expand Up @@ -256,7 +257,7 @@ func (g *Client) cloneCommit(ctx context.Context, url, commit string, opts git.C
return buildCommitWithRef(cc, cloneOpts.ReferenceName)
}

func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts git.CloneOptions) (*git.Commit, error) {
func (g *Client) cloneSemVer(ctx context.Context, url, semverTag string, opts repository.CloneOptions) (*git.Commit, error) {
verConstraint, err := semver.NewConstraint(semverTag)
if err != nil {
return nil, fmt.Errorf("semver parse error: %w", err)
Expand Down
31 changes: 16 additions & 15 deletions git/gogit/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/fluxcd/gitkit"
"github.com/fluxcd/pkg/git"
"github.com/fluxcd/pkg/git/gogit/fs"
"github.com/fluxcd/pkg/git/repository"
"github.com/fluxcd/pkg/gittestserver"
"github.com/fluxcd/pkg/ssh"
)
Expand Down Expand Up @@ -132,8 +133,8 @@ func TestClone_cloneBranch(t *testing.T) {
upstreamPath = repoPath
}

cc, err := ggc.Clone(context.TODO(), upstreamPath, git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
cc, err := ggc.Clone(context.TODO(), upstreamPath, repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: tt.branch,
},
ShallowClone: true,
Expand Down Expand Up @@ -245,8 +246,8 @@ func TestClone_cloneTag(t *testing.T) {
ggc, err := NewClient(tmpDir, &git.AuthOptions{Transport: git.HTTP})
g.Expect(err).ToNot(HaveOccurred())

opts := git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
opts := repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Tag: tt.checkoutTag,
},
ShallowClone: true,
Expand Down Expand Up @@ -338,8 +339,8 @@ func TestClone_cloneCommit(t *testing.T) {
g := NewWithT(t)

tmpDir := t.TempDir()
opts := git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
opts := repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: tt.branch,
Commit: tt.commit,
},
Expand Down Expand Up @@ -452,8 +453,8 @@ func TestClone_cloneSemVer(t *testing.T) {
ggc, err := NewClient(tmpDir, nil)
g.Expect(err).ToNot(HaveOccurred())

opts := git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
opts := repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
SemVer: tt.constraint,
},
ShallowClone: true,
Expand Down Expand Up @@ -559,8 +560,8 @@ func Test_ssh_KeyTypes(t *testing.T) {
ggc, err := NewClient(tmpDir, &authOpts)
g.Expect(err).ToNot(HaveOccurred())

cc, err := ggc.Clone(ctx, repoURL, git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
cc, err := ggc.Clone(ctx, repoURL, repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: git.DefaultBranch,
},
ShallowClone: true,
Expand Down Expand Up @@ -690,8 +691,8 @@ func Test_ssh_KeyExchangeAlgos(t *testing.T) {
ggc, err := NewClient(tmpDir, &authOpts)
g.Expect(err).ToNot(HaveOccurred())

_, err = ggc.Clone(ctx, repoURL, git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
_, err = ggc.Clone(ctx, repoURL, repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: git.DefaultBranch,
},
ShallowClone: true,
Expand Down Expand Up @@ -862,8 +863,8 @@ func Test_ssh_HostKeyAlgos(t *testing.T) {
ggc, err := NewClient(tmpDir, &authOpts)
g.Expect(err).ToNot(HaveOccurred())

_, err = ggc.Clone(ctx, repoURL, git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
_, err = ggc.Clone(ctx, repoURL, repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: git.DefaultBranch,
},
ShallowClone: true,
Expand Down Expand Up @@ -1056,7 +1057,7 @@ func TestClone_CredentialsOverHttp(t *testing.T) {
repoURL = tt.transformURL(ts.URL)
}

_, err = ggc.Clone(context.TODO(), repoURL, git.CloneOptions{})
_, err = ggc.Clone(context.TODO(), repoURL, repository.CloneOptions{})

if tt.expectCloneErr != "" {
g.Expect(err).To(HaveOccurred())
Expand Down
16 changes: 8 additions & 8 deletions git/internal/e2e/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")

func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstreamRepo upstreamRepoInfo) {
// clone repo
_, err := client.Clone(context.TODO(), repoURL.String(), git.CloneOptions{
CheckoutStrategy: git.CheckoutStrategy{
_, err := client.Clone(context.TODO(), repoURL.String(), repository.CloneOptions{
CheckoutStrategy: repository.CheckoutStrategy{
Branch: "main",
},
})
Expand All @@ -55,7 +55,7 @@ func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstre
// commit and push to origin
cc, err := client.Commit(
mockCommitInfo(),
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader(randStringRunes(10)),
}),
)
Expand All @@ -75,7 +75,7 @@ func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstre
// commit to and push new branch
cc, err = client.Commit(
mockCommitInfo(),
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader(randStringRunes(10)),
}),
)
Expand All @@ -94,7 +94,7 @@ func testUsingClone(g *WithT, client repository.Client, repoURL *url.URL, upstre

_, err = client.Commit(
mockCommitInfo(),
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader(randStringRunes(10)),
}),
)
Expand All @@ -113,7 +113,7 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea

cc, err := client.Commit(
mockCommitInfo(),
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader(randStringRunes(10)),
}),
)
Expand All @@ -131,7 +131,7 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea

cc, err = client.Commit(
mockCommitInfo(),
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader(randStringRunes(10)),
}),
)
Expand All @@ -149,7 +149,7 @@ func testUsingInit(g *WithT, client repository.Client, repoURL *url.URL, upstrea

_, err = client.Commit(
mockCommitInfo(),
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader(randStringRunes(10)),
}),
)
Expand Down
10 changes: 5 additions & 5 deletions git/libgit2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (l *Client) Init(ctx context.Context, url, branch string) error {
}
repo, err := git2go.InitRepository(l.path, false)
if err != nil {
return fmt.Errorf("unable to init repository for '%s': %w", url, LibGit2Error(err))
return fmt.Errorf("unable to init repository for '%s': %w", url, libGit2Error(err))
}

l.registerTransportOptions(ctx, url)
Expand Down Expand Up @@ -166,7 +166,7 @@ func (l *Client) Init(ctx context.Context, url, branch string) error {
}
} else {
repo.Free()
return fmt.Errorf("unable to create remote for '%s': %w", url, LibGit2Error(err))
return fmt.Errorf("unable to create remote for '%s': %w", url, libGit2Error(err))
}
}

Expand All @@ -175,7 +175,7 @@ func (l *Client) Init(ctx context.Context, url, branch string) error {
return nil
}

func (l *Client) Clone(ctx context.Context, url string, cloneOpts git.CloneOptions) (*git.Commit, error) {
func (l *Client) Clone(ctx context.Context, url string, cloneOpts repository.CloneOptions) (*git.Commit, error) {
if err := l.validateUrl(url); err != nil {
return nil, err
}
Expand Down Expand Up @@ -238,12 +238,12 @@ func (l *Client) writeFile(path string, reader io.Reader) error {
return nil
}

func (l *Client) Commit(info git.Commit, commitOpts ...git.CommitOption) (string, error) {
func (l *Client) Commit(info git.Commit, commitOpts ...repository.CommitOption) (string, error) {
if l.repository == nil {
return "", git.ErrNoGitRepository
}

options := &git.CommitOptions{}
options := &repository.CommitOptions{}
for _, o := range commitOpts {
o(options)
}
Expand Down
3 changes: 2 additions & 1 deletion git/libgit2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/fluxcd/pkg/git"
"github.com/fluxcd/pkg/git/libgit2/internal/test"
"github.com/fluxcd/pkg/git/libgit2/transport"
"github.com/fluxcd/pkg/git/repository"
"github.com/fluxcd/pkg/gittestserver"
)

Expand Down Expand Up @@ -170,7 +171,7 @@ func TestCommit(t *testing.T) {
},
Message: "testing",
},
git.WithFiles(map[string]io.Reader{
repository.WithFiles(map[string]io.Reader{
"test": strings.NewReader("testing libgit2 commit"),
}),
)
Expand Down
Loading

0 comments on commit 1337974

Please sign in to comment.