|
4 | 4 | package repo
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "fmt" |
7 | 8 | "net/http"
|
| 9 | + "net/url" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
8 | 12 |
|
| 13 | + "code.gitea.io/gitea/modules/base" |
9 | 14 | "code.gitea.io/gitea/modules/context"
|
| 15 | + "code.gitea.io/gitea/modules/git" |
| 16 | + "code.gitea.io/gitea/modules/lfs" |
| 17 | + "code.gitea.io/gitea/modules/structs" |
10 | 18 | files_service "code.gitea.io/gitea/services/repository/files"
|
11 | 19 | )
|
12 | 20 |
|
| 21 | +func GetDirInfos(ctx *context.APIContext) { |
| 22 | + var err error |
| 23 | + branch := ctx.Req.URL.Query().Get("branch") |
| 24 | + if len(branch) == 0 { |
| 25 | + branch = "main" |
| 26 | + } |
| 27 | + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(branch) |
| 28 | + if err != nil { |
| 29 | + ctx.JSON(http.StatusInternalServerError, "failed to get branch commit, error: "+err.Error()) |
| 30 | + return |
| 31 | + } |
| 32 | + path := ctx.Req.URL.Query().Get("path") |
| 33 | + entries, err := getDirectoryEntries(ctx, branch, path) |
| 34 | + if err != nil { |
| 35 | + ctx.JSON(http.StatusInternalServerError, "failed to get directry entries, error: "+err.Error()) |
| 36 | + return |
| 37 | + } |
| 38 | + ctx.JSON(http.StatusOK, entries) |
| 39 | +} |
| 40 | + |
| 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) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 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 | + } |
| 57 | + |
| 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 | + } |
| 81 | + } |
| 82 | + |
| 83 | + var ges = make([]structs.GitEntry, 0, len(commits)) |
| 84 | + for _, c := range commits { |
| 85 | + e := structs.GitEntry{ |
| 86 | + Name: c.Entry.Name(), |
| 87 | + Path: filepath.Join(path, c.Entry.Name()), |
| 88 | + Mode: c.Entry.Mode().String(), |
| 89 | + Type: c.Entry.Type(), |
| 90 | + Size: c.Entry.Size(), |
| 91 | + SHA: c.Commit.ID.String(), |
| 92 | + CommitMsg: c.Commit.CommitMessage, |
| 93 | + CommitterDate: c.Commit.Committer.When, |
| 94 | + } |
| 95 | + e.URL = ctx.Repo.Repository.HTMLURL() + "/raw/branch/" + url.PathEscape(branch) + "/" + url.PathEscape(strings.TrimPrefix(e.Path, "/")) |
| 96 | + e.DownloadUrl = e.URL |
| 97 | + //lfs pointer size is less than 1024 |
| 98 | + if c.Entry.Size() <= 1024 { |
| 99 | + content, _ := c.Entry.Blob().GetBlobContent(1024) |
| 100 | + p, _ := lfs.ReadPointerFromBuffer([]byte(content)) |
| 101 | + if p.IsValid() { |
| 102 | + e.Size = p.Size |
| 103 | + e.IsLfs = true |
| 104 | + e.LfsRelativePath = p.RelativePath() |
| 105 | + e.DownloadUrl = ctx.Repo.Repository.HTMLURL() + "/media/branch/" + url.PathEscape(branch) + "/" + url.PathEscape(strings.TrimPrefix(e.Path, "/")) |
| 106 | + } |
| 107 | + } |
| 108 | + ges = append(ges, e) |
| 109 | + } |
| 110 | + |
| 111 | + return ges, nil |
| 112 | +} |
| 113 | + |
13 | 114 | // GetTree get the tree of a repository.
|
14 | 115 | func GetTree(ctx *context.APIContext) {
|
15 | 116 | // swagger:operation GET /repos/{owner}/{repo}/git/trees/{sha} repository GetTree
|
|
0 commit comments