You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

repo_base_nogogit.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. func init() {
  14. isGogit = false
  15. }
  16. // Repository represents a Git repository.
  17. type Repository struct {
  18. Path string
  19. tagCache *ObjectCache
  20. gpgSettings *GPGSettings
  21. batchInUse bool
  22. batchCancel context.CancelFunc
  23. batchReader *bufio.Reader
  24. batchWriter WriteCloserError
  25. checkInUse bool
  26. checkCancel context.CancelFunc
  27. checkReader *bufio.Reader
  28. checkWriter WriteCloserError
  29. Ctx context.Context
  30. LastCommitCache *LastCommitCache
  31. objectFormat ObjectFormat
  32. }
  33. // openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
  34. func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
  35. return OpenRepository(DefaultContext, repoPath)
  36. }
  37. // OpenRepository opens the repository at the given path with the provided context.
  38. func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
  39. repoPath, err := filepath.Abs(repoPath)
  40. if err != nil {
  41. return nil, err
  42. } else if !isDir(repoPath) {
  43. return nil, util.NewNotExistErrorf("no such file or directory")
  44. }
  45. // Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
  46. if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
  47. return nil, err
  48. }
  49. repo := &Repository{
  50. Path: repoPath,
  51. tagCache: newObjectCache(),
  52. Ctx: ctx,
  53. }
  54. repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
  55. repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repoPath)
  56. return repo, nil
  57. }
  58. // CatFileBatch obtains a CatFileBatch for this repository
  59. func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
  60. if repo.batchCancel == nil || repo.batchInUse {
  61. log.Debug("Opening temporary cat file batch for: %s", repo.Path)
  62. return CatFileBatch(ctx, repo.Path)
  63. }
  64. repo.batchInUse = true
  65. return repo.batchWriter, repo.batchReader, func() {
  66. repo.batchInUse = false
  67. }
  68. }
  69. // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
  70. func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
  71. if repo.checkCancel == nil || repo.checkInUse {
  72. log.Debug("Opening temporary cat file batch-check for: %s", repo.Path)
  73. return CatFileBatchCheck(ctx, repo.Path)
  74. }
  75. repo.checkInUse = true
  76. return repo.checkWriter, repo.checkReader, func() {
  77. repo.checkInUse = false
  78. }
  79. }
  80. func (repo *Repository) Close() error {
  81. if repo == nil {
  82. return nil
  83. }
  84. if repo.batchCancel != nil {
  85. repo.batchCancel()
  86. repo.batchReader = nil
  87. repo.batchWriter = nil
  88. repo.batchCancel = nil
  89. repo.batchInUse = false
  90. }
  91. if repo.checkCancel != nil {
  92. repo.checkCancel()
  93. repo.checkCancel = nil
  94. repo.checkReader = nil
  95. repo.checkWriter = nil
  96. repo.checkInUse = false
  97. }
  98. repo.LastCommitCache = nil
  99. repo.tagCache = nil
  100. return nil
  101. }