Skip to content

WIP: Add commit sha to filepath api #13599

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integrations/api_repo_file_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions integrations/api_repo_file_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions integrations/api_repo_get_contents_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions integrations/api_repo_get_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions integrations/repofiles_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func getExpectedFileResponseForRepofilesCreate(commitID string) *api.FileRespons
SHA: "103ff9234cefeee5ec5361d22b49fbb04d385885",
Type: "file",
Size: 18,
Commit: commitID,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 11 additions & 5 deletions modules/repofiles/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,19 @@ func GetContents(repo *models.Repository, treePath, ref string, forList bool) (*
}
selfURLString := selfURL.String()

lastCommit, err := commit.GetCommitByPath(treePath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be quite slow depending on the version of git present and the size of the repository.

Internally this code may need to be tied in to the last commit cache.

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,
},
Expand Down
2 changes: 2 additions & 0 deletions modules/repofiles/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -34,6 +35,7 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
Type: "file",
Size: 30,
Commit: commit,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
Expand Down
2 changes: 2 additions & 0 deletions modules/repofiles/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,6 +33,7 @@ func getExpectedFileResponse() *api.FileResponse {
SHA: sha,
Type: "file",
Size: 30,
Commit: commit,
Encoding: &encoding,
Content: &content,
URL: &selfURL,
Expand Down
5 changes: 4 additions & 1 deletion modules/structs/repo_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -11574,6 +11579,7 @@
"x-go-name": "Path"
},
"sha": {
"description": "File SHA",
"type": "string",
"x-go-name": "SHA"
},
Expand Down