Skip to content

Commit 9dfdb4f

Browse files
committed
improvement
1 parent abf95a7 commit 9dfdb4f

File tree

3 files changed

+11
-24
lines changed

3 files changed

+11
-24
lines changed

routers/web/repo/tree.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func Tree(ctx *context.Context) {
6565
var results []*files_service.TreeViewNode
6666
var err error
6767
if !recursive {
68-
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, ctx.Repo.TreePath, ctx.Repo.RefFullName, false)
68+
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.RefFullName, false)
6969
} else {
70-
results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, ctx.Repo.TreePath, ctx.Repo.RefFullName)
70+
results, err = files_service.GetTreeInformation(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.TreePath, ctx.Repo.RefFullName)
7171
}
7272
if err != nil {
7373
ctx.ServerError("GetTreeInformation", err)

services/repository/files/tree.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
repo_model "code.gitea.io/gitea/models/repo"
1515
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/gitrepo"
1716
"code.gitea.io/gitea/modules/setting"
1817
api "code.gitea.io/gitea/modules/structs"
1918
"code.gitea.io/gitea/modules/util"
@@ -194,7 +193,7 @@ Example 3: (path: d3/d3d1)
194193
"path": "d3/d3d1/d3d1f2"
195194
}]
196195
*/
197-
func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName, recursive bool) ([]*TreeViewNode, error) {
196+
func GetTreeList(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, treePath string, ref git.RefName, recursive bool) ([]*TreeViewNode, error) {
198197
if repo.IsEmpty {
199198
return nil, nil
200199
}
@@ -211,12 +210,6 @@ func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath stri
211210
}
212211
treePath = cleanTreePath
213212

214-
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
215-
if err != nil {
216-
return nil, err
217-
}
218-
defer closer.Close()
219-
220213
// Get the commit object for the ref
221214
commit, err := gitRepo.GetCommit(ref.String())
222215
if err != nil {
@@ -390,7 +383,7 @@ Example 4: (path: d2/d2f1)
390383
"path": "f1"
391384
},]
392385
*/
393-
func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName) ([]*TreeViewNode, error) {
386+
func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, treePath string, ref git.RefName) ([]*TreeViewNode, error) {
394387
if repo.IsEmpty {
395388
return nil, nil
396389
}
@@ -407,12 +400,6 @@ func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePa
407400
}
408401
treePath = cleanTreePath
409402

410-
gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo)
411-
if err != nil {
412-
return nil, err
413-
}
414-
defer closer.Close()
415-
416403
// Get the commit object for the ref
417404
commit, err := gitRepo.GetCommit(ref.String())
418405
if err != nil {

services/repository/files/tree_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func Test_GetTreeList(t *testing.T) {
6363

6464
refName := git.RefNameFromBranch(ctx1.Repo.Repository.DefaultBranch)
6565

66-
treeList, err := GetTreeList(ctx1, ctx1.Repo.Repository, "", refName, true)
66+
treeList, err := GetTreeList(ctx1, ctx1.Repo.Repository, ctx1.Repo.GitRepo, "", refName, true)
6767
assert.NoError(t, err)
6868
assert.Len(t, treeList, 1)
6969
assert.EqualValues(t, "README.md", treeList[0].Name)
@@ -80,7 +80,7 @@ func Test_GetTreeList(t *testing.T) {
8080

8181
refName = git.RefNameFromBranch(ctx2.Repo.Repository.DefaultBranch)
8282

83-
treeList, err = GetTreeList(ctx2, ctx2.Repo.Repository, "", refName, true)
83+
treeList, err = GetTreeList(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "", refName, true)
8484
assert.NoError(t, err)
8585
assert.Len(t, treeList, 2)
8686

@@ -111,15 +111,15 @@ func Test_GetTreeInformation(t *testing.T) {
111111

112112
refName := git.RefNameFromBranch(ctx1.Repo.Repository.DefaultBranch)
113113

114-
treeList, err := GetTreeInformation(ctx1, ctx1.Repo.Repository, "", refName)
114+
treeList, err := GetTreeInformation(ctx1, ctx1.Repo.Repository, ctx1.Repo.GitRepo, "", refName)
115115
assert.NoError(t, err)
116116
assert.Len(t, treeList, 1)
117117
assert.EqualValues(t, "README.md", treeList[0].Name)
118118
assert.EqualValues(t, "README.md", treeList[0].Path)
119119
assert.EqualValues(t, "blob", treeList[0].Type)
120120
assert.Empty(t, treeList[0].Children)
121121

122-
treeList, err = GetTreeInformation(ctx1, ctx1.Repo.Repository, "README.md", refName)
122+
treeList, err = GetTreeInformation(ctx1, ctx1.Repo.Repository, ctx1.Repo.GitRepo, "README.md", refName)
123123
assert.NoError(t, err)
124124
assert.Len(t, treeList, 1)
125125
assert.EqualValues(t, "README.md", treeList[0].Name)
@@ -136,7 +136,7 @@ func Test_GetTreeInformation(t *testing.T) {
136136

137137
refName = git.RefNameFromBranch(ctx2.Repo.Repository.DefaultBranch)
138138

139-
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "", refName)
139+
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "", refName)
140140
assert.NoError(t, err)
141141
assert.Len(t, treeList, 2)
142142

@@ -150,7 +150,7 @@ func Test_GetTreeInformation(t *testing.T) {
150150
assert.EqualValues(t, "blob", treeList[1].Type)
151151
assert.Empty(t, treeList[1].Children)
152152

153-
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc", refName)
153+
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "doc", refName)
154154
assert.NoError(t, err)
155155
assert.Len(t, treeList, 2)
156156
assert.EqualValues(t, "doc", treeList[0].Name)
@@ -168,7 +168,7 @@ func Test_GetTreeInformation(t *testing.T) {
168168
assert.EqualValues(t, "blob", treeList[1].Type)
169169
assert.Empty(t, treeList[1].Children)
170170

171-
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc/doc.md", refName)
171+
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, ctx2.Repo.GitRepo, "doc/doc.md", refName)
172172
assert.NoError(t, err)
173173
assert.Len(t, treeList, 2)
174174

0 commit comments

Comments
 (0)