2020-12-26 22:34:19 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-26 22:34:19 -05:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-05-10 03:57:45 -04:00
|
|
|
"errors"
|
|
|
|
"net/http"
|
2022-07-01 03:47:44 -04:00
|
|
|
"strings"
|
2020-12-26 22:34:19 -05:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-05-10 03:57:45 -04:00
|
|
|
"code.gitea.io/gitea/modules/private"
|
2020-12-26 22:34:19 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CmdRestoreRepository represents the available restore a repository sub-command.
|
|
|
|
var CmdRestoreRepository = cli.Command{
|
|
|
|
Name: "restore-repo",
|
|
|
|
Usage: "Restore the repository from disk",
|
|
|
|
Description: "This is a command for restoring the repository data.",
|
|
|
|
Action: runRestoreRepository,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "repo_dir, r",
|
|
|
|
Value: "./data",
|
|
|
|
Usage: "Repository dir path to restore from",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "owner_name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Restore destination owner name",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "repo_name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Restore destination repository name",
|
|
|
|
},
|
2022-07-01 03:47:44 -04:00
|
|
|
cli.StringFlag{
|
2020-12-26 22:34:19 -05:00
|
|
|
Name: "units",
|
2022-07-01 03:47:44 -04:00
|
|
|
Value: "",
|
|
|
|
Usage: `Which items will be restored, one or more units should be separated as comma.
|
2020-12-26 22:34:19 -05:00
|
|
|
wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`,
|
|
|
|
},
|
2022-01-26 04:45:51 -05:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "validation",
|
|
|
|
Usage: "Sanity check the content of the files before trying to load them",
|
|
|
|
},
|
2020-12-26 22:34:19 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:43:13 -04:00
|
|
|
func runRestoreRepository(c *cli.Context) error {
|
|
|
|
ctx, cancel := installSignals()
|
|
|
|
defer cancel()
|
|
|
|
|
2023-02-19 11:12:01 -05:00
|
|
|
setting.InitProviderFromExistingFile()
|
|
|
|
setting.LoadCommonSettings()
|
2022-07-01 03:47:44 -04:00
|
|
|
var units []string
|
|
|
|
if s := c.String("units"); s != "" {
|
|
|
|
units = strings.Split(s, ",")
|
|
|
|
}
|
2021-05-10 03:57:45 -04:00
|
|
|
statusCode, errStr := private.RestoreRepo(
|
2021-07-14 10:43:13 -04:00
|
|
|
ctx,
|
|
|
|
c.String("repo_dir"),
|
|
|
|
c.String("owner_name"),
|
|
|
|
c.String("repo_name"),
|
2022-07-01 03:47:44 -04:00
|
|
|
units,
|
2022-01-26 04:45:51 -05:00
|
|
|
c.Bool("validation"),
|
2021-05-10 03:57:45 -04:00
|
|
|
)
|
|
|
|
if statusCode == http.StatusOK {
|
|
|
|
return nil
|
2020-12-26 22:34:19 -05:00
|
|
|
}
|
|
|
|
|
2021-05-10 03:57:45 -04:00
|
|
|
log.Fatal("Failed to restore repository: %v", errStr)
|
|
|
|
return errors.New(errStr)
|
2020-12-26 22:34:19 -05:00
|
|
|
}
|