|
7 | 7 | "fmt"
|
8 | 8 | "net/http"
|
9 | 9 | "net/url"
|
| 10 | + "path/filepath" |
10 | 11 | "strings"
|
11 | 12 |
|
12 | 13 | "code.gitea.io/gitea/modules/base"
|
@@ -37,29 +38,53 @@ func GetDirInfos(ctx *context.APIContext) {
|
37 | 38 | ctx.JSON(http.StatusOK, entries)
|
38 | 39 | }
|
39 | 40 |
|
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) |
42 | 47 | if err != nil {
|
43 |
| - return nil, fmt.Errorf("failed to exec SubTree, cause:%w", err) |
| 48 | + return nil, err |
44 | 49 | }
|
45 | 50 |
|
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 | + } |
51 | 57 |
|
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 | + } |
56 | 81 | }
|
57 | 82 |
|
58 | 83 | var ges = make([]structs.GitEntry, 0, len(commits))
|
59 | 84 | for _, c := range commits {
|
60 | 85 | e := structs.GitEntry{
|
61 | 86 | Name: c.Entry.Name(),
|
62 |
| - Path: folder + "/" + c.Entry.Name(), |
| 87 | + Path: filepath.Join(path, c.Entry.Name()), |
63 | 88 | Mode: c.Entry.Mode().String(),
|
64 | 89 | Type: c.Entry.Type(),
|
65 | 90 | Size: c.Entry.Size(),
|
|
0 commit comments