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:
		
							parent
							
								
									913ac9cacd
								
							
						
					
					
						commit
						9a17e2830b
					
				|  | @ -1,5 +1,5 @@ | ||||||
| // Copyright 2020 The Gitea Authors. All rights reserved.
 |  | ||||||
| // Copyright 2015 The Gogs 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
 | // Use of this source code is governed by a MIT-style
 | ||||||
| // license that can be found in the LICENSE file.
 | // license that can be found in the LICENSE file.
 | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -6,8 +6,9 @@ package git | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"bufio" | 	"bufio" | ||||||
| 	"bytes" | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"os" | ||||||
| 	"sort" | 	"sort" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | @ -49,6 +50,15 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string) | ||||||
| 	} | 	} | ||||||
| 	stats.CommitCountInAllBranches = c | 	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)} | 	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 { | 	if len(branch) == 0 { | ||||||
| 		args = append(args, "--branches=*") | 		args = append(args, "--branches=*") | ||||||
|  | @ -56,12 +66,14 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string) | ||||||
| 		args = append(args, "--first-parent", branch) | 		args = append(args, "--first-parent", branch) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	stdout, err = NewCommand(args...).RunInDirBytes(repo.Path) | 	stderr := new(strings.Builder) | ||||||
| 	if err != nil { | 	err = NewCommand(args...).RunInDirTimeoutEnvFullPipelineFunc( | ||||||
| 		return nil, err | 		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) | 			scanner.Split(bufio.ScanLines) | ||||||
| 			stats.CommitCount = 0 | 			stats.CommitCount = 0 | ||||||
| 			stats.Additions = 0 | 			stats.Additions = 0 | ||||||
|  | @ -130,5 +142,12 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string) | ||||||
| 			stats.ChangedFiles = int64(len(files)) | 			stats.ChangedFiles = int64(len(files)) | ||||||
| 			stats.Authors = a | 			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 | 	return stats, nil | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue