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

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