refactor repo_stats to use os.pipe (#11726)

* refactor repo_stats to use os.pipe

* woops. missing reader.

* stdout not stderr, woops

* Fix copyright date and ensure that the stderr is collected

Signed-off-by: Andrew Thornton <art27@cantab.net>

Co-authored-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
techknowlogick 2020-09-05 16:12:14 -04:00 committed by GitHub
parent 913ac9cacd
commit 9a17e2830b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 73 deletions

View file

@ -1,5 +1,5 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2016 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

View file

@ -6,8 +6,9 @@ package git
import (
"bufio"
"bytes"
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
@ -49,6 +50,15 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
}
stats.CommitCountInAllBranches = c
stdoutReader, stdoutWriter, err := os.Pipe()
if err != nil {
return nil, err
}
defer func() {
_ = stdoutReader.Close()
_ = stdoutWriter.Close()
}()
args := []string{"log", "--numstat", "--no-merges", "--pretty=format:---%n%h%n%an%n%ae%n", "--date=iso", fmt.Sprintf("--since='%s'", since)}
if len(branch) == 0 {
args = append(args, "--branches=*")
@ -56,12 +66,14 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
args = append(args, "--first-parent", branch)
}
stdout, err = NewCommand(args...).RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
stderr := new(strings.Builder)
err = NewCommand(args...).RunInDirTimeoutEnvFullPipelineFunc(
nil, -1, repo.Path,
stdoutWriter, stderr, nil,
func(ctx context.Context, cancel context.CancelFunc) error {
_ = stdoutWriter.Close()
scanner := bufio.NewScanner(bytes.NewReader(stdout))
scanner := bufio.NewScanner(stdoutReader)
scanner.Split(bufio.ScanLines)
stats.CommitCount = 0
stats.Additions = 0
@ -130,5 +142,12 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
stats.ChangedFiles = int64(len(files))
stats.Authors = a
_ = stdoutReader.Close()
return nil
})
if err != nil {
return nil, fmt.Errorf("Failed to get GetCodeActivityStats for repository.\nError: %w\nStderr: %s", err, stderr)
}
return stats, nil
}