From 496b8e39900842c8253250d3eaddf587be35dfa3 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 8 Jul 2022 16:09:07 +0800 Subject: [PATCH 1/2] Use git.HOME_PATH for Git HOME directory (#20114) * Add git.HOME_PATH * add legacy file check * Apply suggestions from code review Co-authored-by: zeripath * pass env GNUPGHOME to git command, move the existing .gitconfig to new home, make the fix for 1.17rc more clear. * set git.HOME_PATH for docker images to default HOME * Revert "set git.HOME_PATH for docker images to default HOME" This reverts commit f120101ddc267cef74e4f4b92c783d5fc8e275a1. * force Gitea to use a stable GNUPGHOME directory * extra check to ensure only process dir or symlink for legacy files * refactor variable name * The legacy dir check (for 1.17-rc1) could be removed with 1.18 release, since users should have upgraded from 1.17-rc to 1.17-stable * Update modules/git/git.go Co-authored-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com> * remove initFixGitHome117rc * Update git.go * Update docs/content/doc/advanced/config-cheat-sheet.en-us.md Co-authored-by: zeripath Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Steven Kriegler <61625851+justusbunsi@users.noreply.github.com> Co-authored-by: Lunny Xiao --- custom/conf/app.example.ini | 5 ++- .../doc/advanced/config-cheat-sheet.en-us.md | 2 ++ docs/content/doc/advanced/signing.en-us.md | 7 ++-- models/unittest/testdb.go | 2 ++ modules/git/command.go | 33 +++++++++++++------ modules/git/git.go | 22 ++++++++----- modules/git/git_test.go | 6 ++-- modules/setting/git.go | 13 +++++++- 8 files changed, 63 insertions(+), 27 deletions(-) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 927dc71166033..fb43ea95a1d44 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -603,7 +603,10 @@ ROUTER = console ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; The path of git executable. If empty, Gitea searches through the PATH environment. -PATH = +;PATH = +;; +;; The HOME directory for Git +;HOME_PATH = %(APP_DATA_PATH)/home ;; ;; Disables highlight of added and removed changes ;DISABLE_DIFF_HIGHLIGHT = false diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index c16a500ef3e82..84e3c6ae33df3 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -948,6 +948,8 @@ Default templates for project boards: ## Git (`git`) - `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment. +- `HOME_PATH`: **%(APP_DATA_PATH)/home**: The HOME directory for Git. + This directory will be used to contain the `.gitconfig` and possible `.gnupg` directories that Gitea's git calls will use. If you can confirm Gitea is the only application running in this environment, you can set it to the normal home directory for Gitea user. - `DISABLE_DIFF_HIGHLIGHT`: **false**: Disables highlight of added and removed changes. - `MAX_GIT_DIFF_LINES`: **1000**: Max number of lines allowed of a single file in diff view. - `MAX_GIT_DIFF_LINE_CHARACTERS`: **5000**: Max character count per line highlighted in diff view. diff --git a/docs/content/doc/advanced/signing.en-us.md b/docs/content/doc/advanced/signing.en-us.md index 8ae2f94e9ece9..9ef94deb75ce4 100644 --- a/docs/content/doc/advanced/signing.en-us.md +++ b/docs/content/doc/advanced/signing.en-us.md @@ -97,10 +97,11 @@ repositories, `SIGNING_KEY=default` could be used to provide different signing keys on a per-repository basis. However, this is clearly not an ideal UI and therefore subject to change. -**Since 1.17**, Gitea runs git in its own home directory `[repository].ROOT` and uses its own config `{[repository].ROOT}/.gitconfig`. +**Since 1.17**, Gitea runs git in its own home directory `[git].HOME_PATH` (default to `%(APP_DATA_PATH)/home`) +and uses its own config `{[git].HOME_PATH}/.gitconfig`. If you have your own customized git config for Gitea, you should set these configs in system git config (aka `/etc/gitconfig`) -or the Gitea internal git config `{[repository].ROOT}/.gitconfig`. -Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[repository].ROOT`. +or the Gitea internal git config `{[git].HOME_PATH}/.gitconfig`. +Related home files for git command (like `.gnupg`) should also be put in Gitea's git home directory `[git].HOME_PATH`. ### `INITIAL_COMMIT` diff --git a/models/unittest/testdb.go b/models/unittest/testdb.go index baea46dbce68f..7f9af553747cb 100644 --- a/models/unittest/testdb.go +++ b/models/unittest/testdb.go @@ -107,6 +107,8 @@ func MainTest(m *testing.M, testOpts *TestOptions) { setting.Packages.Storage.Path = filepath.Join(setting.AppDataPath, "packages") + setting.Git.HomePath = filepath.Join(setting.AppDataPath, "home") + if err = storage.Init(); err != nil { fatalTestError("storage.Init: %v\n", err) } diff --git a/modules/git/command.go b/modules/git/command.go index d71497f1d791e..a1bacbb707a25 100644 --- a/modules/git/command.go +++ b/modules/git/command.go @@ -105,23 +105,36 @@ type RunOpts struct { PipelineFunc func(context.Context, context.CancelFunc) error } -// CommonGitCmdEnvs returns the common environment variables for a "git" command. -func CommonGitCmdEnvs() []string { +func commonBaseEnvs() []string { // at the moment, do not set "GIT_CONFIG_NOSYSTEM", users may have put some configs like "receive.certNonceSeed" in it - return []string{ - fmt.Sprintf("LC_ALL=%s", DefaultLocale), - "GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3 - "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) + envs := []string{ "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config + "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) + } + + // some environment variables should be passed to git command + passThroughEnvKeys := []string{ + "GNUPGHOME", // git may call gnupg to do commit signing + } + for _, key := range passThroughEnvKeys { + if val, ok := os.LookupEnv(key); ok { + envs = append(envs, key+"="+val) + } } + return envs +} + +// CommonGitCmdEnvs returns the common environment variables for a "git" command. +func CommonGitCmdEnvs() []string { + return append(commonBaseEnvs(), []string{ + "LC_ALL=" + DefaultLocale, + "GIT_TERMINAL_PROMPT=0", // avoid prompting for credentials interactively, supported since git v2.3 + }...) } // CommonCmdServEnvs is like CommonGitCmdEnvs but it only returns minimal required environment variables for the "gitea serv" command func CommonCmdServEnvs() []string { - return []string{ - "GIT_NO_REPLACE_OBJECTS=1", // ignore replace references (https://git-scm.com/docs/git-replace) - "HOME=" + HomeDir(), // make Gitea use internal git config only, to prevent conflicts with user's git config - } + return commonBaseEnvs() } // Run runs the command with the RunOpts diff --git a/modules/git/git.go b/modules/git/git.go index 0652a75f9aeff..3bc08ff93b5fa 100644 --- a/modules/git/git.go +++ b/modules/git/git.go @@ -11,6 +11,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "regexp" "runtime" "strings" @@ -19,7 +20,6 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" - "github.com/hashicorp/go-version" ) @@ -126,8 +126,8 @@ func VersionInfo() string { } func checkInit() error { - if setting.RepoRootPath == "" { - return errors.New("can not init Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly") + if setting.Git.HomePath == "" { + return errors.New("unable to init Git's HomeDir, incorrect initialization of the setting and git modules") } if DefaultContext != nil { log.Warn("git module has been initialized already, duplicate init should be fixed") @@ -137,14 +137,14 @@ func checkInit() error { // HomeDir is the home dir for git to store the global config file used by Gitea internally func HomeDir() string { - if setting.RepoRootPath == "" { + if setting.Git.HomePath == "" { // strict check, make sure the git module is initialized correctly. // attention: when the git module is called in gitea sub-command (serv/hook), the log module is not able to show messages to users. // for example: if there is gitea git hook code calling git.NewCommand before git.InitXxx, the integration test won't show the real failure reasons. - log.Fatal("can not get Git's HomeDir (RepoRootPath is empty), the setting and git modules are not initialized correctly") + log.Fatal("Unable to init Git's HomeDir, incorrect initialization of the setting and git modules") return "" } - return setting.RepoRootPath + return setting.Git.HomePath } // InitSimple initializes git module with a very simple step, no config changes, no global command arguments. @@ -175,11 +175,15 @@ func InitOnceWithSync(ctx context.Context) (err error) { } initOnce.Do(func() { - err = InitSimple(ctx) - if err != nil { + if err = InitSimple(ctx); err != nil { return } + // when git works with gnupg (commit signing), there should be a stable home for gnupg commands + if _, ok := os.LookupEnv("GNUPGHOME"); !ok { + _ = os.Setenv("GNUPGHOME", filepath.Join(HomeDir(), ".gnupg")) + } + // Since git wire protocol has been released from git v2.18 if setting.Git.EnableAutoGitWireProtocol && CheckGitVersionAtLeast("2.18") == nil { globalCommandArgs = append(globalCommandArgs, "-c", "protocol.version=2") @@ -206,7 +210,7 @@ func InitOnceWithSync(ctx context.Context) (err error) { // syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem) func syncGitConfig() (err error) { if err = os.MkdirAll(HomeDir(), os.ModePerm); err != nil { - return fmt.Errorf("unable to create directory %s, err: %w", setting.RepoRootPath, err) + return fmt.Errorf("unable to prepare git home directory %s, err: %w", HomeDir(), err) } // Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults" diff --git a/modules/git/git_test.go b/modules/git/git_test.go index c1a9ec351aaa5..c5a63de0644c0 100644 --- a/modules/git/git_test.go +++ b/modules/git/git_test.go @@ -21,12 +21,12 @@ import ( func testRun(m *testing.M) error { _ = log.NewLogger(1000, "console", "console", `{"level":"trace","stacktracelevel":"NONE","stderr":true}`) - repoRootPath, err := os.MkdirTemp(os.TempDir(), "repos") + gitHomePath, err := os.MkdirTemp(os.TempDir(), "git-home") if err != nil { return fmt.Errorf("unable to create temp dir: %w", err) } - defer util.RemoveAll(repoRootPath) - setting.RepoRootPath = repoRootPath + defer util.RemoveAll(gitHomePath) + setting.Git.HomePath = gitHomePath if err = InitOnceWithSync(context.Background()); err != nil { return fmt.Errorf("failed to call Init: %w", err) diff --git a/modules/setting/git.go b/modules/setting/git.go index 9b2698f01e780..266bbc3c5aa3a 100644 --- a/modules/setting/git.go +++ b/modules/setting/git.go @@ -5,6 +5,7 @@ package setting import ( + "path/filepath" "time" "code.gitea.io/gitea/modules/log" @@ -13,6 +14,7 @@ import ( // Git settings var Git = struct { Path string + HomePath string DisableDiffHighlight bool MaxGitDiffLines int MaxGitDiffLineCharacters int @@ -67,7 +69,16 @@ var Git = struct { } func newGit() { - if err := Cfg.Section("git").MapTo(&Git); err != nil { + sec := Cfg.Section("git") + + if err := sec.MapTo(&Git); err != nil { log.Fatal("Failed to map Git settings: %v", err) } + + Git.HomePath = sec.Key("HOME_PATH").MustString("home") + if !filepath.IsAbs(Git.HomePath) { + Git.HomePath = filepath.Join(AppDataPath, Git.HomePath) + } else { + Git.HomePath = filepath.Clean(Git.HomePath) + } } From 49f9d43afefd446287b1b2475d7127d405b7a873 Mon Sep 17 00:00:00 2001 From: Chongyi Zheng Date: Fri, 8 Jul 2022 15:45:12 -0400 Subject: [PATCH 2/2] Implement sync push mirror on commit (#19411) Support synchronizing with the push mirrors whenever new commits are pushed or synced from pull mirror. Related Issues: #18220 Co-authored-by: delvh Co-authored-by: zeripath Co-authored-by: Lunny Xiao --- models/migrations/migrations.go | 2 + models/migrations/v219.go | 30 +++++++++ models/repo/pushmirror.go | 9 +++ modules/mirror/mirror.go | 69 +++++++++++++++++++++ modules/notification/mirror/mirror.go | 45 ++++++++++++++ modules/notification/notification.go | 2 + options/locale/locale_en-US.ini | 3 +- routers/api/v1/repo/mirror.go | 4 +- routers/web/repo/setting.go | 14 +++-- services/forms/repo_form.go | 35 +++++------ services/mirror/mirror.go | 87 +++++---------------------- templates/repo/settings/options.tmpl | 6 ++ 12 files changed, 208 insertions(+), 98 deletions(-) create mode 100644 models/migrations/v219.go create mode 100644 modules/mirror/mirror.go create mode 100644 modules/notification/mirror/mirror.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 0d35ac78d3ece..1b2a743b6d357 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -396,6 +396,8 @@ var migrations = []Migration{ NewMigration("Alter hook_task table TEXT fields to LONGTEXT", alterHookTaskTextFieldsToLongText), // v218 -> v219 NewMigration("Improve Action table indices v2", improveActionTableIndices), + // v219 -> v220 + NewMigration("Add sync_on_commit column to push_mirror table", addSyncOnCommitColForPushMirror), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v219.go b/models/migrations/v219.go new file mode 100644 index 0000000000000..7b2eaa3292704 --- /dev/null +++ b/models/migrations/v219.go @@ -0,0 +1,30 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "time" + + "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/timeutil" + "xorm.io/xorm" +) + +func addSyncOnCommitColForPushMirror(x *xorm.Engine) error { + type PushMirror struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + Repo *repo.Repository `xorm:"-"` + RemoteName string + + SyncOnCommit bool `xorm:"NOT NULL DEFAULT true"` + Interval time.Duration + CreatedUnix timeutil.TimeStamp `xorm:"created"` + LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"` + LastError string `xorm:"text"` + } + + return x.Sync2(new(PushMirror)) +} diff --git a/models/repo/pushmirror.go b/models/repo/pushmirror.go index 048c0c3487b75..0a7dea79c93b3 100644 --- a/models/repo/pushmirror.go +++ b/models/repo/pushmirror.go @@ -23,6 +23,7 @@ type PushMirror struct { Repo *Repository `xorm:"-"` RemoteName string + SyncOnCommit bool `xorm:"NOT NULL DEFAULT true"` Interval time.Duration CreatedUnix timeutil.TimeStamp `xorm:"created"` LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"` @@ -93,6 +94,14 @@ func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) { return mirrors, db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).Find(&mirrors) } +// GetPushMirrorsSyncedOnCommit returns push-mirrors for this repo that should be updated by new commits +func GetPushMirrorsSyncedOnCommit(repoID int64) ([]*PushMirror, error) { + mirrors := make([]*PushMirror, 0, 10) + return mirrors, db.GetEngine(db.DefaultContext). + Where("repo_id=? AND sync_on_commit=?", repoID, true). + Find(&mirrors) +} + // PushMirrorsIterate iterates all push-mirror repositories. func PushMirrorsIterate(limit int, f func(idx int, bean interface{}) error) error { return db.GetEngine(db.DefaultContext). diff --git a/modules/mirror/mirror.go b/modules/mirror/mirror.go new file mode 100644 index 0000000000000..b261bd0242634 --- /dev/null +++ b/modules/mirror/mirror.go @@ -0,0 +1,69 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mirror + +import ( + "code.gitea.io/gitea/modules/graceful" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/queue" + "code.gitea.io/gitea/modules/setting" +) + +var mirrorQueue queue.UniqueQueue + +// SyncType type of sync request +type SyncType int + +const ( + // PullMirrorType for pull mirrors + PullMirrorType SyncType = iota + // PushMirrorType for push mirrors + PushMirrorType +) + +// SyncRequest for the mirror queue +type SyncRequest struct { + Type SyncType + ReferenceID int64 // RepoID for pull mirror, MirrorID for push mirror +} + +// StartSyncMirrors starts a go routine to sync the mirrors +func StartSyncMirrors(queueHandle func(data ...queue.Data) []queue.Data) { + if !setting.Mirror.Enabled { + return + } + mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest)) + + go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run) +} + +// AddPullMirrorToQueue adds repoID to mirror queue +func AddPullMirrorToQueue(repoID int64) { + addMirrorToQueue(PullMirrorType, repoID) +} + +// AddPushMirrorToQueue adds the push mirror to the queue +func AddPushMirrorToQueue(mirrorID int64) { + addMirrorToQueue(PushMirrorType, mirrorID) +} + +func addMirrorToQueue(syncType SyncType, referenceID int64) { + if !setting.Mirror.Enabled { + return + } + go func() { + if err := PushToQueue(syncType, referenceID); err != nil { + log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]. Error: %v", referenceID, err) + } + }() +} + +// PushToQueue adds the sync request to the queue +func PushToQueue(mirrorType SyncType, referenceID int64) error { + return mirrorQueue.Push(&SyncRequest{ + Type: mirrorType, + ReferenceID: referenceID, + }) +} diff --git a/modules/notification/mirror/mirror.go b/modules/notification/mirror/mirror.go new file mode 100644 index 0000000000000..646b09a4ab6e6 --- /dev/null +++ b/modules/notification/mirror/mirror.go @@ -0,0 +1,45 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package mirror + +import ( + repo_model "code.gitea.io/gitea/models/repo" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/log" + mirror_module "code.gitea.io/gitea/modules/mirror" + "code.gitea.io/gitea/modules/notification/base" + "code.gitea.io/gitea/modules/repository" +) + +type mirrorNotifier struct { + base.NullNotifier +} + +var _ base.Notifier = &mirrorNotifier{} + +// NewNotifier create a new mirrorNotifier notifier +func NewNotifier() base.Notifier { + return &mirrorNotifier{} +} + +func (m *mirrorNotifier) NotifyPushCommits(_ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) { + syncPushMirrorWithSyncOnCommit(repo.ID) +} + +func (m *mirrorNotifier) NotifySyncPushCommits(_ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) { + syncPushMirrorWithSyncOnCommit(repo.ID) +} + +func syncPushMirrorWithSyncOnCommit(repoID int64) { + pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(repoID) + if err != nil { + log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err) + return + } + + for _, mirror := range pushMirrors { + mirror_module.AddPushMirrorToQueue(mirror.ID) + } +} diff --git a/modules/notification/notification.go b/modules/notification/notification.go index d60a880bec6a2..bdfed90b7864e 100644 --- a/modules/notification/notification.go +++ b/modules/notification/notification.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/notification/base" "code.gitea.io/gitea/modules/notification/indexer" "code.gitea.io/gitea/modules/notification/mail" + "code.gitea.io/gitea/modules/notification/mirror" "code.gitea.io/gitea/modules/notification/ui" "code.gitea.io/gitea/modules/notification/webhook" "code.gitea.io/gitea/modules/repository" @@ -37,6 +38,7 @@ func NewContext() { RegisterNotifier(indexer.NewNotifier()) RegisterNotifier(webhook.NewNotifier()) RegisterNotifier(action.NewNotifier()) + RegisterNotifier(mirror.NewNotifier()) } // NotifyCreateIssueComment notifies issue comment related message to notifiers diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index eb7ae4774313b..464a7d396c0aa 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -861,8 +861,9 @@ default_branch = Default Branch default_branch_helper = The default branch is the base branch for pull requests and code commits. mirror_prune = Prune mirror_prune_desc = Remove obsolete remote-tracking references -mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable automatic sync. (Minimum interval: %s) +mirror_interval = Mirror Interval (valid time units are 'h', 'm', 's'). 0 to disable periodic sync. (Minimum interval: %s) mirror_interval_invalid = The mirror interval is not valid. +mirror_sync_on_commit = Sync when commits are pushed mirror_address = Clone From URL mirror_address_desc = Put any required credentials in the Authorization section. mirror_address_url_invalid = The provided url is invalid. You must escape all components of the url correctly. diff --git a/routers/api/v1/repo/mirror.go b/routers/api/v1/repo/mirror.go index 1af63c55be01a..3d29383550c32 100644 --- a/routers/api/v1/repo/mirror.go +++ b/routers/api/v1/repo/mirror.go @@ -11,8 +11,8 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/context" + mirror_module "code.gitea.io/gitea/modules/mirror" "code.gitea.io/gitea/modules/setting" - mirror_service "code.gitea.io/gitea/services/mirror" ) // MirrorSync adds a mirrored repository to the sync queue @@ -59,7 +59,7 @@ func MirrorSync(ctx *context.APIContext) { return } - mirror_service.StartToMirror(repo.ID) + mirror_module.AddPullMirrorToQueue(repo.ID) ctx.Status(http.StatusOK) } diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index fae62c102077c..5ded0561c65b5 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -29,6 +29,7 @@ import ( "code.gitea.io/gitea/modules/indexer/stats" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" + mirror_module "code.gitea.io/gitea/modules/mirror" "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" @@ -272,7 +273,7 @@ func SettingsPost(ctx *context.Context) { return } - mirror_service.StartToMirror(repo.ID) + mirror_module.AddPullMirrorToQueue(repo.ID) ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress")) ctx.Redirect(repo.Link() + "/settings") @@ -289,7 +290,7 @@ func SettingsPost(ctx *context.Context) { return } - mirror_service.AddPushMirrorToQueue(m.ID) + mirror_module.AddPushMirrorToQueue(m.ID) ctx.Flash.Info(ctx.Tr("repo.settings.mirror_sync_in_progress")) ctx.Redirect(repo.Link() + "/settings") @@ -357,10 +358,11 @@ func SettingsPost(ctx *context.Context) { } m := &repo_model.PushMirror{ - RepoID: repo.ID, - Repo: repo, - RemoteName: fmt.Sprintf("remote_mirror_%s", remoteSuffix), - Interval: interval, + RepoID: repo.ID, + Repo: repo, + RemoteName: fmt.Sprintf("remote_mirror_%s", remoteSuffix), + SyncOnCommit: form.PushMirrorSyncOnCommit, + Interval: interval, } if err := repo_model.InsertPushMirror(m); err != nil { ctx.ServerError("InsertPushMirror", err) diff --git a/services/forms/repo_form.go b/services/forms/repo_form.go index c9327bbd9b0f8..afecc205f31e8 100644 --- a/services/forms/repo_form.go +++ b/services/forms/repo_form.go @@ -115,23 +115,24 @@ func ParseRemoteAddr(remoteAddr, authUsername, authPassword string) (string, err // RepoSettingForm form for changing repository settings type RepoSettingForm struct { - RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` - Description string `binding:"MaxSize(255)"` - Website string `binding:"ValidUrl;MaxSize(255)"` - Interval string - MirrorAddress string - MirrorUsername string - MirrorPassword string - LFS bool `form:"mirror_lfs"` - LFSEndpoint string `form:"mirror_lfs_endpoint"` - PushMirrorID string - PushMirrorAddress string - PushMirrorUsername string - PushMirrorPassword string - PushMirrorInterval string - Private bool - Template bool - EnablePrune bool + RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"` + Description string `binding:"MaxSize(255)"` + Website string `binding:"ValidUrl;MaxSize(255)"` + Interval string + MirrorAddress string + MirrorUsername string + MirrorPassword string + LFS bool `form:"mirror_lfs"` + LFSEndpoint string `form:"mirror_lfs_endpoint"` + PushMirrorID string + PushMirrorAddress string + PushMirrorUsername string + PushMirrorPassword string + PushMirrorSyncOnCommit bool + PushMirrorInterval string + Private bool + Template bool + EnablePrune bool // Advanced settings EnableWiki bool diff --git a/services/mirror/mirror.go b/services/mirror/mirror.go index 013adac0f4ed1..8321829ad26ba 100644 --- a/services/mirror/mirror.go +++ b/services/mirror/mirror.go @@ -11,38 +11,21 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" + mirror_module "code.gitea.io/gitea/modules/mirror" "code.gitea.io/gitea/modules/queue" "code.gitea.io/gitea/modules/setting" ) -var mirrorQueue queue.UniqueQueue - -// SyncType type of sync request -type SyncType int - -const ( - // PullMirrorType for pull mirrors - PullMirrorType SyncType = iota - // PushMirrorType for push mirrors - PushMirrorType -) - -// SyncRequest for the mirror queue -type SyncRequest struct { - Type SyncType - ReferenceID int64 // RepoID for pull mirror, MirrorID fro push mirror -} - // doMirrorSync causes this request to mirror itself -func doMirrorSync(ctx context.Context, req *SyncRequest) { +func doMirrorSync(ctx context.Context, req *mirror_module.SyncRequest) { if req.ReferenceID == 0 { log.Warn("Skipping mirror sync request, no mirror ID was specified") return } switch req.Type { - case PushMirrorType: + case mirror_module.PushMirrorType: _ = SyncPushMirror(ctx, req.ReferenceID) - case PullMirrorType: + case mirror_module.PullMirrorType: _ = SyncPullMirror(ctx, req.ReferenceID) default: log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID) @@ -60,28 +43,26 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error { log.Trace("Doing: Update") handler := func(idx int, bean interface{}) error { - var item SyncRequest var repo *repo_model.Repository + var mirrorType mirror_module.SyncType + var referenceID int64 + if m, ok := bean.(*repo_model.Mirror); ok { if m.GetRepository() == nil { log.Error("Disconnected mirror found: %d", m.ID) return nil } repo = m.Repo - item = SyncRequest{ - Type: PullMirrorType, - ReferenceID: m.RepoID, - } + mirrorType = mirror_module.PullMirrorType + referenceID = m.RepoID } else if m, ok := bean.(*repo_model.PushMirror); ok { if m.GetRepository() == nil { log.Error("Disconnected push-mirror found: %d", m.ID) return nil } repo = m.Repo - item = SyncRequest{ - Type: PushMirrorType, - ReferenceID: m.ID, - } + mirrorType = mirror_module.PushMirrorType + referenceID = m.ID } else { log.Error("Unknown bean: %v", bean) return nil @@ -95,9 +76,9 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error { } // Push to the Queue - if err := mirrorQueue.Push(&item); err != nil { + if err := mirror_module.PushToQueue(mirrorType, referenceID); err != nil { if err == queue.ErrAlreadyInQueue { - if item.Type == PushMirrorType { + if mirrorType == mirror_module.PushMirrorType { log.Trace("PushMirrors for %-v already queued for sync", repo) } else { log.Trace("PullMirrors for %-v already queued for sync", repo) @@ -142,7 +123,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error { func queueHandle(data ...queue.Data) []queue.Data { for _, datum := range data { - req := datum.(*SyncRequest) + req := datum.(*mirror_module.SyncRequest) doMirrorSync(graceful.GetManager().ShutdownContext(), req) } return nil @@ -150,43 +131,5 @@ func queueHandle(data ...queue.Data) []queue.Data { // InitSyncMirrors initializes a go routine to sync the mirrors func InitSyncMirrors() { - if !setting.Mirror.Enabled { - return - } - mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest)) - - go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run) -} - -// StartToMirror adds repoID to mirror queue -func StartToMirror(repoID int64) { - if !setting.Mirror.Enabled { - return - } - go func() { - err := mirrorQueue.Push(&SyncRequest{ - Type: PullMirrorType, - ReferenceID: repoID, - }) - if err != nil { - log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]: Error: %v", repoID, err) - return - } - }() -} - -// AddPushMirrorToQueue adds the push mirror to the queue -func AddPushMirrorToQueue(mirrorID int64) { - if !setting.Mirror.Enabled { - return - } - go func() { - err := mirrorQueue.Push(&SyncRequest{ - Type: PushMirrorType, - ReferenceID: mirrorID, - }) - if err != nil { - log.Error("Unable to push sync request to the queue for pull mirror repo[%d]: Error: %v", mirrorID, err) - } - }() + mirror_module.StartSyncMirrors(queueHandle) } diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index e76aba761a28d..98cf4f88c870f 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -219,6 +219,12 @@ +
+
+ + +
+