Merge pull request '给content 添加latest_commit 字段' (#40) from wonderful/gitea-1156:develop into develop

This commit is contained in:
yystopf 2021-12-28 15:04:28 +08:00
commit 5384e2fdd1
4 changed files with 67 additions and 7 deletions

View File

@ -5,6 +5,7 @@
package repofiles
import (
"context"
"fmt"
"net/url"
"path"
@ -12,6 +13,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
)
@ -37,7 +39,7 @@ func (ct *ContentType) String() string {
// GetContentsOrList gets the meta data of a file's contents (*ContentsResponse) if treePath not a tree
// directory, otherwise a listing of file contents ([]*ContentsResponse). Ref can be a branch, commit or tag
func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface{}, error) {
func GetContentsOrList(ctx context.Context, repo *models.Repository, treePath, ref string) (interface{}, error) {
if repo.IsEmpty {
return make([]interface{}, 0), nil
}
@ -87,12 +89,38 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
if err != nil {
return nil, err
}
//add at 2021-12-27
commitsInfo, _, err := entries.GetCommitsInfo(ctx, commit, treePath, nil)
if err != nil {
return nil, err
}
// end
for _, e := range entries {
subTreePath := path.Join(treePath, e.Name())
name := e.Blob().Name()
log.Info(" entrie name = %s", name)
subTreePath := path.Join(treePath, name)
fileContentResponse, err := GetContents(repo, subTreePath, origRef, true)
for _, commitInfo := range commitsInfo {
if commitInfo.Entry.Name() == fileContentResponse.Name {
var entryCommit *git.Commit
entryCommit = commitInfo.Commit
if e.IsSubModule() {
entryCommit = commitInfo.SubModuleFile.Commit
}
fileContentResponse.LatestCommit = api.ContentsResponseCommit{
Message: entryCommit.CommitMessage,
LatestCommitSha: entryCommit.ID.String(),
Created: entryCommit.Author.When.Unix(),
}
break
}
}
if err != nil {
return nil, err
}
fileList = append(fileList, fileContentResponse)
}
return fileList, nil

View File

@ -57,6 +57,13 @@ type FileLinksResponse struct {
HTMLURL *string `json:"html"`
}
//add
type ContentsResponseCommit struct {
Message string `json:"message"`
LatestCommitSha string `json:"sha"`
Created int64 `json:"created_at"`
}
// ContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content
type ContentsResponse struct {
Name string `json:"name"`
@ -76,8 +83,9 @@ type ContentsResponse struct {
GitURL *string `json:"git_url"`
DownloadURL *string `json:"download_url"`
// `submodule_git_url` is populated when `type` is `submodule`, otherwise null
SubmoduleGitURL *string `json:"submodule_git_url"`
Links *FileLinksResponse `json:"_links"`
SubmoduleGitURL *string `json:"submodule_git_url"`
Links *FileLinksResponse `json:"_links"`
LatestCommit ContentsResponseCommit `json:"latest_commit"`
}
// FileCommitResponse contains information generated from a Git commit for a repo's file.

View File

@ -557,7 +557,7 @@ func GetContents(ctx *context.APIContext) {
treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref")
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
if fileList, err := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
@ -641,7 +641,7 @@ func GetReadmeContents(ctx *context.APIContext) {
// treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref")
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, "README.md", ref); err != nil {
if fileList, err := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, "README.md", ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return
@ -696,7 +696,7 @@ func GetReadmeContentsByPath(ctx *context.APIContext) {
treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref")
newTreePath := treePath + "/" + "README.md"
if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, newTreePath, ref); err != nil {
if fileList, err := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil {
if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err)
return

View File

@ -13458,6 +13458,9 @@
"type": "string",
"x-go-name": "HTMLURL"
},
"latest_commit": {
"$ref": "#/definitions/ContentsResponseCommit"
},
"name": {
"type": "string",
"x-go-name": "Name"
@ -13497,6 +13500,26 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ContentsResponseCommit": {
"description": "add",
"type": "object",
"properties": {
"created_at": {
"type": "integer",
"format": "int64",
"x-go-name": "Created"
},
"message": {
"type": "string",
"x-go-name": "Message"
},
"sha": {
"type": "string",
"x-go-name": "LatestCommitSha"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ContributorsDto": {
"type": "object",
"properties": {
@ -14878,6 +14901,7 @@
"x-go-name": "Visibility"
},
"website": {
"description": "Website string `json:\"website\" binding:\"ValidUrl;MaxSize(255)\"`",
"type": "string",
"x-go-name": "Website"
}