0
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-07-24 00:55:42 +02:00
gitea/routers/web/repo/tree.go
2024-12-16 09:36:07 -08:00

81 lines
1.6 KiB
Go

// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"net/http"
"path"
"strings"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/context"
"github.com/go-enry/go-enry/v2"
)
// TreeList get all files' entries of a repository
func TreeList(ctx *context.Context) {
tree, err := ctx.Repo.Commit.SubTree("/")
if err != nil {
ctx.ServerError("Repo.Commit.SubTree", err)
return
}
entries, err := tree.ListEntriesRecursiveFast()
if err != nil {
ctx.ServerError("ListEntriesRecursiveFast", err)
return
}
entries.CustomSort(base.NaturalSortLess)
files := make([]string, 0, len(entries))
for _, entry := range entries {
if !isExcludedEntry(entry) {
files = append(files, entry.Name())
}
}
ctx.JSON(http.StatusOK, files)
}
func isExcludedEntry(entry *git.TreeEntry) bool {
if entry.IsDir() {
return true
}
if entry.IsSubModule() {
return true
}
if enry.IsVendor(entry.Name()) {
return true
}
return false
}
func getPossibleBranches(dir string) []string {
cnt := strings.Count(dir, "/")
branches := make([]string, cnt, cnt)
for i := 0; i < cnt; i++ {
branches[i] = dir
dir = path.Dir(dir)
}
return branches
}
func guessRefInfoAndDir(ctx *context.Context, dir string) (git.RefName, string, error) {
branches := getPossibleBranches(dir)
}
func Tree(ctx *context.Context) {
pathParam := ctx.PathParam("*")
dir := path.Dir(pathParam)
refName, realDir, err := guessRefInfoAndDir(ctx, dir)
if err != nil {
ctx.ServerError("guessRefInfoAndDir", err)
return
}
}