/
log.go
78 lines (66 loc) · 2.34 KB
/
log.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"errors"
"sort"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/revlist"
)
// GetCommitsBetweenRange returns the commits (including the new commit,
// excluding the old) between the specified ranges. If the old commit ID is set
// to zero, all commits reachable from the new commit are returned.
//
// The returned commits are sorted by commit IDs. Ideally, they should be
// ordered by occurrence but go-git introduces some randomness here. It might be
// an effect of walking the graph anyway, so the sort by ID ensures the returned
// commit slice is deterministic.
func GetCommitsBetweenRange(repo *git.Repository, commitNewID, commitOldID plumbing.Hash) ([]*object.Commit, error) {
all := false
if commitOldID.IsZero() {
all = true
}
var (
commitRange []plumbing.Hash
err error
)
if all {
commitRange, err = revlist.Objects(repo.Storer, []plumbing.Hash{commitNewID}, nil)
if err != nil {
return nil, err
}
} else {
reachableFromCommitOld, err := revlist.Objects(repo.Storer, []plumbing.Hash{commitOldID}, nil)
if err != nil {
return nil, err
}
commitRange, err = revlist.Objects(repo.Storer, []plumbing.Hash{commitNewID}, reachableFromCommitOld)
if err != nil {
return nil, err
}
}
// FIXME: we should ideally be sorting this in the order of occurrence
// rather than by commit ID. The only reason this is happening is because
// the ordering of commitRange by default is not deterministic. Rather than
// walking through them and identifying the right order, we're sorting by
// commit ID. The intended use case of this function is to get a list of
// commits that are then checked for the changes they introduce. At that
// point, they must be diffed with their parent directly.
sort.Slice(commitRange, func(i, j int) bool {
return commitRange[i].String() < commitRange[j].String()
})
commits := make([]*object.Commit, 0, len(commitRange))
for _, commitID := range commitRange {
commit, err := GetCommit(repo, commitID)
if err != nil {
if errors.Is(err, plumbing.ErrObjectNotFound) {
// Returned for non-commit objects
continue
}
return nil, err
}
commits = append(commits, commit)
}
return commits, nil
}