Skip to content

Commit 5f82527

Browse files
committedJun 25, 2020
feat: add CommitsOnBranchSimple
Uses the bare refs instead of full objects
1 parent 9b9cf3f commit 5f82527

3 files changed

+71
-1
lines changed
 

‎commits_on_branch_simple.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package git
2+
3+
import (
4+
"github.com/go-git/go-git/v5"
5+
"github.com/go-git/go-git/v5/plumbing"
6+
"github.com/go-git/go-git/v5/plumbing/object"
7+
)
8+
9+
type SimpleCommit struct {
10+
Hash [20]byte
11+
Message string
12+
}
13+
14+
// CommitsOnBranchSimple iterates through all references and returns simpleCommits on given branch. \n
15+
// Important to note is that this will provide all commits from anything the branch is connected to.
16+
func (g *Git) CommitsOnBranchSimple(
17+
branchCommit plumbing.Hash,
18+
) ([]SimpleCommit, error) {
19+
var branchCommits []SimpleCommit
20+
21+
branchIter, err := g.repo.Log(&git.LogOptions{
22+
From: branchCommit,
23+
})
24+
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
branchIterErr := branchIter.ForEach(func(commit *object.Commit) error {
30+
branchCommits = append(branchCommits, SimpleCommit{
31+
Hash: commit.Hash,
32+
Message: commit.Message,
33+
})
34+
return nil
35+
})
36+
37+
if branchIterErr != nil {
38+
g.DebugLogger.Printf("Stopped getting commits on branch: %v", branchIterErr)
39+
}
40+
41+
return branchCommits, nil
42+
}

‎commits_on_branch_simple_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package git
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestCommitsOnBranchSimple(t *testing.T) {
10+
repo := setupRepo()
11+
createTestHistory(repo)
12+
13+
head, _ := repo.Head()
14+
15+
testGit := &Git{repo: repo}
16+
17+
commits, err := testGit.CommitsOnBranchSimple(head.Hash())
18+
19+
assert.Equal(t, 4, len(commits))
20+
21+
assert.NoError(t, err)
22+
assert.Equal(t, "third commit on new branch", commits[0].Message)
23+
assert.Equal(t, err, nil)
24+
25+
assert.NoError(t, err)
26+
assert.Equal(t, "second commit on new branch\n\n Long message", commits[1].Message)
27+
assert.Equal(t, err, nil)
28+
}

‎commits_on_branch_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func createTestHistory(repo *git.Repository) {
6262
createCommit(repo, "test commit on master")
6363
createBranch(repo)
6464
createCommit(repo, "commit on new branch")
65-
createCommit(repo, "second commit on new branch")
65+
createCommit(repo, "second commit on new branch\n\n Long message")
6666
createCommit(repo, "third commit on new branch")
6767
}
6868

0 commit comments

Comments
 (0)
Please sign in to comment.