diff --git a/integrations/api_repo_file_create_test.go b/integrations/api_repo_file_create_test.go index 853224f09199b..bb28c29e5d4e0 100644 --- a/integrations/api_repo_file_create_test.go +++ b/integrations/api_repo_file_create_test.go @@ -62,6 +62,7 @@ func getExpectedFileResponseForCreate(commitID, treePath string) *api.FileRespon SHA: sha, Size: 16, Type: "file", + Commit: commitID, Encoding: &encoding, Content: &content, URL: &selfURL, diff --git a/integrations/api_repo_file_update_test.go b/integrations/api_repo_file_update_test.go index 69a94637747f8..6ba497b652e5a 100644 --- a/integrations/api_repo_file_update_test.go +++ b/integrations/api_repo_file_update_test.go @@ -60,6 +60,7 @@ func getExpectedFileResponseForUpdate(commitID, treePath string) *api.FileRespon SHA: sha, Type: "file", Size: 20, + Commit: commitID, Encoding: &encoding, Content: &content, URL: &selfURL, diff --git a/integrations/api_repo_get_contents_list_test.go b/integrations/api_repo_get_contents_list_test.go index ddbd877a40e80..0deb20b2faebf 100644 --- a/integrations/api_repo_get_contents_list_test.go +++ b/integrations/api_repo_get_contents_list_test.go @@ -33,6 +33,7 @@ func getExpectedContentsListResponseForContents(ref, refType string) []*api.Cont SHA: sha, Type: "file", Size: 30, + Commit: "65f1bf27bc3bf70f64657658635e66094edbcb4d", URL: &selfURL, HTMLURL: &htmlURL, GitURL: &gitURL, diff --git a/integrations/api_repo_get_contents_test.go b/integrations/api_repo_get_contents_test.go index 3d4a31be81140..1d39ec94a733d 100644 --- a/integrations/api_repo_get_contents_test.go +++ b/integrations/api_repo_get_contents_test.go @@ -27,12 +27,14 @@ func getExpectedContentsResponseForContents(ref, refType string) *api.ContentsRe htmlURL := setting.AppURL + "user2/repo1/src/" + refType + "/" + ref + "/" + treePath gitURL := setting.AppURL + "api/v1/repos/user2/repo1/git/blobs/" + sha downloadURL := setting.AppURL + "user2/repo1/raw/" + refType + "/" + ref + "/" + treePath + commit := "65f1bf27bc3bf70f64657658635e66094edbcb4d" return &api.ContentsResponse{ Name: treePath, Path: treePath, SHA: sha, Type: "file", Size: 30, + Commit: commit, Encoding: &encoding, Content: &content, URL: &selfURL, diff --git a/integrations/repofiles_update_test.go b/integrations/repofiles_update_test.go index 9e99b36ae4226..2f63eba13351b 100644 --- a/integrations/repofiles_update_test.go +++ b/integrations/repofiles_update_test.go @@ -62,6 +62,7 @@ func getExpectedFileResponseForRepofilesCreate(commitID string) *api.FileRespons SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885", Type: "file", Size: 18, + Commit: commitID, Encoding: &encoding, Content: &content, URL: &selfURL, @@ -129,6 +130,7 @@ func getExpectedFileResponseForRepofilesUpdate(commitID, filename string) *api.F SHA: "dbf8d00e022e05b7e5cf7e535de857de57925647", Type: "file", Size: 43, + Commit: commitID, Encoding: &encoding, Content: &content, URL: &selfURL, diff --git a/modules/repofiles/content.go b/modules/repofiles/content.go index 838bfabdc6b88..67dec67788757 100644 --- a/modules/repofiles/content.go +++ b/modules/repofiles/content.go @@ -146,13 +146,19 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (* } selfURLString := selfURL.String() + lastCommit, err := commit.GetCommitByPath(treePath) + if err != nil { + return nil, err + } + // All content types have these fields in populated contentsResponse := &api.ContentsResponse{ - Name: entry.Name(), - Path: treePath, - SHA: entry.ID.String(), - Size: entry.Size(), - URL: &selfURLString, + Name: entry.Name(), + Path: treePath, + SHA: entry.ID.String(), + Size: entry.Size(), + URL: &selfURLString, + Commit: lastCommit.ID.String(), Links: &api.FileLinksResponse{ Self: &selfURLString, }, diff --git a/modules/repofiles/content_test.go b/modules/repofiles/content_test.go index 278216112296c..aa78d4a3a25d8 100644 --- a/modules/repofiles/content_test.go +++ b/modules/repofiles/content_test.go @@ -22,6 +22,7 @@ func TestMain(m *testing.M) { func getExpectedReadmeContentsResponse() *api.ContentsResponse { treePath := "README.md" sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f" + commit := "65f1bf27bc3bf70f64657658635e66094edbcb4d" encoding := "base64" content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x" selfURL := "https://try.gitea.io/api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master" @@ -34,6 +35,7 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse { SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f", Type: "file", Size: 30, + Commit: commit, Encoding: &encoding, Content: &content, URL: &selfURL, diff --git a/modules/repofiles/file_test.go b/modules/repofiles/file_test.go index aafe816a166a8..6672cb5232d4b 100644 --- a/modules/repofiles/file_test.go +++ b/modules/repofiles/file_test.go @@ -19,6 +19,7 @@ import ( func getExpectedFileResponse() *api.FileResponse { treePath := "README.md" sha := "4b4851ad51df6a7d9f25c979345979eaeb5b349f" + commit := "65f1bf27bc3bf70f64657658635e66094edbcb4d" encoding := "base64" content := "IyByZXBvMQoKRGVzY3JpcHRpb24gZm9yIHJlcG8x" selfURL := setting.AppURL + "api/v1/repos/user2/repo1/contents/" + treePath + "?ref=master" @@ -32,6 +33,7 @@ func getExpectedFileResponse() *api.FileResponse { SHA: sha, Type: "file", Size: 30, + Commit: commit, Encoding: &encoding, Content: &content, URL: &selfURL, diff --git a/modules/structs/repo_file.go b/modules/structs/repo_file.go index c34923e389dfc..5e471b80c6278 100644 --- a/modules/structs/repo_file.go +++ b/modules/structs/repo_file.go @@ -59,10 +59,13 @@ type FileLinksResponse struct { type ContentsResponse struct { Name string `json:"name"` Path string `json:"path"` - SHA string `json:"sha"` + // File SHA + SHA string `json:"sha"` // `type` will be `file`, `dir`, `symlink`, or `submodule` Type string `json:"type"` Size int64 `json:"size"` + // Last commit touching the entry + Commit string `json:"commit"` // `encoding` is populated when `type` is `file`, otherwise null Encoding *string `json:"encoding"` // `content` is populated when `type` is `file`, otherwise null diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index e759a1558ce08..1d7016e8c6a4f 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -11543,6 +11543,11 @@ "_links": { "$ref": "#/definitions/FileLinksResponse" }, + "commit": { + "description": "Last commit touching the entry", + "type": "string", + "x-go-name": "Commit" + }, "content": { "description": "`content` is populated when `type` is `file`, otherwise null", "type": "string", @@ -11574,6 +11579,7 @@ "x-go-name": "Path" }, "sha": { + "description": "File SHA", "type": "string", "x-go-name": "SHA" },