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

Commit bd9ee63

Browse files
committed
Add file path support to GetDirInfos
1 parent 75eb68b commit bd9ee63

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

routers/api/v1/repo/tree.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,52 @@ func GetDirInfos(ctx *context.APIContext) {
3737
ctx.JSON(http.StatusOK, entries)
3838
}
3939

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

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)
50+
if entry.Type() != "tree" {
51+
allEntries = append(allEntries, entry)
52+
commit, err := ctx.Repo.Commit.GetCommitByPath(path)
53+
if err != nil {
54+
return nil, err
55+
}
5156

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)
57+
commitInfo := git.CommitInfo{
58+
Entry: entry,
59+
Commit: commit,
60+
}
61+
commits = append(commits, commitInfo)
62+
63+
} else {
64+
tree, err := ctx.Repo.Commit.SubTree(path)
65+
if err != nil {
66+
return nil, fmt.Errorf("failed to exec SubTree, cause:%w", err)
67+
}
68+
69+
allEntries, err := tree.ListEntries()
70+
if err != nil {
71+
return nil, fmt.Errorf("failed to exec ListEntries, cause:%w", err)
72+
}
73+
allEntries.CustomSort(base.NaturalSortLess)
74+
75+
commits, _, err = allEntries.GetCommitsInfo(ctx, ctx.Repo.Commit, path)
76+
if err != nil {
77+
return nil, fmt.Errorf("failed to exec GetCommitsInfo, cause:%w", err)
78+
}
5679
}
5780

5881
var ges = make([]structs.GitEntry, 0, len(commits))
5982
for _, c := range commits {
6083
e := structs.GitEntry{
6184
Name: c.Entry.Name(),
62-
Path: folder + "/" + c.Entry.Name(),
85+
Path: path + "/" + c.Entry.Name(),
6386
Mode: c.Entry.Mode().String(),
6487
Type: c.Entry.Type(),
6588
Size: c.Entry.Size(),

0 commit comments

Comments
 (0)