Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add log to track git log size #1325

Merged
merged 2 commits into from
May 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions pkg/gitparse/gitparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Commit struct {
Date time.Time
Message strings.Builder
Diffs []Diff
Size int // in bytes
}

// Diff contains the info about a file diff in a commit.
Expand Down Expand Up @@ -191,9 +192,12 @@ func (c *Parser) executeCommand(ctx context.Context, cmd *exec.Cmd) (chan Commit

func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan chan Commit) {
outReader := bufio.NewReader(stdOut)
var currentCommit *Commit
var currentDiff *Diff
var recentlyPassedAuthor bool
var (
currentCommit *Commit
currentDiff *Diff
recentlyPassedAuthor bool
totalLogSize int
)

defer common.RecoverWithExit(ctx)
defer close(commitChan)
Expand All @@ -210,10 +214,12 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
// If there is a currentDiff, add it to currentCommit.
if currentDiff != nil && currentDiff.Content.Len() > 0 {
currentCommit.Diffs = append(currentCommit.Diffs, *currentDiff)
currentCommit.Size += currentDiff.Content.Len()
}
// If there is a currentCommit, send it to the channel.
if currentCommit != nil {
commitChan <- *currentCommit
totalLogSize += currentCommit.Size
}
// Create a new currentDiff and currentCommit
currentDiff = &Diff{}
Expand Down Expand Up @@ -248,6 +254,7 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
if totalSize > c.maxCommitSize {
oldCommit := currentCommit
commitChan <- *currentCommit
totalLogSize += currentCommit.Size
currentCommit = &Commit{
Hash: currentCommit.Hash,
Author: currentCommit.Author,
Expand Down Expand Up @@ -308,15 +315,20 @@ func (c *Parser) FromReader(ctx context.Context, stdOut io.Reader, commitChan ch
break
}
}
cleanupParse(currentCommit, currentDiff, commitChan)
cleanupParse(currentCommit, currentDiff, commitChan, &totalLogSize)

ctx.Logger().V(2).Info("finished parsing git log.", "total_log_size", totalLogSize)
}

func cleanupParse(currentCommit *Commit, currentDiff *Diff, commitChan chan Commit) {
func cleanupParse(currentCommit *Commit, currentDiff *Diff, commitChan chan Commit, totalLogSize *int) {
if currentDiff != nil && currentDiff.Content.Len() > 0 {
currentCommit.Diffs = append(currentCommit.Diffs, *currentDiff)
}
if currentCommit != nil {
commitChan <- *currentCommit
if totalLogSize != nil {
*totalLogSize += currentCommit.Size
}
}
}

Expand Down