2020-10-09 03:47:23 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-10-09 03:47:23 +05:30
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-06-07 05:14:58 +05:30
|
|
|
"context"
|
2020-10-09 03:47:23 +05:30
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-10-09 03:47:23 +05:30
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CacheRef cachhe last commit information of the branch or the tag
|
2023-05-26 06:34:48 +05:30
|
|
|
func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName git.RefName) error {
|
2020-10-09 03:47:23 +05:30
|
|
|
if !setting.CacheService.LastCommit.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-26 06:34:48 +05:30
|
|
|
commit, err := gitRepo.GetCommit(fullRefName.String())
|
2020-10-09 03:47:23 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-25 21:09:42 +05:30
|
|
|
if gitRepo.LastCommitCache == nil {
|
2023-05-26 06:34:48 +05:30
|
|
|
commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(fullRefName.ShortName(), true), commit.CommitsCount)
|
2022-07-25 21:09:42 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache())
|
2020-10-09 03:47:23 +05:30
|
|
|
}
|
|
|
|
|
2022-07-25 21:09:42 +05:30
|
|
|
return commit.CacheCommit(ctx)
|
2020-10-09 03:47:23 +05:30
|
|
|
}
|