Skip to content

Commit

Permalink
git: Return body from list of commits
Browse files Browse the repository at this point in the history
This allow to use body of every commits between two ref.
  • Loading branch information
guerinoni committed Jan 6, 2024
1 parent 586eb8b commit c3adaf7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
18 changes: 13 additions & 5 deletions git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions git/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,22 +471,22 @@ 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",
}},
},
{
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",
},
}
Expand Down
1 change: 1 addition & 0 deletions git/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (r TrackingRef) String() string {
type Commit struct {
Sha string
Title string
Body string
}

type BranchConfig struct {
Expand Down

0 comments on commit c3adaf7

Please sign in to comment.