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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  27. // openRepositoryWithDefaultContext opens the repository at the given path with DefaultContext.
  28. func openRepositoryWithDefaultContext(repoPath string) (*Repository, error) {
  29. return OpenRepository(DefaultContext, repoPath)
  30. }
  31. // OpenRepository opens the repository at the given path with the provided context.
  32. func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
  33. repoPath, err := filepath.Abs(repoPath)
  34. if err != nil {
  35. return nil, err
  36. } else if !isDir(repoPath) {
  37. return nil, errors.New("no such file or directory")
  38. }
  39. // 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!
  40. if err := EnsureValidGitRepository(ctx, repoPath); err != nil {
  41. return nil, err
  42. }
  43. repo := &Repository{
  44. Path: repoPath,
  45. tagCache: newObjectCache(),
  46. Ctx: ctx,
  47. }
  48. repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(ctx, repoPath)
  49. repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(ctx, repo.Path)
  50. return repo, nil
  51. }
  52. // CatFileBatch obtains a CatFileBatch for this repository
  53. func (repo *Repository) CatFileBatch(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
  54. if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
  55. log.Debug("Opening temporary cat file batch for: %s", repo.Path)
  56. return CatFileBatch(ctx, repo.Path)
  57. }
  58. return repo.batchWriter, repo.batchReader, func() {}
  59. }
  60. // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
  61. func (repo *Repository) CatFileBatchCheck(ctx context.Context) (WriteCloserError, *bufio.Reader, func()) {
  62. if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
  63. log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
  64. return CatFileBatchCheck(ctx, repo.Path)
  65. }
  66. return repo.checkWriter, repo.checkReader, func() {}
  67. }
  68. // Close this repository, in particular close the underlying gogitStorage if this is not nil
  69. func (repo *Repository) Close() (err error) {
  70. if repo == nil {
  71. return
  72. }
  73. if repo.batchCancel != nil {
  74. repo.batchCancel()
  75. repo.batchReader = nil
  76. repo.batchWriter = nil
  77. repo.batchCancel = nil
  78. }
  79. if repo.checkCancel != nil {
  80. repo.checkCancel()
  81. repo.checkCancel = nil
  82. repo.checkReader = nil
  83. repo.checkWriter = nil
  84. }
  85. return
  86. }