gitea源码

repo_base_nogogit.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. //go:build !gogit
  5. package git
  6. import (
  7. "bufio"
  8. "context"
  9. "path/filepath"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. const isGogit = false
  14. // Repository represents a Git repository.
  15. type Repository struct {
  16. Path string
  17. tagCache *ObjectCache[*Tag]
  18. gpgSettings *GPGSettings
  19. batchInUse bool
  20. batch *Batch
  21. checkInUse bool
  22. check *Batch
  23. Ctx context.Context
  24. LastCommitCache *LastCommitCache
  25. objectFormat ObjectFormat
  26. }
  27. // OpenRepository opens the repository at the given path with the provided context.
  28. func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
  29. repoPath, err := filepath.Abs(repoPath)
  30. if err != nil {
  31. return nil, err
  32. }
  33. exist, err := util.IsDir(repoPath)
  34. if err != nil {
  35. return nil, err
  36. }
  37. if !exist {
  38. return nil, util.NewNotExistErrorf("no such file or directory")
  39. }
  40. return &Repository{
  41. Path: repoPath,
  42. tagCache: newObjectCache[*Tag](),
  43. Ctx: ctx,
  44. }, nil
  45. }
  46. // CatFileBatch obtains a CatFileBatch for this repository
  47. func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
  48. if repo.batch == nil {
  49. var err error
  50. repo.batch, err = NewBatch(ctx, repo.Path)
  51. if err != nil {
  52. return nil, nil, nil, err
  53. }
  54. }
  55. if !repo.batchInUse {
  56. repo.batchInUse = true
  57. return repo.batch.Writer, repo.batch.Reader, func() {
  58. repo.batchInUse = false
  59. }, nil
  60. }
  61. log.Debug("Opening temporary cat file batch for: %s", repo.Path)
  62. tempBatch, err := NewBatch(ctx, repo.Path)
  63. if err != nil {
  64. return nil, nil, nil, err
  65. }
  66. return tempBatch.Writer, tempBatch.Reader, tempBatch.Close, nil
  67. }
  68. // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
  69. func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func(), error) {
  70. if repo.check == nil {
  71. var err error
  72. repo.check, err = NewBatchCheck(ctx, repo.Path)
  73. if err != nil {
  74. return nil, nil, nil, err
  75. }
  76. }
  77. if !repo.checkInUse {
  78. repo.checkInUse = true
  79. return repo.check.Writer, repo.check.Reader, func() {
  80. repo.checkInUse = false
  81. }, nil
  82. }
  83. log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
  84. tempBatchCheck, err := NewBatchCheck(ctx, repo.Path)
  85. if err != nil {
  86. return nil, nil, nil, err
  87. }
  88. return tempBatchCheck.Writer, tempBatchCheck.Reader, tempBatchCheck.Close, nil
  89. }
  90. func (repo *Repository) Close() error {
  91. if repo == nil {
  92. return nil
  93. }
  94. if repo.batch != nil {
  95. repo.batch.Close()
  96. repo.batch = nil
  97. repo.batchInUse = false
  98. }
  99. if repo.check != nil {
  100. repo.check.Close()
  101. repo.check = nil
  102. repo.checkInUse = false
  103. }
  104. repo.LastCommitCache = nil
  105. repo.tagCache = nil
  106. return nil
  107. }