Skip to content

Commit

Permalink
git: styling nitpicks
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <hello@hidde.co>
  • Loading branch information
hiddeco committed Oct 25, 2021
1 parent 59dc284 commit 6b88b91
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
4 changes: 2 additions & 2 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 9 additions & 9 deletions pkg/git/gogit/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions pkg/git/libgit2/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions pkg/git/libgit2/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -210,31 +210,31 @@ 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,
},
{
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,
},
{
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,
},
{
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,
Expand Down Expand Up @@ -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
Expand Down
32 changes: 16 additions & 16 deletions pkg/git/strategy/strategy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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())

Expand Down Expand Up @@ -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{
Expand Down

0 comments on commit 6b88b91

Please sign in to comment.