Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/git: Include commit message and URL in checkout error #579

Merged
merged 1 commit into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion controllers/gitrepository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
reference: &sourcev1.GitRepositoryRef{Branch: "main"},
waitForReason: sourcev1.GitOperationFailedReason,
expectStatus: metav1.ConditionFalse,
expectMessage: "unable to clone: user rejected certificate",
expectMessage: "user rejected certificate",
gitImplementation: sourcev1.LibGit2Implementation,
}),
Entry("self signed libgit2 with CA", refTestCase{
Expand Down
10 changes: 10 additions & 0 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ func (c *Commit) Verify(keyRing ...string) (string, error) {
return "", fmt.Errorf("failed to verify commit with any of the given key rings")
}

// ShortMessage returns the first 50 characters of a commit subject.
func (c *Commit) ShortMessage() string {
subject := strings.Split(c.Message, "\n")[0]
r := []rune(subject)
if len(r) > 50 {
return fmt.Sprintf("%s...", string(r[0:50]))
}
return subject
}

type CheckoutStrategy interface {
Checkout(ctx context.Context, path, url string, config *AuthOptions) (*Commit, error)
}
45 changes: 45 additions & 0 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,48 @@ func TestCommit_Verify(t *testing.T) {
})
}
}

func TestCommit_ShortMessage(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "short message",
input: "a short commit message",
want: "a short commit message",
},
{
name: "long message",
input: "hello world - a long commit message for testing long messages",
want: "hello world - a long commit message for testing lo...",
},
{
name: "multi line commit message",
input: `title of the commit

detailed description
of the commit`,
want: "title of the commit",
},
{
name: "message with unicodes",
input: "a message with unicode characters 你好世界 🏞️ 🏕️ ⛩️ 🌌",
want: "a message with unicode characters 你好世界 🏞️ 🏕️ ⛩️ 🌌",
},
{
name: "empty commit message",
input: "",
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

c := Commit{Message: tt.input}
g.Expect(c.ShortMessage()).To(Equal(tt.want))
})
}
}
1 change: 1 addition & 0 deletions pkg/git/gogit/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ func buildCommitWithRef(c *object.Commit, ref plumbing.ReferenceName) (*git.Comm
Committer: buildSignature(c.Committer),
Signature: c.PGPSignature,
Encoded: b,
Message: c.Message,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/git/libgit2/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
CheckoutBranch: c.Branch,
})
if err != nil {
return nil, fmt.Errorf("unable to clone: %w", gitutil.LibGit2Error(err))
return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err))
}
defer repo.Free()
head, err := repo.Head()
Expand Down