From 6f0748a71cd950f95bab20c196080355b86c3f3f Mon Sep 17 00:00:00 2001 From: hang Date: Tue, 28 Dec 2021 15:00:29 +0800 Subject: [PATCH] add latest_commit for contents --- modules/repofiles/content.go | 32 ++++++++++++++++++++++++++++++-- modules/structs/repo_file.go | 12 ++++++++++-- routers/api/v1/repo/file.go | 6 +++--- templates/swagger/v1_json.tmpl | 24 ++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/modules/repofiles/content.go b/modules/repofiles/content.go index 838bfabdc..1ca95bfef 100644 --- a/modules/repofiles/content.go +++ b/modules/repofiles/content.go @@ -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 diff --git a/modules/structs/repo_file.go b/modules/structs/repo_file.go index 71733c90e..3b40224e9 100644 --- a/modules/structs/repo_file.go +++ b/modules/structs/repo_file.go @@ -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. diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index bc3289dea..a57ab4dc9 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -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 diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 5d0efc88c..335cd8924 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -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" }