summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorGusted <williamzijl7@hotmail.com>2022-07-30 10:09:04 +0200
committerGitHub <noreply@github.com>2022-07-30 16:09:04 +0800
commit692707f14519b677de4601e5f40469989f82eed6 (patch)
treec59906253034305e2b5be2c6e95bcd76e99a5e51 /modules
parent2b94b02f33d472c0ae76c398aebe527ba9490ada (diff)
downloadgitea-692707f14519b677de4601e5f40469989f82eed6.tar.gz
gitea-692707f14519b677de4601e5f40469989f82eed6.zip
Add latest commit's SHA to content response (#20398)
* 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 #12840 * Add swagger * Fix NPE * Fix tests * Hook into LastCommitCache * Move AddLastCommitCache to a common nogogit and gogit file Signed-off-by: Andrew Thornton <art27@cantab.net> * Prevent NPE Co-authored-by: Andrew Thornton <art27@cantab.net> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'modules')
-rw-r--r--modules/git/repo_commit.go18
-rw-r--r--modules/structs/repo_file.go7
2 files changed, 22 insertions, 3 deletions
diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go
index e6fec4d1a3..7ff23af42e 100644
--- a/modules/git/repo_commit.go
+++ b/modules/git/repo_commit.go
@@ -11,6 +11,7 @@ import (
"strconv"
"strings"
+ "code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/setting"
)
@@ -434,3 +435,20 @@ func (repo *Repository) IsCommitInBranch(commitID, branch string) (r bool, err e
}
return len(stdout) > 0, err
}
+
+func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error {
+ if repo.LastCommitCache == nil {
+ commitsCount, err := cache.GetInt64(cacheKey, func() (int64, error) {
+ commit, err := repo.GetCommit(sha)
+ if err != nil {
+ return 0, err
+ }
+ return commit.CommitsCount()
+ })
+ if err != nil {
+ return err
+ }
+ repo.LastCommitCache = NewLastCommitCache(commitsCount, fullName, repo, cache.GetCache())
+ }
+ return nil
+}
diff --git a/modules/structs/repo_file.go b/modules/structs/repo_file.go
index 135e6484cd..ce1a9fe4be 100644
--- a/modules/structs/repo_file.go
+++ b/modules/structs/repo_file.go
@@ -87,9 +87,10 @@ type FileLinksResponse struct {
// ContentsResponse contains information about a repo's entry's (dir, file, symlink, submodule) metadata and content
type ContentsResponse struct {
- Name string `json:"name"`
- Path string `json:"path"`
- SHA string `json:"sha"`
+ Name string `json:"name"`
+ Path string `json:"path"`
+ SHA string `json:"sha"`
+ LastCommitSHA string `json:"last_commit_sha"`
// `type` will be `file`, `dir`, `symlink`, or `submodule`
Type string `json:"type"`
Size int64 `json:"size"`