git: Return body from list of commits

This allow to use body of every commits between two ref.
This commit is contained in:
Federico Guerinoni 2023-12-05 22:54:52 +01:00
parent 28c3a40558
commit 6dfae58b47
3 changed files with 17 additions and 8 deletions

View file

@ -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)

View file

@ -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",
},
}

View file

@ -67,6 +67,7 @@ func (r TrackingRef) String() string {
type Commit struct {
Sha string
Title string
Body string
}
type BranchConfig struct {