Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Jan 14, 2025
1 parent abf95a7 commit 9dfdb4f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
4 changes: 2 additions & 2 deletions routers/web/repo/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 2 additions & 15 deletions services/repository/files/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions services/repository/files/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -111,15 +111,15 @@ 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)
assert.EqualValues(t, "README.md", treeList[0].Path)
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)
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)

Expand Down

0 comments on commit 9dfdb4f

Please sign in to comment.