1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-01-04 09:17:43 +03:00

add function for recusive

This commit is contained in:
Lunny Xiao 2024-12-26 00:30:03 -08:00
parent 15f4b920b7
commit 2178347348
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 98 additions and 2 deletions

View File

@ -68,12 +68,20 @@ func Tree(ctx *context.Context) {
defer closer.Close()
refName := gitRepo.UnstableGuessRefByShortName(ref)
if !recursive {
results, err := files_service.GetTreeList(ctx, ctx.Repo.Repository, dir, refName, false)
if err != nil {
ctx.ServerError("GetTreeList", err)
return
}
ctx.JSON(http.StatusOK, results)
return
}
results, err := files_service.GetTreeList(ctx, ctx.Repo.Repository, dir, refName, recursive)
results, err := files_service.GetTreeInformation(ctx, ctx.Repo.Repository, dir, refName)
if err != nil {
ctx.ServerError("GetTreeList", err)
return
}
ctx.JSON(http.StatusOK, results)
}

View File

@ -210,3 +210,91 @@ func GetTreeList(ctx context.Context, repo *repo_model.Repository, treePath stri
return treeList, nil
}
// GetTreeInformation returns the first level directories and files and all the trees of the path to treePath.
// If treePath is a directory, list all subdirectories and files of treePath.
func GetTreeInformation(ctx context.Context, repo *repo_model.Repository, treePath string, ref git.RefName) ([]*TreeEntry, error) {
if repo.IsEmpty {
return nil, nil
}
if ref == "" {
ref = git.RefNameFromBranch(repo.DefaultBranch)
}
// Check that the path given in opts.treePath is valid (not a git path)
cleanTreePath := CleanUploadFileName(treePath)
if cleanTreePath == "" && treePath != "" {
return nil, ErrFilenameInvalid{
Path: treePath,
}
}
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 {
return nil, err
}
// get root entries
rootEntry, err := commit.GetTreeEntryByPath("")
if err != nil {
return nil, err
}
rootEntries, err := rootEntry.Tree().ListEntries()
if err != nil {
return nil, err
}
var treeList []*TreeEntry
for _, rootEntry := range rootEntries {
treeList = append(treeList, &TreeEntry{
Name: rootEntry.Name(),
IsFile: rootEntry.Mode() != git.EntryModeTree,
Path: rootEntry.Name(),
})
}
if treePath == "" {
return treeList, nil
}
listEntry, err := commit.GetTreeEntryByPath(treePath)
if err != nil {
return nil, err
}
dir := treePath
// list current entry or parent entry if it's a file's children
// If the entry is a file, we return a FileContentResponse object
if listEntry.IsRegular() {
dir = path.Dir(treePath)
if dir == "" {
return treeList, nil
}
listEntry, err = commit.GetTreeEntryByPath(dir)
if err != nil {
return nil, err
}
}
entries, err := listEntry.Tree().ListEntries()
if err != nil {
return nil, err
}
for _, entry := range entries {
treeList = append(treeList, &TreeEntry{
Name: entry.Name(),
IsFile: entry.Mode() != git.EntryModeTree,
Path: path.Join(treePath, entry.Name()),
})
}
return treeList, nil
}