generated from fallion/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommits_on_branch.go
40 lines (31 loc) · 1.02 KB
/
commits_on_branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package git
import (
"errors"
"github.com/apex/log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
// ErrCommonCommitFound is used for identifying when the iterator has reached the common commit
var ErrCommonCommitFound = errors.New("common commit found")
// CommitsOnBranch iterates through all references and returns commit hashes on given branch. \n
// Important to note is that this will provide all commits from anything the branch is connected to.
func (g *Git) CommitsOnBranch(
branchCommit plumbing.Hash,
) ([]plumbing.Hash, error) {
var branchCommits []plumbing.Hash
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, commit.Hash)
return nil
})
if branchIterErr != nil {
log.Debugf("Stopped getting commits on branch: %v", branchIterErr)
}
return branchCommits, nil
}