0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-19 08:30:49 +02:00

Some renames and use type instead of isFile

This commit is contained in:
Lunny Xiao 2025-01-12 21:35:36 -08:00
parent 3c863223c5
commit 67a749f52c
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 83 additions and 66 deletions

View File

@ -59,7 +59,7 @@ func Tree(ctx *context.Context) {
refFullName := git.RefName("refs/" + ctx.FormTrim("ref")) refFullName := git.RefName("refs/" + ctx.FormTrim("ref"))
recursive := ctx.FormBool("recursive") recursive := ctx.FormBool("recursive")
var results []*files_service.TreeEntry var results []*files_service.TreeViewNode
var err error var err error
if !recursive { if !recursive {
results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, treePath, refFullName, false) results, err = files_service.GetTreeList(ctx, ctx.Repo.Repository, treePath, refFullName, false)

View File

@ -123,11 +123,27 @@ func GetTreeBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git
return tree, nil return tree, nil
} }
type TreeEntry struct { func entryModeString(entryMode git.EntryMode) string {
switch entryMode {
case git.EntryModeBlob:
return "blob"
case git.EntryModeExec:
return "exec"
case git.EntryModeSymlink:
return "symlink"
case git.EntryModeCommit:
return "commit" // submodule
case git.EntryModeTree:
return "tree"
}
return "unknown"
}
type TreeViewNode struct {
Name string `json:"name"` Name string `json:"name"`
IsFile bool `json:"isFile"` Type string `json:"type"`
Path string `json:"path"` Path string `json:"path"`
Children []*TreeEntry `json:"children,omitempty"` Children []*TreeViewNode `json:"children,omitempty"`
} }
/* /*
@ -178,7 +194,7 @@ Example 3: (path: d3/d3d1)
"path": "d3/d3d1/d3d1f2" "path": "d3/d3d1/d3d1f2"
}] }]
*/ */
func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName, recursive bool) ([]*TreeEntry, error) { func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName, recursive bool) ([]*TreeViewNode, error) {
if repo.IsEmpty { if repo.IsEmpty {
return nil, nil return nil, nil
} }
@ -231,36 +247,36 @@ func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath stri
return nil, err return nil, err
} }
var treeList []*TreeEntry var treeViewNodes []*TreeViewNode
mapTree := make(map[string][]*TreeEntry) mapTree := make(map[string][]*TreeViewNode)
for _, e := range entries { for _, e := range entries {
subTreePath := path.Join(treePath, e.Name()) subTreePath := path.Join(treePath, e.Name())
if strings.Contains(e.Name(), "/") { if strings.Contains(e.Name(), "/") {
mapTree[path.Dir(e.Name())] = append(mapTree[path.Dir(e.Name())], &TreeEntry{ mapTree[path.Dir(e.Name())] = append(mapTree[path.Dir(e.Name())], &TreeViewNode{
Name: path.Base(e.Name()), Name: path.Base(e.Name()),
IsFile: e.Mode() != git.EntryModeTree, Type: entryModeString(e.Mode()),
Path: subTreePath, Path: subTreePath,
}) })
} else { } else {
treeList = append(treeList, &TreeEntry{ treeViewNodes = append(treeViewNodes, &TreeViewNode{
Name: e.Name(), Name: e.Name(),
IsFile: e.Mode() != git.EntryModeTree, Type: entryModeString(e.Mode()),
Path: subTreePath, Path: subTreePath,
}) })
} }
} }
for _, tree := range treeList { for _, node := range treeViewNodes {
if !tree.IsFile { if node.Type == "tree" {
tree.Children = mapTree[tree.Path] node.Children = mapTree[node.Path]
sortTreeEntries(tree.Children) sortTreeViewNodes(node.Children)
} }
} }
sortTreeEntries(treeList) sortTreeViewNodes(treeViewNodes)
return treeList, nil return treeViewNodes, nil
} }
// GetTreeInformation returns the first level directories and files and all the trees of the path to treePath. // GetTreeInformation returns the first level directories and files and all the trees of the path to treePath.
@ -374,7 +390,7 @@ Example 4: (path: d2/d2f1)
"path": "f1" "path": "f1"
},] },]
*/ */
func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName) ([]*TreeEntry, error) { func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName) ([]*TreeViewNode, error) {
if repo.IsEmpty { if repo.IsEmpty {
return nil, nil return nil, nil
} }
@ -422,35 +438,35 @@ func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePa
} }
} }
treeList := make([]*TreeEntry, 0, len(rootEntries)) treeViewNodes := make([]*TreeViewNode, 0, len(rootEntries))
fields := strings.Split(dir, "/") fields := strings.Split(dir, "/")
var parentEntry *TreeEntry var parentNode *TreeViewNode
for _, entry := range rootEntries { for _, entry := range rootEntries {
treeEntry := &TreeEntry{ node := &TreeViewNode{
Name: entry.Name(), Name: entry.Name(),
IsFile: entry.Mode() != git.EntryModeTree, Type: entryModeString(entry.Mode()),
Path: entry.Name(), Path: entry.Name(),
} }
treeList = append(treeList, treeEntry) treeViewNodes = append(treeViewNodes, node)
if dir != "" && fields[0] == entry.Name() { if dir != "" && fields[0] == entry.Name() {
parentEntry = treeEntry parentNode = node
} }
} }
sortTreeEntries(treeList) sortTreeViewNodes(treeViewNodes)
if dir == "" || parentEntry == nil { if dir == "" || parentNode == nil {
return treeList, nil return treeViewNodes, nil
} }
for i := 1; i < len(fields); i++ { for i := 1; i < len(fields); i++ {
parentEntry.Children = []*TreeEntry{ parentNode.Children = []*TreeViewNode{
{ {
Name: fields[i], Name: fields[i],
IsFile: false, Type: "tree",
Path: path.Join(fields[:i+1]...), Path: path.Join(fields[:i+1]...),
}, },
} }
parentEntry = parentEntry.Children[0] parentNode = parentNode.Children[0]
} }
tree, err := commit.Tree.SubTree(dir) tree, err := commit.Tree.SubTree(dir)
@ -463,22 +479,23 @@ func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePa
} }
for _, entry := range entries { for _, entry := range entries {
parentEntry.Children = append(parentEntry.Children, &TreeEntry{ parentNode.Children = append(parentNode.Children, &TreeViewNode{
Name: entry.Name(), Name: entry.Name(),
IsFile: entry.Mode() != git.EntryModeTree, Type: entryModeString(entry.Mode()),
Path: path.Join(dir, entry.Name()), Path: path.Join(dir, entry.Name()),
}) })
} }
sortTreeEntries(parentEntry.Children) sortTreeViewNodes(parentNode.Children)
return treeList, nil return treeViewNodes, nil
} }
// sortTreeEntries list directory first and with alpha sequence // sortTreeViewNodes list directory first and with alpha sequence
func sortTreeEntries(entries []*TreeEntry) { func sortTreeViewNodes(nodes []*TreeViewNode) {
sort.Slice(entries, func(i, j int) bool { sort.Slice(nodes, func(i, j int) bool {
if entries[i].IsFile != entries[j].IsFile { if nodes[i].Type != nodes[j].Type {
return !entries[i].IsFile // folder and submodule first
return nodes[i].Type == "tree" || nodes[i].Type == "commit"
} }
return entries[i].Name < entries[j].Name return nodes[i].Name < nodes[j].Name
}) })
} }

View File

@ -68,7 +68,7 @@ func Test_GetTreeList(t *testing.T) {
assert.Len(t, treeList, 1) assert.Len(t, treeList, 1)
assert.EqualValues(t, "README.md", treeList[0].Name) assert.EqualValues(t, "README.md", treeList[0].Name)
assert.EqualValues(t, "README.md", treeList[0].Path) assert.EqualValues(t, "README.md", treeList[0].Path)
assert.True(t, treeList[0].IsFile) assert.EqualValues(t, "blob", treeList[0].Type)
assert.Empty(t, treeList[0].Children) assert.Empty(t, treeList[0].Children)
ctx2, _ := contexttest.MockContext(t, "org3/repo3") ctx2, _ := contexttest.MockContext(t, "org3/repo3")
@ -86,17 +86,17 @@ func Test_GetTreeList(t *testing.T) {
assert.EqualValues(t, "doc", treeList[0].Name) assert.EqualValues(t, "doc", treeList[0].Name)
assert.EqualValues(t, "doc", treeList[0].Path) assert.EqualValues(t, "doc", treeList[0].Path)
assert.False(t, treeList[0].IsFile) assert.EqualValues(t, "tree", treeList[0].Type)
assert.Len(t, treeList[0].Children, 1) assert.Len(t, treeList[0].Children, 1)
assert.EqualValues(t, "doc.md", treeList[0].Children[0].Name) assert.EqualValues(t, "doc.md", treeList[0].Children[0].Name)
assert.EqualValues(t, "doc/doc.md", treeList[0].Children[0].Path) assert.EqualValues(t, "doc/doc.md", treeList[0].Children[0].Path)
assert.True(t, treeList[0].Children[0].IsFile) assert.EqualValues(t, "blob", treeList[0].Children[0].Type)
assert.Empty(t, treeList[0].Children[0].Children) assert.Empty(t, treeList[0].Children[0].Children)
assert.EqualValues(t, "README.md", treeList[1].Name) assert.EqualValues(t, "README.md", treeList[1].Name)
assert.EqualValues(t, "README.md", treeList[1].Path) assert.EqualValues(t, "README.md", treeList[1].Path)
assert.True(t, treeList[1].IsFile) assert.EqualValues(t, "blob", treeList[1].Type)
assert.Empty(t, treeList[1].Children) assert.Empty(t, treeList[1].Children)
} }
@ -116,7 +116,7 @@ func Test_GetTreeInformation(t *testing.T) {
assert.Len(t, treeList, 1) assert.Len(t, treeList, 1)
assert.EqualValues(t, "README.md", treeList[0].Name) assert.EqualValues(t, "README.md", treeList[0].Name)
assert.EqualValues(t, "README.md", treeList[0].Path) assert.EqualValues(t, "README.md", treeList[0].Path)
assert.True(t, treeList[0].IsFile) assert.EqualValues(t, "blob", treeList[0].Type)
assert.Empty(t, treeList[0].Children) assert.Empty(t, treeList[0].Children)
treeList, err = GetTreeInformation(ctx1, ctx1.Repo.Repository, "README.md", refName) treeList, err = GetTreeInformation(ctx1, ctx1.Repo.Repository, "README.md", refName)
@ -124,7 +124,7 @@ func Test_GetTreeInformation(t *testing.T) {
assert.Len(t, treeList, 1) assert.Len(t, treeList, 1)
assert.EqualValues(t, "README.md", treeList[0].Name) assert.EqualValues(t, "README.md", treeList[0].Name)
assert.EqualValues(t, "README.md", treeList[0].Path) assert.EqualValues(t, "README.md", treeList[0].Path)
assert.True(t, treeList[0].IsFile) assert.EqualValues(t, "blob", treeList[0].Type)
assert.Empty(t, treeList[0].Children) assert.Empty(t, treeList[0].Children)
ctx2, _ := contexttest.MockContext(t, "org3/repo3") ctx2, _ := contexttest.MockContext(t, "org3/repo3")
@ -142,12 +142,12 @@ func Test_GetTreeInformation(t *testing.T) {
assert.EqualValues(t, "doc", treeList[0].Name) assert.EqualValues(t, "doc", treeList[0].Name)
assert.EqualValues(t, "doc", treeList[0].Path) assert.EqualValues(t, "doc", treeList[0].Path)
assert.False(t, treeList[0].IsFile) assert.EqualValues(t, "tree", treeList[0].Type)
assert.Empty(t, treeList[0].Children) assert.Empty(t, treeList[0].Children)
assert.EqualValues(t, "README.md", treeList[1].Name) assert.EqualValues(t, "README.md", treeList[1].Name)
assert.EqualValues(t, "README.md", treeList[1].Path) assert.EqualValues(t, "README.md", treeList[1].Path)
assert.True(t, treeList[1].IsFile) assert.EqualValues(t, "blob", treeList[1].Type)
assert.Empty(t, treeList[1].Children) assert.Empty(t, treeList[1].Children)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc", refName) treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc", refName)
@ -155,17 +155,17 @@ func Test_GetTreeInformation(t *testing.T) {
assert.Len(t, treeList, 2) assert.Len(t, treeList, 2)
assert.EqualValues(t, "doc", treeList[0].Name) assert.EqualValues(t, "doc", treeList[0].Name)
assert.EqualValues(t, "doc", treeList[0].Path) assert.EqualValues(t, "doc", treeList[0].Path)
assert.False(t, treeList[0].IsFile) assert.EqualValues(t, "tree", treeList[0].Type)
assert.Len(t, treeList[0].Children, 1) assert.Len(t, treeList[0].Children, 1)
assert.EqualValues(t, "doc.md", treeList[0].Children[0].Name) assert.EqualValues(t, "doc.md", treeList[0].Children[0].Name)
assert.EqualValues(t, "doc/doc.md", treeList[0].Children[0].Path) assert.EqualValues(t, "doc/doc.md", treeList[0].Children[0].Path)
assert.True(t, treeList[0].Children[0].IsFile) assert.EqualValues(t, "blob", treeList[0].Children[0].Type)
assert.Empty(t, treeList[0].Children[0].Children) assert.Empty(t, treeList[0].Children[0].Children)
assert.EqualValues(t, "README.md", treeList[1].Name) assert.EqualValues(t, "README.md", treeList[1].Name)
assert.EqualValues(t, "README.md", treeList[1].Path) assert.EqualValues(t, "README.md", treeList[1].Path)
assert.True(t, treeList[1].IsFile) assert.EqualValues(t, "blob", treeList[1].Type)
assert.Empty(t, treeList[1].Children) assert.Empty(t, treeList[1].Children)
treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc/doc.md", refName) treeList, err = GetTreeInformation(ctx2, ctx2.Repo.Repository, "doc/doc.md", refName)
@ -174,16 +174,16 @@ func Test_GetTreeInformation(t *testing.T) {
assert.EqualValues(t, "doc", treeList[0].Name) assert.EqualValues(t, "doc", treeList[0].Name)
assert.EqualValues(t, "doc", treeList[0].Path) assert.EqualValues(t, "doc", treeList[0].Path)
assert.False(t, treeList[0].IsFile) assert.EqualValues(t, "tree", treeList[0].Type)
assert.Len(t, treeList[0].Children, 1) assert.Len(t, treeList[0].Children, 1)
assert.EqualValues(t, "doc.md", treeList[0].Children[0].Name) assert.EqualValues(t, "doc.md", treeList[0].Children[0].Name)
assert.EqualValues(t, "doc/doc.md", treeList[0].Children[0].Path) assert.EqualValues(t, "doc/doc.md", treeList[0].Children[0].Path)
assert.True(t, treeList[0].Children[0].IsFile) assert.EqualValues(t, "blob", treeList[0].Children[0].Type)
assert.Empty(t, treeList[0].Children[0].Children) assert.Empty(t, treeList[0].Children[0].Children)
assert.EqualValues(t, "README.md", treeList[1].Name) assert.EqualValues(t, "README.md", treeList[1].Name)
assert.EqualValues(t, "README.md", treeList[1].Path) assert.EqualValues(t, "README.md", treeList[1].Path)
assert.True(t, treeList[1].IsFile) assert.EqualValues(t, "blob", treeList[1].Type)
assert.Empty(t, treeList[1].Children) assert.Empty(t, treeList[1].Children)
} }