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

Commit 9f6ca8b

Browse files
author
泽华
committed
Merge branch 'fix-system-webhooks-bug' into 'main'
Fix system webhooks bug See merge request product/starhub/gitea-forked!2
2 parents 367deb1 + 88873ed commit 9f6ca8b

File tree

3 files changed

+116
-6
lines changed

3 files changed

+116
-6
lines changed

modules/structs/repo_tree.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33

44
package structs
55

6+
import "time"
7+
68
// GitEntry represents a git tree
79
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"`
10+
Name string `json:"name"`
11+
Path string `json:"path"`
12+
Mode string `json:"mode"`
13+
Type string `json:"type"`
14+
Size int64 `json:"size"`
15+
SHA string `json:"sha"`
16+
URL string `json:"url"`
17+
CommitMsg string `json:"commit_msg"`
18+
CommitterDate time.Time `json:"committer_date"`
19+
IsLfs bool `json:"is_lfs"`
20+
LfsRelativePath string `json:"lfs_relative_path"`
21+
DownloadUrl string `json:"download_url"`
1422
}
1523

1624
// 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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,113 @@
44
package repo
55

66
import (
7+
"fmt"
78
"net/http"
9+
"net/url"
10+
"path/filepath"
11+
"strings"
812

13+
"code.gitea.io/gitea/modules/base"
914
"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"
1018
files_service "code.gitea.io/gitea/services/repository/files"
1119
)
1220

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

0 commit comments

Comments
 (0)