Skip to content

Commit 6139834

Browse files
zeripathtechknowlogicklunny
authored
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 <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Lunny Xiao <[email protected]>
1 parent b673a24 commit 6139834

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

modules/git/batch_reader.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"bufio"
99
"bytes"
1010
"context"
11+
"fmt"
1112
"io"
1213
"math"
14+
"runtime"
1315
"strconv"
1416
"strings"
1517

@@ -40,9 +42,14 @@ func CatFileBatchCheck(repoPath string) (WriteCloserError, *bufio.Reader, func()
4042
<-closed
4143
}
4244

45+
_, filename, line, _ := runtime.Caller(2)
46+
filename = strings.TrimPrefix(filename, callerPrefix)
47+
4348
go func() {
4449
stderr := strings.Builder{}
45-
err := NewCommandContext(ctx, "cat-file", "--batch-check").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
50+
err := NewCommandContext(ctx, "cat-file", "--batch-check").
51+
SetDescription(fmt.Sprintf("%s cat-file --batch-check [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
52+
RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
4653
if err != nil {
4754
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
4855
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
@@ -76,9 +83,14 @@ func CatFileBatch(repoPath string) (WriteCloserError, *bufio.Reader, func()) {
7683
<-closed
7784
}
7885

86+
_, filename, line, _ := runtime.Caller(2)
87+
filename = strings.TrimPrefix(filename, callerPrefix)
88+
7989
go func() {
8090
stderr := strings.Builder{}
81-
err := NewCommandContext(ctx, "cat-file", "--batch").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
91+
err := NewCommandContext(ctx, "cat-file", "--batch").
92+
SetDescription(fmt.Sprintf("%s cat-file --batch [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
93+
RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
8294
if err != nil {
8395
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
8496
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
@@ -292,3 +304,10 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
292304
sha = shaBuf
293305
return
294306
}
307+
308+
var callerPrefix string
309+
310+
func init() {
311+
_, filename, _, _ := runtime.Caller(0)
312+
callerPrefix = strings.TrimSuffix(filename, "modules/git/batch_reader.go")
313+
}

routers/api/v1/repo/file.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,15 @@ func GetArchive(ctx *context.APIContext) {
119119
// "$ref": "#/responses/notFound"
120120

121121
repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
122-
gitRepo, err := git.OpenRepository(repoPath)
123-
if err != nil {
124-
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
125-
return
122+
if ctx.Repo.GitRepo == nil {
123+
gitRepo, err := git.OpenRepository(repoPath)
124+
if err != nil {
125+
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
126+
return
127+
}
128+
ctx.Repo.GitRepo = gitRepo
129+
defer gitRepo.Close()
126130
}
127-
ctx.Repo.GitRepo = gitRepo
128-
defer gitRepo.Close()
129131

130132
repo.Download(ctx.Context)
131133
}

0 commit comments

Comments
 (0)