@@ -17,6 +17,7 @@ package service
1717import (
1818 "context"
1919 "errors"
20+ "io"
2021 "net/url"
2122 "os"
2223 "path"
@@ -25,16 +26,13 @@ import (
2526 "sync"
2627
2728 "github.com/go-git/go-git/v5"
28- "github.com/go-git/go-git/v5/plumbing"
2929 "github.com/go-git/go-git/v5/plumbing/object"
3030 "github.com/google/go-github/v29/github"
3131 log "github.com/sirupsen/logrus"
3232
3333 "github.com/jimschubert/changelog/model"
3434)
3535
36- var foundError = errors .New ("found" )
37-
3836type gitService struct {
3937 contextual * Contextual
4038 config * model.Config
@@ -59,6 +57,7 @@ func (s gitService) GetContextual() *Contextual {
5957 return s .contextual
6058}
6159
60+ //goland:noinspection ALL
6261func (s gitService ) Process (parentContext * context.Context , wg * sync.WaitGroup , ciChan chan * model.ChangeItem , from string , to string ) error {
6362 wg .Add (1 )
6463 defer wg .Done ()
@@ -85,47 +84,37 @@ func (s gitService) Process(parentContext *context.Context, wg *sync.WaitGroup,
8584 }
8685
8786 toTag , err := repo .Tag (to )
88- if err != nil {
87+ if toTag == nil || err != nil {
8988 log .WithFields (log.Fields {"error" : err , "to" : to }).Fatalf ("Unable to find 'to' tag." )
9089 }
9190
92- commitIter , err := repo .Log ( & git. LogOptions { From : fromTag . Hash ()} )
91+ startCommit , err := repo .CommitObject ( toTag . Hash ())
9392 if err != nil {
94- log .WithFields (log.Fields {"error" : err }).Fatal ("Unable to retrieve commit information from git repository." )
95- }
96-
97- var iterated []plumbing.Hash
98-
99- //noinspection GoNilness
100- err = commitIter .ForEach (func (c * object.Commit ) error {
101- // If no previous tag is found then from and to are equal
102- if fromTag .Hash () == toTag .Hash () {
103- return nil
104- }
105- if c .Hash == toTag .Hash () {
106- return foundError
107- }
108- iterated = append (iterated , c .Hash )
109- return nil
110- })
111-
112- if err != nil && err != foundError {
11393 return err
11494 }
11595
116- for _ , hash := range iterated {
117- commit , err := repo .CommitObject (hash )
118- if err != nil {
119- log .WithFields (log.Fields {"commit" : hash }).Fatalf ("Failed while process commits." )
96+ // NewCommitIterBSF returns a CommitIter that walks the commit history,
97+ // starting at the given commit and visiting its parents in pre-order.
98+ err = object .NewCommitIterBSF (startCommit , nil , nil ).ForEach (func (commit * object.Commit ) error {
99+ if commit .Hash == fromTag .Hash () {
100+ return io .EOF
120101 }
121-
122102 wg .Add (1 )
123103 go func (commit * object.Commit ) {
124104 newContext , newCancel := contextual .CreateContext (parentContext )
125105 defer newCancel ()
126106 s .convertToChangeItem (commit , ciChan , wg , & newContext )
127107 }(commit )
108+ return nil
109+ })
110+
111+ if err != nil && ! errors .Is (err , io .EOF ){
112+ log .WithFields (log.Fields {
113+ "from" : fromTag .Hash ().String (),
114+ "to" : toTag .Hash ().String (),
115+ }).Fatalf ("Failed while processing commits." )
128116 }
117+
129118 return nil
130119}
131120
0 commit comments