Use merge-base as a starting point

Set it to target branch and check only the commits we are interested in.
If the commit is already in the branch, we just get an old behaviour.

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-07-13 11:51:07 +03:00
parent 102e7d41c0
commit 2f1de6fc41

32
main.go
View file

@ -9,6 +9,7 @@ import (
"strings" "strings"
git "github.com/go-git/go-git/v5" git "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/object"
gha "github.com/sethvargo/go-githubactions" gha "github.com/sethvargo/go-githubactions"
) )
@ -37,14 +38,17 @@ func main() {
} }
// Limit number of iterations. // Limit number of iterations.
from := gha.GetInput("from") var lca *object.Commit
if from := gha.GetInput("from"); from != "" {
lca = getMergeBase(r, head, from)
}
// Processing result. // Processing result.
var fail bool var fail bool
_ = commits.ForEach(func(c *object.Commit) error { _ = commits.ForEach(func(c *object.Commit) error {
// Stop iterator when limit is reached. // Stop iterator when limit is reached.
if len(from) != 0 && strings.HasPrefix(c.ID().String(), from) { if lca != nil && c.Hash == lca.Hash {
return errors.New("stop") return errors.New("stop")
} }
@ -81,3 +85,27 @@ func main() {
os.Exit(1) os.Exit(1)
} }
} }
func getMergeBase(r *git.Repository, head *plumbing.Reference, from string) *object.Commit {
h, err := r.ResolveRevision(plumbing.Revision(from))
if err != nil {
log.Fatalf("Failed to resolve a reference: %v", err)
}
to, err := r.CommitObject(*h)
if err != nil {
log.Fatalf("Failed to get commit object: %v", err)
}
other, err := r.CommitObject(head.Hash())
if err != nil {
log.Fatalf("Failed to get HEAD commit object: %v", err)
}
cc, err := to.MergeBase(other)
if err != nil {
log.Fatalf("Failed to determine merge-base: %v", err)
}
return cc[0]
}