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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // +build !gogit
  7. package git
  8. import (
  9. "bufio"
  10. "context"
  11. "errors"
  12. "path/filepath"
  13. "code.gitea.io/gitea/modules/log"
  14. )
  15. // Repository represents a Git repository.
  16. type Repository struct {
  17. Path string
  18. tagCache *ObjectCache
  19. gpgSettings *GPGSettings
  20. batchCancel context.CancelFunc
  21. batchReader *bufio.Reader
  22. batchWriter WriteCloserError
  23. checkCancel context.CancelFunc
  24. checkReader *bufio.Reader
  25. checkWriter WriteCloserError
  26. }
  27. // OpenRepository opens the repository at the given path.
  28. func OpenRepository(repoPath string) (*Repository, error) {
  29. repoPath, err := filepath.Abs(repoPath)
  30. if err != nil {
  31. return nil, err
  32. } else if !isDir(repoPath) {
  33. return nil, errors.New("no such file or directory")
  34. }
  35. repo := &Repository{
  36. Path: repoPath,
  37. tagCache: newObjectCache(),
  38. }
  39. repo.batchWriter, repo.batchReader, repo.batchCancel = CatFileBatch(repoPath)
  40. repo.checkWriter, repo.checkReader, repo.checkCancel = CatFileBatchCheck(repo.Path)
  41. return repo, nil
  42. }
  43. // CatFileBatch obtains a CatFileBatch for this repository
  44. func (repo *Repository) CatFileBatch() (WriteCloserError, *bufio.Reader, func()) {
  45. if repo.batchCancel == nil || repo.batchReader.Buffered() > 0 {
  46. log.Debug("Opening temporary cat file batch for: %s", repo.Path)
  47. return CatFileBatch(repo.Path)
  48. }
  49. return repo.batchWriter, repo.batchReader, func() {}
  50. }
  51. // CatFileBatchCheck obtains a CatFileBatchCheck for this repository
  52. func (repo *Repository) CatFileBatchCheck() (WriteCloserError, *bufio.Reader, func()) {
  53. if repo.checkCancel == nil || repo.checkReader.Buffered() > 0 {
  54. log.Debug("Opening temporary cat file batch-check: %s", repo.Path)
  55. return CatFileBatchCheck(repo.Path)
  56. }
  57. return repo.checkWriter, repo.checkReader, func() {}
  58. }
  59. // Close this repository, in particular close the underlying gogitStorage if this is not nil
  60. func (repo *Repository) Close() {
  61. if repo == nil {
  62. return
  63. }
  64. if repo.batchCancel != nil {
  65. repo.batchCancel()
  66. repo.batchReader = nil
  67. repo.batchWriter = nil
  68. repo.batchCancel = nil
  69. }
  70. if repo.checkCancel != nil {
  71. repo.checkCancel()
  72. repo.checkCancel = nil
  73. repo.checkReader = nil
  74. repo.checkWriter = nil
  75. }
  76. }