diff options
author | zeripath | 2021-09-19 17:07:35 +0100 |
---|---|---|
committer | GitHub | 2021-09-20 00:07:35 +0800 |
commit | 6139834e765cad7e929ca9ef76f1b4ce57c21cb5 (patch) | |
tree | 61d871003cd8d4710c98e2a62375f2da1e1b60b7 | |
parent | b673a24ee6bc5429bea1023437aaec8c61ba8851 (diff) |
Add caller to cat-file batch calls (#17082) (#17089)
Some people still appear to report unclosed cat-files. This PR simply adds the caller
to the process descriptor for the CatFileBatch and CatFileBatchCheck calls.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
-rw-r--r-- | modules/git/batch_reader.go | 23 | ||||
-rw-r--r-- | routers/api/v1/repo/file.go | 14 |
2 files changed, 29 insertions, 8 deletions
diff --git a/modules/git/batch_reader.go b/modules/git/batch_reader.go index 164e64381..8e3c23251 100644 --- a/modules/git/batch_reader.go +++ b/modules/git/batch_reader.go @@ -8,8 +8,10 @@ import ( "bufio" "bytes" "context" + "fmt" "io" "math" + "runtime" "strconv" "strings" @@ -40,9 +42,14 @@ func CatFileBatchCheck(repoPath string) (WriteCloserError, *bufio.Reader, func() <-closed } + _, filename, line, _ := runtime.Caller(2) + filename = strings.TrimPrefix(filename, callerPrefix) + go func() { stderr := strings.Builder{} - err := NewCommandContext(ctx, "cat-file", "--batch-check").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader) + err := NewCommandContext(ctx, "cat-file", "--batch-check"). + SetDescription(fmt.Sprintf("%s cat-file --batch-check [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)). + RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader) if err != nil { _ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String())) _ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String())) @@ -76,9 +83,14 @@ func CatFileBatch(repoPath string) (WriteCloserError, *bufio.Reader, func()) { <-closed } + _, filename, line, _ := runtime.Caller(2) + filename = strings.TrimPrefix(filename, callerPrefix) + go func() { stderr := strings.Builder{} - err := NewCommandContext(ctx, "cat-file", "--batch").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader) + err := NewCommandContext(ctx, "cat-file", "--batch"). + SetDescription(fmt.Sprintf("%s cat-file --batch [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)). + RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader) if err != nil { _ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String())) _ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String())) @@ -292,3 +304,10 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn sha = shaBuf return } + +var callerPrefix string + +func init() { + _, filename, _, _ := runtime.Caller(0) + callerPrefix = strings.TrimSuffix(filename, "modules/git/batch_reader.go") +} diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index e6427ea4f..db98955be 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -119,13 +119,15 @@ func GetArchive(ctx *context.APIContext) { // "$ref": "#/responses/notFound" repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) - gitRepo, err := git.OpenRepository(repoPath) - if err != nil { - ctx.Error(http.StatusInternalServerError, "OpenRepository", err) - return + if ctx.Repo.GitRepo == nil { + gitRepo, err := git.OpenRepository(repoPath) + if err != nil { + ctx.Error(http.StatusInternalServerError, "OpenRepository", err) + return + } + ctx.Repo.GitRepo = gitRepo + defer gitRepo.Close() } - ctx.Repo.GitRepo = gitRepo - defer gitRepo.Close() repo.Download(ctx.Context) } |