mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 20:36:07 +01:00 
			
		
		
		
	Add caller to cat-file batch calls (#17082)
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>
This commit is contained in:
		@@ -8,8 +8,10 @@ import (
 | 
				
			|||||||
	"bufio"
 | 
						"bufio"
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"math"
 | 
						"math"
 | 
				
			||||||
 | 
						"runtime"
 | 
				
			||||||
	"strconv"
 | 
						"strconv"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -40,9 +42,14 @@ func CatFileBatchCheck(repoPath string) (WriteCloserError, *bufio.Reader, func()
 | 
				
			|||||||
		<-closed
 | 
							<-closed
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, filename, line, _ := runtime.Caller(2)
 | 
				
			||||||
 | 
						filename = strings.TrimPrefix(filename, callerPrefix)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		stderr := strings.Builder{}
 | 
							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 {
 | 
							if err != nil {
 | 
				
			||||||
			_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
 | 
								_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
 | 
				
			||||||
			_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
 | 
								_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
 | 
				
			||||||
@@ -76,9 +83,14 @@ func CatFileBatch(repoPath string) (WriteCloserError, *bufio.Reader, func()) {
 | 
				
			|||||||
		<-closed
 | 
							<-closed
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						_, filename, line, _ := runtime.Caller(2)
 | 
				
			||||||
 | 
						filename = strings.TrimPrefix(filename, callerPrefix)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	go func() {
 | 
						go func() {
 | 
				
			||||||
		stderr := strings.Builder{}
 | 
							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 {
 | 
							if err != nil {
 | 
				
			||||||
			_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
 | 
								_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
 | 
				
			||||||
			_ = batchStdinReader.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
 | 
						sha = shaBuf
 | 
				
			||||||
	return
 | 
						return
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var callerPrefix string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						_, filename, _, _ := runtime.Caller(0)
 | 
				
			||||||
 | 
						callerPrefix = strings.TrimSuffix(filename, "modules/git/batch_reader.go")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -119,6 +119,7 @@ func GetArchive(ctx *context.APIContext) {
 | 
				
			|||||||
	//     "$ref": "#/responses/notFound"
 | 
						//     "$ref": "#/responses/notFound"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
 | 
						repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
 | 
				
			||||||
 | 
						if ctx.Repo.GitRepo == nil {
 | 
				
			||||||
		gitRepo, err := git.OpenRepository(repoPath)
 | 
							gitRepo, err := git.OpenRepository(repoPath)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
 | 
								ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
 | 
				
			||||||
@@ -126,6 +127,7 @@ func GetArchive(ctx *context.APIContext) {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		ctx.Repo.GitRepo = gitRepo
 | 
							ctx.Repo.GitRepo = gitRepo
 | 
				
			||||||
		defer gitRepo.Close()
 | 
							defer gitRepo.Close()
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	repo.Download(ctx.Context)
 | 
						repo.Download(ctx.Context)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user