Skip to content
This repository was archived by the owner on Sep 18, 2024. It is now read-only.

Commit 88873ed

Browse files
author
Da.Lei
committed
Merge branch 'add-file-path-support-to-get-dir-infos' into 'fix-system-webhooks-bug'
Add file path support to GetDirInfos See merge request product/starhub/gitea-forked!1
2 parents 75eb68b + 246acf2 commit 88873ed

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

routers/api/v1/repo/tree.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"net/http"
99
"net/url"
10+
"path/filepath"
1011
"strings"
1112

1213
"code.gitea.io/gitea/modules/base"
@@ -37,29 +38,53 @@ func GetDirInfos(ctx *context.APIContext) {
3738
ctx.JSON(http.StatusOK, entries)
3839
}
3940

40-
func getDirectoryEntries(ctx *context.APIContext, branch, folder string) ([]structs.GitEntry, error) {
41-
tree, err := ctx.Repo.Commit.SubTree(folder)
41+
func getDirectoryEntries(ctx *context.APIContext, branch, path string) ([]structs.GitEntry, error) {
42+
var (
43+
allEntries git.Entries
44+
commits []git.CommitInfo
45+
)
46+
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(path)
4247
if err != nil {
43-
return nil, fmt.Errorf("failed to exec SubTree, cause:%w", err)
48+
return nil, err
4449
}
4550

46-
allEntries, err := tree.ListEntries()
47-
if err != nil {
48-
return nil, fmt.Errorf("failed to exec ListEntries, cause:%w", err)
49-
}
50-
allEntries.CustomSort(base.NaturalSortLess)
51+
if entry.Type() != "tree" {
52+
allEntries = append(allEntries, entry)
53+
commit, err := ctx.Repo.Commit.GetCommitByPath(path)
54+
if err != nil {
55+
return nil, err
56+
}
5157

52-
var commits []git.CommitInfo
53-
commits, _, err = allEntries.GetCommitsInfo(ctx, ctx.Repo.Commit, folder)
54-
if err != nil {
55-
return nil, fmt.Errorf("failed to exec GetCommitsInfo, cause:%w", err)
58+
commitInfo := git.CommitInfo{
59+
Entry: entry,
60+
Commit: commit,
61+
}
62+
commits = append(commits, commitInfo)
63+
path = filepath.Dir(path)
64+
65+
} else {
66+
tree, err := ctx.Repo.Commit.SubTree(path)
67+
if err != nil {
68+
return nil, fmt.Errorf("failed to exec SubTree, cause:%w", err)
69+
}
70+
71+
allEntries, err := tree.ListEntries()
72+
if err != nil {
73+
return nil, fmt.Errorf("failed to exec ListEntries, cause:%w", err)
74+
}
75+
allEntries.CustomSort(base.NaturalSortLess)
76+
77+
commits, _, err = allEntries.GetCommitsInfo(ctx, ctx.Repo.Commit, path)
78+
if err != nil {
79+
return nil, fmt.Errorf("failed to exec GetCommitsInfo, cause:%w", err)
80+
}
5681
}
5782

5883
var ges = make([]structs.GitEntry, 0, len(commits))
5984
for _, c := range commits {
6085
e := structs.GitEntry{
6186
Name: c.Entry.Name(),
62-
Path: folder + "/" + c.Entry.Name(),
87+
Path: filepath.Join(path, c.Entry.Name()),
6388
Mode: c.Entry.Mode().String(),
6489
Type: c.Entry.Type(),
6590
Size: c.Entry.Size(),

0 commit comments

Comments
 (0)