diff --git a/pkg/git/git.go b/pkg/git/git.go index 59744eadb..780243157 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -62,8 +62,8 @@ type Commit struct { // String returns a string representation of the Commit, composed // out the last part of the Reference element, and/or Hash. -// For example: -// 'tags/a0c14dc8580a23f79bc654faa79c4f62b46c2c22'. +// For example: 'tag-1/a0c14dc8580a23f79bc654faa79c4f62b46c2c22', +// for a "tag-1" tag. func (c *Commit) String() string { if short := strings.SplitAfterN(c.Reference, "/", 3); len(short) == 3 { return fmt.Sprintf("%s/%s", short[2], c.Hash) diff --git a/pkg/git/gogit/checkout.go b/pkg/git/gogit/checkout.go index ca8c1a800..10ed58c92 100644 --- a/pkg/git/gogit/checkout.go +++ b/pkg/git/gogit/checkout.go @@ -89,7 +89,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g if err != nil { return nil, fmt.Errorf("failed to resolve commit object for HEAD '%s': %w", head.Hash(), err) } - return commitWithRef(cc, ref) + return buildCommitWithRef(cc, ref) } type CheckoutTag struct { @@ -127,7 +127,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git. if err != nil { return nil, fmt.Errorf("failed to resolve commit object for HEAD '%s': %w", head.Hash(), err) } - return commitWithRef(cc, ref) + return buildCommitWithRef(cc, ref) } type CheckoutCommit struct { @@ -175,7 +175,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, opts *g if err != nil { return nil, fmt.Errorf("failed to checkout commit '%s': %w", c.Commit, err) } - return commitWithRef(cc, cloneOpts.ReferenceName) + return buildCommitWithRef(cc, cloneOpts.ReferenceName) } type CheckoutSemVer struct { @@ -287,15 +287,15 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g if err != nil { return nil, fmt.Errorf("failed to resolve commit object for HEAD '%s': %w", head.Hash(), err) } - return commitWithRef(cc, ref) + return buildCommitWithRef(cc, ref) } -func commitWithRef(c *object.Commit, ref plumbing.ReferenceName) (*git.Commit, error) { +func buildCommitWithRef(c *object.Commit, ref plumbing.ReferenceName) (*git.Commit, error) { if c == nil { return nil, errors.New("failed to construct commit: no object") } - // Encode commit components, excluding signature into SignedData.. + // Encode commit components excluding signature into SignedData. encoded := &plumbing.MemoryObject{} if err := c.EncodeWithoutSignature(encoded); err != nil { return nil, fmt.Errorf("failed to encode commit '%s': %w", c.Hash, err) @@ -311,14 +311,14 @@ func commitWithRef(c *object.Commit, ref plumbing.ReferenceName) (*git.Commit, e return &git.Commit{ Hash: []byte(c.Hash.String()), Reference: ref.String(), - Author: signature(c.Author), - Committer: signature(c.Committer), + Author: buildSignature(c.Author), + Committer: buildSignature(c.Committer), Signature: c.PGPSignature, Encoded: b, }, nil } -func signature(s object.Signature) git.Signature { +func buildSignature(s object.Signature) git.Signature { return git.Signature{ Name: s.Name, Email: s.Email, diff --git a/pkg/git/libgit2/checkout.go b/pkg/git/libgit2/checkout.go index 8e3d36a75..3d60d9c78 100644 --- a/pkg/git/libgit2/checkout.go +++ b/pkg/git/libgit2/checkout.go @@ -81,7 +81,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g return nil, fmt.Errorf("could not find commit '%s' in branch '%s': %w", head.Target(), c.Branch, err) } defer cc.Free() - return commit(cc, "refs/heads/"+c.Branch), nil + return buildCommit(cc, "refs/heads/"+c.Branch), nil } type CheckoutTag struct { @@ -104,7 +104,7 @@ func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, opts *git. return nil, err } defer cc.Free() - return commit(cc, "refs/tags/"+c.Tag), nil + return buildCommit(cc, "refs/tags/"+c.Tag), nil } type CheckoutCommit struct { @@ -130,7 +130,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, opts *g if err != nil { return nil, fmt.Errorf("git checkout error: %w", err) } - return commit(cc, ""), nil + return buildCommit(cc, ""), nil } type CheckoutSemVer struct { @@ -228,7 +228,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, opts *g return nil, err } defer cc.Free() - return commit(cc, "refs/tags/"+t), nil + return buildCommit(cc, "refs/tags/"+t), nil } // checkoutDetachedDwim attempts to perform a detached HEAD checkout by first DWIMing the short name @@ -285,20 +285,20 @@ func headCommit(repo *git2go.Repository) (*git2go.Commit, error) { return c, nil } -func commit(c *git2go.Commit, ref string) *git.Commit { +func buildCommit(c *git2go.Commit, ref string) *git.Commit { sig, msg, _ := c.ExtractSignature() return &git.Commit{ Hash: []byte(c.Id().String()), Reference: ref, - Author: signature(c.Author()), - Committer: signature(c.Committer()), + Author: buildSignature(c.Author()), + Committer: buildSignature(c.Committer()), Signature: sig, Encoded: []byte(msg), Message: c.Message(), } } -func signature(s *git2go.Signature) git.Signature { +func buildSignature(s *git2go.Signature) git.Signature { return git.Signature{ Name: s.Name, Email: s.Email, diff --git a/pkg/git/libgit2/transport_test.go b/pkg/git/libgit2/transport_test.go index a3e9bf711..6f1e9545b 100644 --- a/pkg/git/libgit2/transport_test.go +++ b/pkg/git/libgit2/transport_test.go @@ -132,7 +132,7 @@ orKqTuAPzXK7imQk6+OycYABbqCtC/9qmwRd8wwn7sF97DtYfK8WuNHtFalCAwyi Kom08eUK8skxAzfDDijZPh10VtJ66uBoiDPdT+uCBehcBIcmSTrKjFGX -----END CERTIFICATE-----` - knownHosts string = `github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==` + knownHostsFixture string = `github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==` ) func Test_x509Callback(t *testing.T) { @@ -210,7 +210,7 @@ func Test_knownHostsCallback(t *testing.T) { { name: "Match", host: "github.com", - knownHosts: []byte(knownHosts), + knownHosts: []byte(knownHostsFixture), hostkey: git2go.HostkeyCertificate{Kind: git2go.HostkeySHA1 | git2go.HostkeyMD5, HashSHA1: sha1Fingerprint("v2toJdKXfFEaR1u++4iq1UqSrHM")}, expectedHost: "github.com", want: git2go.ErrorCodeOK, @@ -218,7 +218,7 @@ func Test_knownHostsCallback(t *testing.T) { { name: "Match with port", host: "github.com", - knownHosts: []byte(knownHosts), + knownHosts: []byte(knownHostsFixture), hostkey: git2go.HostkeyCertificate{Kind: git2go.HostkeySHA1 | git2go.HostkeyMD5, HashSHA1: sha1Fingerprint("v2toJdKXfFEaR1u++4iq1UqSrHM")}, expectedHost: "github.com:22", want: git2go.ErrorCodeOK, @@ -226,7 +226,7 @@ func Test_knownHostsCallback(t *testing.T) { { name: "Hostname mismatch", host: "github.com", - knownHosts: []byte(knownHosts), + knownHosts: []byte(knownHostsFixture), hostkey: git2go.HostkeyCertificate{Kind: git2go.HostkeySHA1 | git2go.HostkeyMD5, HashSHA1: sha1Fingerprint("v2toJdKXfFEaR1u++4iq1UqSrHM")}, expectedHost: "example.com", want: git2go.ErrorCodeUser, @@ -234,7 +234,7 @@ func Test_knownHostsCallback(t *testing.T) { { name: "Hostkey mismatch", host: "github.com", - knownHosts: []byte(knownHosts), + knownHosts: []byte(knownHostsFixture), hostkey: git2go.HostkeyCertificate{Kind: git2go.HostkeyMD5, HashMD5: md5Fingerprint("\xb6\x03\x0e\x39\x97\x9e\xd0\xe7\x24\xce\xa3\x77\x3e\x01\x42\x09")}, expectedHost: "github.com", want: git2go.ErrorCodeCertificate, @@ -269,7 +269,7 @@ func Test_parseKnownHosts(t *testing.T) { t.Run(tt.name, func(t *testing.T) { g := NewWithT(t) - knownKeys, err := parseKnownHosts(knownHosts) + knownKeys, err := parseKnownHosts(knownHostsFixture) if err != nil { t.Error(err) return diff --git a/pkg/git/strategy/strategy_test.go b/pkg/git/strategy/strategy_test.go index de1761589..9ce0ea9b5 100644 --- a/pkg/git/strategy/strategy_test.go +++ b/pkg/git/strategy/strategy_test.go @@ -37,21 +37,21 @@ func TestCheckoutStrategyForImplementation_Auth(t *testing.T) { gitImpls := []git.Implementation{gogit.Implementation, libgit2.Implementation} type testCase struct { - name string - transport git.TransportType - getRepoURL func(g *WithT, srv *gittestserver.GitServer, repoPath string) string - getAuthOpts func(g *WithT, u *url.URL, user string, pswd string, ca []byte) *git.AuthOptions - wantFunc func(g *WithT, cs git.CheckoutStrategy, dir string, repoURL string, authOpts *git.AuthOptions) + name string + transport git.TransportType + repoURLFunc func(g *WithT, srv *gittestserver.GitServer, repoPath string) string + authOptsFunc func(g *WithT, u *url.URL, user string, pswd string, ca []byte) *git.AuthOptions + wantFunc func(g *WithT, cs git.CheckoutStrategy, dir string, repoURL string, authOpts *git.AuthOptions) } cases := []testCase{ { - name: "http cloning", + name: "HTTP clone", transport: git.HTTP, - getRepoURL: func(g *WithT, srv *gittestserver.GitServer, repoPath string) string { + repoURLFunc: func(g *WithT, srv *gittestserver.GitServer, repoPath string) string { return srv.HTTPAddressWithCredentials() + "/" + repoPath }, - getAuthOpts: func(g *WithT, u *url.URL, user string, pswd string, ca []byte) *git.AuthOptions { + authOptsFunc: func(g *WithT, u *url.URL, user string, pswd string, ca []byte) *git.AuthOptions { return &git.AuthOptions{ Transport: git.HTTP, Username: user, @@ -64,12 +64,12 @@ func TestCheckoutStrategyForImplementation_Auth(t *testing.T) { }, }, { - name: "https cloning", + name: "HTTPS clone", transport: git.HTTPS, - getRepoURL: func(g *WithT, srv *gittestserver.GitServer, repoPath string) string { + repoURLFunc: func(g *WithT, srv *gittestserver.GitServer, repoPath string) string { return srv.HTTPAddress() + "/" + repoPath }, - getAuthOpts: func(g *WithT, u *url.URL, user, pswd string, ca []byte) *git.AuthOptions { + authOptsFunc: func(g *WithT, u *url.URL, user, pswd string, ca []byte) *git.AuthOptions { return &git.AuthOptions{ Transport: git.HTTPS, Username: user, @@ -83,12 +83,12 @@ func TestCheckoutStrategyForImplementation_Auth(t *testing.T) { }, }, { - name: "ssh cloning", + name: "SSH clone", transport: git.SSH, - getRepoURL: func(g *WithT, srv *gittestserver.GitServer, repoPath string) string { + repoURLFunc: func(g *WithT, srv *gittestserver.GitServer, repoPath string) string { return getSSHRepoURL(srv.SSHAddress(), repoPath) }, - getAuthOpts: func(g *WithT, u *url.URL, user, pswd string, ca []byte) *git.AuthOptions { + authOptsFunc: func(g *WithT, u *url.URL, user, pswd string, ca []byte) *git.AuthOptions { knownhosts, err := ssh.ScanHostKey(u.Host, 5*time.Second) g.Expect(err).ToNot(HaveOccurred()) @@ -163,10 +163,10 @@ func TestCheckoutStrategyForImplementation_Auth(t *testing.T) { err = gitServer.InitRepo("testdata/repo1", branch, repoPath) g.Expect(err).ToNot(HaveOccurred()) - repoURL := tt.getRepoURL(g, gitServer, repoPath) + repoURL := tt.repoURLFunc(g, gitServer, repoPath) u, err := url.Parse(repoURL) g.Expect(err).ToNot(HaveOccurred()) - authOpts := tt.getAuthOpts(g, u, username, password, exampleCA) + authOpts := tt.authOptsFunc(g, u, username, password, exampleCA) // Get the checkout strategy. checkoutOpts := git.CheckoutOptions{