初始提交: Gitea 项目代码
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/queue"
|
||||
"gitea.dev/modules/setting"
|
||||
)
|
||||
|
||||
// doMirrorSync causes this request to mirror itself
|
||||
func doMirrorSync(ctx context.Context, req *SyncRequest) {
|
||||
if req.ReferenceID == 0 {
|
||||
log.Warn("Skipping mirror sync request, no mirror ID was specified")
|
||||
return
|
||||
}
|
||||
switch req.Type {
|
||||
case PushMirrorType:
|
||||
_ = SyncPushMirror(ctx, req.ReferenceID)
|
||||
case PullMirrorType:
|
||||
_ = SyncPullMirror(ctx, req.ReferenceID)
|
||||
default:
|
||||
log.Error("Unknown Request type in queue: %v for MirrorID[%d]", req.Type, req.ReferenceID)
|
||||
}
|
||||
}
|
||||
|
||||
var errLimit = errors.New("reached limit")
|
||||
|
||||
// Update checks and updates mirror repositories.
|
||||
func Update(ctx context.Context, pullLimit, pushLimit int) error {
|
||||
if !setting.Mirror.Enabled {
|
||||
log.Warn("Mirror feature disabled, but cron job enabled: skip update")
|
||||
return nil
|
||||
}
|
||||
log.Trace("Doing: Update")
|
||||
|
||||
handler := func(bean any) error {
|
||||
var repo *repo_model.Repository
|
||||
var mirrorType SyncType
|
||||
var referenceID int64
|
||||
|
||||
if m, ok := bean.(*repo_model.Mirror); ok {
|
||||
if m.GetRepository(ctx) == nil {
|
||||
log.Error("Disconnected mirror found: %d", m.ID)
|
||||
return nil
|
||||
}
|
||||
repo = m.Repo
|
||||
mirrorType = PullMirrorType
|
||||
referenceID = m.RepoID
|
||||
} else if m, ok := bean.(*repo_model.PushMirror); ok {
|
||||
if m.GetRepository(ctx) == nil {
|
||||
log.Error("Disconnected push-mirror found: %d", m.ID)
|
||||
return nil
|
||||
}
|
||||
repo = m.Repo
|
||||
mirrorType = PushMirrorType
|
||||
referenceID = m.ID
|
||||
} else {
|
||||
log.Error("Unknown bean: %v", bean)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Check we've not been cancelled
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return errors.New("aborted")
|
||||
default:
|
||||
}
|
||||
|
||||
// Push to the Queue
|
||||
if err := PushToQueue(mirrorType, referenceID); err != nil {
|
||||
if err == queue.ErrAlreadyInQueue {
|
||||
if mirrorType == PushMirrorType {
|
||||
log.Trace("PushMirrors for %-v already queued for sync", repo)
|
||||
} else {
|
||||
log.Trace("PullMirrors for %-v already queued for sync", repo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
pullMirrorsRequested := 0
|
||||
if pullLimit != 0 {
|
||||
if err := repo_model.MirrorsIterate(ctx, pullLimit, func(_ int, bean any) error {
|
||||
if err := handler(bean); err != nil {
|
||||
return err
|
||||
}
|
||||
pullMirrorsRequested++
|
||||
return nil
|
||||
}); err != nil && err != errLimit {
|
||||
log.Error("MirrorsIterate: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
pushMirrorsRequested := 0
|
||||
if pushLimit != 0 {
|
||||
if err := repo_model.PushMirrorsIterate(ctx, pushLimit, func(idx int, bean any) error {
|
||||
if err := handler(bean); err != nil {
|
||||
return err
|
||||
}
|
||||
pushMirrorsRequested++
|
||||
return nil
|
||||
}); err != nil && err != errLimit {
|
||||
log.Error("PushMirrorsIterate: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
log.Trace("Finished: Update: %d pull mirrors and %d push mirrors queued", pullMirrorsRequested, pushMirrorsRequested)
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitSyncMirrors initializes a go routine to sync the mirrors
|
||||
func InitSyncMirrors() {
|
||||
StartSyncMirrors()
|
||||
}
|
||||
@@ -0,0 +1,484 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
repo_model "gitea.dev/models/repo"
|
||||
system_model "gitea.dev/models/system"
|
||||
"gitea.dev/modules/cache"
|
||||
"gitea.dev/modules/git"
|
||||
"gitea.dev/modules/git/gitcmd"
|
||||
giturl "gitea.dev/modules/git/url"
|
||||
"gitea.dev/modules/gitrepo"
|
||||
"gitea.dev/modules/globallock"
|
||||
"gitea.dev/modules/lfs"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/process"
|
||||
"gitea.dev/modules/proxy"
|
||||
repo_module "gitea.dev/modules/repository"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/timeutil"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/services/migrations"
|
||||
notify_service "gitea.dev/services/notify"
|
||||
repo_service "gitea.dev/services/repository"
|
||||
)
|
||||
|
||||
// UpdateAddress writes new address to Git repository and database
|
||||
func UpdateAddress(ctx context.Context, m *repo_model.Mirror, addr string) error {
|
||||
u, err := giturl.ParseGitURL(addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid addr: %v", err)
|
||||
}
|
||||
|
||||
remoteName := m.GetRemoteName()
|
||||
repo := m.GetRepository(ctx)
|
||||
// Remove old remote
|
||||
err = gitrepo.GitRemoteRemove(ctx, repo, remoteName)
|
||||
if err != nil && !git.IsRemoteNotExistError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
err = gitrepo.GitRemoteAdd(ctx, repo, remoteName, addr, gitrepo.RemoteOptionMirrorFetch)
|
||||
if err != nil && !git.IsRemoteNotExistError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if repo_service.HasWiki(ctx, m.Repo) {
|
||||
wikiRemotePath := repo_module.WikiRemoteURL(ctx, addr)
|
||||
// Remove old remote of wiki
|
||||
err = gitrepo.GitRemoteRemove(ctx, repo.WikiStorageRepo(), remoteName)
|
||||
if err != nil && !git.IsRemoteNotExistError(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
err = gitrepo.GitRemoteAdd(ctx, repo.WikiStorageRepo(), remoteName, wikiRemotePath, gitrepo.RemoteOptionMirrorFetch)
|
||||
if err != nil && !git.IsRemoteNotExistError(err) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// erase authentication before storing in database
|
||||
u.User = nil
|
||||
m.Repo.OriginalURL = u.String()
|
||||
return repo_model.UpdateRepositoryColsNoAutoTime(ctx, m.Repo, "original_url")
|
||||
}
|
||||
|
||||
func pruneBrokenReferences(ctx context.Context, m *repo_model.Mirror, gitRepo gitrepo.Repository, timeout time.Duration) error {
|
||||
cmd := gitcmd.NewCommand("remote", "prune").AddDynamicArguments(m.GetRemoteName()).WithTimeout(timeout)
|
||||
stdout, _, pruneErr := gitrepo.RunCmdString(ctx, gitRepo, cmd)
|
||||
if pruneErr != nil {
|
||||
// sanitize the output, since it may contain the remote address, which may contain a password
|
||||
stderrMessage := util.SanitizeCredentialURLs(pruneErr.Stderr())
|
||||
stdoutMessage := util.SanitizeCredentialURLs(stdout)
|
||||
|
||||
log.Error("Failed to prune mirror repository %s references:\nStdout: %s\nStderr: %s\nErr: %v", gitRepo.RelativePath(), stdoutMessage, stderrMessage, pruneErr)
|
||||
desc := fmt.Sprintf("Failed to prune mirror repository (%s) references: %s", m.Repo.FullName(), stderrMessage)
|
||||
if err := system_model.CreateRepositoryNotice(desc); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
}
|
||||
return pruneErr
|
||||
}
|
||||
|
||||
// checkRecoverableSyncError takes an error message from a git fetch command and returns false if it should be a fatal/blocking error
|
||||
func checkRecoverableSyncError(stderrMessage string) bool {
|
||||
switch {
|
||||
case strings.Contains(stderrMessage, "unable to resolve reference") && strings.Contains(stderrMessage, "reference broken"):
|
||||
return true
|
||||
case strings.Contains(stderrMessage, "remote error") && strings.Contains(stderrMessage, "not our ref"):
|
||||
return true
|
||||
case strings.Contains(stderrMessage, "cannot lock ref") && strings.Contains(stderrMessage, "but expected"):
|
||||
return true
|
||||
case strings.Contains(stderrMessage, "cannot lock ref") && strings.Contains(stderrMessage, "unable to resolve reference"):
|
||||
return true
|
||||
case strings.Contains(stderrMessage, "Unable to create") && strings.Contains(stderrMessage, ".lock"):
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// runSync returns true if sync finished without error.
|
||||
func runSync(ctx context.Context, m *repo_model.Mirror) ([]*repo_module.SyncResult, bool) {
|
||||
log.Trace("SyncMirrors [repo: %-v]: running git remote update...", m.Repo)
|
||||
|
||||
remoteURL, remoteErr := gitrepo.GitRemoteGetURL(ctx, m.Repo, m.GetRemoteName())
|
||||
if remoteErr != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: GetRemoteURL Error %v", m.Repo, remoteErr)
|
||||
return nil, false
|
||||
}
|
||||
envs := proxy.EnvWithProxy(remoteURL.URL)
|
||||
timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second
|
||||
|
||||
// use fetch but not remote update because git fetch support --tags but remote update doesn't
|
||||
cmdFetch := func() *gitcmd.Command {
|
||||
cmd := gitcmd.NewCommand("fetch", "--tags")
|
||||
if m.EnablePrune {
|
||||
cmd.AddArguments("--prune")
|
||||
}
|
||||
return cmd.AddDynamicArguments(m.GetRemoteName()).WithTimeout(timeout).WithEnv(envs)
|
||||
}
|
||||
|
||||
var err error
|
||||
fetchStdout, fetchStderr, err := gitrepo.RunCmdString(ctx, m.Repo, cmdFetch())
|
||||
if err != nil {
|
||||
// sanitize the output, since it may contain the remote address, which may contain a password
|
||||
stderrMessage := util.SanitizeCredentialURLs(fetchStderr)
|
||||
stdoutMessage := util.SanitizeCredentialURLs(fetchStdout)
|
||||
|
||||
// Now check if the error is a resolve reference due to broken reference
|
||||
if checkRecoverableSyncError(fetchStderr) {
|
||||
log.Warn("SyncMirrors [repo: %-v]: failed to update mirror repository due to broken references:\nStdout: %s\nStderr: %s\nErr: %v\nAttempting Prune", m.Repo, stdoutMessage, stderrMessage, err)
|
||||
err = nil
|
||||
// Attempt prune
|
||||
pruneErr := pruneBrokenReferences(ctx, m, m.Repo, timeout)
|
||||
if pruneErr == nil {
|
||||
// Successful prune - reattempt mirror
|
||||
fetchStdout, fetchStderr, err = gitrepo.RunCmdString(ctx, m.Repo, cmdFetch())
|
||||
if err != nil {
|
||||
// sanitize the output, since it may contain the remote address, which may contain a password
|
||||
stderrMessage = util.SanitizeCredentialURLs(fetchStderr)
|
||||
stdoutMessage = util.SanitizeCredentialURLs(fetchStdout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there is still an error (or there always was an error)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to update mirror repository:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdoutMessage, stderrMessage, err)
|
||||
desc := fmt.Sprintf("Failed to update mirror repository (%s): %s", m.Repo.FullName(), stderrMessage)
|
||||
if err := system_model.CreateRepositoryNotice(desc); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
if err := gitrepo.WriteCommitGraph(ctx, m.Repo); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: %v", m.Repo, err)
|
||||
}
|
||||
|
||||
gitRepo, err := gitrepo.OpenRepository(ctx, m.Repo)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to OpenRepository: %v", m.Repo, err)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if m.LFS && setting.LFS.StartServer {
|
||||
log.Trace("SyncMirrors [repo: %-v]: syncing LFS objects...", m.Repo)
|
||||
endpoint := lfs.DetermineEndpoint(remoteURL.String(), m.LFSEndpoint)
|
||||
lfsClient := lfs.NewClient(endpoint, migrations.NewMigrationHTTPTransport())
|
||||
if err = repo_module.StoreMissingLfsObjectsInRepository(ctx, m.Repo, gitRepo, lfsClient); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to synchronize LFS objects for repository: %v", m.Repo.FullName(), err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: syncing branches...", m.Repo)
|
||||
_, results, err := repo_module.SyncRepoBranchesWithRepo(ctx, m.Repo, gitRepo, 0)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to synchronize branches: %v", m.Repo, err)
|
||||
}
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: syncing releases with tags...", m.Repo)
|
||||
tagResults, err := repo_module.SyncReleasesWithTags(ctx, m.Repo, gitRepo)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to synchronize tags to releases: %v", m.Repo, err)
|
||||
}
|
||||
results = append(results, tagResults...)
|
||||
gitRepo.Close()
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: updating size of repository", m.Repo)
|
||||
if err := repo_module.UpdateRepoSize(ctx, m.Repo); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to update size for mirror repository: %v", m.Repo.FullName(), err)
|
||||
}
|
||||
|
||||
cmdRemoteUpdatePrune := func() *gitcmd.Command {
|
||||
return gitcmd.NewCommand("remote", "update", "--prune").
|
||||
AddDynamicArguments(m.GetRemoteName()).WithTimeout(timeout).WithEnv(envs)
|
||||
}
|
||||
|
||||
if repo_service.HasWiki(ctx, m.Repo) {
|
||||
log.Trace("SyncMirrors [repo: %-v Wiki]: running git remote update...", m.Repo)
|
||||
// the result of "git remote update" is in stderr
|
||||
stdout, stderr, err := gitrepo.RunCmdString(ctx, m.Repo.WikiStorageRepo(), cmdRemoteUpdatePrune())
|
||||
if err != nil {
|
||||
// sanitize the output, since it may contain the remote address, which may contain a password
|
||||
stderrMessage := util.SanitizeCredentialURLs(stderr)
|
||||
stdoutMessage := util.SanitizeCredentialURLs(stdout)
|
||||
|
||||
// Now check if the error is a resolve reference due to broken reference
|
||||
if checkRecoverableSyncError(stderrMessage) {
|
||||
log.Warn("SyncMirrors [repo: %-v Wiki]: failed to update mirror wiki repository due to broken references:\nStdout: %s\nStderr: %s\nErr: %v\nAttempting Prune", m.Repo, stdoutMessage, stderrMessage, err)
|
||||
err = nil
|
||||
|
||||
// Attempt prune
|
||||
pruneErr := pruneBrokenReferences(ctx, m, m.Repo.WikiStorageRepo(), timeout)
|
||||
if pruneErr == nil {
|
||||
// Successful prune - reattempt mirror
|
||||
stdout, stderr, err = gitrepo.RunCmdString(ctx, m.Repo.WikiStorageRepo(), cmdRemoteUpdatePrune())
|
||||
if err != nil {
|
||||
stderrMessage = util.SanitizeCredentialURLs(stderr)
|
||||
stdoutMessage = util.SanitizeCredentialURLs(stdout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If there is still an error (or there always was an error)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v Wiki]: failed to update mirror repository wiki:\nStdout: %s\nStderr: %s\nErr: %v", m.Repo, stdoutMessage, stderrMessage, err)
|
||||
desc := fmt.Sprintf("Failed to update mirror repository wiki (%s): %s", m.Repo.FullName(), stderrMessage)
|
||||
if err := system_model.CreateRepositoryNotice(desc); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if err := gitrepo.WriteCommitGraph(ctx, m.Repo.WikiStorageRepo()); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: %v", m.Repo, err)
|
||||
}
|
||||
}
|
||||
log.Trace("SyncMirrors [repo: %-v Wiki]: git remote update complete", m.Repo)
|
||||
}
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo)
|
||||
branches, _, err := gitrepo.GetBranchesByPath(ctx, m.Repo, 0, 0)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to GetBranches: %v", m.Repo, err)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
for _, branch := range branches {
|
||||
cache.Remove(m.Repo.GetCommitsCountCacheKey(branch, true))
|
||||
}
|
||||
|
||||
m.UpdatedUnix = timeutil.TimeStampNow()
|
||||
return results, true
|
||||
}
|
||||
|
||||
func getRepoPullMirrorLockKey(repoID int64) string {
|
||||
return fmt.Sprintf("repo_pull_mirror_%d", repoID)
|
||||
}
|
||||
|
||||
// SyncPullMirror starts the sync of the pull mirror and schedules the next run.
|
||||
func SyncPullMirror(ctx context.Context, repoID int64) bool {
|
||||
log.Trace("SyncMirrors [repo_id: %v]", repoID)
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
// There was a panic whilst syncMirrors...
|
||||
log.Error("PANIC whilst SyncMirrors[repo_id: %d] Panic: %v\nStacktrace: %s", repoID, err, log.Stack(2))
|
||||
}()
|
||||
|
||||
releaser, err := globallock.Lock(ctx, getRepoPullMirrorLockKey(repoID))
|
||||
if err != nil {
|
||||
log.Error("globallock.Lock(): %v", err)
|
||||
return false
|
||||
}
|
||||
defer releaser()
|
||||
|
||||
m, err := repo_model.GetMirrorByRepoID(ctx, repoID)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo_id: %v]: unable to GetMirrorByRepoID: %v", repoID, err)
|
||||
return false
|
||||
}
|
||||
repo := m.GetRepository(ctx) // force load repository of mirror
|
||||
|
||||
ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Syncing Mirror %s/%s", m.Repo.OwnerName, m.Repo.Name))
|
||||
defer finished()
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: Running Sync", m.Repo)
|
||||
results, ok := runSync(ctx, m)
|
||||
if !ok {
|
||||
if err = repo_model.TouchMirror(ctx, m); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to TouchMirror: %v", m.Repo, err)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: Scheduling next update", m.Repo)
|
||||
m.ScheduleNextUpdate()
|
||||
m.LastSyncUnix = m.UpdatedUnix
|
||||
if err = repo_model.UpdateMirror(ctx, m); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: failed to UpdateMirror with next update date: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
|
||||
gitRepo, err := gitrepo.OpenRepository(ctx, m.Repo)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to OpenRepository: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: %d branches updated", m.Repo, len(results))
|
||||
if len(results) > 0 {
|
||||
if ok := checkAndUpdateEmptyRepository(ctx, m, results); !ok {
|
||||
log.Error("SyncMirrors [repo: %-v]: checkAndUpdateEmptyRepository: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
// Discard GitHub pull requests, i.e. refs/pull/*
|
||||
if result.RefName.IsPull() {
|
||||
continue
|
||||
}
|
||||
|
||||
// Create reference
|
||||
if result.OldCommitID == "" {
|
||||
commitID, err := gitRepo.GetRefCommitID(result.RefName.String())
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to GetRefCommitID [ref_name: %s]: %v", m.Repo, result.RefName, err)
|
||||
continue
|
||||
}
|
||||
objectFormat := git.ObjectFormatFromName(m.Repo.ObjectFormatName)
|
||||
notify_service.SyncPushCommits(ctx, m.Repo.MustOwner(ctx), m.Repo, &repo_module.PushUpdateOptions{
|
||||
RefFullName: result.RefName,
|
||||
OldCommitID: objectFormat.EmptyObjectID().String(),
|
||||
NewCommitID: commitID,
|
||||
}, repo_module.NewPushCommits())
|
||||
notify_service.SyncCreateRef(ctx, m.Repo.MustOwner(ctx), m.Repo, result.RefName, commitID)
|
||||
continue
|
||||
}
|
||||
|
||||
// Delete reference
|
||||
if result.NewCommitID == "" {
|
||||
notify_service.SyncDeleteRef(ctx, m.Repo.MustOwner(ctx), m.Repo, result.RefName)
|
||||
continue
|
||||
}
|
||||
|
||||
// Push commits
|
||||
oldCommitID, err := gitrepo.GetFullCommitID(ctx, repo, result.OldCommitID)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to get GetFullCommitID[%s]: %v", m.Repo, result.OldCommitID, err)
|
||||
continue
|
||||
}
|
||||
newCommitID, err := gitrepo.GetFullCommitID(ctx, repo, result.NewCommitID)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to get GetFullCommitID [%s]: %v", m.Repo, result.NewCommitID, err)
|
||||
continue
|
||||
}
|
||||
commits, err := gitRepo.CommitsBetweenIDs(newCommitID, oldCommitID)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to get CommitsBetweenIDs [new_commit_id: %s, old_commit_id: %s]: %v", m.Repo, newCommitID, oldCommitID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
theCommits := repo_module.GitToPushCommits(commits)
|
||||
if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum {
|
||||
theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum]
|
||||
}
|
||||
|
||||
newCommit, err := gitRepo.GetCommit(newCommitID)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to get commit %s: %v", m.Repo, newCommitID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
theCommits.HeadCommit = repo_module.CommitToPushCommit(newCommit)
|
||||
theCommits.CompareURL = m.Repo.ComposeCompareURL(oldCommitID, newCommitID)
|
||||
|
||||
notify_service.SyncPushCommits(ctx, m.Repo.MustOwner(ctx), m.Repo, &repo_module.PushUpdateOptions{
|
||||
RefFullName: result.RefName,
|
||||
OldCommitID: oldCommitID,
|
||||
NewCommitID: newCommitID,
|
||||
}, theCommits)
|
||||
}
|
||||
log.Trace("SyncMirrors [repo: %-v]: done notifying updated branches/tags - now updating last commit time", m.Repo)
|
||||
|
||||
isEmpty, err := gitRepo.IsEmpty()
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to check empty git repo: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
if !isEmpty {
|
||||
// Get latest commit date and update to current repository updated time
|
||||
commitDate, err := gitrepo.GetLatestCommitTime(ctx, m.Repo)
|
||||
if err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to GetLatestCommitDate: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
|
||||
if err = repo_model.UpdateRepositoryUpdatedTime(ctx, m.RepoID, commitDate); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to update repository 'updated_unix': %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Update License
|
||||
if err = repo_service.AddRepoToLicenseUpdaterQueue(&repo_service.LicenseUpdaterOptions{
|
||||
RepoID: m.Repo.ID,
|
||||
}); err != nil {
|
||||
log.Error("SyncMirrors [repo: %-v]: unable to add repo to license updater queue: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
|
||||
log.Trace("SyncMirrors [repo: %-v]: Successfully updated", m.Repo)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func checkAndUpdateEmptyRepository(ctx context.Context, m *repo_model.Mirror, results []*repo_module.SyncResult) bool {
|
||||
if !m.Repo.IsEmpty {
|
||||
return true
|
||||
}
|
||||
|
||||
hasDefault := false
|
||||
hasMaster := false
|
||||
hasMain := false
|
||||
defaultBranchName := m.Repo.DefaultBranch
|
||||
if len(defaultBranchName) == 0 {
|
||||
defaultBranchName = setting.Repository.DefaultBranch
|
||||
}
|
||||
firstName := ""
|
||||
for _, result := range results {
|
||||
if !result.RefName.IsBranch() {
|
||||
continue
|
||||
}
|
||||
|
||||
name := result.RefName.BranchName()
|
||||
if len(firstName) == 0 {
|
||||
firstName = name
|
||||
}
|
||||
|
||||
hasDefault = hasDefault || name == defaultBranchName
|
||||
hasMaster = hasMaster || name == "master"
|
||||
hasMain = hasMain || name == "main"
|
||||
}
|
||||
|
||||
if len(firstName) > 0 {
|
||||
if hasDefault {
|
||||
m.Repo.DefaultBranch = defaultBranchName
|
||||
} else if hasMaster {
|
||||
m.Repo.DefaultBranch = "master"
|
||||
} else if hasMain {
|
||||
m.Repo.DefaultBranch = "main"
|
||||
} else {
|
||||
m.Repo.DefaultBranch = firstName
|
||||
}
|
||||
// Update the git repository default branch
|
||||
if err := gitrepo.SetDefaultBranch(ctx, m.Repo, m.Repo.DefaultBranch); err != nil {
|
||||
log.Error("Failed to update default branch of underlying git repository %-v. Error: %v", m.Repo, err)
|
||||
return false
|
||||
}
|
||||
m.Repo.IsEmpty = false
|
||||
// Update the is empty and default_branch columns
|
||||
if err := repo_model.UpdateRepositoryColsWithAutoTime(ctx, m.Repo, "default_branch", "is_empty"); err != nil {
|
||||
log.Error("Failed to update default branch of repository %-v. Error: %v", m.Repo, err)
|
||||
desc := fmt.Sprintf("Failed to update default branch of repository (%s): %v", m.Repo.FullName(), err)
|
||||
if err = system_model.CreateRepositoryNotice(desc); err != nil {
|
||||
log.Error("CreateRepositoryNotice: %v", err)
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_checkRecoverableSyncError(t *testing.T) {
|
||||
cases := []struct {
|
||||
recoverable bool
|
||||
message string
|
||||
}{
|
||||
// A race condition in http git-fetch where certain refs were listed on the remote and are no longer there, would exit status 128
|
||||
{true, "fatal: remote error: upload-pack: not our ref 988881adc9fc3655077dc2d4d757d480b5ea0e11"},
|
||||
// A race condition where a local gc/prune removes a named ref during a git-fetch would exit status 1
|
||||
{true, "cannot lock ref 'refs/pull/123456/merge': unable to resolve reference 'refs/pull/134153/merge'"},
|
||||
// A race condition in http git-fetch where named refs were listed on the remote and are no longer there
|
||||
{true, "error: cannot lock ref 'refs/remotes/origin/foo': unable to resolve reference 'refs/remotes/origin/foo': reference broken"},
|
||||
// A race condition in http git-fetch where named refs were force-pushed during the update, would exit status 128
|
||||
{true, "error: cannot lock ref 'refs/pull/123456/merge': is at 988881adc9fc3655077dc2d4d757d480b5ea0e11 but expected 7f894307ffc9553edbd0b671cab829786866f7b2"},
|
||||
// A race condition with other local git operations, such as git-maintenance, would exit status 128 (well, "Unable" the "U" is uppercase)
|
||||
{true, "fatal: Unable to create '/data/gitea-repositories/foo-org/bar-repo.git/./objects/info/commit-graphs/commit-graph-chain.lock': File exists."},
|
||||
// Missing or unauthorized credentials, would exit status 128
|
||||
{false, "fatal: Authentication failed for 'https://example.com/foo-does-not-exist/bar.git/'"},
|
||||
// A non-existent remote repository, would exit status 128
|
||||
{false, "fatal: Could not read from remote repository."},
|
||||
// A non-functioning proxy, would exit status 128
|
||||
{false, "fatal: unable to access 'https://example.com/foo-does-not-exist/bar.git/': Failed to connect to configured-https-proxy port 1080 after 0 ms: Couldn't connect to server"},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
assert.Equal(t, c.recoverable, checkRecoverableSyncError(c.message), "test case: %s", c.message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,266 @@
|
||||
// Copyright 2021 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
repo_model "gitea.dev/models/repo"
|
||||
"gitea.dev/modules/git"
|
||||
"gitea.dev/modules/gitrepo"
|
||||
"gitea.dev/modules/lfs"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/process"
|
||||
"gitea.dev/modules/proxy"
|
||||
"gitea.dev/modules/repository"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/timeutil"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/services/migrations"
|
||||
repo_service "gitea.dev/services/repository"
|
||||
)
|
||||
|
||||
var stripExitStatus = regexp.MustCompile(`exit status \d+ - `)
|
||||
|
||||
// AddPushMirrorRemote registers the push mirror remote.
|
||||
func AddPushMirrorRemote(ctx context.Context, m *repo_model.PushMirror, addr string) error {
|
||||
addRemoteAndConfig := func(storageRepo gitrepo.Repository, addr string) error {
|
||||
if err := gitrepo.GitRemoteAdd(ctx, storageRepo, m.RemoteName, addr, gitrepo.RemoteOptionMirrorPush); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := gitrepo.GitConfigAdd(ctx, storageRepo, "remote."+m.RemoteName+".push", "+refs/heads/*:refs/heads/*"); err != nil {
|
||||
return err
|
||||
}
|
||||
return gitrepo.GitConfigAdd(ctx, storageRepo, "remote."+m.RemoteName+".push", "+refs/tags/*:refs/tags/*")
|
||||
}
|
||||
|
||||
if err := addRemoteAndConfig(m.Repo, addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repo_service.HasWiki(ctx, m.Repo) {
|
||||
wikiRemoteURL := repository.WikiRemoteURL(ctx, addr)
|
||||
if len(wikiRemoteURL) > 0 {
|
||||
if err := addRemoteAndConfig(m.Repo.WikiStorageRepo(), wikiRemoteURL); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemovePushMirrorRemote removes the push mirror remote.
|
||||
func RemovePushMirrorRemote(ctx context.Context, m *repo_model.PushMirror) error {
|
||||
_ = m.GetRepository(ctx)
|
||||
if err := gitrepo.GitRemoteRemove(ctx, m.Repo, m.RemoteName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repo_service.HasWiki(ctx, m.Repo) {
|
||||
if err := gitrepo.GitRemoteRemove(ctx, m.Repo.WikiStorageRepo(), m.RemoteName); err != nil {
|
||||
// The wiki remote may not exist
|
||||
log.Warn("Wiki Remote[%d] could not be removed: %v", m.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SyncPushMirror starts the sync of the push mirror and schedules the next run.
|
||||
func SyncPushMirror(ctx context.Context, mirrorID int64) bool {
|
||||
log.Trace("SyncPushMirror [mirror: %d]", mirrorID)
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
// There was a panic whilst syncPushMirror...
|
||||
log.Error("PANIC whilst syncPushMirror[%d] Panic: %v\nStacktrace: %s", mirrorID, err, log.Stack(2))
|
||||
}()
|
||||
|
||||
// TODO: Handle "!exist" better
|
||||
m, exist, err := db.GetByID[repo_model.PushMirror](ctx, mirrorID)
|
||||
if err != nil || !exist {
|
||||
log.Error("GetPushMirrorByID [%d]: %v", mirrorID, err)
|
||||
return false
|
||||
}
|
||||
|
||||
_ = m.GetRepository(ctx)
|
||||
|
||||
m.LastError = ""
|
||||
|
||||
ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Syncing PushMirror %s/%s to %s", m.Repo.OwnerName, m.Repo.Name, m.RemoteName))
|
||||
defer finished()
|
||||
|
||||
log.Trace("SyncPushMirror [mirror: %d][repo: %-v]: Running Sync", m.ID, m.Repo)
|
||||
err = runPushSync(ctx, m)
|
||||
if err != nil {
|
||||
log.Error("SyncPushMirror [mirror: %d][repo: %-v]: %v", m.ID, m.Repo, err)
|
||||
m.LastError = stripExitStatus.ReplaceAllLiteralString(err.Error(), "")
|
||||
}
|
||||
|
||||
m.LastUpdateUnix = timeutil.TimeStampNow()
|
||||
|
||||
if err := repo_model.UpdatePushMirror(ctx, m); err != nil {
|
||||
log.Error("UpdatePushMirror [%d]: %v", m.ID, err)
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
log.Trace("SyncPushMirror [mirror: %d][repo: %-v]: Finished", m.ID, m.Repo)
|
||||
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func runPushSync(ctx context.Context, m *repo_model.PushMirror) error {
|
||||
timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second
|
||||
|
||||
performPush := func(repo *repo_model.Repository, isWiki bool) error {
|
||||
var storageRepo gitrepo.Repository = repo
|
||||
if isWiki {
|
||||
storageRepo = repo.WikiStorageRepo()
|
||||
}
|
||||
remoteURL, err := gitrepo.GitRemoteGetURL(ctx, storageRepo, m.RemoteName)
|
||||
if err != nil {
|
||||
log.Error("GetRemoteURL(%s) Error %v", storageRepo.RelativePath(), err)
|
||||
return errors.New("Unexpected error")
|
||||
}
|
||||
|
||||
if setting.LFS.StartServer {
|
||||
log.Trace("SyncMirrors [repo: %-v]: syncing LFS objects...", m.Repo)
|
||||
|
||||
gitRepo, err := gitrepo.OpenRepository(ctx, storageRepo)
|
||||
if err != nil {
|
||||
log.Error("OpenRepository: %v", err)
|
||||
return errors.New("Unexpected error")
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
|
||||
endpoint := lfs.DetermineEndpoint(remoteURL.String(), "")
|
||||
lfsClient := lfs.NewClient(endpoint, migrations.NewMigrationHTTPTransport())
|
||||
if err := pushAllLFSObjects(ctx, gitRepo, lfsClient); err != nil {
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Trace("Pushing %s mirror[%d] remote %s", storageRepo.RelativePath(), m.ID, m.RemoteName)
|
||||
|
||||
envs := proxy.EnvWithProxy(remoteURL.URL)
|
||||
if err := gitrepo.PushToExternal(ctx, storageRepo, git.PushOptions{
|
||||
Remote: m.RemoteName,
|
||||
Force: true,
|
||||
Mirror: true,
|
||||
Timeout: timeout,
|
||||
Env: envs,
|
||||
}); err != nil {
|
||||
log.Error("Error pushing %s mirror[%d] remote %s: %v", storageRepo.RelativePath(), m.ID, m.RemoteName, err)
|
||||
|
||||
return util.SanitizeErrorCredentialURLs(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
err := performPush(m.Repo, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if repo_service.HasWiki(ctx, m.Repo) {
|
||||
if _, err := gitrepo.GitRemoteGetURL(ctx, m.Repo.WikiStorageRepo(), m.RemoteName); err == nil {
|
||||
err := performPush(m.Repo, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if !errors.Is(err, util.ErrNotExist) {
|
||||
log.Error("GetRemote of wiki failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func pushAllLFSObjects(ctx context.Context, gitRepo *git.Repository, lfsClient lfs.Client) error {
|
||||
contentStore := lfs.NewContentStore()
|
||||
|
||||
pointerChan := make(chan lfs.PointerBlob)
|
||||
errChan := make(chan error, 1)
|
||||
go func() {
|
||||
errChan <- lfs.SearchPointerBlobs(ctx, gitRepo, pointerChan)
|
||||
}()
|
||||
|
||||
uploadObjects := func(pointers []lfs.Pointer) error {
|
||||
err := lfsClient.Upload(ctx, pointers, func(p lfs.Pointer, objectError error) (io.ReadCloser, error) {
|
||||
if objectError != nil {
|
||||
return nil, objectError
|
||||
}
|
||||
|
||||
content, err := contentStore.Get(p)
|
||||
if err != nil {
|
||||
log.Error("Error reading LFS object %v: %v", p, err)
|
||||
}
|
||||
return content, err
|
||||
})
|
||||
if err != nil {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
default:
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var batch []lfs.Pointer
|
||||
for pointerBlob := range pointerChan {
|
||||
exists, err := contentStore.Exists(pointerBlob.Pointer)
|
||||
if err != nil {
|
||||
log.Error("Error checking if LFS object %v exists: %v", pointerBlob.Pointer, err)
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
log.Trace("Skipping missing LFS object %v", pointerBlob.Pointer)
|
||||
continue
|
||||
}
|
||||
|
||||
batch = append(batch, pointerBlob.Pointer)
|
||||
if len(batch) >= lfsClient.BatchSize() {
|
||||
if err := uploadObjects(batch); err != nil {
|
||||
return err
|
||||
}
|
||||
batch = nil
|
||||
}
|
||||
}
|
||||
if len(batch) > 0 {
|
||||
if err := uploadObjects(batch); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
err := <-errChan
|
||||
if err != nil {
|
||||
log.Error("Error enumerating LFS objects for repository: %v", err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func syncPushMirrorWithSyncOnCommit(ctx context.Context, repoID int64) {
|
||||
pushMirrors, err := repo_model.GetPushMirrorsSyncedOnCommit(ctx, repoID)
|
||||
if err != nil {
|
||||
log.Error("repo_model.GetPushMirrorsSyncedOnCommit failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, mirror := range pushMirrors {
|
||||
AddPushMirrorToQueue(mirror.ID)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
repo_model "gitea.dev/models/repo"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/repository"
|
||||
notify_service "gitea.dev/services/notify"
|
||||
)
|
||||
|
||||
func init() {
|
||||
notify_service.RegisterNotifier(&mirrorNotifier{})
|
||||
}
|
||||
|
||||
type mirrorNotifier struct {
|
||||
notify_service.NullNotifier
|
||||
}
|
||||
|
||||
var _ notify_service.Notifier = &mirrorNotifier{}
|
||||
|
||||
func (m *mirrorNotifier) PushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
|
||||
syncPushMirrorWithSyncOnCommit(ctx, repo.ID)
|
||||
}
|
||||
|
||||
func (m *mirrorNotifier) SyncPushCommits(ctx context.Context, _ *user_model.User, repo *repo_model.Repository, _ *repository.PushUpdateOptions, _ *repository.PushCommits) {
|
||||
syncPushMirrorWithSyncOnCommit(ctx, repo.ID)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package mirror
|
||||
|
||||
import (
|
||||
"gitea.dev/modules/graceful"
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/queue"
|
||||
"gitea.dev/modules/setting"
|
||||
)
|
||||
|
||||
var mirrorQueue *queue.WorkerPoolQueue[*SyncRequest]
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func queueHandler(items ...*SyncRequest) []*SyncRequest {
|
||||
for _, req := range items {
|
||||
doMirrorSync(graceful.GetManager().ShutdownContext(), req)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartSyncMirrors starts a go routine to sync the mirrors
|
||||
func StartSyncMirrors() {
|
||||
if !setting.Mirror.Enabled {
|
||||
return
|
||||
}
|
||||
mirrorQueue = queue.CreateUniqueQueue(graceful.GetManager().ShutdownContext(), "mirror", queueHandler)
|
||||
if mirrorQueue == nil {
|
||||
log.Fatal("Unable to create mirror queue")
|
||||
}
|
||||
go graceful.GetManager().RunWithCancel(mirrorQueue)
|
||||
}
|
||||
|
||||
// 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,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user