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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. //go:build !gogit
  6. package git
  7. import (
  8. "bufio"
  9. "context"
  10. "errors"
  11. "path/filepath"
  12. "code.gitea.io/gitea/modules/log"
  13. )
  14. // Repository represents a Git repository.
  15. type Repository struct {
  16. Path string
  17. tagCache *ObjectCache
  18. gpgSettings *GPGSettings
  19. batchCancel context.CancelFunc
  20. batchReader *bufio.Reader
  21. batchWriter WriteCloserError
  22. checkCancel context.CancelFunc
  23. checkReader *bufio.Reader
  24. checkWriter WriteCloserError
  25. Ctx context.Context
  26. LastCommitCache *LastCommitCache
  27. }
  28. // openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
  29. func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
  30. return OpenRepository(DefaultContext, repoPath)
  31. }
  32. // OpenRepository opens the repository at the given path with the provided context.
  33. func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
  34. repoPath, err := filepath.Abs(repoPath)
  35. if err != nil {
  36. return nil, err
  37. } else if !isDir(repoPath) {
  38. return nil, errors.New("no such file or directory")
  39. }
  40. // 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!
  41. if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
  42. return nil, err
  43. }
  44. repo := &Repository{
  45. Path: repoPath,
  46. tagCache: newObjectCache(),
  47. Ctx: ctx,
  48. }
  49. repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
  50. repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)
  51. return repo, nil
  52. }
  53. // CatFileBatch obtains a CatFileBatch for this repository
  54. func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
  55. if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
  56. log.Debug("Opening temporary cat file batch for: %s", repo.Path)
  57. return CatFileBatch(ctx, repo.Path)
  58. }
  59. return repo.batchWriter, repo.batchReader, func() {}
  60. }
  61. // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
  62. func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
  63. if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
  64. log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
  65. return CatFileBatchCheck(ctx, repo.Path)
  66. }
  67. return repo.checkWriter, repo.checkReader, func() {}
  68. }
  69. // Close this repository, in particular close the underlying gogitStorage if this is not nil
  70. func (repo *Repository) Close() (err error) {
  71. if repo == nil {
  72. return
  73. }
  74. if repo.batchCancel != nil {
  75. repo.batchCancel()
  76. repo.batchReader = nil
  77. repo.batchWriter = nil
  78. repo.batchCancel = nil
  79. }
  80. if repo.checkCancel != nil {
  81. repo.checkCancel()
  82. repo.checkCancel = nil
  83. repo.checkReader = nil
  84. repo.checkWriter = nil
  85. }
  86. repo.LastCommitCache = nil
  87. repo.tagCache = nil
  88. return err
  89. }