klesh commented on code in PR #2050:
URL: https://github.com/apache/incubator-devlake/pull/2050#discussion_r897443699


##########
plugins/gitextractor/parser/repo.go:
##########
@@ -0,0 +1,335 @@
+package parser
+
+import (
+       "context"
+       "fmt"
+       "github.com/apache/incubator-devlake/models/domainlayer"
+       "github.com/apache/incubator-devlake/models/domainlayer/code"
+       "github.com/apache/incubator-devlake/plugins/core"
+       "github.com/apache/incubator-devlake/plugins/gitextractor/models"
+       git "github.com/libgit2/git2go/v33"
+)
+
+type GitRepo struct {
+       store   models.Store
+       taskCtx core.TaskContext
+       ctx     context.Context
+       logger  core.Logger
+       id      string
+       repo    *git.Repository
+       cleanup func()
+}
+
+func (r *GitRepo) CollectAll(subtaskCtx core.SubTaskContext) error {
+       r.taskCtx.SetProgress(0, -1)
+       err := r.CollectTags(subtaskCtx)
+       if err != nil {
+               return err
+       }
+       err = r.CollectBranches(subtaskCtx)
+       if err != nil {
+               return err
+       }
+       return r.CollectCommits(subtaskCtx)
+}
+
+func (r *GitRepo) Close() error {
+       defer func() {
+               if r.cleanup != nil {
+                       r.cleanup()
+               }
+       }()
+       return r.store.Close()
+}
+
+func (r *GitRepo) CountTags() (int, error) {
+       tags, err := r.repo.Tags.List()
+       if err != nil {
+               return 0, err
+       }
+       return len(tags), nil
+}
+
+func (r *GitRepo) CountBranches() (int, error) {
+       var repoInter *git.BranchIterator
+       repoInter, err := r.repo.NewBranchIterator(git.BranchAll)
+       if err != nil {
+               return 0, err
+       }
+       count := 0
+       err = repoInter.ForEach(func(branch *git.Branch, branchType 
git.BranchType) error {
+               select {
+               case <-r.ctx.Done():
+                       return r.ctx.Err()
+               default:
+                       break
+               }
+               if branch.IsBranch() {
+                       count++
+               }
+               return nil
+       })
+       return count, err
+}
+
+func (r *GitRepo) CountCommits() (int, error) {
+       odb, err := r.repo.Odb()
+       if err != nil {
+               return 0, err
+       }
+       count := 0
+       err = odb.ForEach(func(id *git.Oid) error {
+               select {
+               case <-r.ctx.Done():
+                       return r.ctx.Err()
+               default:
+                       break
+               }
+               commit, _ := r.repo.LookupCommit(id)
+               if commit != nil {
+                       count++
+               }
+               return nil
+       })
+       return count, err
+}
+
+func (r *GitRepo) CollectTags(_ core.SubTaskContext) error {

Review Comment:
   Aah, yes, you are correct. please do so



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to