Skip to content

Commit 2fd356b

Browse files
author
Gusted
committed
Add latest commit's SHA to content response
- When requesting the contents of a filepath, add the latest commit's SHA to the requested file. - Resolves go-gitea#12840
1 parent 17ce5f8 commit 2fd356b

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

modules/structs/repo_file.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ type FileLinksResponse struct {
8787

8888
// ContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content
8989
type ContentsResponse struct {
90-
Name string `json:"name"`
91-
Path string `json:"path"`
92-
SHA string `json:"sha"`
90+
Name string `json:"name"`
91+
Path string `json:"path"`
92+
SHA string `json:"sha"`
93+
LastCommitSHA string `json:"last_commit_sha"`
9394
// `type` will be `file`, `dir`, `symlink`, or `submodule`
9495
Type string `json:"type"`
9596
Size int64 `json:"size"`

services/repository/files/content.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,19 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref
149149
}
150150
selfURLString := selfURL.String()
151151

152+
lastCommit, err := gitRepo.GetCommitByPath(treePath)
153+
if err != nil {
154+
return nil, err
155+
}
156+
152157
// All content types have these fields in populated
153158
contentsResponse := &api.ContentsResponse{
154-
Name: entry.Name(),
155-
Path: treePath,
156-
SHA: entry.ID.String(),
157-
Size: entry.Size(),
158-
URL: &selfURLString,
159+
Name: entry.Name(),
160+
Path: treePath,
161+
SHA: entry.ID.String(),
162+
LastCommitSHA: lastCommit.ID.String(),
163+
Size: entry.Size(),
164+
URL: &selfURLString,
159165
Links: &api.FileLinksResponse{
160166
Self: &selfURLString,
161167
},

services/repository/files/content_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,18 @@ func getExpectedReadmeContentsResponse() *api.ContentsResponse {
3333
gitURL := "https://try.gitea.io/api/v1/repos/user2/repo1/git/blobs/" + sha
3434
downloadURL := "https://try.gitea.io/user2/repo1/raw/branch/master/" + treePath
3535
return &api.ContentsResponse{
36-
Name: treePath,
37-
Path: treePath,
38-
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
39-
Type: "file",
40-
Size: 30,
41-
Encoding: &encoding,
42-
Content: &content,
43-
URL: &selfURL,
44-
HTMLURL: &htmlURL,
45-
GitURL: &gitURL,
46-
DownloadURL: &downloadURL,
36+
Name: treePath,
37+
Path: treePath,
38+
SHA: "4b4851ad51df6a7d9f25c979345979eaeb5b349f",
39+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
40+
Type: "file",
41+
Size: 30,
42+
Encoding: &encoding,
43+
Content: &content,
44+
URL: &selfURL,
45+
HTMLURL: &htmlURL,
46+
GitURL: &gitURL,
47+
DownloadURL: &downloadURL,
4748
Links: &api.FileLinksResponse{
4849
Self: &selfURL,
4950
GitURL: &gitURL,

services/repository/files/file_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ func getExpectedFileResponse() *api.FileResponse {
4343
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
4444
return &api.FileResponse{
4545
Content: &api.ContentsResponse{
46-
Name: treePath,
47-
Path: treePath,
48-
SHA: sha,
49-
Type: "file",
50-
Size: 30,
51-
Encoding: &encoding,
52-
Content: &content,
53-
URL: &selfURL,
54-
HTMLURL: &htmlURL,
55-
GitURL: &gitURL,
56-
DownloadURL: &downloadURL,
46+
Name: treePath,
47+
Path: treePath,
48+
SHA: sha,
49+
LastCommitSHA: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
50+
Type: "file",
51+
Size: 30,
52+
Encoding: &encoding,
53+
Content: &content,
54+
URL: &selfURL,
55+
HTMLURL: &htmlURL,
56+
GitURL: &gitURL,
57+
DownloadURL: &downloadURL,
5758
Links: &api.FileLinksResponse{
5859
Self: &selfURL,
5960
GitURL: &gitURL,

0 commit comments

Comments
 (0)