gitea源码

gitrepo.go 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitrepo
  4. import (
  5. "context"
  6. "fmt"
  7. "io"
  8. "path/filepath"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/reqctx"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/util"
  13. )
  14. // Repository represents a git repository which stored in a disk
  15. type Repository interface {
  16. RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path
  17. }
  18. // RelativePath should be an unix style path like username/reponame.git
  19. // This method should change it according to the current OS.
  20. func repoPath(repo Repository) string {
  21. return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.RelativePath()))
  22. }
  23. // OpenRepository opens the repository at the given relative path with the provided context.
  24. func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
  25. return git.OpenRepository(ctx, repoPath(repo))
  26. }
  27. // contextKey is a value for use with context.WithValue.
  28. type contextKey struct {
  29. repoPath string
  30. }
  31. // RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it
  32. // The caller must call "defer gitRepo.Close()"
  33. func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) {
  34. reqCtx := reqctx.FromContext(ctx)
  35. if reqCtx != nil {
  36. gitRepo, err := RepositoryFromRequestContextOrOpen(reqCtx, repo)
  37. return gitRepo, util.NopCloser{}, err
  38. }
  39. gitRepo, err := OpenRepository(ctx, repo)
  40. return gitRepo, gitRepo, err
  41. }
  42. // RepositoryFromRequestContextOrOpen opens the repository at the given relative path in the provided request context.
  43. // Caller shouldn't close the git repo manually, the git repo will be automatically closed when the request context is done.
  44. func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Repository) (*git.Repository, error) {
  45. ck := contextKey{repoPath: repoPath(repo)}
  46. if gitRepo, ok := ctx.Value(ck).(*git.Repository); ok {
  47. return gitRepo, nil
  48. }
  49. gitRepo, err := git.OpenRepository(ctx, ck.repoPath)
  50. if err != nil {
  51. return nil, err
  52. }
  53. ctx.AddCloser(gitRepo)
  54. ctx.SetContextValue(ck, gitRepo)
  55. return gitRepo, nil
  56. }
  57. // IsRepositoryExist returns true if the repository directory exists in the disk
  58. func IsRepositoryExist(ctx context.Context, repo Repository) (bool, error) {
  59. return util.IsExist(repoPath(repo))
  60. }
  61. // DeleteRepository deletes the repository directory from the disk, it will return
  62. // nil if the repository does not exist.
  63. func DeleteRepository(ctx context.Context, repo Repository) error {
  64. return util.RemoveAll(repoPath(repo))
  65. }
  66. // RenameRepository renames a repository's name on disk
  67. func RenameRepository(ctx context.Context, repo, newRepo Repository) error {
  68. if err := util.Rename(repoPath(repo), repoPath(newRepo)); err != nil {
  69. return fmt.Errorf("rename repository directory: %w", err)
  70. }
  71. return nil
  72. }
  73. func InitRepository(ctx context.Context, repo Repository, objectFormatName string) error {
  74. return git.InitRepository(ctx, repoPath(repo), true, objectFormatName)
  75. }