Skip to content

Commit

Permalink
pkg/git: Include commit message and URL in error
Browse files Browse the repository at this point in the history
go-git: Include the commit message in the returned commit object.
libgit2: Set the URL in the checkout error.

Add new method Commit.ShortMessage() for returning short commit
message.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Feb 14, 2022
1 parent af0226b commit 649d33c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
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

0 comments on commit 649d33c

Please sign in to comment.