新增:获取仓库下所有文件文件夹内容(包含最后一次commit信息)

This commit is contained in:
yystopf 2022-12-23 17:57:04 +08:00
parent 7cadb49770
commit 5fa04a24db
4 changed files with 259 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package structs
import (
gitea_api "code.gitea.io/gitea/modules/structs"
)
type ContentsResponse struct {
*gitea_api.ContentsResponse
LatestCommit ContentsResponseCommit `json:"latest_commit"`
}
type ContentsResponseCommit struct {
Message string `json:"message"`
Sha string `json:"sha"`
CreatedAt int64 `json:"created_at"`
}

View File

@ -114,6 +114,10 @@ func Routers(ctx gocontext.Context) *web.Route {
m.Group("/file_commits", func() {
m.Get("/*", context.ReferencesGitRepo(), repo.GetFileAllCommits)
})
m.Group("/contents", func() {
m.Get("", repo.GetContentsList)
m.Get("/*", repo.GetContents)
}, reqRepoReader(unit.TypeCode))
}, repoAssignment())
})
m.Group("/users", func() {

View File

@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/modules/git"
api "code.gitea.io/gitea/modules/structs"
files_service "code.gitea.io/gitea/services/repository/files"
hat_files_service "code.gitlink.org.cn/Gitlink/gitea_hat.git/services/repository/files"
)
func GetReadmeContents(ctx *context.APIContext) {
@ -107,3 +108,30 @@ func GetReadmeContentsByPath(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, fileList)
}
}
func GetContents(ctx *context.APIContext) {
if !canReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "GetContents", repo_model.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.Doer.ID,
RepoName: ctx.Repo.Repository.Name,
})
return
}
treePath := ctx.Params("*")
ref := ctx.FormTrim("ref")
if fileList, err := hat_files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
}
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
} else {
ctx.JSON(http.StatusOK, fileList)
}
}
func GetContentsList(ctx *context.APIContext) {
GetContents(ctx)
}

View File

@ -0,0 +1,211 @@
package files
import (
"context"
"fmt"
"net/url"
"path"
"strings"
"code.gitea.io/gitea/models"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
gitea_api "code.gitea.io/gitea/modules/structs"
gitea_files_service "code.gitea.io/gitea/services/repository/files"
hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
)
func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePath, ref string) (interface{}, error) {
if repo.IsEmpty {
return make([]interface{}, 0), nil
}
if ref == "" {
ref = repo.DefaultBranch
}
origRef := ref
cleanTreePath := gitea_files_service.CleanUploadFileName(treePath)
if cleanTreePath == "" && treePath != "" {
return nil, models.ErrFilenameInvalid{
Path: treePath,
}
}
treePath = cleanTreePath
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
defer closer.Close()
commit, err := gitRepo.GetCommit(ref)
if err != nil {
return nil, err
}
entry, err := commit.GetTreeEntryByPath(treePath)
if err != nil {
return nil, err
}
if entry.Type() != "tree" {
return GetContents(ctx, repo, treePath, origRef, false)
}
var fileList []*hat_api.ContentsResponse
gitTree, err := commit.SubTree(treePath)
if err != nil {
return nil, err
}
entries, err := gitTree.ListEntries()
if err != nil {
return nil, err
}
for _, e := range entries {
subTreePath := path.Join(treePath, e.Name())
fileContentResponse, err := GetContents(ctx, repo, subTreePath, origRef, true)
if err != nil {
return nil, err
}
fileList = append(fileList, fileContentResponse)
}
return fileList, nil
}
func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref string, forList bool) (*hat_api.ContentsResponse, error) {
if ref == "" {
ref = repo.DefaultBranch
}
origRef := ref
cleanTreePath := gitea_files_service.CleanUploadFileName(treePath)
if cleanTreePath == "" && treePath != "" {
return nil, models.ErrFilenameInvalid{
Path: treePath,
}
}
treePath = cleanTreePath
gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath())
if err != nil {
return nil, err
}
defer closer.Close()
commit, err := gitRepo.GetCommit(ref)
if err != nil {
return nil, err
}
commitID := commit.ID.String()
if len(ref) >= 4 && strings.HasPrefix(commitID, ref) {
ref = commit.ID.String()
}
entry, err := commit.GetTreeEntryByPath(treePath)
if err != nil {
return nil, err
}
refType := gitRepo.GetRefType(ref)
if refType == "invalid" {
return nil, fmt.Errorf("no commit found for the ref [ref %s]", ref)
}
selfURL, err := url.Parse(fmt.Sprintf("%s/contents/%s?ref=%s", repo.APIURL(), treePath, origRef))
if err != nil {
return nil, err
}
selfURLString := selfURL.String()
err = gitRepo.AddLastCommitCache(repo.GetCommitsCountCacheKey(ref, refType != git.ObjectCommit), repo.FullName(), commitID)
if err != nil {
return nil, err
}
lastCommit, err := commit.GetCommitByPath(treePath)
if err != nil {
return nil, err
}
contentsResponse := &hat_api.ContentsResponse{
ContentsResponse: &gitea_api.ContentsResponse{
Name: entry.Name(),
Path: treePath,
SHA: entry.ID.String(),
LastCommitSHA: lastCommit.ID.String(),
Size: entry.Size(),
URL: &selfURLString,
Links: &gitea_api.FileLinksResponse{
Self: &selfURLString,
},
},
LatestCommit: hat_api.ContentsResponseCommit{
Message: lastCommit.CommitMessage,
Sha: lastCommit.ID.String(),
CreatedAt: lastCommit.Author.When.Unix(),
},
}
// Now populate the rest of the ContentsResponse based on entry type
if entry.IsRegular() || entry.IsExecutable() {
contentsResponse.Type = string(gitea_files_service.ContentTypeRegular)
if blobResponse, err := gitea_files_service.GetBlobBySHA(ctx, repo, gitRepo, entry.ID.String()); err != nil {
return nil, err
} else if !forList {
// We don't show the content if we are getting a list of FileContentResponses
contentsResponse.Encoding = &blobResponse.Encoding
contentsResponse.Content = &blobResponse.Content
}
} else if entry.IsDir() {
contentsResponse.Type = string(gitea_files_service.ContentTypeDir)
} else if entry.IsLink() {
contentsResponse.Type = string(gitea_files_service.ContentTypeLink)
// The target of a symlink file is the content of the file
targetFromContent, err := entry.Blob().GetBlobContent()
if err != nil {
return nil, err
}
contentsResponse.Target = &targetFromContent
} else if entry.IsSubModule() {
contentsResponse.Type = string(gitea_files_service.ContentTypeSubmodule)
submodule, err := commit.GetSubModule(treePath)
if err != nil {
return nil, err
}
contentsResponse.SubmoduleGitURL = &submodule.URL
}
// Handle links
if entry.IsRegular() || entry.IsLink() {
downloadURL, err := url.Parse(fmt.Sprintf("%s/raw/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
if err != nil {
return nil, err
}
downloadURLString := downloadURL.String()
contentsResponse.DownloadURL = &downloadURLString
}
if !entry.IsSubModule() {
htmlURL, err := url.Parse(fmt.Sprintf("%s/src/%s/%s/%s", repo.HTMLURL(), refType, ref, treePath))
if err != nil {
return nil, err
}
htmlURLString := htmlURL.String()
contentsResponse.HTMLURL = &htmlURLString
contentsResponse.Links.HTMLURL = &htmlURLString
gitURL, err := url.Parse(fmt.Sprintf("%s/git/blobs/%s", repo.APIURL(), entry.ID.String()))
if err != nil {
return nil, err
}
gitURLString := gitURL.String()
contentsResponse.GitURL = &gitURLString
contentsResponse.Links.GitURL = &gitURLString
}
return contentsResponse, nil
}