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.0KB

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