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)
}
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 deletions int64
for _, stat := range c.FileStats {
@ -101,27 +123,14 @@ func MapCommit(c *git.Commit) (*types.Commit, error) {
deletions += stat.Deletions
}
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: types.CommitStats{
Total: types.ChangeStats{
Insertions: insertions,
Deletions: deletions,
Changes: insertions + deletions,
},
Files: mapFileStats(c),
return &types.CommitStats{
Total: types.ChangeStats{
Insertions: insertions,
Deletions: deletions,
Changes: insertions + deletions,
},
}, nil
Files: mapFileStats(c),
}
}
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

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