diff --git a/cmd/admin.go b/cmd/admin.go index 74bfa5a6c670..6c9480e76eb7 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -11,6 +11,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" @@ -122,7 +123,7 @@ func runRepoSyncReleases(_ *cli.Context) error { log.Trace("Processing next %d repos of %d", len(repos), count) for _, repo := range repos { log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath()) - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { log.Warn("OpenRepository: %v", err) continue diff --git a/models/activities/repo_activity.go b/models/activities/repo_activity.go index 91f5ac12bdc0..ba5e4959f0df 100644 --- a/models/activities/repo_activity.go +++ b/models/activities/repo_activity.go @@ -14,6 +14,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "xorm.io/xorm" ) @@ -65,7 +66,7 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom return nil, fmt.Errorf("FillUnresolvedIssues: %w", err) } if code { - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return nil, fmt.Errorf("OpenRepository: %w", err) } @@ -82,7 +83,7 @@ func GetActivityStats(ctx context.Context, repo *repo_model.Repository, timeFrom // GetActivityStatsTopAuthors returns top author stats for git commits for all branches func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository, timeFrom time.Time, count int) ([]*ActivityAuthorData, error) { - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return nil, fmt.Errorf("OpenRepository: %w", err) } diff --git a/models/issues/comment.go b/models/issues/comment.go index 8a3bae5b883d..c63fcab8944f 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -18,7 +18,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/references" @@ -762,8 +762,7 @@ func (c *Comment) LoadPushCommits(ctx context.Context) (err error) { c.OldCommit = data.CommitIDs[0] c.NewCommit = data.CommitIDs[1] } else { - repoPath := c.Issue.Repo.RepoPath() - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, c.Issue.Repo) if err != nil { return err } diff --git a/models/issues/pull.go b/models/issues/pull.go index 4ae6e38ae180..2cb1e1b971e9 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -19,6 +19,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -865,7 +866,7 @@ func PullRequestCodeOwnersReview(ctx context.Context, pull *Issue, pr *PullReque return err } - repo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + repo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { return err } diff --git a/models/repo/repo.go b/models/repo/repo.go index 4401041cdd52..13493ba6e80e 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -196,6 +196,14 @@ func init() { db.RegisterModel(new(Repository)) } +func (repo *Repository) GetName() string { + return repo.Name +} + +func (repo *Repository) GetOwnerName() string { + return repo.OwnerName +} + // SanitizedOriginalURL returns a sanitized OriginalURL func (repo *Repository) SanitizedOriginalURL() string { if repo.OriginalURL == "" { diff --git a/modules/context/api.go b/modules/context/api.go index f41228ad7674..e226264a8735 100644 --- a/modules/context/api.go +++ b/modules/context/api.go @@ -11,11 +11,11 @@ import ( "net/url" "strings" - repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" mc "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -224,7 +224,7 @@ func APIContexter() func(http.Handler) http.Handler { defer baseCleanUp() ctx.Base.AppendContextValue(apiContextKey, ctx) - ctx.Base.AppendContextValueFunc(git.RepositoryContextKey, func() any { return ctx.Repo.GitRepo }) + ctx.Base.AppendContextValueFunc(gitrepo.RepositoryContextKey, func() any { return ctx.Repo.GitRepo }) // If request sends files, parse them here otherwise the Query() can't be parsed and the CsrfToken will be invalid. if ctx.Req.Method == "POST" && strings.Contains(ctx.Req.Header.Get("Content-Type"), "multipart/form-data") { @@ -278,10 +278,9 @@ func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) (cancel context // For API calls. if ctx.Repo.GitRepo == nil { - repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - gitRepo, err := git.OpenRepository(ctx, repoPath) + gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { - ctx.Error(http.StatusInternalServerError, "RepoRef Invalid repo "+repoPath, err) + ctx.Error(http.StatusInternalServerError, fmt.Sprintf("Open Repository %v failed", ctx.Repo.Repository.FullName()), err) return cancel } ctx.Repo.GitRepo = gitRepo diff --git a/modules/context/context.go b/modules/context/context.go index 8a94e958b516..d19c5d119830 100644 --- a/modules/context/context.go +++ b/modules/context/context.go @@ -17,7 +17,7 @@ import ( "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" mc "code.gitea.io/gitea/modules/cache" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/templates" @@ -163,7 +163,7 @@ func Contexter() func(next http.Handler) http.Handler { ctx.Data["PageData"] = ctx.PageData ctx.Base.AppendContextValue(WebContextKey, ctx) - ctx.Base.AppendContextValueFunc(git.RepositoryContextKey, func() any { return ctx.Repo.GitRepo }) + ctx.Base.AppendContextValueFunc(gitrepo.RepositoryContextKey, func() any { return ctx.Repo.GitRepo }) ctx.Csrf = PrepareCSRFProtector(csrfOpts, ctx) diff --git a/modules/context/repo.go b/modules/context/repo.go index 8d82be1990da..75ebfec70544 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -23,6 +23,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" code_indexer "code.gitea.io/gitea/modules/indexer/code" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" @@ -633,7 +634,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { return nil } - gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(userName, repoName)) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { if strings.Contains(err.Error(), "repository does not exist") || strings.Contains(err.Error(), "no such file or directory") { log.Error("Repository %-v has a broken repository on the file system: %s Error: %v", ctx.Repo.Repository, ctx.Repo.Repository.RepoPath(), err) @@ -645,7 +646,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { } return nil } - ctx.ServerError("RepoAssignment Invalid repo "+repo_model.RepoPath(userName, repoName), err) + ctx.ServerError("RepoAssignment Invalid repo "+repo.FullName(), err) return nil } if ctx.Repo.GitRepo != nil { @@ -920,10 +921,9 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context ) if ctx.Repo.GitRepo == nil { - repoPath := repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name) - ctx.Repo.GitRepo, err = git.OpenRepository(ctx, repoPath) + ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { - ctx.ServerError("RepoRef Invalid repo "+repoPath, err) + ctx.ServerError(fmt.Sprintf("Open Repository %v failed", ctx.Repo.Repository.FullName()), err) return nil } // We opened it, we should close it diff --git a/modules/contexttest/context_tests.go b/modules/contexttest/context_tests.go index 8994c1e451c4..9ca028bb6ec2 100644 --- a/modules/contexttest/context_tests.go +++ b/modules/contexttest/context_tests.go @@ -18,7 +18,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/templates" "code.gitea.io/gitea/modules/translation" "code.gitea.io/gitea/modules/web/middleware" @@ -107,7 +107,7 @@ func LoadRepoCommit(t *testing.T, ctx gocontext.Context) { assert.FailNow(t, "context is not *context.Context or *context.APIContext") } - gitRepo, err := git.OpenRepository(ctx, repo.Repository.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo.Repository) assert.NoError(t, err) defer gitRepo.Close() branch, err := gitRepo.GetHEADBranch() @@ -137,7 +137,7 @@ func LoadUser(t *testing.T, ctx gocontext.Context, userID int64) { func LoadGitRepo(t *testing.T, ctx *context.Context) { assert.NoError(t, ctx.Repo.Repository.LoadOwner(ctx)) var err error - ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath()) + ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) assert.NoError(t, err) } diff --git a/modules/git/repo_base.go b/modules/git/repo_base.go index a9d91d2deb2d..6c148d9af516 100644 --- a/modules/git/repo_base.go +++ b/modules/git/repo_base.go @@ -3,48 +3,4 @@ package git -import ( - "context" - "io" -) - var isGogit bool - -// contextKey is a value for use with context.WithValue. -type contextKey struct { - name string -} - -// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context -var RepositoryContextKey = &contextKey{"repository"} - -// RepositoryFromContext attempts to get the repository from the context -func RepositoryFromContext(ctx context.Context, path string) *Repository { - value := ctx.Value(RepositoryContextKey) - if value == nil { - return nil - } - - if repo, ok := value.(*Repository); ok && repo != nil { - if repo.Path == path { - return repo - } - } - - return nil -} - -type nopCloser func() - -func (nopCloser) Close() error { return nil } - -// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it -func RepositoryFromContextOrOpen(ctx context.Context, path string) (*Repository, io.Closer, error) { - gitRepo := RepositoryFromContext(ctx, path) - if gitRepo != nil { - return gitRepo, nopCloser(nil), nil - } - - gitRepo, err := OpenRepository(ctx, path) - return gitRepo, gitRepo, err -} diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go index 59580939757c..979c5dec9175 100644 --- a/modules/git/repo_branch.go +++ b/modules/git/repo_branch.go @@ -86,29 +86,6 @@ func (repo *Repository) GetBranch(branch string) (*Branch, error) { }, nil } -// GetBranchesByPath returns a branch by it's path -// if limit = 0 it will not limit -func GetBranchesByPath(ctx context.Context, path string, skip, limit int) ([]*Branch, int, error) { - gitRepo, err := OpenRepository(ctx, path) - if err != nil { - return nil, 0, err - } - defer gitRepo.Close() - - return gitRepo.GetBranches(skip, limit) -} - -// GetBranchCommitID returns a branch commit ID by its name -func GetBranchCommitID(ctx context.Context, path, branch string) (string, error) { - gitRepo, err := OpenRepository(ctx, path) - if err != nil { - return "", err - } - defer gitRepo.Close() - - return gitRepo.GetBranchCommitID(branch) -} - // GetBranches returns a slice of *git.Branch func (repo *Repository) GetBranches(skip, limit int) ([]*Branch, int, error) { brs, countAll, err := repo.GetBranchNames(skip, limit) diff --git a/modules/git/repo_branch_gogit.go b/modules/git/repo_branch_gogit.go index 1c0d9a18aa9d..d1ec14d81155 100644 --- a/modules/git/repo_branch_gogit.go +++ b/modules/git/repo_branch_gogit.go @@ -7,7 +7,6 @@ package git import ( - "context" "sort" "strings" @@ -95,34 +94,6 @@ func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { return branchNames, len(branchData), nil } -// WalkReferences walks all the references from the repository -// refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty. -func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refname string) error) (int, error) { - repo := RepositoryFromContext(ctx, repoPath) - if repo == nil { - var err error - repo, err = OpenRepository(ctx, repoPath) - if err != nil { - return 0, err - } - defer repo.Close() - } - - i := 0 - iter, err := repo.gogitRepo.References() - if err != nil { - return i, err - } - defer iter.Close() - - err = iter.ForEach(func(ref *plumbing.Reference) error { - err := walkfn(ref.Hash().String(), string(ref.Name())) - i++ - return err - }) - return i, err -} - // WalkReferences walks all the references from the repository func (repo *Repository) WalkReferences(arg ObjectType, skip, limit int, walkfn func(sha1, refname string) error) (int, error) { i := 0 diff --git a/modules/git/repo_branch_nogogit.go b/modules/git/repo_branch_nogogit.go index b1e7c8b73e64..470faebe25f7 100644 --- a/modules/git/repo_branch_nogogit.go +++ b/modules/git/repo_branch_nogogit.go @@ -65,11 +65,6 @@ func (repo *Repository) GetBranchNames(skip, limit int) ([]string, int, error) { return callShowRef(repo.Ctx, repo.Path, BranchPrefix, TrustedCmdArgs{BranchPrefix, "--sort=-committerdate"}, skip, limit) } -// WalkReferences walks all the references from the repository -func WalkReferences(ctx context.Context, repoPath string, walkfn func(sha1, refname string) error) (int, error) { - return walkShowRef(ctx, repoPath, nil, 0, 0, walkfn) -} - // WalkReferences walks all the references from the repository // refType should be empty, ObjectTag or ObjectBranch. All other values are equivalent to empty. func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walkfn func(sha1, refname string) error) (int, error) { @@ -81,12 +76,12 @@ func (repo *Repository) WalkReferences(refType ObjectType, skip, limit int, walk args = TrustedCmdArgs{BranchPrefix, "--sort=-committerdate"} } - return walkShowRef(repo.Ctx, repo.Path, args, skip, limit, walkfn) + return WalkShowRef(repo.Ctx, repo.Path, args, skip, limit, walkfn) } // callShowRef return refs, if limit = 0 it will not limit func callShowRef(ctx context.Context, repoPath, trimPrefix string, extraArgs TrustedCmdArgs, skip, limit int) (branchNames []string, countAll int, err error) { - countAll, err = walkShowRef(ctx, repoPath, extraArgs, skip, limit, func(_, branchName string) error { + countAll, err = WalkShowRef(ctx, repoPath, extraArgs, skip, limit, func(_, branchName string) error { branchName = strings.TrimPrefix(branchName, trimPrefix) branchNames = append(branchNames, branchName) @@ -95,7 +90,7 @@ func callShowRef(ctx context.Context, repoPath, trimPrefix string, extraArgs Tru return branchNames, countAll, err } -func walkShowRef(ctx context.Context, repoPath string, extraArgs TrustedCmdArgs, skip, limit int, walkfn func(sha1, refname string) error) (countAll int, err error) { +func WalkShowRef(ctx context.Context, repoPath string, extraArgs TrustedCmdArgs, skip, limit int, walkfn func(sha1, refname string) error) (countAll int, err error) { stdoutReader, stdoutWriter := io.Pipe() defer func() { _ = stdoutReader.Close() @@ -189,7 +184,7 @@ func walkShowRef(ctx context.Context, repoPath string, extraArgs TrustedCmdArgs, // GetRefsBySha returns all references filtered with prefix that belong to a sha commit hash func (repo *Repository) GetRefsBySha(sha, prefix string) ([]string, error) { var revList []string - _, err := walkShowRef(repo.Ctx, repo.Path, nil, 0, 0, func(walkSha, refname string) error { + _, err := WalkShowRef(repo.Ctx, repo.Path, nil, 0, 0, func(walkSha, refname string) error { if walkSha == sha && strings.HasPrefix(refname, prefix) { revList = append(revList, refname) } diff --git a/modules/gitrepo/branch.go b/modules/gitrepo/branch.go new file mode 100644 index 000000000000..dcaf92668d15 --- /dev/null +++ b/modules/gitrepo/branch.go @@ -0,0 +1,32 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo + +import ( + "context" + + "code.gitea.io/gitea/modules/git" +) + +// GetBranchesByPath returns a branch by its path +// if limit = 0 it will not limit +func GetBranchesByPath(ctx context.Context, repo Repository, skip, limit int) ([]*git.Branch, int, error) { + gitRepo, err := OpenRepository(ctx, repo) + if err != nil { + return nil, 0, err + } + defer gitRepo.Close() + + return gitRepo.GetBranches(skip, limit) +} + +func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (string, error) { + gitRepo, err := OpenRepository(ctx, repo) + if err != nil { + return "", err + } + defer gitRepo.Close() + + return gitRepo.GetBranchCommitID(branch) +} diff --git a/modules/gitrepo/gitrepo.go b/modules/gitrepo/gitrepo.go new file mode 100644 index 000000000000..d89f8f9c0c88 --- /dev/null +++ b/modules/gitrepo/gitrepo.go @@ -0,0 +1,103 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo + +import ( + "context" + "io" + "path/filepath" + "strings" + + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/setting" +) + +type Repository interface { + GetName() string + GetOwnerName() string +} + +func repoPath(repo Repository) string { + return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".git") +} + +func wikiPath(repo Repository) string { + return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git") +} + +// OpenRepository opens the repository at the given relative path with the provided context. +func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, error) { + return git.OpenRepository(ctx, repoPath(repo)) +} + +func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) { + return git.OpenRepository(ctx, wikiPath(repo)) +} + +// contextKey is a value for use with context.WithValue. +type contextKey struct { + name string +} + +// RepositoryContextKey is a context key. It is used with context.Value() to get the current Repository for the context +var RepositoryContextKey = &contextKey{"repository"} + +// RepositoryFromContext attempts to get the repository from the context +func repositoryFromContext(ctx context.Context, repo Repository) *git.Repository { + value := ctx.Value(RepositoryContextKey) + if value == nil { + return nil + } + + if gitRepo, ok := value.(*git.Repository); ok && gitRepo != nil { + if gitRepo.Path == repoPath(repo) { + return gitRepo + } + } + + return nil +} + +type nopCloser func() + +func (nopCloser) Close() error { return nil } + +// RepositoryFromContextOrOpen attempts to get the repository from the context or just opens it +func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) { + gitRepo := repositoryFromContext(ctx, repo) + if gitRepo != nil { + return gitRepo, nopCloser(nil), nil + } + + gitRepo, err := OpenRepository(ctx, repo) + return gitRepo, gitRepo, err +} + +// repositoryFromContextPath attempts to get the repository from the context +func repositoryFromContextPath(ctx context.Context, path string) *git.Repository { + value := ctx.Value(RepositoryContextKey) + if value == nil { + return nil + } + + if repo, ok := value.(*git.Repository); ok && repo != nil { + if repo.Path == path { + return repo + } + } + + return nil +} + +// RepositoryFromContextOrOpenPath attempts to get the repository from the context or just opens it +// Deprecated: Use RepositoryFromContextOrOpen instead +func RepositoryFromContextOrOpenPath(ctx context.Context, path string) (*git.Repository, io.Closer, error) { + gitRepo := repositoryFromContextPath(ctx, path) + if gitRepo != nil { + return gitRepo, nopCloser(nil), nil + } + + gitRepo, err := git.OpenRepository(ctx, path) + return gitRepo, gitRepo, err +} diff --git a/modules/gitrepo/walk_gogit.go b/modules/gitrepo/walk_gogit.go new file mode 100644 index 000000000000..6370faf08e7d --- /dev/null +++ b/modules/gitrepo/walk_gogit.go @@ -0,0 +1,40 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build gogit + +package gitrepo + +import ( + "context" + + "github.com/go-git/go-git/v5/plumbing" +) + +// WalkReferences walks all the references from the repository +// refname is empty, ObjectTag or ObjectBranch. All other values should be treated as equivalent to empty. +func WalkReferences(ctx context.Context, repo Repository, walkfn func(sha1, refname string) error) (int, error) { + gitRepo := repositoryFromContext(ctx, repo) + if gitRepo == nil { + var err error + gitRepo, err = OpenRepository(ctx, repo) + if err != nil { + return 0, err + } + defer gitRepo.Close() + } + + i := 0 + iter, err := gitRepo.GoGitRepo().References() + if err != nil { + return i, err + } + defer iter.Close() + + err = iter.ForEach(func(ref *plumbing.Reference) error { + err := walkfn(ref.Hash().String(), string(ref.Name())) + i++ + return err + }) + return i, err +} diff --git a/modules/gitrepo/walk_nogogit.go b/modules/gitrepo/walk_nogogit.go new file mode 100644 index 000000000000..ff9555996dff --- /dev/null +++ b/modules/gitrepo/walk_nogogit.go @@ -0,0 +1,17 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build !gogit + +package gitrepo + +import ( + "context" + + "code.gitea.io/gitea/modules/git" +) + +// WalkReferences walks all the references from the repository +func WalkReferences(ctx context.Context, repo Repository, walkfn func(sha1, refname string) error) (int, error) { + return git.WalkShowRef(ctx, repoPath(repo), nil, 0, 0, walkfn) +} diff --git a/modules/indexer/stats/db.go b/modules/indexer/stats/db.go index 163843b47fa5..98a977c7008f 100644 --- a/modules/indexer/stats/db.go +++ b/modules/indexer/stats/db.go @@ -8,6 +8,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -35,7 +36,7 @@ func (db *DBIndexer) Index(id int64) error { return err } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { if err.Error() == "no such file or directory" { return nil diff --git a/modules/repository/branch.go b/modules/repository/branch.go index cd45f162276f..e448490f4ac1 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -11,6 +11,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" ) @@ -24,9 +25,9 @@ func SyncRepoBranches(ctx context.Context, repoID, doerID int64) (int64, error) log.Debug("SyncRepoBranches: in Repo[%d:%s]", repo.ID, repo.FullName()) - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { - log.Error("OpenRepository[%s]: %w", repo.RepoPath(), err) + log.Error("OpenRepository[%s]: %w", repo.FullName(), err) return 0, err } defer gitRepo.Close() diff --git a/modules/repository/generate.go b/modules/repository/generate.go index f8478b8c1852..b32c4e058e50 100644 --- a/modules/repository/generate.go +++ b/modules/repository/generate.go @@ -19,6 +19,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" @@ -271,7 +272,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r repo.DefaultBranch = templateRepo.DefaultBranch } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return fmt.Errorf("openRepository: %w", err) } diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 5352da0378d9..fc3af040719f 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -19,6 +19,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migration" @@ -291,7 +292,7 @@ func SyncRepoTags(ctx context.Context, repoID int64) error { return err } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return err } diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index edbfbcc568ba..bd02a8afc434 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -16,6 +16,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" repo_module "code.gitea.io/gitea/modules/repository" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -643,7 +644,7 @@ func CreateBranchProtection(ctx *context.APIContext) { } else { if !isPlainRule { if ctx.Repo.GitRepo == nil { - ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath()) + ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return @@ -920,7 +921,7 @@ func EditBranchProtection(ctx *context.APIContext) { } else { if !isPlainRule { if ctx.Repo.GitRepo == nil { - ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath()) + ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 94e634461c27..065d6bf8b2c3 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -21,6 +21,7 @@ import ( "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" @@ -279,9 +280,8 @@ func GetArchive(ctx *context.APIContext) { // "404": // "$ref": "#/responses/notFound" - repoPath := repo_model.RepoPath(ctx.Params(":username"), ctx.Params(":reponame")) if ctx.Repo.GitRepo == nil { - gitRepo, err := git.OpenRepository(ctx, repoPath) + gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index b1cb7011f1d4..eaf406e64d8a 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -23,6 +23,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -906,9 +907,9 @@ func MergePullRequest(ctx *context.APIContext) { if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil { headRepo = ctx.Repo.GitRepo } else { - headRepo, err = git.OpenRepository(ctx, pr.HeadRepo.RepoPath()) + headRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo) if err != nil { - ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err) + ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.FullName()), err) return } defer headRepo.Close() @@ -1004,7 +1005,7 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) headRepo = ctx.Repo.Repository headGitRepo = ctx.Repo.GitRepo } else { - headGitRepo, err = git.OpenRepository(ctx, repo_model.RepoPath(headUser.Name, headRepo.Name)) + headGitRepo, err = gitrepo.OpenRepository(ctx, headRepo) if err != nil { ctx.Error(http.StatusInternalServerError, "OpenRepository", err) return nil, nil, nil, nil, "", "" @@ -1308,7 +1309,7 @@ func GetPullRequestCommits(ctx *context.APIContext) { } var prInfo *git.CompareInfo - baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo) if err != nil { ctx.ServerError("OpenRepository", err) return diff --git a/routers/api/v1/repo/pull_review.go b/routers/api/v1/repo/pull_review.go index 7b9445be4c06..07d8f4877bb3 100644 --- a/routers/api/v1/repo/pull_review.go +++ b/routers/api/v1/repo/pull_review.go @@ -13,7 +13,7 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/utils" @@ -329,8 +329,7 @@ func CreatePullReview(ctx *context.APIContext) { // if CommitID is empty, set it as lastCommitID if opts.CommitID == "" { - - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.Issue.Repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.Issue.Repo) if err != nil { ctx.Error(http.StatusInternalServerError, "git.OpenRepository", err) return diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 436b83adf2b3..2efdccb56990 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -21,6 +21,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/label" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" @@ -718,7 +719,7 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err if ctx.Repo.GitRepo == nil && !repo.IsEmpty { var err error - ctx.Repo.GitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.RepoPath()) + ctx.Repo.GitRepo, err = gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.Error(http.StatusInternalServerError, "Unable to OpenRepository", err) return err diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 8e5ecce310e3..ba3e978a8353 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -12,6 +12,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -475,7 +476,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) // findWikiRepoCommit opens the wiki repo and returns the latest commit, writing to context on error. // The caller is responsible for closing the returned repo again func findWikiRepoCommit(ctx *context.APIContext) (*git.Repository, *git.Commit) { - wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath()) + wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository) if err != nil { if git.IsErrNotExist(err) || err.Error() == "no such file or directory" { diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go index 39714e343f01..2299cdc24754 100644 --- a/routers/api/v1/utils/git.go +++ b/routers/api/v1/utils/git.go @@ -10,6 +10,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" ) @@ -79,7 +80,7 @@ func ConvertToObjectID(ctx gocontext.Context, repo *context.Repository, commitID } } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.Repository.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo.Repository) if err != nil { return objectFormat.EmptyObjectID(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err) } diff --git a/routers/private/internal_repo.go b/routers/private/internal_repo.go index 5e7e82b03c96..615239d479d9 100644 --- a/routers/private/internal_repo.go +++ b/routers/private/internal_repo.go @@ -10,7 +10,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" gitea_context "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/private" ) @@ -28,7 +28,7 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc { return nil } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err) ctx.JSON(http.StatusInternalServerError, private.Response{ diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 00157d44c99c..32fa973ef61e 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -22,6 +22,7 @@ import ( "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitgraph" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" @@ -279,7 +280,7 @@ func Diff(ctx *context.Context) { ) if ctx.Data["PageIsWiki"] != nil { - gitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath()) + gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.ServerError("Repo.GitRepo.GetCommit", err) return @@ -404,7 +405,7 @@ func Diff(ctx *context.Context) { func RawDiff(ctx *context.Context) { var gitRepo *git.Repository if ctx.Data["PageIsWiki"] != nil { - wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath()) + wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.ServerError("OpenRepository", err) return diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 5ae48e1ce814..a3593815b8b7 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -28,6 +28,7 @@ import ( "code.gitea.io/gitea/modules/context" csv_module "code.gitea.io/gitea/modules/csv" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/setting" @@ -408,7 +409,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { ci.HeadRepo = ctx.Repo.Repository ci.HeadGitRepo = ctx.Repo.GitRepo } else if has { - ci.HeadGitRepo, err = git.OpenRepository(ctx, ci.HeadRepo.RepoPath()) + ci.HeadGitRepo, err = gitrepo.OpenRepository(ctx, ci.HeadRepo) if err != nil { ctx.ServerError("OpenRepository", err) return nil @@ -688,7 +689,7 @@ func PrepareCompareDiff( } func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repository) (branches, tags []string, err error) { - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return nil, nil, err } @@ -876,7 +877,7 @@ func ExcerptBlob(ctx *context.Context) { gitRepo := ctx.Repo.GitRepo if ctx.FormBool("wiki") { var err error - gitRepo, err = git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath()) + gitRepo, err = gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.ServerError("OpenRepository", err) return diff --git a/routers/web/repo/editor_test.go b/routers/web/repo/editor_test.go index 67fb277d5c2b..c28c3ef1d632 100644 --- a/routers/web/repo/editor_test.go +++ b/routers/web/repo/editor_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/contexttest" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "github.com/stretchr/testify/assert" ) @@ -66,7 +67,7 @@ func TestGetClosestParentWithFiles(t *testing.T) { repo := ctx.Repo.Repository branch := repo.DefaultBranch - gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(branch) var expectedTreePath string // Should return the root dir, empty string, since there are no subdirs in this repo diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index e36d7092af28..b265cf47548b 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -29,6 +29,7 @@ import ( "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" issue_template "code.gitea.io/gitea/modules/issue/template" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -530,7 +531,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C if pull.BaseRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil { baseGitRepo = ctx.Repo.GitRepo } else { - baseGitRepo, err := git.OpenRepository(ctx, pull.BaseRepo.RepoPath()) + baseGitRepo, err := gitrepo.OpenRepository(ctx, pull.BaseRepo) if err != nil { ctx.ServerError("OpenRepository", err) return nil @@ -582,7 +583,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *issues_model.Issue) *git.C var headBranchSha string // HeadRepo may be missing if pull.HeadRepo != nil { - headGitRepo, err := git.OpenRepository(ctx, pull.HeadRepo.RepoPath()) + headGitRepo, err := gitrepo.OpenRepository(ctx, pull.HeadRepo) if err != nil { ctx.ServerError("OpenRepository", err) return nil @@ -1314,9 +1315,9 @@ func MergePullRequest(ctx *context.Context) { if ctx.Repo != nil && ctx.Repo.Repository != nil && pr.HeadRepoID == ctx.Repo.Repository.ID && ctx.Repo.GitRepo != nil { headRepo = ctx.Repo.GitRepo } else { - headRepo, err = git.OpenRepository(ctx, pr.HeadRepo.RepoPath()) + headRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo) if err != nil { - ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err) + ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.FullName()), err) return } defer headRepo.Close() @@ -1537,9 +1538,9 @@ func CleanUpPullRequest(ctx *context.Context) { gitBaseRepo = ctx.Repo.GitRepo } else { // If not just open it - gitBaseRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + gitBaseRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { - ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.RepoPath()), err) + ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.BaseRepo.FullName()), err) return } defer gitBaseRepo.Close() @@ -1552,9 +1553,9 @@ func CleanUpPullRequest(ctx *context.Context) { gitRepo = ctx.Repo.GitRepo } else if pr.BaseRepoID != pr.HeadRepoID { // Otherwise just load it up - gitRepo, err = git.OpenRepository(ctx, pr.HeadRepo.RepoPath()) + gitRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo) if err != nil { - ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.RepoPath()), err) + ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.FullName()), err) return } defer gitRepo.Close() diff --git a/routers/web/repo/wiki.go b/routers/web/repo/wiki.go index 4e09f046cf85..5e7b971e67b7 100644 --- a/routers/web/repo/wiki.go +++ b/routers/web/repo/wiki.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -92,7 +93,7 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error) } func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) { - wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath()) + wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository) if err != nil { ctx.ServerError("OpenRepository", err) return nil, nil, err diff --git a/routers/web/repo/wiki_test.go b/routers/web/repo/wiki_test.go index ae050df967a5..d3decdae2def 100644 --- a/routers/web/repo/wiki_test.go +++ b/routers/web/repo/wiki_test.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/contexttest" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/services/forms" wiki_service "code.gitea.io/gitea/services/wiki" @@ -26,7 +27,7 @@ const ( ) func wikiEntry(t *testing.T, repo *repo_model.Repository, wikiName wiki_service.WebPath) *git.TreeEntry { - wikiRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) + wikiRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo) assert.NoError(t, err) defer wikiRepo.Close() commit, err := wikiRepo.GetBranchCommit("master") diff --git a/routers/web/shared/user/header.go b/routers/web/shared/user/header.go index 0f8d64e7b24d..a2c0abb47e4a 100644 --- a/routers/web/shared/user/header.go +++ b/routers/web/shared/user/header.go @@ -13,6 +13,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/markdown" @@ -90,7 +91,7 @@ func FindUserProfileReadme(ctx *context.Context, doer *user_model.User) (profile if err == nil { perm, err := access_model.GetUserRepoPermission(ctx, profileDbRepo, doer) if err == nil && !profileDbRepo.IsEmpty && perm.CanRead(unit.TypeCode) { - if profileGitRepo, err = git.OpenRepository(ctx, profileDbRepo.RepoPath()); err != nil { + if profileGitRepo, err = gitrepo.OpenRepository(ctx, profileDbRepo); err != nil { log.Error("FindUserProfileReadme failed to OpenRepository: %v", err) } else { if commit, err := profileGitRepo.GetBranchCommit(profileDbRepo.DefaultBranch); err != nil { diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index 2a3ffb76f30d..9900de3d2ecd 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -20,6 +20,7 @@ import ( user_model "code.gitea.io/gitea/models/user" actions_module "code.gitea.io/gitea/modules/actions" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -128,7 +129,7 @@ func notify(ctx context.Context, input *notifyInput) error { return nil } - gitRepo, err := git.OpenRepository(context.Background(), input.Repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(context.Background(), input.Repo) if err != nil { return fmt.Errorf("git.OpenRepository: %w", err) } diff --git a/services/asymkey/sign.go b/services/asymkey/sign.go index 0c4aac815693..2f5d76a29334 100644 --- a/services/asymkey/sign.go +++ b/services/asymkey/sign.go @@ -13,8 +13,10 @@ import ( "code.gitea.io/gitea/models/db" git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" + repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/setting" @@ -167,7 +169,8 @@ Loop: } // SignWikiCommit determines if we should sign the commits to this repository wiki -func SignWikiCommit(ctx context.Context, repoWikiPath string, u *user_model.User) (bool, string, *git.Signature, error) { +func SignWikiCommit(ctx context.Context, repo *repo_model.Repository, u *user_model.User) (bool, string, *git.Signature, error) { + repoWikiPath := repo.WikiPath() rules := signingModeFromStrings(setting.Repository.Signing.Wiki) signingKey, sig := SigningKey(ctx, repoWikiPath) if signingKey == "" { @@ -201,7 +204,7 @@ Loop: return false, "", nil, &ErrWontSign{twofa} } case parentSigned: - gitRepo, err := git.OpenRepository(ctx, repoWikiPath) + gitRepo, err := gitrepo.OpenWikiRepository(ctx, repo) if err != nil { return false, "", nil, err } diff --git a/services/automerge/automerge.go b/services/automerge/automerge.go index bf713c44314d..bd427bef9f73 100644 --- a/services/automerge/automerge.go +++ b/services/automerge/automerge.go @@ -17,6 +17,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -111,7 +112,7 @@ func MergeScheduledPullRequest(ctx context.Context, sha string, repo *repo_model } func getPullRequestsByHeadSHA(ctx context.Context, sha string, repo *repo_model.Repository, filter func(*issues_model.PullRequest) bool) (map[int64]*issues_model.PullRequest, error) { - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return nil, err } @@ -190,7 +191,7 @@ func handlePull(pullID int64, sha string) { return } - headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath()) + headGitRepo, err := gitrepo.OpenRepository(ctx, pr.HeadRepo) if err != nil { log.Error("OpenRepository %-v: %v", pr.HeadRepo, err) return @@ -246,7 +247,7 @@ func handlePull(pullID int64, sha string) { return } - baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { log.Error("OpenRepository %-v: %v", pr.BaseRepo, err) return diff --git a/services/convert/pull.go b/services/convert/pull.go index 7eebe20426c3..6d98121ed54c 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -12,6 +12,7 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" api "code.gitea.io/gitea/modules/structs" ) @@ -101,7 +102,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr() } - gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err) return nil @@ -127,7 +128,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u } if pr.Flow == issues_model.PullRequestFlowAGit { - gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { log.Error("OpenRepository[%s]: %v", pr.GetGitRefName(), err) return nil @@ -154,7 +155,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u apiPullRequest.Head.RepoID = pr.HeadRepo.ID apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p) - headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath()) + headGitRepo, err := gitrepo.OpenRepository(ctx, pr.HeadRepo) if err != nil { log.Error("OpenRepository[%s]: %v", pr.HeadRepo.RepoPath(), err) return nil @@ -190,7 +191,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u } if len(apiPullRequest.Head.Sha) == 0 && len(apiPullRequest.Head.Ref) != 0 { - baseGitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { log.Error("OpenRepository[%s]: %v", pr.BaseRepo.RepoPath(), err) return nil diff --git a/services/doctor/misc.go b/services/doctor/misc.go index f0b5966b5440..9300c3a25c9c 100644 --- a/services/doctor/misc.go +++ b/services/doctor/misc.go @@ -16,6 +16,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -91,7 +92,7 @@ func checkEnablePushOptions(ctx context.Context, logger log.Logger, autofix bool if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { numRepos++ - r, err := git.OpenRepository(ctx, repo.RepoPath()) + r, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return err } diff --git a/services/migrations/gitea_uploader.go b/services/migrations/gitea_uploader.go index 5a4392c66775..7b21d9f4d243 100644 --- a/services/migrations/gitea_uploader.go +++ b/services/migrations/gitea_uploader.go @@ -21,6 +21,7 @@ import ( user_model "code.gitea.io/gitea/models/user" base_module "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/label" "code.gitea.io/gitea/modules/log" base "code.gitea.io/gitea/modules/migration" @@ -139,7 +140,7 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate if err != nil { return err } - g.gitRepo, err = git.OpenRepository(g.ctx, r.RepoPath()) + g.gitRepo, err = gitrepo.OpenRepository(g.ctx, r) return err } diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index 4ec0361dfb46..c8102c6b8b71 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" base "code.gitea.io/gitea/modules/migration" @@ -249,7 +250,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) { Author: &signature, Message: "Initial Commit", })) - fromGitRepo, err := git.OpenRepository(git.DefaultContext, fromRepo.RepoPath()) + fromGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, fromRepo) assert.NoError(t, err) defer fromGitRepo.Close() baseSHA, err := fromGitRepo.GetBranchCommitID(baseRef) @@ -292,7 +293,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) { Author: &signature, Message: "branch2 commit", })) - forkGitRepo, err := git.OpenRepository(git.DefaultContext, forkRepo.RepoPath()) + forkGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, forkRepo) assert.NoError(t, err) defer forkGitRepo.Close() forkHeadSHA, err := forkGitRepo.GetBranchCommitID(forkHeadRef) diff --git a/services/mirror/mirror_pull.go b/services/mirror/mirror_pull.go index 6f03e14ab08b..3418cf90dfe3 100644 --- a/services/mirror/mirror_pull.go +++ b/services/mirror/mirror_pull.go @@ -13,6 +13,7 @@ import ( system_model "code.gitea.io/gitea/models/system" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -300,7 +301,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo log.Error("SyncMirrors [repo: %-v]: %v", m.Repo, err) } - gitRepo, err := git.OpenRepository(ctx, repoPath) + gitRepo, err := gitrepo.OpenRepository(ctx, m.Repo) if err != nil { log.Error("SyncMirrors [repo: %-v]: failed to OpenRepository: %v", m.Repo, err) return nil, false @@ -396,7 +397,7 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo } log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo) - branches, _, err := git.GetBranchesByPath(ctx, m.Repo.RepoPath(), 0, 0) + branches, _, err := gitrepo.GetBranchesByPath(ctx, m.Repo, 0, 0) if err != nil { log.Error("SyncMirrors [repo: %-v]: failed to GetBranches: %v", m.Repo, err) return nil, false @@ -453,7 +454,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool { log.Trace("SyncMirrors [repo: %-v]: no branches updated", m.Repo) } else { log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results)) - gitRepo, err = git.OpenRepository(ctx, m.Repo.RepoPath()) + gitRepo, err = gitrepo.OpenRepository(ctx, m.Repo) if err != nil { log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err) return false diff --git a/services/mirror/mirror_push.go b/services/mirror/mirror_push.go index b117e79faca9..21ba0afeffd6 100644 --- a/services/mirror/mirror_push.go +++ b/services/mirror/mirror_push.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -131,7 +132,11 @@ func SyncPushMirror(ctx context.Context, mirrorID int64) bool { func runPushSync(ctx context.Context, m *repo_model.PushMirror) error { timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second - performPush := func(path string) error { + performPush := func(repo *repo_model.Repository, isWiki bool) error { + path := repo.RepoPath() + if isWiki { + path = repo.WikiPath() + } remoteURL, err := git.GetRemoteURL(ctx, path, m.RemoteName) if err != nil { log.Error("GetRemoteAddress(%s) Error %v", path, err) @@ -141,7 +146,12 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error { if setting.LFS.StartServer { log.Trace("SyncMirrors [repo: %-v]: syncing LFS objects...", m.Repo) - gitRepo, err := git.OpenRepository(ctx, path) + var gitRepo *git.Repository + if isWiki { + gitRepo, err = gitrepo.OpenWikiRepository(ctx, repo) + } else { + gitRepo, err = gitrepo.OpenRepository(ctx, repo) + } if err != nil { log.Error("OpenRepository: %v", err) return errors.New("Unexpected error") @@ -171,16 +181,15 @@ func runPushSync(ctx context.Context, m *repo_model.PushMirror) error { return nil } - err := performPush(m.Repo.RepoPath()) + err := performPush(m.Repo, false) if err != nil { return err } if m.Repo.HasWiki() { - wikiPath := m.Repo.WikiPath() - _, err := git.GetRemoteAddress(ctx, wikiPath, m.RemoteName) + _, err := git.GetRemoteAddress(ctx, m.Repo.WikiPath(), m.RemoteName) if err == nil { - err := performPush(wikiPath) + err := performPush(m.Repo, true) if err != nil { return err } diff --git a/services/pull/check.go b/services/pull/check.go index ebe4c6d61b03..dd6c3ed23014 100644 --- a/services/pull/check.go +++ b/services/pull/check.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -215,7 +216,7 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com return nil, fmt.Errorf("GetFullCommitID(%s) in %s: %w", prHeadRef, pr.BaseRepo.FullName(), err) } - gitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { return nil, fmt.Errorf("%-v OpenRepository: %w", pr.BaseRepo, err) } diff --git a/services/pull/comment.go b/services/pull/comment.go index 14fba52f1e7e..d538b118d5bf 100644 --- a/services/pull/comment.go +++ b/services/pull/comment.go @@ -9,7 +9,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/json" ) @@ -17,8 +17,7 @@ import ( // isForcePush will be true if oldCommit isn't on the branch // Commit on baseBranch will skip func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) { - repoPath := repo.RepoPath() - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return nil, false, err } diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index 39d60380ff20..b73816c7eb22 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -11,6 +11,7 @@ import ( git_model "code.gitea.io/gitea/models/git" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" @@ -116,7 +117,7 @@ func GetPullRequestCommitStatusState(ctx context.Context, pr *issues_model.PullR } // check if all required status checks are successful - headGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) + headGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo) if err != nil { return "", errors.Wrap(err, "OpenRepository") } diff --git a/services/pull/merge_rebase.go b/services/pull/merge_rebase.go index a88f805ef025..ecf376220e2a 100644 --- a/services/pull/merge_rebase.go +++ b/services/pull/merge_rebase.go @@ -9,6 +9,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" ) @@ -57,7 +58,7 @@ func doMergeRebaseFastForward(ctx *mergeContext) error { } // Original repo to read template from. - baseGitRepo, err := git.OpenRepository(ctx, ctx.pr.BaseRepo.RepoPath()) + baseGitRepo, err := gitrepo.OpenRepository(ctx, ctx.pr.BaseRepo) if err != nil { log.Error("Unable to get Git repo for rebase: %v", err) return err diff --git a/services/pull/merge_squash.go b/services/pull/merge_squash.go index f52a2301d906..197d8102dd96 100644 --- a/services/pull/merge_squash.go +++ b/services/pull/merge_squash.go @@ -10,6 +10,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -24,7 +25,7 @@ func getAuthorSignatureSquash(ctx *mergeContext) (*git.Signature, error) { // Try to get an signature from the same user in one of the commits, as the // poster email might be private or commits might have a different signature // than the primary email address of the poster. - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, ctx.tmpBasePath) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpenPath(ctx, ctx.tmpBasePath) if err != nil { log.Error("%-v Unable to open base repository: %v", ctx.pr, err) return nil, err diff --git a/services/pull/patch.go b/services/pull/patch.go index acaff04bda47..12b79a06253d 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -19,6 +19,7 @@ import ( "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -35,7 +36,7 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io return err } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo) if err != nil { return fmt.Errorf("OpenRepository: %w", err) } diff --git a/services/pull/pull.go b/services/pull/pull.go index 930954bdfdfa..e1ea4357fc5d 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/modules/container" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -62,7 +63,7 @@ func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *iss assigneeCommentMap := make(map[int64]*issues_model.Comment) // add first push codes comment - baseGitRepo, err := git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, err := gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { return err } @@ -269,9 +270,9 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest if err != nil { return fmt.Errorf("GetRepositoryByIDCtx: %w", err) } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { - return fmt.Errorf("git.OpenRepository: %w", err) + return fmt.Errorf("gitrepo.OpenRepository: %w", err) } go func() { // FIXME: graceful: We need to tell the manager we're doing something... @@ -614,7 +615,7 @@ func CloseBranchPulls(ctx context.Context, doer *user_model.User, repoID int64, // CloseRepoBranchesPulls close all pull requests which head branches are in the given repository, but only whose base repo is not in the given repository func CloseRepoBranchesPulls(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) error { - branches, _, err := git.GetBranchesByPath(ctx, repo.RepoPath(), 0, 0) + branches, _, err := gitrepo.GetBranchesByPath(ctx, repo, 0, 0) if err != nil { return err } @@ -671,7 +672,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ } } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo) if err != nil { log.Error("Unable to open head repository: Error: %v", err) return "" @@ -845,7 +846,7 @@ func GetIssuesAllCommitStatus(ctx context.Context, issues issues_model.IssueList } gitRepo, ok := gitRepos[issue.RepoID] if !ok { - gitRepo, err = git.OpenRepository(ctx, issue.Repo.RepoPath()) + gitRepo, err = gitrepo.OpenRepository(ctx, issue.Repo) if err != nil { log.Error("Cannot open git repository %-v for issue #%d[%d]. Error: %v", issue.Repo, issue.Index, issue.ID, err) continue @@ -882,7 +883,7 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *issues_model.PullRequest, br if err = pr.LoadBaseRepo(ctx); err != nil { return false, err } - baseGitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo) if err != nil { return false, err } @@ -902,7 +903,7 @@ func IsHeadEqualWithBranch(ctx context.Context, pr *issues_model.PullRequest, br } else { var closer io.Closer - headGitRepo, closer, err = git.RepositoryFromContextOrOpen(ctx, pr.HeadRepo.RepoPath()) + headGitRepo, closer, err = gitrepo.RepositoryFromContextOrOpen(ctx, pr.HeadRepo) if err != nil { return false, err } diff --git a/services/pull/pull_test.go b/services/pull/pull_test.go index d63227a7d5e9..787910bf760f 100644 --- a/services/pull/pull_test.go +++ b/services/pull/pull_test.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "github.com/stretchr/testify/assert" ) @@ -41,7 +42,7 @@ func TestPullRequest_GetDefaultMergeMessage_InternalTracker(t *testing.T) { pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}) assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext)) - gitRepo, err := git.OpenRepository(git.DefaultContext, pr.BaseRepo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, pr.BaseRepo) assert.NoError(t, err) defer gitRepo.Close() @@ -71,7 +72,7 @@ func TestPullRequest_GetDefaultMergeMessage_ExternalTracker(t *testing.T) { pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2, BaseRepo: baseRepo}) assert.NoError(t, pr.LoadBaseRepo(db.DefaultContext)) - gitRepo, err := git.OpenRepository(git.DefaultContext, pr.BaseRepo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, pr.BaseRepo) assert.NoError(t, err) defer gitRepo.Close() diff --git a/services/pull/review.go b/services/pull/review.go index e48f3801547d..d4ea97561253 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -16,6 +16,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/util" @@ -170,7 +171,7 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo if err := pr.LoadBaseRepo(ctx); err != nil { return nil, fmt.Errorf("LoadBaseRepo: %w", err) } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, pr.BaseRepo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, pr.BaseRepo) if err != nil { return nil, fmt.Errorf("RepositoryFromContextOrOpen: %w", err) } diff --git a/services/release/release.go b/services/release/release.go index f17682ae0f8f..c1d13126f0b3 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -16,6 +16,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/repository" @@ -168,7 +169,7 @@ func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.R } } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return err } diff --git a/services/release/release_test.go b/services/release/release_test.go index 4b57262981b5..3d0681f1e171 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/services/attachment" _ "code.gitea.io/gitea/models/actions" @@ -29,9 +30,8 @@ func TestRelease_Create(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - repoPath := repo_model.RepoPath(user.Name, repo.Name) - gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() @@ -135,9 +135,8 @@ func TestRelease_Update(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - repoPath := repo_model.RepoPath(user.Name, repo.Name) - gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() @@ -278,9 +277,8 @@ func TestRelease_createTag(t *testing.T) { user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - repoPath := repo_model.RepoPath(user.Name, repo.Name) - gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 2e9b0c822f95..bfb965063f33 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -17,6 +17,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" @@ -126,7 +127,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r repo.IsEmpty = false // Don't bother looking this repo in the context it won't be there - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return fmt.Errorf("openRepository: %w", err) } diff --git a/services/repository/archiver/archiver.go b/services/repository/archiver/archiver.go index c2ad4d484a69..01c58f0ce402 100644 --- a/services/repository/archiver/archiver.go +++ b/services/repository/archiver/archiver.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -209,7 +210,7 @@ func doArchive(ctx context.Context, r *ArchiveRequest) (*repo_model.RepoArchiver return nil, fmt.Errorf("archiver.LoadRepo failed: %w", err) } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return nil, err } diff --git a/services/repository/branch.go b/services/repository/branch.go index c1e6625ed476..e2e50297afac 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -17,6 +17,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/queue" @@ -160,7 +161,7 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g if pr.HasMerged { baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID] if !ok { - baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath()) + baseGitRepo, err = gitrepo.OpenRepository(ctx, pr.BaseRepo) if err != nil { return nil, fmt.Errorf("OpenRepository: %v", err) } @@ -190,13 +191,9 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g }, nil } -func GetBranchCommitID(ctx context.Context, repo *repo_model.Repository, branch string) (string, error) { - return git.GetBranchCommitID(ctx, repo.RepoPath(), branch) -} - // checkBranchName validates branch name with existing repository branches func checkBranchName(ctx context.Context, repo *repo_model.Repository, name string) error { - _, err := git.WalkReferences(ctx, repo.RepoPath(), func(_, refName string) error { + _, err := gitrepo.WalkReferences(ctx, repo, func(_, refName string) error { branchRefName := strings.TrimPrefix(refName, git.BranchPrefix) switch { case branchRefName == name: diff --git a/services/repository/create.go b/services/repository/create.go index 0e89573343e6..a648c0d81678 100644 --- a/services/repository/create.go +++ b/services/repository/create.go @@ -16,6 +16,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/options" repo_module "code.gitea.io/gitea/modules/repository" @@ -175,7 +176,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re if len(opts.DefaultBranch) > 0 { repo.DefaultBranch = opts.DefaultBranch - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { return fmt.Errorf("openRepository: %w", err) } diff --git a/services/repository/files/commit.go b/services/repository/files/commit.go index 048e41e6fdd3..16a15e06a7cc 100644 --- a/services/repository/files/commit.go +++ b/services/repository/files/commit.go @@ -12,6 +12,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/services/automerge" ) @@ -23,7 +24,7 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato repoPath := repo.RepoPath() // confirm that commit is exist - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return fmt.Errorf("OpenRepository[%s]: %w", repoPath, err) } diff --git a/services/repository/files/content.go b/services/repository/files/content.go index 30d62fbcdf7c..c278d7f8355c 100644 --- a/services/repository/files/content.go +++ b/services/repository/files/content.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -58,7 +59,7 @@ func GetContentsOrList(ctx context.Context, repo *repo_model.Repository, treePat } treePath = cleanTreePath - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return nil, err } @@ -133,7 +134,7 @@ func GetContents(ctx context.Context, repo *repo_model.Repository, treePath, ref } treePath = cleanTreePath - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return nil, err } diff --git a/services/repository/files/content_test.go b/services/repository/files/content_test.go index 3ad3e3ab98b2..d50847789ace 100644 --- a/services/repository/files/content_test.go +++ b/services/repository/files/content_test.go @@ -6,10 +6,9 @@ package files import ( "testing" - repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/contexttest" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" api "code.gitea.io/gitea/modules/structs" _ "code.gitea.io/gitea/models/actions" @@ -235,7 +234,7 @@ func TestGetBlobBySHA(t *testing.T) { ctx.SetParams(":id", "1") ctx.SetParams(":sha", sha) - gitRepo, err := git.OpenRepository(ctx, repo_model.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)) + gitRepo, err := gitrepo.OpenRepository(ctx, ctx.Repo.Repository) if err != nil { t.Fail() } diff --git a/services/repository/files/file_test.go b/services/repository/files/file_test.go index 4e67ad141085..675ddbddb3e8 100644 --- a/services/repository/files/file_test.go +++ b/services/repository/files/file_test.go @@ -8,7 +8,7 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/contexttest" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -109,7 +109,7 @@ func TestGetFileResponseFromCommit(t *testing.T) { repo := ctx.Repo.Repository branch := repo.DefaultBranch treePath := "README.md" - gitRepo, _ := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(ctx, repo) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(branch) expectedFileResponse := getExpectedFileResponse() diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go index 14f8caaa8cf7..f6d5643dc91c 100644 --- a/services/repository/files/patch.go +++ b/services/repository/files/patch.go @@ -13,6 +13,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" asymkey_service "code.gitea.io/gitea/services/asymkey" @@ -42,7 +43,7 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode opts.NewBranch = opts.OldBranch } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return err } diff --git a/services/repository/files/update.go b/services/repository/files/update.go index 1892043304e2..f223daf3a9fd 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -16,6 +16,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -78,7 +79,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use opts.NewBranch = opts.OldBranch } - gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.RepoPath()) + gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) if err != nil { return nil, err } diff --git a/services/repository/fork.go b/services/repository/fork.go index a8ff2717b068..f9c13a109eba 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -14,6 +14,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/structs" @@ -167,7 +168,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork return fmt.Errorf("createDelegateHooks: %w", err) } - gitRepo, err := git.OpenRepository(txCtx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(txCtx, repo) if err != nil { return fmt.Errorf("OpenRepository: %w", err) } @@ -190,7 +191,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork log.Error("Copy language stat from oldRepo failed: %v", err) } - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { log.Error("Open created git repository failed: %v", err) } else { diff --git a/services/repository/hooks.go b/services/repository/hooks.go index 7b82f36b43fd..97e9e290a3e3 100644 --- a/services/repository/hooks.go +++ b/services/repository/hooks.go @@ -10,7 +10,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/webhook" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" @@ -52,13 +52,13 @@ func SyncRepositoryHooks(ctx context.Context) error { // GenerateGitHooks generates git hooks from a template repository func GenerateGitHooks(ctx context.Context, templateRepo, generateRepo *repo_model.Repository) error { - generateGitRepo, err := git.OpenRepository(ctx, generateRepo.RepoPath()) + generateGitRepo, err := gitrepo.OpenRepository(ctx, generateRepo) if err != nil { return err } defer generateGitRepo.Close() - templateGitRepo, err := git.OpenRepository(ctx, templateRepo.RepoPath()) + templateGitRepo, err := gitrepo.OpenRepository(ctx, templateRepo) if err != nil { return err } diff --git a/services/repository/lfs.go b/services/repository/lfs.go index b437fda15d57..4504f796bd2f 100644 --- a/services/repository/lfs.go +++ b/services/repository/lfs.go @@ -12,6 +12,7 @@ import ( git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" @@ -69,7 +70,7 @@ func GarbageCollectLFSMetaObjectsForRepo(ctx context.Context, repo *repo_model.R } }() - gitRepo, err := git.OpenRepository(ctx, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { log.Error("Unable to open git repository %-v: %v", repo, err) return err diff --git a/services/repository/push.go b/services/repository/push.go index e86eebde8110..bedcf6f2524b 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -16,6 +16,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/cache" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" @@ -86,17 +87,15 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { return fmt.Errorf("GetRepositoryByOwnerAndName failed: %w", err) } - repoPath := repo.RepoPath() - - gitRepo, err := git.OpenRepository(ctx, repoPath) + gitRepo, err := gitrepo.OpenRepository(ctx, repo) if err != nil { - return fmt.Errorf("OpenRepository[%s]: %w", repoPath, err) + return fmt.Errorf("OpenRepository[%s]: %w", repo.FullName(), err) } defer gitRepo.Close() objectFormat, err := gitRepo.GetObjectFormat() if err != nil { - return fmt.Errorf("unknown repository ObjectFormat [%s]: %w", repoPath, err) + return fmt.Errorf("unknown repository ObjectFormat [%s]: %w", repo.FullName(), err) } if err = repo_module.UpdateRepoSize(ctx, repo); err != nil { diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index ce54a00da73b..50d52d3140fe 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -191,7 +191,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model committer := doer.NewGitSig() - sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo.WikiPath(), doer) + sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo, doer) if sign { commitTreeOpts.KeyID = signingKey if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { @@ -314,7 +314,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model committer := doer.NewGitSig() - sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo.WikiPath(), doer) + sign, signingKey, signer, _ := asymkey_service.SignWikiCommit(ctx, repo, doer) if sign { commitTreeOpts.KeyID = signingKey if repo.GetTrustModel() == repo_model.CommitterTrustModel || repo.GetTrustModel() == repo_model.CollaboratorCommitterTrustModel { diff --git a/services/wiki/wiki_test.go b/services/wiki/wiki_test.go index 277fa086ac13..59c77060f2c2 100644 --- a/services/wiki/wiki_test.go +++ b/services/wiki/wiki_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" _ "code.gitea.io/gitea/models/actions" @@ -164,7 +165,7 @@ func TestRepository_AddWikiPage(t *testing.T) { webPath := UserTitleToWebPath("", userTitle) assert.NoError(t, AddWikiPage(git.DefaultContext, doer, repo, webPath, wikiContent, commitMsg)) // Now need to show that the page has been added: - gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) + gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo) if !assert.NoError(t, err) { return } @@ -212,7 +213,7 @@ func TestRepository_EditWikiPage(t *testing.T) { assert.NoError(t, EditWikiPage(git.DefaultContext, doer, repo, "Home", webPath, newWikiContent, commitMsg)) // Now need to show that the page has been added: - gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) + gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo) assert.NoError(t, err) masterTree, err := gitRepo.GetTree(DefaultBranch) assert.NoError(t, err) @@ -236,7 +237,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { assert.NoError(t, DeleteWikiPage(git.DefaultContext, doer, repo, "Home")) // Now need to show that the page has been added: - gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) + gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo) if !assert.NoError(t, err) { return } @@ -251,7 +252,7 @@ func TestRepository_DeleteWikiPage(t *testing.T) { func TestPrepareWikiFileName(t *testing.T) { unittest.PrepareTestEnv(t) repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) - gitRepo, err := git.OpenRepository(git.DefaultContext, repo.WikiPath()) + gitRepo, err := gitrepo.OpenWikiRepository(git.DefaultContext, repo) if !assert.NoError(t, err) { return } diff --git a/tests/integration/api_packages_cargo_test.go b/tests/integration/api_packages_cargo_test.go index 6b8154af457a..c0705e0de5cf 100644 --- a/tests/integration/api_packages_cargo_test.go +++ b/tests/integration/api_packages_cargo_test.go @@ -18,7 +18,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/json" cargo_module "code.gitea.io/gitea/modules/packages/cargo" "code.gitea.io/gitea/modules/setting" @@ -78,7 +78,7 @@ func testPackageCargo(t *testing.T, _ *neturl.URL) { assert.NoError(t, err) readGitContent := func(t *testing.T, path string) string { - gitRepo, err := git.OpenRepository(db.DefaultContext, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() diff --git a/tests/integration/api_releases_test.go b/tests/integration/api_releases_test.go index e070bd05b5a4..5b1ab76ce97b 100644 --- a/tests/integration/api_releases_test.go +++ b/tests/integration/api_releases_test.go @@ -18,6 +18,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/tests" @@ -109,7 +110,7 @@ func TestAPICreateAndUpdateRelease(t *testing.T) { session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) - gitRepo, err := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() @@ -172,7 +173,7 @@ func TestAPICreateReleaseToDefaultBranchOnExistingTag(t *testing.T) { session := loginUser(t, owner.LowerName) token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) - gitRepo, err := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() diff --git a/tests/integration/api_repo_file_create_test.go b/tests/integration/api_repo_file_create_test.go index f78909eb3258..0d192a1fe84a 100644 --- a/tests/integration/api_repo_file_create_test.go +++ b/tests/integration/api_repo_file_create_test.go @@ -18,7 +18,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -167,7 +167,7 @@ func TestAPICreateFile(t *testing.T) { req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath), &createFileOptions). AddTokenAuth(token2) resp := MakeRequest(t, req, http.StatusCreated) - gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1) commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) latestCommit, _ := gitRepo.GetCommitByPath(treePath) expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String()) @@ -285,7 +285,7 @@ func TestAPICreateFile(t *testing.T) { AddTokenAuth(token2) resp = MakeRequest(t, req, http.StatusCreated) emptyRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "empty-repo"}) // public repo - gitRepo, _ := git.OpenRepository(stdCtx.Background(), emptyRepo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), emptyRepo) commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) latestCommit, _ := gitRepo.GetCommitByPath(treePath) expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String()) diff --git a/tests/integration/api_repo_file_update_test.go b/tests/integration/api_repo_file_update_test.go index 7e88f6cd8036..195a1090c7c0 100644 --- a/tests/integration/api_repo_file_update_test.go +++ b/tests/integration/api_repo_file_update_test.go @@ -17,7 +17,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -135,7 +135,7 @@ func TestAPIUpdateFile(t *testing.T) { req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", user2.Name, repo1.Name, treePath), &updateFileOptions). AddTokenAuth(token2) resp := MakeRequest(t, req, http.StatusOK) - gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1) commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName) lasCommit, _ := gitRepo.GetCommitByPath(treePath) expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String()) diff --git a/tests/integration/api_repo_files_change_test.go b/tests/integration/api_repo_files_change_test.go index d500d48b3609..ab5cf19a9c88 100644 --- a/tests/integration/api_repo_files_change_test.go +++ b/tests/integration/api_repo_files_change_test.go @@ -16,7 +16,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" @@ -96,7 +96,7 @@ func TestAPIChangeFiles(t *testing.T) { req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/contents", user2.Name, repo1.Name), &changeFilesOptions). AddTokenAuth(token2) resp := MakeRequest(t, req, http.StatusCreated) - gitRepo, _ := git.OpenRepository(stdCtx.Background(), repo1.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1) commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName) createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath) updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath) diff --git a/tests/integration/api_repo_get_contents_list_test.go b/tests/integration/api_repo_get_contents_list_test.go index 86313f5e3b20..1ba74490a30d 100644 --- a/tests/integration/api_repo_get_contents_list_test.go +++ b/tests/integration/api_repo_get_contents_list_test.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" repo_service "code.gitea.io/gitea/services/repository" @@ -71,7 +72,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) { token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) // Get the commit ID of the default branch - gitRepo, err := git.OpenRepository(git.DefaultContext, repo1.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo1) assert.NoError(t, err) defer gitRepo.Close() diff --git a/tests/integration/api_repo_get_contents_test.go b/tests/integration/api_repo_get_contents_test.go index ffbdfcb0fab8..68a860811720 100644 --- a/tests/integration/api_repo_get_contents_test.go +++ b/tests/integration/api_repo_get_contents_test.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" repo_service "code.gitea.io/gitea/services/repository" @@ -73,7 +74,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) { token4 := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) // Get the commit ID of the default branch - gitRepo, err := git.OpenRepository(git.DefaultContext, repo1.RepoPath()) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo1) assert.NoError(t, err) defer gitRepo.Close() diff --git a/tests/integration/api_repo_git_tags_test.go b/tests/integration/api_repo_git_tags_test.go index 2e8510ab1b3a..937f6a829c2c 100644 --- a/tests/integration/api_repo_git_tags_test.go +++ b/tests/integration/api_repo_git_tags_test.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/tests" @@ -32,7 +33,7 @@ func TestAPIGitTags(t *testing.T) { _ = git.NewCommand(git.DefaultContext, "config", "user.name").AddDynamicArguments(user.Name).Run(&git.RunOpts{Dir: repo.RepoPath()}) _ = git.NewCommand(git.DefaultContext, "config", "user.email").AddDynamicArguments(user.Email).Run(&git.RunOpts{Dir: repo.RepoPath()}) - gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit("master") diff --git a/tests/integration/mirror_pull_test.go b/tests/integration/mirror_pull_test.go index 1e0edd9a2dd0..2e71b80fbb5d 100644 --- a/tests/integration/mirror_pull_test.go +++ b/tests/integration/mirror_pull_test.go @@ -12,6 +12,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/migration" "code.gitea.io/gitea/modules/repository" mirror_service "code.gitea.io/gitea/services/mirror" @@ -54,7 +55,7 @@ func TestMirrorPull(t *testing.T) { mirror, err := repository.MigrateRepositoryGitData(ctx, user, mirrorRepo, opts, nil) assert.NoError(t, err) - gitRepo, err := git.OpenRepository(git.DefaultContext, repoPath) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo) assert.NoError(t, err) defer gitRepo.Close() diff --git a/tests/integration/mirror_push_test.go b/tests/integration/mirror_push_test.go index c6f0c856163b..3dc719593c30 100644 --- a/tests/integration/mirror_push_test.go +++ b/tests/integration/mirror_push_test.go @@ -17,6 +17,7 @@ import ( user_model "code.gitea.io/gitea/models/user" gitea_context "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/services/migrations" mirror_service "code.gitea.io/gitea/services/mirror" @@ -55,14 +56,14 @@ func testMirrorPush(t *testing.T, u *url.URL) { ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID) assert.True(t, ok) - srcGitRepo, err := git.OpenRepository(git.DefaultContext, srcRepo.RepoPath()) + srcGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, srcRepo) assert.NoError(t, err) defer srcGitRepo.Close() srcCommit, err := srcGitRepo.GetBranchCommit("master") assert.NoError(t, err) - mirrorGitRepo, err := git.OpenRepository(git.DefaultContext, mirrorRepo.RepoPath()) + mirrorGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, mirrorRepo) assert.NoError(t, err) defer mirrorGitRepo.Close() diff --git a/tests/integration/pull_merge_test.go b/tests/integration/pull_merge_test.go index 2aa6742a56ac..fcd7fecd52dd 100644 --- a/tests/integration/pull_merge_test.go +++ b/tests/integration/pull_merge_test.go @@ -25,6 +25,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/translation" @@ -255,7 +256,7 @@ func TestCantMergeConflict(t *testing.T) { BaseBranch: "base", }) - gitRepo, err := git.OpenRepository(git.DefaultContext, repo_model.RepoPath(user1.Name, repo1.Name)) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo1) assert.NoError(t, err) err = pull.Merge(context.Background(), pr, user1, gitRepo, repo_model.MergeStyleMerge, "", "CONFLICT", false) @@ -348,7 +349,7 @@ func TestCantMergeUnrelated(t *testing.T) { session.MakeRequest(t, req, http.StatusCreated) // Now this PR could be marked conflict - or at least a race may occur - so drop down to pure code at this point... - gitRepo, err := git.OpenRepository(git.DefaultContext, path) + gitRepo, err := gitrepo.OpenRepository(git.DefaultContext, repo1) assert.NoError(t, err) pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ HeadRepoID: repo1.ID, diff --git a/tests/integration/repofiles_change_test.go b/tests/integration/repofiles_change_test.go index e0edf07299b5..19fbd1754cc2 100644 --- a/tests/integration/repofiles_change_test.go +++ b/tests/integration/repofiles_change_test.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/contexttest" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" files_service "code.gitea.io/gitea/services/repository/files" @@ -262,7 +263,7 @@ func TestChangeRepoFilesForCreate(t *testing.T) { // asserts assert.NoError(t, err) - gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) defer gitRepo.Close() commitID, _ := gitRepo.GetBranchCommitID(opts.NewBranch) @@ -299,7 +300,7 @@ func TestChangeRepoFilesForUpdate(t *testing.T) { // asserts assert.NoError(t, err) - gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(opts.NewBranch) @@ -335,7 +336,7 @@ func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) { // asserts assert.NoError(t, err) - gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(opts.NewBranch) @@ -386,7 +387,7 @@ func TestChangeRepoFilesWithoutBranchNames(t *testing.T) { // asserts assert.NoError(t, err) - gitRepo, _ := git.OpenRepository(git.DefaultContext, repo.RepoPath()) + gitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, repo) defer gitRepo.Close() commit, _ := gitRepo.GetBranchCommit(repo.DefaultBranch)