Skip to content

make sure repo_dir is empty before 'dump-repo' #20205

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

Merged
merged 12 commits into from
Jul 14, 2022
Prev Previous commit
Next Next commit
Update dump_repo.go
  • Loading branch information
wxiaoguang authored Jul 13, 2022
commit 896de908d12e8f36fe4a6a270ac6f0f54e0bcb6b
15 changes: 12 additions & 3 deletions cmd/dump_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cmd
import (
"context"
"errors"
"fmt"
"os"
"strings"

Expand All @@ -16,6 +17,7 @@ import (
base "code.gitea.io/gitea/modules/migration"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/migrations"

"github.com/urfave/cli"
Expand Down Expand Up @@ -161,13 +163,20 @@ func runDumpRepository(ctx *cli.Context) error {
}

repoDir := ctx.String("repo_dir")
if dir, _ := os.ReadDir(repoDir); len(dir) > 0 {
return errors.New("`repo_dir` path '" + repoDir + "' already exists and is not an empty directory.")
if exists, err := util.IsExist(repoDir); err != nil {
return fmt.Errorf("unable to stat repo_dir %q: %v", repoDir, err)
} else if exists {
if isDir, _ := util.IsDir(repoDir); !isDir {
return fmt.Errorf("repo_dir %q already exists and is not a directory", repoDir)
}
if dir, _ := os.ReadDir(repoDir); len(dir) > 0 {
return fmt.Errorf("repo_dir %q is not empty", repoDir)
}
}

if err := migrations.DumpRepository(
context.Background(),
repoDir,
ctx.String("repo_dir"),
ctx.String("owner_name"),
opts,
); err != nil {
Expand Down