|
4 | 4 | package repo
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "fmt" |
7 | 8 | "net/http"
|
8 | 9 |
|
| 10 | + "code.gitea.io/gitea/modules/base" |
9 | 11 | "code.gitea.io/gitea/modules/context"
|
| 12 | + "code.gitea.io/gitea/modules/git" |
| 13 | + "code.gitea.io/gitea/modules/lfs" |
| 14 | + "code.gitea.io/gitea/modules/structs" |
10 | 15 | files_service "code.gitea.io/gitea/services/repository/files"
|
11 | 16 | )
|
12 | 17 |
|
| 18 | +func GetDirInfos(ctx *context.APIContext) { |
| 19 | + var err error |
| 20 | + branch := ctx.Req.URL.Query().Get("branch") |
| 21 | + if len(branch) == 0 { |
| 22 | + branch = "main" |
| 23 | + } |
| 24 | + ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(branch) |
| 25 | + if err != nil { |
| 26 | + ctx.JSON(http.StatusInternalServerError, "failed to get branch commit, error: "+err.Error()) |
| 27 | + return |
| 28 | + } |
| 29 | + path := ctx.Req.URL.Query().Get("path") |
| 30 | + entries, err := getDirectoryEntries(ctx, path) |
| 31 | + if err != nil { |
| 32 | + ctx.JSON(http.StatusInternalServerError, "failed to get directry entries, error: "+err.Error()) |
| 33 | + return |
| 34 | + } |
| 35 | + ctx.JSON(http.StatusOK, entries) |
| 36 | +} |
| 37 | + |
| 38 | +func getDirectoryEntries(ctx *context.APIContext, folder string) ([]structs.GitEntry, error) { |
| 39 | + tree, err := ctx.Repo.Commit.SubTree(folder) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("failed to exec SubTree, cause:%w", err) |
| 42 | + } |
| 43 | + |
| 44 | + allEntries, err := tree.ListEntries() |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to exec ListEntries, cause:%w", err) |
| 47 | + } |
| 48 | + allEntries.CustomSort(base.NaturalSortLess) |
| 49 | + |
| 50 | + var commits []git.CommitInfo |
| 51 | + commits, _, err = allEntries.GetCommitsInfo(ctx, ctx.Repo.Commit, folder) |
| 52 | + if err != nil { |
| 53 | + return nil, fmt.Errorf("failed to exec GetCommitsInfo, cause:%w", err) |
| 54 | + } |
| 55 | + var ges = make([]structs.GitEntry, 0, len(commits)) |
| 56 | + for _, c := range commits { |
| 57 | + |
| 58 | + e := structs.GitEntry{ |
| 59 | + Path: c.Entry.Name(), |
| 60 | + Mode: c.Entry.Mode().String(), |
| 61 | + Type: c.Entry.Type(), |
| 62 | + Size: c.Entry.Size(), |
| 63 | + SHA: c.Commit.ID.String(), |
| 64 | + URL: "", |
| 65 | + CommitMsg: c.Commit.CommitMessage, |
| 66 | + } |
| 67 | + //lfs pointer size is less than 1024 |
| 68 | + if c.Entry.Size() <= 1024 { |
| 69 | + content, _ := c.Entry.Blob().GetBlobContent(1024) |
| 70 | + p, _ := lfs.ReadPointerFromBuffer([]byte(content)) |
| 71 | + if p.IsValid() { |
| 72 | + e.Size = p.Size |
| 73 | + e.IsLfs = true |
| 74 | + } |
| 75 | + } |
| 76 | + ges = append(ges, e) |
| 77 | + } |
| 78 | + |
| 79 | + return ges, nil |
| 80 | +} |
| 81 | + |
13 | 82 | // GetTree get the tree of a repository.
|
14 | 83 | func GetTree(ctx *context.APIContext) {
|
15 | 84 | // swagger:operation GET /repos/{owner}/{repo}/git/trees/{sha} repository GetTree
|
|
0 commit comments