mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-04 06:07:40 +00:00
refactor: make git package handle all git operations (#38543)
before: gitrepo vs git packages after: git package fully handle all git operations by the way, use `WithRepo(repo)` instead of `WithDir(repo.Path)` to hide path details. benefits: 1. remove all unnecessary wrappers, developers no need to struggle with "which package should be used" 2. simplify code, RepositoryFacade can (will) be used everywhere, all "path" details are (will be) hidden
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package git
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gitea.dev/modules/git/gitcmd"
|
||||
"gitea.dev/modules/util"
|
||||
)
|
||||
|
||||
// IsRepositoryExist returns true if the repository directory exists in the disk
|
||||
func IsRepositoryExist(ctx context.Context, repo RepositoryFacade) (bool, error) {
|
||||
return util.IsExist(gitcmd.RepoLocalPath(repo))
|
||||
}
|
||||
|
||||
// DeleteRepository deletes the repository directory from the disk, it will return
|
||||
// nil if the repository does not exist.
|
||||
func DeleteRepository(ctx context.Context, repo RepositoryFacade) error {
|
||||
return util.RemoveAll(gitcmd.RepoLocalPath(repo))
|
||||
}
|
||||
|
||||
// RenameRepository renames a repository's name on disk
|
||||
func RenameRepository(ctx context.Context, repo, newRepo RepositoryFacade) error {
|
||||
dstDir := gitcmd.RepoLocalPath(newRepo)
|
||||
if err := os.MkdirAll(filepath.Dir(dstDir), os.ModePerm); err != nil {
|
||||
return fmt.Errorf("Failed to create dir %s: %w", filepath.Dir(dstDir), err)
|
||||
}
|
||||
|
||||
if err := util.Rename(gitcmd.RepoLocalPath(repo), dstDir); err != nil {
|
||||
return fmt.Errorf("rename repository directory: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func InitRepository(ctx context.Context, repo RepositoryFacade, objectFormatName string) error {
|
||||
return InitRepositoryLocal(ctx, gitcmd.RepoLocalPath(repo), true, objectFormatName)
|
||||
}
|
||||
|
||||
func GetRepoFS(repo RepositoryFacade) fs.FS {
|
||||
return os.DirFS(gitcmd.RepoLocalPath(repo))
|
||||
}
|
||||
|
||||
func IsRepoFileExist(ctx context.Context, repo RepositoryFacade, relativeFilePath string) (bool, error) {
|
||||
absoluteFilePath := filepath.Join(gitcmd.RepoLocalPath(repo), relativeFilePath)
|
||||
return util.IsExist(absoluteFilePath)
|
||||
}
|
||||
|
||||
func IsRepoDirExist(ctx context.Context, repo RepositoryFacade, relativeDirPath string) (bool, error) {
|
||||
absoluteDirPath := filepath.Join(gitcmd.RepoLocalPath(repo), relativeDirPath)
|
||||
return util.IsDir(absoluteDirPath)
|
||||
}
|
||||
|
||||
func RemoveRepoFileOrDir(ctx context.Context, repo RepositoryFacade, relativeFileOrDirPath string) error {
|
||||
absoluteFilePath := filepath.Join(gitcmd.RepoLocalPath(repo), relativeFileOrDirPath)
|
||||
return util.Remove(absoluteFilePath)
|
||||
}
|
||||
|
||||
func CreateRepoFile(ctx context.Context, repo RepositoryFacade, relativeFilePath string) (io.WriteCloser, error) {
|
||||
absoluteFilePath := filepath.Join(gitcmd.RepoLocalPath(repo), relativeFilePath)
|
||||
if err := os.MkdirAll(filepath.Dir(absoluteFilePath), os.ModePerm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return os.Create(absoluteFilePath)
|
||||
}
|
||||
Reference in New Issue
Block a user