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_gogit.go 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "context"
  9. "errors"
  10. "path/filepath"
  11. gitealog "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/go-git/go-billy/v5/osfs"
  14. gogit "github.com/go-git/go-git/v5"
  15. "github.com/go-git/go-git/v5/plumbing/cache"
  16. "github.com/go-git/go-git/v5/storage/filesystem"
  17. )
  18. // Repository represents a Git repository.
  19. type Repository struct {
  20. Path string
  21. tagCache *ObjectCache
  22. gogitRepo *gogit.Repository
  23. gogitStorage *filesystem.Storage
  24. gpgSettings *GPGSettings
  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 within the context.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. fs := osfs.New(repoPath)
  40. _, err = fs.Stat(".git")
  41. if err == nil {
  42. fs, err = fs.Chroot(".git")
  43. if err != nil {
  44. return nil, err
  45. }
  46. }
  47. storage := filesystem.NewStorageWithOptions(fs, cache.NewObjectLRUDefault(), filesystem.Options{KeepDescriptors: true, LargeObjectThreshold: setting.Git.LargeObjectThreshold})
  48. gogitRepo, err := gogit.Open(storage, fs)
  49. if err != nil {
  50. return nil, err
  51. }
  52. return &Repository{
  53. Path: repoPath,
  54. gogitRepo: gogitRepo,
  55. gogitStorage: storage,
  56. tagCache: newObjectCache(),
  57. Ctx: ctx,
  58. }, nil
  59. }
  60. // Close this repository, in particular close the underlying gogitStorage if this is not nil
  61. func (repo *Repository) Close() (err error) {
  62. if repo == nil || repo.gogitStorage == nil {
  63. return
  64. }
  65. if err := repo.gogitStorage.Close(); err != nil {
  66. gitealog.Error("Error closing storage: %v", err)
  67. }
  68. return
  69. }
  70. // GoGitRepo gets the go-git repo representation
  71. func (repo *Repository) GoGitRepo() *gogit.Repository {
  72. return repo.gogitRepo
  73. }