improvement

This commit is contained in:
Lunny Xiao 2025-01-13 19:50:06 -08:00
parent abf95a76db
commit 9dfdb4fc08
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 11 additions and 24 deletions

View File

@ -65,9 +65,9 @@ func Tree(ctx *context.Context) {
var results []*files_service.TreeViewNode
var err error
if !recursive {
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, ctx.Repo.TreePath, ctx.Repo.RefFullName, false)
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.RefFullName, false)
} else {
results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, ctx.Repo.TreePath, ctx.Repo.RefFullName)
results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.RefFullName)
}
if err != nil {
ctx.ServerError("GetTreeInformation", err)

View File

@ -13,7 +13,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
@ -194,7 +193,7 @@ Example 3: (path: d3/d3d1)
"path": "d3/d3d1/d3d1f2"
}]
*/
func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName, recursive bool) ([]*TreeViewNode, error) {
func GetTreeList(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, treePath string, ref git.RefName, recursive bool) ([]*TreeViewNode, error) {
if repo.IsEmpty {
return nil, nil
}
@ -211,12 +210,6 @@ func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath stri
}
treePath = cleanTreePath
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return nil, err
}
defer closer.Close()
// Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref.String())
if err != nil {
@ -390,7 +383,7 @@ Example 4: (path: d2/d2f1)
"path": "f1"
},]
*/
func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName) ([]*TreeViewNode, error) {
func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, treePath string, ref git.RefName) ([]*TreeViewNode, error) {
if repo.IsEmpty {
return nil, nil
}
@ -407,12 +400,6 @@ func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePa
}
treePath = cleanTreePath
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
if err != nil {
return nil, err
}
defer closer.Close()
// Get the commit object for the ref
commit, err := gitRepo.GetCommit(ref.String())
if err != nil {

View File

@ -63,7 +63,7 @@ func Test_GetTreeList(t *testing.T) {
refName := git.RefNameFromBranch(ctx1.Repo.Repository.DefaultBranch)
treeList, err := GetTreeList(ctx1, ctx1.Repo.Repository, "", refName, true)
treeList, err := GetTreeList(ctx1, ctx1.Repo.Repository, ctx1.Repo.GitRepo, "", refName, true)
assert.NoError(t, err)
assert.Len(t, treeList, 1)
assert.EqualValues(t, "README.md", treeList[0].Name)
@ -80,7 +80,7 @@ func Test_GetTreeList(t *testing.T) {
refName = git.RefNameFromBranch(ctx2.Repo.Repository.DefaultBranch)
treeList, err = GetTreeList(ctx2, ctx2.Repo.Repository, "", refName, true)
treeList, err = GetTreeList(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "", refName, true)
assert.NoError(t, err)
assert.Len(t, treeList, 2)
@ -111,7 +111,7 @@ func Test_GetTreeInformation(t *testing.T) {
refName := git.RefNameFromBranch(ctx1.Repo.Repository.DefaultBranch)
treeList, err := GetTreeInformation(ctx1, ctx1.Repo.Repository, "", refName)
treeList, err := GetTreeInformation(ctx1, ctx1.Repo.Repository, ctx1.Repo.GitRepo, "", refName)
assert.NoError(t, err)
assert.Len(t, treeList, 1)
assert.EqualValues(t, "README.md", treeList[0].Name)
@ -119,7 +119,7 @@ func Test_GetTreeInformation(t *testing.T) {
assert.EqualValues(t, "blob", treeList[0].Type)
assert.Empty(t, treeList[0].Children)
treeList, err = GetTreeInformation(ctx1, ctx1.Repo.Repository, "README.md", refName)
treeList, err = GetTreeInformation(ctx1, ctx1.Repo.Repository, ctx1.Repo.GitRepo, "README.md", refName)
assert.NoError(t, err)
assert.Len(t, treeList, 1)
assert.EqualValues(t, "README.md", treeList[0].Name)
@ -136,7 +136,7 @@ func Test_GetTreeInformation(t *testing.T) {
refName = git.RefNameFromBranch(ctx2.Repo.Repository.DefaultBranch)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "", refName)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "", refName)
assert.NoError(t, err)
assert.Len(t, treeList, 2)
@ -150,7 +150,7 @@ func Test_GetTreeInformation(t *testing.T) {
assert.EqualValues(t, "blob", treeList[1].Type)
assert.Empty(t, treeList[1].Children)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc", refName)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "doc", refName)
assert.NoError(t, err)
assert.Len(t, treeList, 2)
assert.EqualValues(t, "doc", treeList[0].Name)
@ -168,7 +168,7 @@ func Test_GetTreeInformation(t *testing.T) {
assert.EqualValues(t, "blob", treeList[1].Type)
assert.Empty(t, treeList[1].Children)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc/doc.md", refName)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "doc/doc.md", refName)
assert.NoError(t, err)
assert.Len(t, treeList, 2)