Skip to content

Commit

Permalink
feat: add CommitsOnBranchSimple
Browse files Browse the repository at this point in the history
Uses the bare refs instead of full objects
  • Loading branch information
fallion committed Jun 25, 2020
1 parent 9b9cf3f commit 5f82527
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
42 changes: 42 additions & 0 deletions commits_on_branch_simple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package git

import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)

type SimpleCommit struct {
Hash [20]byte
Message string
}

// CommitsOnBranchSimple iterates through all references and returns simpleCommits on given branch. \n
// Important to note is that this will provide all commits from anything the branch is connected to.
func (g *Git) CommitsOnBranchSimple(
branchCommit plumbing.Hash,
) ([]SimpleCommit, error) {
var branchCommits []SimpleCommit

branchIter, err := g.repo.Log(&git.LogOptions{
From: branchCommit,
})

if err != nil {
return nil, err
}

branchIterErr := branchIter.ForEach(func(commit *object.Commit) error {
branchCommits = append(branchCommits, SimpleCommit{
Hash: commit.Hash,
Message: commit.Message,
})
return nil
})

if branchIterErr != nil {
g.DebugLogger.Printf("Stopped getting commits on branch: %v", branchIterErr)
}

return branchCommits, nil
}
28 changes: 28 additions & 0 deletions commits_on_branch_simple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package git

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCommitsOnBranchSimple(t *testing.T) {
repo := setupRepo()
createTestHistory(repo)

head, _ := repo.Head()

testGit := &Git{repo: repo}

commits, err := testGit.CommitsOnBranchSimple(head.Hash())

assert.Equal(t, 4, len(commits))

assert.NoError(t, err)
assert.Equal(t, "third commit on new branch", commits[0].Message)
assert.Equal(t, err, nil)

assert.NoError(t, err)
assert.Equal(t, "second commit on new branch\n\n Long message", commits[1].Message)
assert.Equal(t, err, nil)
}
2 changes: 1 addition & 1 deletion commits_on_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func createTestHistory(repo *git.Repository) {
createCommit(repo, "test commit on master")
createBranch(repo)
createCommit(repo, "commit on new branch")
createCommit(repo, "second commit on new branch")
createCommit(repo, "second commit on new branch\n\n Long message")
createCommit(repo, "third commit on new branch")
}

Expand Down

0 comments on commit 5f82527

Please sign in to comment.