mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-30 19:08:37 +00:00 
			
		
		
		
	Slightly simplify LastCommitCache (#20444)
The LastCommitCache code is a little complex and there is unnecessary duplication between the gogit and nogogit variants. This PR adds the LastCommitCache as a field to the git.Repository and pre-creates it in the ReferencesGit helpers etc. There has been some simplification and unification of the variant code. Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
		| @@ -8,8 +8,10 @@ import ( | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
|  | ||||
| 	"code.gitea.io/gitea/modules/cache" | ||||
| 	"code.gitea.io/gitea/modules/context" | ||||
| 	"code.gitea.io/gitea/modules/git" | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
| ) | ||||
|  | ||||
| // ResolveRefOrSha resolve ref to sha if exist | ||||
| @@ -19,6 +21,7 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string { | ||||
| 		return "" | ||||
| 	} | ||||
|  | ||||
| 	sha := ref | ||||
| 	// Search branches and tags | ||||
| 	for _, refType := range []string{"heads", "tags"} { | ||||
| 		refSHA, lastMethodName, err := searchRefCommitByType(ctx, refType, ref) | ||||
| @@ -27,10 +30,27 @@ func ResolveRefOrSha(ctx *context.APIContext, ref string) string { | ||||
| 			return "" | ||||
| 		} | ||||
| 		if refSHA != "" { | ||||
| 			return refSHA | ||||
| 			sha = refSHA | ||||
| 			break | ||||
| 		} | ||||
| 	} | ||||
| 	return ref | ||||
|  | ||||
| 	if ctx.Repo.GitRepo != nil && ctx.Repo.GitRepo.LastCommitCache == nil { | ||||
| 		commitsCount, err := cache.GetInt64(ctx.Repo.Repository.GetCommitsCountCacheKey(ref, true), func() (int64, error) { | ||||
| 			commit, err := ctx.Repo.GitRepo.GetCommit(sha) | ||||
| 			if err != nil { | ||||
| 				return 0, err | ||||
| 			} | ||||
| 			return commit.CommitsCount() | ||||
| 		}) | ||||
| 		if err != nil { | ||||
| 			log.Error("Unable to get commits count for %s in %s. Error: %v", sha, ctx.Repo.Repository.FullName(), err) | ||||
| 			return sha | ||||
| 		} | ||||
| 		ctx.Repo.GitRepo.LastCommitCache = git.NewLastCommitCache(commitsCount, ctx.Repo.Repository.FullName(), ctx.Repo.GitRepo, cache.GetCache()) | ||||
| 	} | ||||
|  | ||||
| 	return sha | ||||
| } | ||||
|  | ||||
| // GetGitRefs return git references based on filter | ||||
|   | ||||
		Reference in New Issue
	
	Block a user