2020-12-27 11:34:19 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-12-27 11:34:19 +08:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-07-14 21:52:18 +08:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-12-27 11:34:19 +08:00
|
|
|
"strings"
|
|
|
|
|
2025-03-27 19:40:14 +00:00
|
|
|
"forgejo.org/modules/git"
|
|
|
|
"forgejo.org/modules/log"
|
|
|
|
base "forgejo.org/modules/migration"
|
|
|
|
"forgejo.org/modules/setting"
|
|
|
|
"forgejo.org/modules/structs"
|
|
|
|
"forgejo.org/modules/util"
|
|
|
|
"forgejo.org/services/convert"
|
|
|
|
"forgejo.org/services/migrations"
|
2020-12-27 11:34:19 +08:00
|
|
|
|
2025-06-01 22:16:37 +02:00
|
|
|
"github.com/urfave/cli/v3"
|
2020-12-27 11:34:19 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// CmdDumpRepository represents the available dump repository sub-command.
|
2025-06-01 22:16:37 +02:00
|
|
|
func cmdDumpRepository() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "dump-repo",
|
|
|
|
Usage: "Dump the repository from git/github/gitea/gitlab",
|
|
|
|
Description: "This is a command for dumping the repository data.",
|
|
|
|
Action: runDumpRepository,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "git_service",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Git service, git, github, gitea, gitlab. If clone_addr could be recognized, this could be ignored.",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo_dir",
|
|
|
|
Aliases: []string{"r"},
|
|
|
|
Value: "./data",
|
|
|
|
Usage: "Repository dir path to store the data",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "clone_addr",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The URL will be clone, currently could be a git/github/gitea/gitlab http/https URL",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "auth_username",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The username to visit the clone_addr",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "auth_password",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The password to visit the clone_addr",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "auth_token",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The personal token to visit the clone_addr",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "owner_name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The data will be stored on a directory with owner name if not empty",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "repo_name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The data will be stored on a directory with repository name if not empty",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "units",
|
|
|
|
Value: "",
|
|
|
|
Usage: `Which items will be migrated, one or more units should be separated as comma.
|
2020-12-27 11:34:19 +08:00
|
|
|
wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`,
|
2025-06-01 22:16:37 +02:00
|
|
|
},
|
2020-12-27 11:34:19 +08:00
|
|
|
},
|
2025-06-01 22:16:37 +02:00
|
|
|
}
|
2020-12-27 11:34:19 +08:00
|
|
|
}
|
|
|
|
|
2025-06-01 22:16:37 +02:00
|
|
|
func runDumpRepository(stdCtx context.Context, ctx *cli.Command) error {
|
[gitea] week 2025-19 cherry pick (gitea/main -> forgejo) (#7909)
## Checklist
- [x] go to the last cherry-pick PR (forgejo/forgejo#7804) to figure out how far it went: [gitea@a2024953c5](https://github.com/go-gitea/gitea/commit/a2024953c5914c5a7d59d236262f9bd94b65b996)
- [x] cherry-pick and open PR (forgejo/forgejo#7909)
- [ ] have the PR pass the CI
- end-to-end (specially important if there are actions related changes)
- [ ] add `run-end-to-end` label
- [ ] check the result
- [ ] write release notes
- [ ] assign reviewers
- [ ] 48h later, last call
- merge 1 hour after the last call
## Legend
- :question: - No decision about the commit has been made.
- :cherries: - The commit has been cherry picked.
- :fast_forward: - The commit has been skipped.
- :bulb: - The commit has been skipped, but should be ported to Forgejo.
- :writing_hand: - The commit has been skipped, and a port to Forgejo already exists.
## Commits
- :cherries: [`gitea`](https://github.com/go-gitea/gitea/commit/e92c4f18083ed312b69591ebb77e0f504ee77025) -> [`forgejo`](https://codeberg.org/forgejo/forgejo/commit/56fa2caef32c4b0e5017f4b09188ad1dfc8d3603) Add missing setting load in dump-repo command ([gitea#34479](https://github.com/go-gitea/gitea/pull/34479))
- :cherries: [`gitea`](https://github.com/go-gitea/gitea/commit/7b518bc6c79035a53c0b752680d833fce5e1a2fe) -> [`forgejo`](https://codeberg.org/forgejo/forgejo/commit/6e5299606a1bd42cb45ed472a84ba797cf2fa790) Change "rejected" to "changes requested" in 3rd party PR review notification ([gitea#34481](https://github.com/go-gitea/gitea/pull/34481))
## TODO
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/972381097c6b3488bf9d5c3c4fde04775b2e3a3c) Fix url validation in webhook add/edit API ([gitea#34492](https://github.com/go-gitea/gitea/pull/34492))
Relevant input validation but test needs more backport.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/59df03b5542f05a1a927996fbf6483f7da363e03) Fix get / delete runner to use consistent http 404 and 500 status ([gitea#34480](https://github.com/go-gitea/gitea/pull/34480))
It may be relevant to Forgejo as well
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/1e2f3514b9afd903e19a0413e5d925515e13abf8) Add endpoint deleting workflow run ([gitea#34337](https://github.com/go-gitea/gitea/pull/34337))
Actions, it would be worth having in Forgejo as well.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/5cb4cbf044e2f0483afc92516bb4b9aff6ea2b9a) Fix repo broken check ([gitea#34444](https://github.com/go-gitea/gitea/pull/34444))
Check wether this is relevant to us, port if yes.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/355e9a9d544aa2d3f3a17b06cdb2bf1ceb290fd7) Add a webhook push test for dev branch ([gitea#34421](https://github.com/go-gitea/gitea/pull/34421))
Enhances webhook integration tests.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/34281bc198a5ad9a1faa5285d4648b05d7218aaa) Fix bug webhook milestone is not right. ([gitea#34419](https://github.com/go-gitea/gitea/pull/34419))
Testcode diverged, port required.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/780e92ea99646dfefbe11734a4845fbf304be83c) Only git operations should update `last changed` of a repository ([gitea#34388](https://github.com/go-gitea/gitea/pull/34388))
Port required, would benefit from additional tests.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/b07e03956af8f29464067b19cb5cacee358b592f) When updating comment, if the content is the same, just return and not update the databse ([gitea#34422](https://github.com/go-gitea/gitea/pull/34422))
Codebase diverged, port required.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/71a11872091634f1370374ef123d32798ec0447d) Fix incorrect divergence cache after switching default branch ([gitea#34370](https://github.com/go-gitea/gitea/pull/34370))
Depends on previous gitea changes, port needed.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/4c611bf280c501c22c6a58e94d9e3ce6a73214df) Add a button editing action secret ([gitea#34348](https://github.com/go-gitea/gitea/pull/34348))
This is an interesting feature and it has tests as well. Feature request covering this: https://codeberg.org/forgejo/forgejo/issues/7882
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/2fbc8f9e87fc37f21997bf32d9b29fc16e92780c) Fix LFS file not stored in LFS when uploaded/edited via API or web UI ([gitea#34367](https://github.com/go-gitea/gitea/pull/34367))
Our code diverged - pls. check relevance & maybe port.
------
- :bulb: [`gitea`](https://github.com/go-gitea/gitea/commit/020e774b915512815637aac743ddbe595c5eede5) feat: add label 'state' to metric 'gitea_users' ([gitea#34326](https://github.com/go-gitea/gitea/pull/34326))
Adjust our existing tests while porting this.
------
## Skipped
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/ec10c6ba5a6c4c3a5ab9bdf47616d6058c6fb59c) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/d89eed998f9e4dc84d0ef02451703590d7a8ef51) Fix edithook api can not update package, status and workflow_job events ([gitea#34495](https://github.com/go-gitea/gitea/pull/34495))
- gitea actions specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/b6c066747400927407a6b4807165d2de40c7495b) Add R-HNF to the TRANSLATORS file ([gitea#34494](https://github.com/go-gitea/gitea/pull/34494))
- gitea translators update specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/6fbf0e67383dd2970d8e6c309ebc5c634732866c) nix flake update ([gitea#34476](https://github.com/go-gitea/gitea/pull/34476))
- gitea dependency update specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/c24f4b3d2912224164ee3a75850a6a31bdd041f1) Add migrations tests ([gitea#34456](https://github.com/go-gitea/gitea/pull/34456))
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/bf338bb9e231a8f9ccef7de2e13a0fdf871fc680) Fix project board view ([gitea#34470](https://github.com/go-gitea/gitea/pull/34470))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/319d03fbc049de34a6fa0bc3019107ec5b724ff2) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/dd500ce5598848e4f50999b627155ec8520932d3) Fix Workflow run Not Found page ([gitea#34459](https://github.com/go-gitea/gitea/pull/34459))
- gitea actions specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/b6bf128f1e1a943df85c1ce276d0317c8689ffca) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/a0595add72db4a5fb421579b9c6bb7dae1392c86) Fix remove org user failure on mssql ([gitea#34449](https://github.com/go-gitea/gitea/pull/34449))
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/b5fd3e7210cfbcb87015f3a5b8c3f25eab2a5715) Fix comment textarea scroll issue in Firefox ([gitea#34438](https://github.com/go-gitea/gitea/pull/34438))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/4011e2245bcd96f53077f73b7a33b1a754f7151f) Fix releases sidebar navigation link ([gitea#34436](https://github.com/go-gitea/gitea/pull/34436))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/0902d42fc753cd5f266046f003307285fe9507d5) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/4a98ab05403ef1900937487d434bc075812b0303) Remove legacy template helper functions ([gitea#34426](https://github.com/go-gitea/gitea/pull/34426))
- gitea specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/9b8609e017aef8376eb59d9fd3e428e35f9caeda) Fix GetUsersByEmails ([gitea#34423](https://github.com/go-gitea/gitea/pull/34423))
- gitea specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/0f63a5ef48b23c6ab26a4b13cfd26edbe4efbfa3) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/ad271444e912ddf44591451292b39b0d6b859955) Fix a bug when uploading file via lfs ssh command ([gitea#34408](https://github.com/go-gitea/gitea/pull/34408))
:skiP: present with PR #7752
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/8b16ab719cab24805beb2189af7ee960ca94d524) Merge and tweak markup editor expander CSS ([gitea#34409](https://github.com/go-gitea/gitea/pull/34409))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/2ecd73d2e586cd4ff2001246d54c4affe0e1ccec) Bump `@github/relative-time-element` to v4.4.8 ([gitea#34413](https://github.com/go-gitea/gitea/pull/34413))
- gitea dependency update specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/179068fddbb463f3a34162730649d82acec522d3) Refactor commit message rendering and fix bugs ([gitea#34412](https://github.com/go-gitea/gitea/pull/34412))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/44aadc37c9c0810f3a41189929ae21c613b6bc98) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/f63822fe64b0759dc7e38b467eaa7c41b71d8c5d) Fix autofocus behavior ([gitea#34397](https://github.com/go-gitea/gitea/pull/34397))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/82071ee7300d478f56519ec30be0213b18a7882c) [skip ci] Updated translations via Crowdin
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/bbfc21e74f69a9b1ac83271923210c731edd2873) Fix "The sidebar of the repository file list does not have a fixed height #34298" ([gitea#34321](https://github.com/go-gitea/gitea/pull/34321))
- gitea ui specific specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/dd886d729f6206ad878aa5e0f0f3d4d20ea9a208) Update JS and PY dependencies ([gitea#34391](https://github.com/go-gitea/gitea/pull/34391))
- gitea dependency update specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/2a660b4a1b6913f80981d8840b3dc129a4eb7f26) Upgrade go-github v61 -> v71 ([gitea#34385](https://github.com/go-gitea/gitea/pull/34385))
- gitea dependency update specific
------
- :fast_forward: [`gitea`](https://github.com/go-gitea/gitea/commit/6bd8fe53537172fb4df976962115f1b928bf2993) Bump `@github/relative-time-element` to v4.4.7 ([gitea#34384](https://github.com/go-gitea/gitea/pull/34384))
- gitea dependency update specific
------
<details>
<summary><h2>Stats</h2></summary>
<br>
Between [`gitea@a2024953c5`](https://github.com/go-gitea/gitea/commit/a2024953c5914c5a7d59d236262f9bd94b65b996) and [`gitea@ec10c6ba5a`](https://github.com/go-gitea/gitea/commit/ec10c6ba5a6c4c3a5ab9bdf47616d6058c6fb59c), **41** commits have been reviewed. We picked **2**, skipped **27**, and decided to port **12**.
</details>
Co-authored-by: Sebastian Weigand <s.weigand.phy@gmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7909
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
Co-committed-by: Michael Jerger <michael.jerger@meissa-gmbh.de>
2025-06-27 13:59:07 +02:00
|
|
|
setupConsoleLogger(log.INFO, log.CanColorStderr, os.Stderr)
|
|
|
|
|
|
|
|
// setting.DisableLoggerInit()
|
|
|
|
setting.LoadSettings() // cannot access skip_tls_verify settings otherwise
|
|
|
|
|
2025-06-01 22:16:37 +02:00
|
|
|
stdCtx, cancel := installSignals(stdCtx)
|
2021-11-07 11:11:27 +08:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
if err := initDB(stdCtx); err != nil {
|
2020-12-27 11:34:19 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-01 07:48:25 +08:00
|
|
|
// migrations.GiteaLocalUploader depends on git module
|
|
|
|
if err := git.InitSimple(context.Background()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-27 01:56:58 +01:00
|
|
|
log.Info("AppPath: %s", setting.AppPath)
|
|
|
|
log.Info("AppWorkPath: %s", setting.AppWorkPath)
|
|
|
|
log.Info("Custom path: %s", setting.CustomPath)
|
2023-02-20 00:12:01 +08:00
|
|
|
log.Info("Log path: %s", setting.Log.RootPath)
|
2021-09-14 02:24:57 +01:00
|
|
|
log.Info("Configuration file: %s", setting.CustomConf)
|
2020-12-27 11:34:19 +08:00
|
|
|
|
|
|
|
var (
|
|
|
|
serviceType structs.GitServiceType
|
|
|
|
cloneAddr = ctx.String("clone_addr")
|
|
|
|
serviceStr = ctx.String("git_service")
|
|
|
|
)
|
|
|
|
|
|
|
|
if strings.HasPrefix(strings.ToLower(cloneAddr), "https://github.com/") {
|
|
|
|
serviceStr = "github"
|
|
|
|
} else if strings.HasPrefix(strings.ToLower(cloneAddr), "https://gitlab.com/") {
|
|
|
|
serviceStr = "gitlab"
|
|
|
|
} else if strings.HasPrefix(strings.ToLower(cloneAddr), "https://gitea.com/") {
|
|
|
|
serviceStr = "gitea"
|
|
|
|
}
|
|
|
|
if serviceStr == "" {
|
|
|
|
return errors.New("git_service missed or clone_addr cannot be recognized")
|
|
|
|
}
|
|
|
|
serviceType = convert.ToGitServiceType(serviceStr)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
opts := base.MigrateOptions{
|
2020-12-27 11:34:19 +08:00
|
|
|
GitServiceType: serviceType,
|
|
|
|
CloneAddr: cloneAddr,
|
|
|
|
AuthUsername: ctx.String("auth_username"),
|
|
|
|
AuthPassword: ctx.String("auth_password"),
|
|
|
|
AuthToken: ctx.String("auth_token"),
|
|
|
|
RepoName: ctx.String("repo_name"),
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.String("units")) == 0 {
|
|
|
|
opts.Wiki = true
|
|
|
|
opts.Issues = true
|
|
|
|
opts.Milestones = true
|
|
|
|
opts.Labels = true
|
|
|
|
opts.Releases = true
|
|
|
|
opts.Comments = true
|
|
|
|
opts.PullRequests = true
|
|
|
|
opts.ReleaseAssets = true
|
|
|
|
} else {
|
|
|
|
units := strings.Split(ctx.String("units"), ",")
|
|
|
|
for _, unit := range units {
|
2022-07-01 15:47:44 +08:00
|
|
|
switch strings.ToLower(strings.TrimSpace(unit)) {
|
|
|
|
case "":
|
|
|
|
continue
|
2020-12-27 11:34:19 +08:00
|
|
|
case "wiki":
|
|
|
|
opts.Wiki = true
|
|
|
|
case "issues":
|
|
|
|
opts.Issues = true
|
|
|
|
case "milestones":
|
|
|
|
opts.Milestones = true
|
|
|
|
case "labels":
|
|
|
|
opts.Labels = true
|
|
|
|
case "releases":
|
|
|
|
opts.Releases = true
|
|
|
|
case "release_assets":
|
|
|
|
opts.ReleaseAssets = true
|
|
|
|
case "comments":
|
|
|
|
opts.Comments = true
|
|
|
|
case "pull_requests":
|
|
|
|
opts.PullRequests = true
|
2022-07-01 15:47:44 +08:00
|
|
|
default:
|
|
|
|
return errors.New("invalid unit: " + unit)
|
2020-12-27 11:34:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-14 21:52:18 +08:00
|
|
|
// the repo_dir will be removed if error occurs in DumpRepository
|
|
|
|
// make sure the directory doesn't exist or is empty, prevent from deleting user files
|
|
|
|
repoDir := ctx.String("repo_dir")
|
|
|
|
if exists, err := util.IsExist(repoDir); err != nil {
|
2022-10-24 21:29:17 +02:00
|
|
|
return fmt.Errorf("unable to stat repo_dir %q: %w", repoDir, err)
|
2022-07-14 21:52:18 +08:00
|
|
|
} else if exists {
|
|
|
|
if isDir, _ := util.IsDir(repoDir); !isDir {
|
|
|
|
return fmt.Errorf("repo_dir %q already exists but it's not a directory", repoDir)
|
|
|
|
}
|
|
|
|
if dir, _ := os.ReadDir(repoDir); len(dir) > 0 {
|
|
|
|
return fmt.Errorf("repo_dir %q is not empty", repoDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 11:34:19 +08:00
|
|
|
if err := migrations.DumpRepository(
|
|
|
|
context.Background(),
|
2022-07-14 21:52:18 +08:00
|
|
|
repoDir,
|
2020-12-27 11:34:19 +08:00
|
|
|
ctx.String("owner_name"),
|
|
|
|
opts,
|
|
|
|
); err != nil {
|
|
|
|
log.Fatal("Failed to dump repository: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Dump finished!!!")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|