Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify how git repositories are opened #28937

Merged
merged 22 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions modules/gitrepo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ package gitrepo
import (
"context"

repo_model "code.gitea.io/gitea/models/repo"
"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 *repo_model.Repository, skip, limit int) ([]*git.Branch, int, error) {
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
Expand All @@ -22,7 +21,7 @@ func GetBranchesByPath(ctx context.Context, repo *repo_model.Repository, skip, l
return gitRepo.GetBranches(skip, limit)
}

func GetBranchCommitID(ctx context.Context, repo *repo_model.Repository, branch string) (string, error) {
func GetBranchCommitID(ctx context.Context, repo Repository, branch string) (string, error) {
gitRepo, err := OpenRepository(ctx, repo)
if err != nil {
return "", err
Expand Down
40 changes: 21 additions & 19 deletions modules/gitrepo/gitrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ package gitrepo

import (
"context"
"fmt"
"io"
"path/filepath"
"strings"

repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/setting"
)

type Repository = git.Repository
type Repository interface {
FullName() string
lunny marked this conversation as resolved.
Show resolved Hide resolved
}

// OpenRepository opens the repository at the given relative path with the provided context.
func OpenRepository(ctx context.Context, repo *repo_model.Repository) (*Repository, error) {
return git.OpenRepository(ctx, repo.RepoPath())
func repoPath(fullName string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(fullName)+".git")
}

func OpenWikiRepository(ctx context.Context, repo *repo_model.Repository) (*Repository, error) {
return git.OpenRepository(ctx, repo.WikiPath())
func wikiPath(fullName string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(fullName)+".wiki.git")
}

// DeleteRepository deletes the repository at the given relative path with the provided context.
func DeleteRepository(ctx context.Context, repo *repo_model.Repository) error {
if err := util.RemoveAll(repo.RepoPath()); err != nil {
return fmt.Errorf("failed to remove %s: %w", repo.FullName(), err)
}
return nil
// 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.FullName()))
}

func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
return git.OpenRepository(ctx, wikiPath(repo.FullName()))
}

// contextKey is a value for use with context.WithValue.
Expand All @@ -41,14 +43,14 @@ type contextKey struct {
var RepositoryContextKey = &contextKey{"repository"}

// RepositoryFromContext attempts to get the repository from the context
func repositoryFromContext(ctx context.Context, repo *repo_model.Repository) *Repository {
func repositoryFromContext(ctx context.Context, repo Repository) *git.Repository {
value := ctx.Value(RepositoryContextKey)
if value == nil {
return nil
}

if gitRepo, ok := value.(*Repository); ok && gitRepo != nil {
if gitRepo.Path == repo.RepoPath() {
if gitRepo, ok := value.(*git.Repository); ok && gitRepo != nil {
if gitRepo.Path == repoPath(repo.FullName()) {
return gitRepo
}
}
Expand All @@ -61,7 +63,7 @@ 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 *repo_model.Repository) (*Repository, io.Closer, error) {
func RepositoryFromContextOrOpen(ctx context.Context, repo Repository) (*git.Repository, io.Closer, error) {
gitRepo := repositoryFromContext(ctx, repo)
if gitRepo != nil {
return gitRepo, nopCloser(nil), nil
Expand Down
4 changes: 1 addition & 3 deletions modules/gitrepo/walk_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ package gitrepo
import (
"context"

repo_model "code.gitea.io/gitea/models/repo"

"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 *repo_model.Repository, walkfn func(sha1, refname string) error) (int, error) {
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
Expand Down
5 changes: 2 additions & 3 deletions modules/gitrepo/walk_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ package gitrepo
import (
"context"

repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
)

// WalkReferences walks all the references from the repository
func WalkReferences(ctx context.Context, repo *repo_model.Repository, walkfn func(sha1, refname string) error) (int, error) {
return git.WalkShowRef(ctx, repo.RepoPath(), nil, 0, 0, walkfn)
func WalkReferences(ctx context.Context, repo Repository, walkfn func(sha1, refname string) error) (int, error) {
return git.WalkShowRef(ctx, repoPath(repo.FullName()), nil, 0, 0, walkfn)
}
Loading