Skip to content

Commit d049f19

Browse files
committed
pkg/git: Include commit message and URL in error
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 messages. Signed-off-by: Sunny <darkowlzz@protonmail.com>
1 parent af0226b commit d049f19

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

controllers/gitrepository_controller_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ var _ = Describe("GitRepositoryReconciler", func() {
383383
reference: &sourcev1.GitRepositoryRef{Branch: "main"},
384384
waitForReason: sourcev1.GitOperationFailedReason,
385385
expectStatus: metav1.ConditionFalse,
386-
expectMessage: "unable to clone: user rejected certificate",
386+
expectMessage: "user rejected certificate",
387387
gitImplementation: sourcev1.LibGit2Implementation,
388388
}),
389389
Entry("self signed libgit2 with CA", refTestCase{

pkg/git/git.go

+10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ func (c *Commit) Verify(keyRing ...string) (string, error) {
9393
return "", fmt.Errorf("failed to verify commit with any of the given key rings")
9494
}
9595

96+
// ShortMessage returns the first 50 characters of a commit subject.
97+
func (c *Commit) ShortMessage() string {
98+
subject := strings.Split(c.Message, "\n")[0]
99+
r := []rune(subject)
100+
if len(r) > 50 {
101+
return fmt.Sprintf("%s...", string(r[0:50]))
102+
}
103+
return subject
104+
}
105+
96106
type CheckoutStrategy interface {
97107
Checkout(ctx context.Context, path, url string, config *AuthOptions) (*Commit, error)
98108
}

pkg/git/git_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,48 @@ func TestCommit_Verify(t *testing.T) {
218218
})
219219
}
220220
}
221+
222+
func TestCommit_ShortMessage(t *testing.T) {
223+
tests := []struct {
224+
name string
225+
input string
226+
want string
227+
}{
228+
{
229+
name: "short message",
230+
input: "a short commit message",
231+
want: "a short commit message",
232+
},
233+
{
234+
name: "long message",
235+
input: "hello world - a long commit message for testing long messages",
236+
want: "hello world - a long commit message for testing lo...",
237+
},
238+
{
239+
name: "multi line commit message",
240+
input: `title of the commit
241+
242+
detailed description
243+
of the commit`,
244+
want: "title of the commit",
245+
},
246+
{
247+
name: "message with unicodes",
248+
input: "a message with unicode characters 你好世界 🏞️ 🏕️ ⛩️ 🌌",
249+
want: "a message with unicode characters 你好世界 🏞️ 🏕️ ⛩️ 🌌",
250+
},
251+
{
252+
name: "empty commit message",
253+
input: "",
254+
want: "",
255+
},
256+
}
257+
for _, tt := range tests {
258+
t.Run(tt.name, func(t *testing.T) {
259+
g := NewWithT(t)
260+
261+
c := Commit{Message: tt.input}
262+
g.Expect(c.ShortMessage()).To(Equal(tt.want))
263+
})
264+
}
265+
}

pkg/git/gogit/checkout.go

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ func buildCommitWithRef(c *object.Commit, ref plumbing.ReferenceName) (*git.Comm
315315
Committer: buildSignature(c.Committer),
316316
Signature: c.PGPSignature,
317317
Encoded: b,
318+
Message: c.Message,
318319
}, nil
319320
}
320321

pkg/git/libgit2/checkout.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, opts *g
6969
CheckoutBranch: c.Branch,
7070
})
7171
if err != nil {
72-
return nil, fmt.Errorf("unable to clone: %w", gitutil.LibGit2Error(err))
72+
return nil, fmt.Errorf("unable to clone '%s': %w", url, gitutil.LibGit2Error(err))
7373
}
7474
defer repo.Free()
7575
head, err := repo.Head()

0 commit comments

Comments
 (0)