|
| 1 | +// Copyright 2020 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | +"context" |
| 9 | +"errors" |
| 10 | +"strings" |
| 11 | + |
| 12 | +"code.gitea.io/gitea/modules/convert" |
| 13 | +"code.gitea.io/gitea/modules/log" |
| 14 | +"code.gitea.io/gitea/modules/migrations" |
| 15 | +"code.gitea.io/gitea/modules/migrations/base" |
| 16 | +"code.gitea.io/gitea/modules/setting" |
| 17 | +"code.gitea.io/gitea/modules/structs" |
| 18 | + |
| 19 | +"github.com/urfave/cli" |
| 20 | +) |
| 21 | + |
| 22 | +// CmdDumpRepository represents the available dump repository sub-command. |
| 23 | +varCmdDumpRepository= cli.Command{ |
| 24 | +Name: "dump-repo", |
| 25 | +Usage: "Dump the repository from git/github/gitea/gitlab", |
| 26 | +Description: "This is a command for dumping the repository data.", |
| 27 | +Action: runDumpRepository, |
| 28 | +Flags: []cli.Flag{ |
| 29 | + cli.StringFlag{ |
| 30 | +Name: "git_service", |
| 31 | +Value: "", |
| 32 | +Usage: "Git service, git, github, gitea, gitlab. If clone_addr could be recognized, this could be ignored.", |
| 33 | + }, |
| 34 | + cli.StringFlag{ |
| 35 | +Name: "repo_dir, r", |
| 36 | +Value: "./data", |
| 37 | +Usage: "Repository dir path to store the data", |
| 38 | + }, |
| 39 | + cli.StringFlag{ |
| 40 | +Name: "clone_addr", |
| 41 | +Value: "", |
| 42 | +Usage: "The URL will be clone, currently could be a git/github/gitea/gitlab http/https URL", |
| 43 | + }, |
| 44 | + cli.StringFlag{ |
| 45 | +Name: "auth_username", |
| 46 | +Value: "", |
| 47 | +Usage: "The username to visit the clone_addr", |
| 48 | + }, |
| 49 | + cli.StringFlag{ |
| 50 | +Name: "auth_password", |
| 51 | +Value: "", |
| 52 | +Usage: "The password to visit the clone_addr", |
| 53 | + }, |
| 54 | + cli.StringFlag{ |
| 55 | +Name: "auth_token", |
| 56 | +Value: "", |
| 57 | +Usage: "The personal token to visit the clone_addr", |
| 58 | + }, |
| 59 | + cli.StringFlag{ |
| 60 | +Name: "owner_name", |
| 61 | +Value: "", |
| 62 | +Usage: "The data will be stored on a directory with owner name if not empty", |
| 63 | + }, |
| 64 | + cli.StringFlag{ |
| 65 | +Name: "repo_name", |
| 66 | +Value: "", |
| 67 | +Usage: "The data will be stored on a directory with repository name if not empty", |
| 68 | + }, |
| 69 | + cli.StringFlag{ |
| 70 | +Name: "units", |
| 71 | +Value: "", |
| 72 | +Usage: `Which items will be migrated, one or more units should be separated as comma. |
| 73 | +wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`, |
| 74 | + }, |
| 75 | + }, |
| 76 | +} |
| 77 | + |
| 78 | +funcrunDumpRepository(ctx*cli.Context) error{ |
| 79 | +iferr:=initDB(); err!=nil{ |
| 80 | +returnerr |
| 81 | + } |
| 82 | + |
| 83 | +log.Trace("AppPath: %s", setting.AppPath) |
| 84 | +log.Trace("AppWorkPath: %s", setting.AppWorkPath) |
| 85 | +log.Trace("Custom path: %s", setting.CustomPath) |
| 86 | +log.Trace("Log path: %s", setting.LogRootPath) |
| 87 | +setting.InitDBConfig() |
| 88 | + |
| 89 | +var ( |
| 90 | +serviceType structs.GitServiceType |
| 91 | +cloneAddr=ctx.String("clone_addr") |
| 92 | +serviceStr=ctx.String("git_service") |
| 93 | + ) |
| 94 | + |
| 95 | +ifstrings.HasPrefix(strings.ToLower(cloneAddr), "https://github.com/"){ |
| 96 | +serviceStr="github" |
| 97 | + } elseifstrings.HasPrefix(strings.ToLower(cloneAddr), "https://gitlab.com/"){ |
| 98 | +serviceStr="gitlab" |
| 99 | + } elseifstrings.HasPrefix(strings.ToLower(cloneAddr), "https://gitea.com/"){ |
| 100 | +serviceStr="gitea" |
| 101 | + } |
| 102 | +ifserviceStr==""{ |
| 103 | +returnerrors.New("git_service missed or clone_addr cannot be recognized") |
| 104 | + } |
| 105 | +serviceType=convert.ToGitServiceType(serviceStr) |
| 106 | + |
| 107 | +varopts= base.MigrateOptions{ |
| 108 | +GitServiceType: serviceType, |
| 109 | +CloneAddr: cloneAddr, |
| 110 | +AuthUsername: ctx.String("auth_username"), |
| 111 | +AuthPassword: ctx.String("auth_password"), |
| 112 | +AuthToken: ctx.String("auth_token"), |
| 113 | +RepoName: ctx.String("repo_name"), |
| 114 | + } |
| 115 | + |
| 116 | +iflen(ctx.String("units")) ==0{ |
| 117 | +opts.Wiki=true |
| 118 | +opts.Issues=true |
| 119 | +opts.Milestones=true |
| 120 | +opts.Labels=true |
| 121 | +opts.Releases=true |
| 122 | +opts.Comments=true |
| 123 | +opts.PullRequests=true |
| 124 | +opts.ReleaseAssets=true |
| 125 | + } else{ |
| 126 | +units:=strings.Split(ctx.String("units"), ",") |
| 127 | +for_, unit:=rangeunits{ |
| 128 | +switchstrings.ToLower(unit){ |
| 129 | +case"wiki": |
| 130 | +opts.Wiki=true |
| 131 | +case"issues": |
| 132 | +opts.Issues=true |
| 133 | +case"milestones": |
| 134 | +opts.Milestones=true |
| 135 | +case"labels": |
| 136 | +opts.Labels=true |
| 137 | +case"releases": |
| 138 | +opts.Releases=true |
| 139 | +case"release_assets": |
| 140 | +opts.ReleaseAssets=true |
| 141 | +case"comments": |
| 142 | +opts.Comments=true |
| 143 | +case"pull_requests": |
| 144 | +opts.PullRequests=true |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +iferr:=migrations.DumpRepository( |
| 150 | +context.Background(), |
| 151 | +ctx.String("repo_dir"), |
| 152 | +ctx.String("owner_name"), |
| 153 | +opts, |
| 154 | + ); err!=nil{ |
| 155 | +log.Fatal("Failed to dump repository: %v", err) |
| 156 | +returnerr |
| 157 | + } |
| 158 | + |
| 159 | +log.Trace("Dump finished!!!") |
| 160 | + |
| 161 | +returnnil |
| 162 | +} |
0 commit comments