Some small refactors (#36163)

This commit is contained in:
Lunny Xiao
2025-12-15 11:55:44 -08:00
committed by GitHub
parent 26602fd207
commit da087270ff
6 changed files with 11 additions and 14 deletions

View File

@@ -109,8 +109,8 @@ func IsRepoDirExist(ctx context.Context, repo Repository, relativeDirPath string
return util.IsDir(absoluteDirPath) return util.IsDir(absoluteDirPath)
} }
func RemoveRepoFile(ctx context.Context, repo Repository, relativeFilePath string) error { func RemoveRepoFileOrDir(ctx context.Context, repo Repository, relativeFileOrDirPath string) error {
absoluteFilePath := filepath.Join(repoPath(repo), relativeFilePath) absoluteFilePath := filepath.Join(repoPath(repo), relativeFileOrDirPath)
return util.Remove(absoluteFilePath) return util.Remove(absoluteFilePath)
} }

View File

@@ -5,12 +5,10 @@ package doctor
import ( import (
"context" "context"
"os"
"path/filepath"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
) )
func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error { func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) error {
@@ -21,18 +19,18 @@ func checkOldArchives(ctx context.Context, logger log.Logger, autofix bool) erro
return nil return nil
} }
p := filepath.Join(repo.RepoPath(), "archives") isDir, err := gitrepo.IsRepoDirExist(ctx, repo, "archives")
isDir, err := util.IsDir(p)
if err != nil { if err != nil {
log.Warn("check if %s is directory failed: %v", p, err) log.Warn("check if %s is directory failed: %v", repo.FullName(), err)
} }
if isDir { if isDir {
numRepos++ numRepos++
if autofix { if autofix {
if err := os.RemoveAll(p); err == nil { err := gitrepo.RemoveRepoFileOrDir(ctx, repo, "archives")
if err == nil {
numReposUpdated++ numReposUpdated++
} else { } else {
log.Warn("remove %s failed: %v", p, err) log.Warn("remove %s failed: %v", repo.FullName(), err)
} }
} }
} }

View File

@@ -151,7 +151,7 @@ func checkDaemonExport(ctx context.Context, logger log.Logger, autofix bool) err
numNeedUpdate++ numNeedUpdate++
if autofix { if autofix {
if !isPublic && isExist { if !isPublic && isExist {
if err = gitrepo.RemoveRepoFile(ctx, repo, daemonExportFile); err != nil { if err = gitrepo.RemoveRepoFileOrDir(ctx, repo, daemonExportFile); err != nil {
log.Error("Failed to remove %s:%s: %v", repo.FullName(), daemonExportFile, err) log.Error("Failed to remove %s:%s: %v", repo.FullName(), daemonExportFile, err)
} }
} else if isPublic && !isExist { } else if isPublic && !isExist {

View File

@@ -33,7 +33,7 @@ func GetCompareInfo(ctx context.Context, baseRepo, headRepo *repo_model.Reposito
) )
// We don't need a temporary remote for same repository. // We don't need a temporary remote for same repository.
if headGitRepo.Path != baseRepo.RepoPath() { if baseRepo.ID != headRepo.ID {
// Add a temporary remote // Add a temporary remote
tmpRemote = strconv.FormatInt(time.Now().UnixNano(), 10) tmpRemote = strconv.FormatInt(time.Now().UnixNano(), 10)
if err = gitrepo.GitRemoteAdd(ctx, headRepo, tmpRemote, baseRepo.RepoPath()); err != nil { if err = gitrepo.GitRemoteAdd(ctx, headRepo, tmpRemote, baseRepo.RepoPath()); err != nil {

View File

@@ -526,7 +526,6 @@ func UpdateBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git
} }
pushOpts := git.PushOptions{ pushOpts := git.PushOptions{
Remote: repo.RepoPath(),
Branch: fmt.Sprintf("%s:%s%s", newCommit.ID.String(), git.BranchPrefix, branchName), Branch: fmt.Sprintf("%s:%s%s", newCommit.ID.String(), git.BranchPrefix, branchName),
Env: repo_module.PushingEnvironment(doer, repo), Env: repo_module.PushingEnvironment(doer, repo),
Force: isForcePush || force, Force: isForcePush || force,

View File

@@ -257,7 +257,7 @@ func CheckDaemonExportOK(ctx context.Context, repo *repo_model.Repository) error
isPublic := !repo.IsPrivate && repo.Owner.Visibility == structs.VisibleTypePublic isPublic := !repo.IsPrivate && repo.Owner.Visibility == structs.VisibleTypePublic
if !isPublic && isExist { if !isPublic && isExist {
if err = gitrepo.RemoveRepoFile(ctx, repo, daemonExportFile); err != nil { if err = gitrepo.RemoveRepoFileOrDir(ctx, repo, daemonExportFile); err != nil {
log.Error("Failed to remove %s: %v", daemonExportFile, err) log.Error("Failed to remove %s: %v", daemonExportFile, err)
} }
} else if isPublic && !isExist { } else if isPublic && !isExist {