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,13 +94,6 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
return nil, fmt.Errorf("failed to map committer: %w", err)
}
var insertions int64
var deletions int64
for _, stat := range c.FileStats {
insertions += stat.Insertions
deletions += stat.Deletions
}
parentSHAs := make([]string, len(c.ParentSHAs))
for i, sha := range c.ParentSHAs {
parentSHAs[i] = sha.String()
@ -113,15 +106,31 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
Message: c.Message,
Author: *author,
Committer: *committer,
Stats: types.CommitStats{
Stats: mapStats(c),
},
nil
}
func mapStats(c *git.Commit) *types.CommitStats {
if len(c.FileStats) == 0 {
return nil
}
var insertions int64
var deletions int64
for _, stat := range c.FileStats {
insertions += stat.Insertions
deletions += stat.Deletions
}
return &types.CommitStats{
Total: types.ChangeStats{
Insertions: insertions,
Deletions: deletions,
Changes: insertions + deletions,
},
Files: mapFileStats(c),
},
}, nil
}
}
func mapFileStats(c *git.Commit) []types.CommitFileStats {

View File

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

View File

@ -83,7 +83,7 @@ type Commit struct {
Message string `json:"message"`
Author Signature `json:"author"`
Committer Signature `json:"committer"`
Stats CommitStats `json:"stats,omitempty"`
Stats *CommitStats `json:"stats,omitempty"`
}
type Signature struct {