gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package renderhelper
  4. import (
  5. "context"
  6. "io"
  7. "code.gitea.io/gitea/modules/git"
  8. "code.gitea.io/gitea/modules/gitrepo"
  9. "code.gitea.io/gitea/modules/log"
  10. )
  11. type commitChecker struct {
  12. ctx context.Context
  13. commitCache map[string]bool
  14. gitRepoFacade gitrepo.Repository
  15. gitRepo *git.Repository
  16. gitRepoCloser io.Closer
  17. }
  18. func newCommitChecker(ctx context.Context, gitRepo gitrepo.Repository) *commitChecker {
  19. return &commitChecker{ctx: ctx, commitCache: make(map[string]bool), gitRepoFacade: gitRepo}
  20. }
  21. func (c *commitChecker) Close() error {
  22. if c != nil && c.gitRepoCloser != nil {
  23. return c.gitRepoCloser.Close()
  24. }
  25. return nil
  26. }
  27. func (c *commitChecker) IsCommitIDExisting(commitID string) bool {
  28. exist, inCache := c.commitCache[commitID]
  29. if inCache {
  30. return exist
  31. }
  32. if c.gitRepo == nil {
  33. r, closer, err := gitrepo.RepositoryFromContextOrOpen(c.ctx, c.gitRepoFacade)
  34. if err != nil {
  35. log.Error("unable to open repository: %s Error: %v", gitrepo.RepoGitURL(c.gitRepoFacade), err)
  36. return false
  37. }
  38. c.gitRepo, c.gitRepoCloser = r, closer
  39. }
  40. exist = c.gitRepo.IsReferenceExist(commitID) // Don't use IsObjectExist since it doesn't support short hashes with gogit edition.
  41. c.commitCache[commitID] = exist
  42. return exist
  43. }