Add omitempty to stats and run get change info for changes and types in parallel (#2105)

This commit is contained in:
Darko Draskovic 2024-06-14 15:41:53 +00:00 committed by Harness
parent 329181ed1a
commit fe7b1941ef
3 changed files with 55 additions and 32 deletions

View File

@ -94,6 +94,28 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
return nil, fmt.Errorf("failed to map committer: %w", err) return nil, fmt.Errorf("failed to map committer: %w", err)
} }
parentSHAs := make([]string, len(c.ParentSHAs))
for i, sha := range c.ParentSHAs {
parentSHAs[i] = sha.String()
}
return &types.Commit{
SHA: c.SHA.String(),
ParentSHAs: parentSHAs,
Title: c.Title,
Message: c.Message,
Author: *author,
Committer: *committer,
Stats: mapStats(c),
},
nil
}
func mapStats(c *git.Commit) *types.CommitStats {
if len(c.FileStats) == 0 {
return nil
}
var insertions int64 var insertions int64
var deletions int64 var deletions int64
for _, stat := range c.FileStats { for _, stat := range c.FileStats {
@ -101,27 +123,14 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
deletions += stat.Deletions deletions += stat.Deletions
} }
parentSHAs := make([]string, len(c.ParentSHAs)) return &types.CommitStats{
for i, sha := range c.ParentSHAs { Total: types.ChangeStats{
parentSHAs[i] = sha.String() Insertions: insertions,
} Deletions: deletions,
Changes: insertions + deletions,
return &types.Commit{
SHA: c.SHA.String(),
ParentSHAs: parentSHAs,
Title: c.Title,
Message: c.Message,
Author: *author,
Committer: *committer,
Stats: types.CommitStats{
Total: types.ChangeStats{
Insertions: insertions,
Deletions: deletions,
Changes: insertions + deletions,
},
Files: mapFileStats(c),
}, },
}, nil Files: mapFileStats(c),
}
} }
func mapFileStats(c *git.Commit) []types.CommitFileStats { func mapFileStats(c *git.Commit) []types.CommitFileStats {

View File

@ -31,6 +31,7 @@ import (
"github.com/harness/gitness/git/sha" "github.com/harness/gitness/git/sha"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
) )
// CommitGPGSignature represents a git commit signature part. // CommitGPGSignature represents a git commit signature part.
@ -255,15 +256,28 @@ func getCommitFileStats(
repoPath string, repoPath string,
sha sha.SHA, sha sha.SHA,
) ([]CommitFileStats, error) { ) ([]CommitFileStats, error) {
g, ctx := errgroup.WithContext(ctx)
var changeInfoChanges map[string]changeInfoChange
var changeInfoTypes map[string]changeInfoType var changeInfoTypes map[string]changeInfoType
changeInfoTypes, err := getChangeInfoTypes(ctx, repoPath, sha)
if err != nil { g.Go(func() error {
var err error
changeInfoChanges, err = getChangeInfoChanges(ctx, repoPath, sha)
return err
})
g.Go(func() error {
var err error
changeInfoTypes, err = getChangeInfoTypes(ctx, repoPath, sha)
return err
})
if err := g.Wait(); err != nil {
return nil, fmt.Errorf("failed to get change infos: %w", err) return nil, fmt.Errorf("failed to get change infos: %w", err)
} }
changeInfoChanges, err := getChangeInfoChanges(ctx, repoPath, sha) if len(changeInfoTypes) == 0 {
if err != nil { return []CommitFileStats{}, nil
return []CommitFileStats{}, fmt.Errorf("failed to get change infos: %w", err)
} }
fileStats := make([]CommitFileStats, len(changeInfoChanges)) fileStats := make([]CommitFileStats, len(changeInfoChanges))

View File

@ -77,13 +77,13 @@ type CommitStats struct {
} }
type Commit struct { type Commit struct {
SHA string `json:"sha"` SHA string `json:"sha"`
ParentSHAs []string `json:"parent_shas,omitempty"` ParentSHAs []string `json:"parent_shas,omitempty"`
Title string `json:"title"` Title string `json:"title"`
Message string `json:"message"` Message string `json:"message"`
Author Signature `json:"author"` Author Signature `json:"author"`
Committer Signature `json:"committer"` Committer Signature `json:"committer"`
Stats CommitStats `json:"stats,omitempty"` Stats *CommitStats `json:"stats,omitempty"`
} }
type Signature struct { type Signature struct {