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

Commit bc85588

Browse files
author
Lei Da
committed
add new api GetDirInfos to display basic directory info
1 parent 72875fd commit bc85588

File tree

3 files changed

+78
-6
lines changed

3 files changed

+78
-6
lines changed

modules/structs/repo_tree.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package structs
55

66
// GitEntry represents a git tree
77
type GitEntry struct {
8-
Path string `json:"path"`
9-
Mode string `json:"mode"`
10-
Type string `json:"type"`
11-
Size int64 `json:"size"`
12-
SHA string `json:"sha"`
13-
URL string `json:"url"`
8+
Path string `json:"path"`
9+
Mode string `json:"mode"`
10+
Type string `json:"type"`
11+
Size int64 `json:"size"`
12+
SHA string `json:"sha"`
13+
URL string `json:"url"`
14+
CommitMsg string `json:"commit_msg"`
15+
IsLfs bool `json:"is_lfs"`
1416
}
1517

1618
// GitTreeResponse returns a git tree

routers/api/v1/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,7 @@ func Routes() *web.Route {
12341234
})
12351235
m.Get("/refs", repo.GetGitAllRefs)
12361236
m.Get("/refs/*", repo.GetGitRefs)
1237+
m.Get("/dir", repo.GetDirInfos)
12371238
m.Get("/trees/{sha}", repo.GetTree)
12381239
m.Get("/blobs/{sha}", repo.GetBlob)
12391240
m.Get("/tags/{sha}", repo.GetAnnotatedTag)

routers/api/v1/repo/tree.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,81 @@
44
package repo
55

66
import (
7+
"fmt"
78
"net/http"
89

10+
"code.gitea.io/gitea/modules/base"
911
"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"
1015
files_service "code.gitea.io/gitea/services/repository/files"
1116
)
1217

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+
1382
// GetTree get the tree of a repository.
1483
func GetTree(ctx *context.APIContext) {
1584
// swagger:operation GET /repos/{owner}/{repo}/git/trees/{sha} repository GetTree

0 commit comments

Comments
 (0)