From 7162f0cffde547ba0e3cb7e9eea3a9a5c2a66e14 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Dec 2024 20:31:31 -0800 Subject: [PATCH] Use gitrepo.GetTreePathLatestCommit to get file lastest commit instead from cache --- modules/gitrepo/tree_path.go | 27 +++++++++++++++++++++++ modules/gitrepo/tree_path_test.go | 36 +++++++++++++++++++++++++++++++ routers/api/v1/repo/file.go | 14 ++++-------- routers/web/repo/download.go | 17 ++++++--------- 4 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 modules/gitrepo/tree_path.go create mode 100644 modules/gitrepo/tree_path_test.go diff --git a/modules/gitrepo/tree_path.go b/modules/gitrepo/tree_path.go new file mode 100644 index 0000000000000..cded9f3a55dbe --- /dev/null +++ b/modules/gitrepo/tree_path.go @@ -0,0 +1,27 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo + +import ( + "context" + + "code.gitea.io/gitea/modules/git" +) + +func GetTreePathLatestCommit(ctx context.Context, repo Repository, commitID, treePath string) (*git.Commit, error) { + gitRepo, closer, err := RepositoryFromContextOrOpen(ctx, repo) + if err != nil { + return nil, err + } + defer closer.Close() + + stdout, _, err := git.NewCommand(ctx, "rev-list", "-1"). + AddDynamicArguments(commitID).AddArguments("--").AddDynamicArguments(treePath). + RunStdString(&git.RunOpts{Dir: repoPath(repo)}) + if err != nil { + return nil, err + } + + return gitRepo.GetCommit(stdout) +} diff --git a/modules/gitrepo/tree_path_test.go b/modules/gitrepo/tree_path_test.go new file mode 100644 index 0000000000000..1b8f4554d705a --- /dev/null +++ b/modules/gitrepo/tree_path_test.go @@ -0,0 +1,36 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo_test + +import ( + "context" + "testing" + + _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/perm/access" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + "code.gitea.io/gitea/modules/gitrepo" + + "github.com/stretchr/testify/assert" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m) +} + +func Test_GetTreePathLatestCommit(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2}) + commitID, err := gitrepo.GetBranchCommitID(context.Background(), repo, repo.DefaultBranch) + assert.NoError(t, err) + assert.EqualValues(t, "1032bbf17fbc0d9c95bb5418dabe8f8c99278700", commitID) + + commit, err := gitrepo.GetTreePathLatestCommit(context.Background(), repo, repo.DefaultBranch, "Home.md") + assert.NoError(t, err) + assert.NotNil(t, commit) + assert.EqualValues(t, "2c54faec6c45d31c1abfaecdab471eac6633738a", commit.ID.String()) +} diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 6591b9a752faa..267b74c24f1b7 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -11,7 +11,6 @@ import ( "fmt" "io" "net/http" - "path" "strings" "time" @@ -242,19 +241,14 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn return nil, nil, nil } - info, _, err := git.Entries([]*git.TreeEntry{entry}).GetCommitsInfo(ctx, ctx.Repo.Commit, path.Dir("/" + ctx.Repo.TreePath)[1:]) + latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetCommitsInfo", err) + ctx.Error(http.StatusInternalServerError, "GetTreePathLatestCommit", err) return nil, nil, nil } + when := &latestCommit.Committer.When - if len(info) == 1 { - // Not Modified - lastModified = &info[0].Commit.Committer.When - } - blob = entry.Blob() - - return blob, entry, lastModified + return entry.Blob(), entry, when } // GetArchive get archive of a repository diff --git a/routers/web/repo/download.go b/routers/web/repo/download.go index 1ed907b2f97c2..3c00d35c35153 100644 --- a/routers/web/repo/download.go +++ b/routers/web/repo/download.go @@ -5,11 +5,11 @@ package repo import ( - "path" "time" git_model "code.gitea.io/gitea/models/git" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" @@ -82,7 +82,7 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob, lastModified *time.Tim return common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified) } -func getBlobForEntry(ctx *context.Context) (blob *git.Blob, lastModified *time.Time) { +func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) { entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath) if err != nil { if git.IsErrNotExist(err) { @@ -98,19 +98,14 @@ func getBlobForEntry(ctx *context.Context) (blob *git.Blob, lastModified *time.T return nil, nil } - info, _, err := git.Entries([]*git.TreeEntry{entry}).GetCommitsInfo(ctx, ctx.Repo.Commit, path.Dir("/" + ctx.Repo.TreePath)[1:]) + latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { - ctx.ServerError("GetCommitsInfo", err) + ctx.ServerError("GetTreePathLatestCommit", err) return nil, nil } + lastModified := &latestCommit.Committer.When - if len(info) == 1 { - // Not Modified - lastModified = &info[0].Commit.Committer.When - } - blob = entry.Blob() - - return blob, lastModified + return entry.Blob(), lastModified } // SingleDownload download a file by repos path