From 5232245606ba2abd6773a54920f7bc633528aff2 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Dec 2024 20:31:31 -0800 Subject: [PATCH 1/7] 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 From e0f7b89c4bcf763c9920317ef8a6fe54798f1071 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Dec 2024 20:59:12 -0800 Subject: [PATCH 2/7] Fix checks --- modules/gitrepo/tree_path_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/modules/gitrepo/tree_path_test.go b/modules/gitrepo/tree_path_test.go index 1b8f4554d705a..cd98c1ef4dd04 100644 --- a/modules/gitrepo/tree_path_test.go +++ b/modules/gitrepo/tree_path_test.go @@ -7,13 +7,14 @@ 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" + _ "code.gitea.io/gitea/models/actions" + _ "code.gitea.io/gitea/models/activities" + _ "code.gitea.io/gitea/models/perm/access" + "github.com/stretchr/testify/assert" ) From 16506ad8f5eab8da87ea36ef9a95b150d675d16d Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 25 Dec 2024 22:22:42 -0800 Subject: [PATCH 3/7] Move NewCommand to git module --- modules/git/tree.go | 11 +++++++++++ modules/gitrepo/tree_path.go | 8 +++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index 1da4a9fa5debc..4fe1e0efd9c9d 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -62,3 +62,14 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error return filelist, err } + +// GetTreePathLatestCommitID returns the latest commit ID of a tree path +func (repo *Repository) GetTreePathLatestCommitID(refName, treePath string) (string, error) { + stdout, _, err := NewCommand(repo.Ctx, "rev-list", "-1"). + AddDynamicArguments(refName).AddArguments("--").AddDynamicArguments(treePath). + RunStdString(&RunOpts{Dir: repo.Path}) + if err != nil { + return "", err + } + return stdout, nil +} diff --git a/modules/gitrepo/tree_path.go b/modules/gitrepo/tree_path.go index cded9f3a55dbe..5b20985e98a4a 100644 --- a/modules/gitrepo/tree_path.go +++ b/modules/gitrepo/tree_path.go @@ -9,19 +9,17 @@ import ( "code.gitea.io/gitea/modules/git" ) -func GetTreePathLatestCommit(ctx context.Context, repo Repository, commitID, treePath string) (*git.Commit, error) { +func GetTreePathLatestCommit(ctx context.Context, repo Repository, refName, 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)}) + latestCommitID, err := gitRepo.GetTreePathLatestCommitID(refName, treePath) if err != nil { return nil, err } - return gitRepo.GetCommit(stdout) + return gitRepo.GetCommit(latestCommitID) } From 2f05073e5771e2a672b5f26d3af825f42a11e4d4 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 26 Dec 2024 11:03:58 -0800 Subject: [PATCH 4/7] Use the function directly --- modules/git/tree.go | 8 +++---- modules/git/tree_test.go | 15 +++++++++++++ modules/gitrepo/tree_path.go | 25 --------------------- modules/gitrepo/tree_path_test.go | 37 ------------------------------- routers/api/v1/repo/file.go | 9 +++++++- routers/web/repo/download.go | 9 +++++++- 6 files changed, 35 insertions(+), 68 deletions(-) delete mode 100644 modules/gitrepo/tree_path.go delete mode 100644 modules/gitrepo/tree_path_test.go diff --git a/modules/git/tree.go b/modules/git/tree.go index 4fe1e0efd9c9d..7832b40e01518 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -63,13 +63,13 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error return filelist, err } -// GetTreePathLatestCommitID returns the latest commit ID of a tree path -func (repo *Repository) GetTreePathLatestCommitID(refName, treePath string) (string, error) { +// GetTreePathLatestCommitID returns the latest commit of a tree path +func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Commit, error) { stdout, _, err := NewCommand(repo.Ctx, "rev-list", "-1"). AddDynamicArguments(refName).AddArguments("--").AddDynamicArguments(treePath). RunStdString(&RunOpts{Dir: repo.Path}) if err != nil { - return "", err + return nil, err } - return stdout, nil + return repo.GetCommit(stdout) } diff --git a/modules/git/tree_test.go b/modules/git/tree_test.go index 6d2b5c84d5069..5fee64b038755 100644 --- a/modules/git/tree_test.go +++ b/modules/git/tree_test.go @@ -25,3 +25,18 @@ func TestSubTree_Issue29101(t *testing.T) { assert.True(t, IsErrNotExist(err)) } } + +func Test_GetTreePathLatestCommit(t *testing.T) { + repo, err := openRepositoryWithDefaultContext(filepath.Join(testReposDir, "repo6_blame")) + assert.NoError(t, err) + defer repo.Close() + + commitID, err := repo.GetBranchCommitID("master") + assert.NoError(t, err) + assert.EqualValues(t, "544d8f7a3b15927cddf2299b4b562d6ebd71b6a7", commitID) + + commit, err := repo.GetTreePathLatestCommit("master", "blame.txt") + assert.NoError(t, err) + assert.NotNil(t, commit) + assert.EqualValues(t, "45fb6cbc12f970b04eacd5cd4165edd11c8d7376", commit.ID.String()) +} diff --git a/modules/gitrepo/tree_path.go b/modules/gitrepo/tree_path.go deleted file mode 100644 index 5b20985e98a4a..0000000000000 --- a/modules/gitrepo/tree_path.go +++ /dev/null @@ -1,25 +0,0 @@ -// 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, refName, treePath string) (*git.Commit, error) { - gitRepo, closer, err := RepositoryFromContextOrOpen(ctx, repo) - if err != nil { - return nil, err - } - defer closer.Close() - - latestCommitID, err := gitRepo.GetTreePathLatestCommitID(refName, treePath) - if err != nil { - return nil, err - } - - return gitRepo.GetCommit(latestCommitID) -} diff --git a/modules/gitrepo/tree_path_test.go b/modules/gitrepo/tree_path_test.go deleted file mode 100644 index cd98c1ef4dd04..0000000000000 --- a/modules/gitrepo/tree_path_test.go +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package gitrepo_test - -import ( - "context" - "testing" - - repo_model "code.gitea.io/gitea/models/repo" - "code.gitea.io/gitea/models/unittest" - "code.gitea.io/gitea/modules/gitrepo" - - _ "code.gitea.io/gitea/models/actions" - _ "code.gitea.io/gitea/models/activities" - _ "code.gitea.io/gitea/models/perm/access" - - "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 267b74c24f1b7..8fd814b3967f6 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -241,7 +241,14 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn return nil, nil, nil } - latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository) + if err != nil { + ctx.Error(http.StatusInternalServerError, "RepositoryFromContextOrOpen", err) + return nil, nil, nil + } + defer closer.Close() + + latestCommit, err := gitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTreePathLatestCommit", err) return nil, nil, nil diff --git a/routers/web/repo/download.go b/routers/web/repo/download.go index 3c00d35c35153..743038eb1e7d7 100644 --- a/routers/web/repo/download.go +++ b/routers/web/repo/download.go @@ -98,7 +98,14 @@ func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) { return nil, nil } - latestCommit, err := gitrepo.GetTreePathLatestCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository) + if err != nil { + ctx.ServerError("RepositoryFromContextOrOpen", err) + return nil, nil + } + defer closer.Close() + + latestCommit, err := gitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { ctx.ServerError("GetTreePathLatestCommit", err) return nil, nil From 421011f283104cc61a56f086ab1fdf2f71102b06 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 26 Dec 2024 15:33:54 -0800 Subject: [PATCH 5/7] Fix bug --- modules/git/tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index 7832b40e01518..6717a5b5a7b15 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -66,7 +66,7 @@ func (repo *Repository) LsTree(ref string, filenames ...string) ([]string, error // GetTreePathLatestCommitID returns the latest commit of a tree path func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Commit, error) { stdout, _, err := NewCommand(repo.Ctx, "rev-list", "-1"). - AddDynamicArguments(refName).AddArguments("--").AddDynamicArguments(treePath). + AddDynamicArguments(refName).AddDashesAndList(treePath). RunStdString(&RunOpts{Dir: repo.Path}) if err != nil { return nil, err From aa06e02f8366eb57444b047cd46d12d0a8f24d67 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 26 Dec 2024 18:49:13 -0800 Subject: [PATCH 6/7] Use exist gitRepo --- routers/api/v1/repo/file.go | 9 +-------- routers/web/repo/download.go | 10 +--------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 8fd814b3967f6..8a4f78a3d770f 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -241,14 +241,7 @@ func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEn return nil, nil, nil } - gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository) - if err != nil { - ctx.Error(http.StatusInternalServerError, "RepositoryFromContextOrOpen", err) - return nil, nil, nil - } - defer closer.Close() - - latestCommit, err := gitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) + latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { ctx.Error(http.StatusInternalServerError, "GetTreePathLatestCommit", err) return nil, nil, nil diff --git a/routers/web/repo/download.go b/routers/web/repo/download.go index 743038eb1e7d7..cb1163c70b7ed 100644 --- a/routers/web/repo/download.go +++ b/routers/web/repo/download.go @@ -9,7 +9,6 @@ import ( 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" @@ -98,14 +97,7 @@ func getBlobForEntry(ctx *context.Context) (*git.Blob, *time.Time) { return nil, nil } - gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, ctx.Repo.Repository) - if err != nil { - ctx.ServerError("RepositoryFromContextOrOpen", err) - return nil, nil - } - defer closer.Close() - - latestCommit, err := gitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) + latestCommit, err := ctx.Repo.GitRepo.GetTreePathLatestCommit(ctx.Repo.Commit.ID.String(), ctx.Repo.TreePath) if err != nil { ctx.ServerError("GetTreePathLatestCommit", err) return nil, nil From 781fa018df97783ddfc4e8dccf7a85617af2be28 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 26 Dec 2024 20:21:00 -0800 Subject: [PATCH 7/7] Fix bug --- modules/git/tree.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/git/tree.go b/modules/git/tree.go index 6717a5b5a7b15..d35dc58d8d0ed 100644 --- a/modules/git/tree.go +++ b/modules/git/tree.go @@ -71,5 +71,5 @@ func (repo *Repository) GetTreePathLatestCommit(refName, treePath string) (*Comm if err != nil { return nil, err } - return repo.GetCommit(stdout) + return repo.GetCommit(strings.TrimSpace(stdout)) }