add latest_commit for contents

This commit is contained in:
hang 2021-12-28 15:00:29 +08:00
parent d4eec1b75c
commit 6f0748a71c
4 changed files with 67 additions and 7 deletions

View File

@ -5,6 +5,7 @@
package repofiles package repofiles
import ( import (
"context"
"fmt" "fmt"
"net/url" "net/url"
"path" "path"
@ -12,6 +13,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs" 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 // 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 // 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 { if repo.IsEmpty {
return make([]interface{}, 0), nil return make([]interface{}, 0), nil
} }
@ -87,12 +89,38 @@ func GetContentsOrList(repo *models.Repository, treePath, ref string) (interface
if err != nil { if err != nil {
return nil, err 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 { 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) 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 { if err != nil {
return nil, err return nil, err
} }
fileList = append(fileList, fileContentResponse) fileList = append(fileList, fileContentResponse)
} }
return fileList, nil return fileList, nil

View File

@ -57,6 +57,13 @@ type FileLinksResponse struct {
HTMLURL *string `json:"html"` 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 // ContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content
type ContentsResponse struct { type ContentsResponse struct {
Name string `json:"name"` Name string `json:"name"`
@ -76,8 +83,9 @@ type ContentsResponse struct {
GitURL *string `json:"git_url"` GitURL *string `json:"git_url"`
DownloadURL *string `json:"download_url"` DownloadURL *string `json:"download_url"`
// `submodule_git_url` is populated when `type` is `submodule`, otherwise null // `submodule_git_url` is populated when `type` is `submodule`, otherwise null
SubmoduleGitURL *string `json:"submodule_git_url"` SubmoduleGitURL *string `json:"submodule_git_url"`
Links *FileLinksResponse `json:"_links"` Links *FileLinksResponse `json:"_links"`
LatestCommit ContentsResponseCommit `json:"latest_commit"`
} }
// FileCommitResponse contains information generated from a Git commit for a repo's file. // 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("*") treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref") 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) { if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err) ctx.NotFound("GetContentsOrList", err)
return return
@ -641,7 +641,7 @@ func GetReadmeContents(ctx *context.APIContext) {
// treePath := ctx.Params("*") // treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref") 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) { if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err) ctx.NotFound("GetContentsOrList", err)
return return
@ -696,7 +696,7 @@ func GetReadmeContentsByPath(ctx *context.APIContext) {
treePath := ctx.Params("*") treePath := ctx.Params("*")
ref := ctx.QueryTrim("ref") ref := ctx.QueryTrim("ref")
newTreePath := treePath + "/" + "README.md" 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) { if git.IsErrNotExist(err) {
ctx.NotFound("GetContentsOrList", err) ctx.NotFound("GetContentsOrList", err)
return return

View File

@ -13458,6 +13458,9 @@
"type": "string", "type": "string",
"x-go-name": "HTMLURL" "x-go-name": "HTMLURL"
}, },
"latest_commit": {
"$ref": "#/definitions/ContentsResponseCommit"
},
"name": { "name": {
"type": "string", "type": "string",
"x-go-name": "Name" "x-go-name": "Name"
@ -13497,6 +13500,26 @@
}, },
"x-go-package": "code.gitea.io/gitea/modules/structs" "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": { "ContributorsDto": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -14878,6 +14901,7 @@
"x-go-name": "Visibility" "x-go-name": "Visibility"
}, },
"website": { "website": {
"description": "Website string `json:\"website\" binding:\"ValidUrl;MaxSize(255)\"`",
"type": "string", "type": "string",
"x-go-name": "Website" "x-go-name": "Website"
} }