2020-10-08 15:17:23 -07:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2021-06-06 16:44:58 -07:00
|
|
|
"context"
|
2020-10-08 15:17:23 -07:00
|
|
|
"strings"
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2020-10-08 15:17:23 -07:00
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getRefName(fullRefName string) string {
|
|
|
|
if strings.HasPrefix(fullRefName, git.TagPrefix) {
|
|
|
|
return fullRefName[len(git.TagPrefix):]
|
|
|
|
} else if strings.HasPrefix(fullRefName, git.BranchPrefix) {
|
|
|
|
return fullRefName[len(git.BranchPrefix):]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheRef cachhe last commit information of the branch or the tag
|
2021-12-09 18:27:50 -07:00
|
|
|
func CacheRef(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, fullRefName string) error {
|
2020-10-08 15:17:23 -07:00
|
|
|
if !setting.CacheService.LastCommit.Enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := gitRepo.GetCommit(fullRefName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-25 08:39:42 -07:00
|
|
|
if gitRepo.LastCommitCache == nil {
|
|
|
|
commitsCount, err := cache.GetInt64(repo.GetCommitsCountCacheKey(getRefName(fullRefName), true), commit.CommitsCount)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, repo.FullName(), gitRepo, cache.GetCache())
|
2020-10-08 15:17:23 -07:00
|
|
|
}
|
|
|
|
|
2022-07-25 08:39:42 -07:00
|
|
|
return commit.CacheCommit(ctx)
|
2020-10-08 15:17:23 -07:00
|
|
|
}
|