Skip to content

Commit 7a09bc9

Browse files
authored
Avoid creating unnecessary temporary cat file sub process (#33942)
Extract from #33934 In the same goroutine, we should reuse the exist cat file sub process which exist in `git.Repository` to avoid creating a unnecessary temporary subprocess. This PR reuse the exist cate file writer and reader in `getCommitFromBatchReader`. It also move `prepareLatestCommitInfo` before creating dataRc which will hold the writer so other git operation will create a temporary cat file subprocess.
1 parent 013b268 commit 7a09bc9

File tree

8 files changed

+43
-29
lines changed

8 files changed

+43
-29
lines changed

modules/git/repo_commit_nogogit.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ func (repo *Repository) getCommit(id ObjectID) (*Commit, error) {
8181

8282
_, _ = wr.Write([]byte(id.String() + "\n"))
8383

84-
return repo.getCommitFromBatchReader(rd, id)
84+
return repo.getCommitFromBatchReader(wr, rd, id)
8585
}
8686

87-
func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id ObjectID) (*Commit, error) {
87+
func (repo *Repository) getCommitFromBatchReader(wr WriteCloserError, rd *bufio.Reader, id ObjectID) (*Commit, error) {
8888
_, typ, size, err := ReadBatchLine(rd)
8989
if err != nil {
9090
if errors.Is(err, io.EOF) || IsErrNotExist(err) {
@@ -112,7 +112,11 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id ObjectID)
112112
return nil, err
113113
}
114114

115-
commit, err := tag.Commit(repo)
115+
if _, err := wr.Write([]byte(tag.Object.String() + "\n")); err != nil {
116+
return nil, err
117+
}
118+
119+
commit, err := repo.getCommitFromBatchReader(wr, rd, tag.Object)
116120
if err != nil {
117121
return nil, err
118122
}

modules/git/repo_tag_nogogit.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ func (repo *Repository) GetTagType(id ObjectID) (string, error) {
4141
return "", err
4242
}
4343
_, typ, _, err := ReadBatchLine(rd)
44-
if IsErrNotExist(err) {
45-
return "", ErrNotExist{ID: id.String()}
44+
if err != nil {
45+
if IsErrNotExist(err) {
46+
return "", ErrNotExist{ID: id.String()}
47+
}
48+
return "", err
4649
}
4750
return typ, nil
4851
}

modules/git/repo_tree_nogogit.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ func (repo *Repository) getTree(id ObjectID) (*Tree, error) {
3535
if err != nil {
3636
return nil, err
3737
}
38-
commit, err := tag.Commit(repo)
38+
39+
if _, err := wr.Write([]byte(tag.Object.String() + "\n")); err != nil {
40+
return nil, err
41+
}
42+
commit, err := repo.getCommitFromBatchReader(wr, rd, tag.Object)
3943
if err != nil {
4044
return nil, err
4145
}

modules/git/tag.go

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ type Tag struct {
2121
Signature *CommitSignature
2222
}
2323

24-
// Commit return the commit of the tag reference
25-
func (tag *Tag) Commit(gitRepo *Repository) (*Commit, error) {
26-
return gitRepo.getCommit(tag.Object)
27-
}
28-
2924
func parsePayloadSignature(data []byte, messageStart int) (payload, msg, sign string) {
3025
pos := messageStart
3126
signStart, signEnd := -1, -1

modules/repository/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func PushUpdateAddTag(ctx context.Context, repo *repo_model.Repository, gitRepo
126126
if err != nil {
127127
return fmt.Errorf("unable to GetTag: %w", err)
128128
}
129-
commit, err := tag.Commit(gitRepo)
129+
commit, err := gitRepo.GetTagCommit(tag.Name)
130130
if err != nil {
131131
return fmt.Errorf("unable to get tag Commit: %w", err)
132132
}

routers/api/v1/repo/tag.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func GetAnnotatedTag(ctx *context.APIContext) {
110110
if tag, err := ctx.Repo.GitRepo.GetAnnotatedTag(sha); err != nil {
111111
ctx.APIError(http.StatusBadRequest, err)
112112
} else {
113-
commit, err := tag.Commit(ctx.Repo.GitRepo)
113+
commit, err := ctx.Repo.GitRepo.GetTagCommit(tag.Name)
114114
if err != nil {
115115
ctx.APIError(http.StatusBadRequest, err)
116116
}

routers/web/repo/view_file.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,31 @@ import (
3030
"github.com/nektos/act/pkg/model"
3131
)
3232

33+
func prepareLatestCommitInfo(ctx *context.Context) bool {
34+
commit, err := ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
35+
if err != nil {
36+
ctx.ServerError("GetCommitByPath", err)
37+
return false
38+
}
39+
40+
return loadLatestCommitData(ctx, commit)
41+
}
42+
3343
func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
3444
ctx.Data["IsViewFile"] = true
3545
ctx.Data["HideRepoInfo"] = true
36-
blob := entry.Blob()
37-
buf, dataRc, fInfo, err := getFileReader(ctx, ctx.Repo.Repository.ID, blob)
38-
if err != nil {
39-
ctx.ServerError("getFileReader", err)
46+
47+
if !prepareLatestCommitInfo(ctx) {
4048
return
4149
}
42-
defer dataRc.Close()
50+
51+
blob := entry.Blob()
4352

4453
ctx.Data["Title"] = ctx.Tr("repo.file.title", ctx.Repo.Repository.Name+"/"+path.Base(ctx.Repo.TreePath), ctx.Repo.RefFullName.ShortName())
4554
ctx.Data["FileIsSymlink"] = entry.IsLink()
4655
ctx.Data["FileName"] = blob.Name()
4756
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/raw/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
4857

49-
commit, err := ctx.Repo.Commit.GetCommitByPath(ctx.Repo.TreePath)
50-
if err != nil {
51-
ctx.ServerError("GetCommitByPath", err)
52-
return
53-
}
54-
55-
if !loadLatestCommitData(ctx, commit) {
56-
return
57-
}
58-
5958
if ctx.Repo.TreePath == ".editorconfig" {
6059
_, editorconfigWarning, editorconfigErr := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
6160
if editorconfigWarning != nil {
@@ -90,6 +89,15 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
9089
isDisplayingSource := ctx.FormString("display") == "source"
9190
isDisplayingRendered := !isDisplayingSource
9291

92+
// Don't call any other repository functions depends on git.Repository until the dataRc closed to
93+
// avoid create unnecessary temporary cat file.
94+
buf, dataRc, fInfo, err := getFileReader(ctx, ctx.Repo.Repository.ID, blob)
95+
if err != nil {
96+
ctx.ServerError("getFileReader", err)
97+
return
98+
}
99+
defer dataRc.Close()
100+
93101
if fInfo.isLFSFile {
94102
ctx.Data["RawFileLink"] = ctx.Repo.RepoLink + "/media/" + ctx.Repo.RefTypeNameSubURL() + "/" + util.PathEscapeSegments(ctx.Repo.TreePath)
95103
}

services/repository/push.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo
385385
if err != nil {
386386
return fmt.Errorf("GetTag: %w", err)
387387
}
388-
commit, err := tag.Commit(gitRepo)
388+
commit, err := gitRepo.GetTagCommit(tag.Name)
389389
if err != nil {
390390
return fmt.Errorf("Commit: %w", err)
391391
}

0 commit comments

Comments
 (0)