From 6dfae58b47e5ee0335c013d1f2b4638497c413a6 Mon Sep 17 00:00:00 2001 From: Federico Guerinoni Date: Tue, 5 Dec 2023 22:54:52 +0100 Subject: [PATCH] git: Return body from list of commits This allow to use body of every commits between two ref. --- git/client.go | 18 +++++++++++++----- git/client_test.go | 6 +++--- git/objects.go | 1 + 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/git/client.go b/git/client.go index 3ac6a4754..ca004cdce 100644 --- a/git/client.go +++ b/git/client.go @@ -228,7 +228,7 @@ func (c *Client) UncommittedChangeCount(ctx context.Context) (int, error) { } func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commit, error) { - args := []string{"-c", "log.ShowSignature=false", "log", "--pretty=format:%H,%s", "--cherry", fmt.Sprintf("%s...%s", baseRef, headRef)} + args := []string{"-c", "log.ShowSignature=false", "log", "--pretty=format:%H,%s,%b", "--cherry", fmt.Sprintf("%s...%s", baseRef, headRef)} cmd, err := c.Command(ctx, args...) if err != nil { return nil, err @@ -240,15 +240,23 @@ func (c *Client) Commits(ctx context.Context, baseRef, headRef string) ([]*Commi commits := []*Commit{} sha := 0 title := 1 + body := 2 for _, line := range outputLines(out) { - split := strings.SplitN(line, ",", 2) - if len(split) != 2 { + split := strings.Split(line, ",") + if len(split) < 2 { continue } - commits = append(commits, &Commit{ + + c := &Commit{ Sha: split[sha], Title: split[title], - }) + } + + if len(split) > 2 { + c.Body = split[body] + } + + commits = append(commits, c) } if len(commits) == 0 { return nil, fmt.Errorf("could not find any commits between %s and %s", baseRef, headRef) diff --git a/git/client_test.go b/git/client_test.go index 841a496da..f11e49098 100644 --- a/git/client_test.go +++ b/git/client_test.go @@ -471,7 +471,7 @@ func TestClientCommits(t *testing.T) { { name: "get commits", cmdStdout: "6a6872b918c601a0e730710ad8473938a7516d30,testing testability test", - wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s --cherry SHA1...SHA2`, + wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s,%b --cherry SHA1...SHA2`, wantCommits: []*Commit{{ Sha: "6a6872b918c601a0e730710ad8473938a7516d30", Title: "testing testability test", @@ -479,14 +479,14 @@ func TestClientCommits(t *testing.T) { }, { name: "no commits between SHAs", - wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s --cherry SHA1...SHA2`, + wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s,%b --cherry SHA1...SHA2`, wantErrorMsg: "could not find any commits between SHA1 and SHA2", }, { name: "git error", cmdExitStatus: 1, cmdStderr: "git error message", - wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s --cherry SHA1...SHA2`, + wantCmdArgs: `path/to/git -c log.ShowSignature=false log --pretty=format:%H,%s,%b --cherry SHA1...SHA2`, wantErrorMsg: "failed to run git: git error message", }, } diff --git a/git/objects.go b/git/objects.go index 952b6c335..c33d92b7c 100644 --- a/git/objects.go +++ b/git/objects.go @@ -67,6 +67,7 @@ func (r TrackingRef) String() string { type Commit struct { Sha string Title string + Body string } type BranchConfig struct {