From 99283415bcbaa8acfe4d249ce3040de2f3a8b006 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 16 May 2023 03:02:10 +0800 Subject: [PATCH] Refactor Pull Mirror and fix out-of-sync bugs (#24732) The "mirror" table and "repository" table might be out-of-sync in some cases. It means that "IsMirror=true" but "Mirror=nil" This PR removes unnecessary "Mirror" field, rename "Mirror" to "PullMirror" and fix nil panic bug. Screenshot of changed templates: ![image](https://github.com/go-gitea/gitea/assets/2114189/c0f2bdfc-5911-43ea-b989-b19619de4235) ![image](https://github.com/go-gitea/gitea/assets/2114189/1078b41a-484f-4c06-8c2f-edb9e658275d) --- models/repo/repo.go | 4 ++-- modules/context/repo.go | 9 ++------- routers/web/repo/setting.go | 27 ++++++++++++++++++--------- services/convert/repository.go | 7 +++---- templates/repo/header.tmpl | 4 ++-- templates/repo/settings/options.tmpl | 27 ++++++++++++++++++--------- 6 files changed, 45 insertions(+), 33 deletions(-) diff --git a/models/repo/repo.go b/models/repo/repo.go index 7cbd5867b7..00e875407c 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -149,8 +149,8 @@ type Repository struct { IsEmpty bool `xorm:"INDEX"` IsArchived bool `xorm:"INDEX"` IsMirror bool `xorm:"INDEX"` - *Mirror `xorm:"-"` - Status RepositoryStatus `xorm:"NOT NULL DEFAULT 0"` + + Status RepositoryStatus `xorm:"NOT NULL DEFAULT 0"` RenderingMetas map[string]string `xorm:"-"` DocumentRenderingMetas map[string]string `xorm:"-"` diff --git a/modules/context/repo.go b/modules/context/repo.go index b20ea26e4e..5e90e8aec0 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -61,7 +61,6 @@ type Repository struct { RepoLink string CloneLink repo_model.CloneLink CommitsCount int64 - Mirror *repo_model.Mirror PullRequest *PullRequest } @@ -380,13 +379,9 @@ func repoAssignment(ctx *Context, repo *repo_model.Repository) { ctx.Data["Permission"] = &ctx.Repo.Permission if repo.IsMirror { - ctx.Repo.Mirror, err = repo_model.GetMirrorByRepoID(ctx, repo.ID) + pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID) if err == nil { - ctx.Repo.Mirror.Repo = repo - ctx.Data["IsPullMirror"] = true - ctx.Data["MirrorEnablePrune"] = ctx.Repo.Mirror.EnablePrune - ctx.Data["MirrorInterval"] = ctx.Repo.Mirror.Interval - ctx.Data["Mirror"] = ctx.Repo.Mirror + ctx.Data["PullMirror"] = pullMirror } else if err != repo_model.ErrMirrorNotExist { ctx.ServerError("GetMirrorByRepoID", err) return diff --git a/routers/web/repo/setting.go b/routers/web/repo/setting.go index 16b718c919..578b70194e 100644 --- a/routers/web/repo/setting.go +++ b/routers/web/repo/setting.go @@ -198,6 +198,15 @@ func SettingsPost(ctx *context.Context) { return } + pullMirror, err := repo_model.GetMirrorByRepoID(ctx, ctx.Repo.Repository.ID) + if err == repo_model.ErrMirrorNotExist { + ctx.NotFound("", nil) + return + } + if err != nil { + ctx.ServerError("GetMirrorByRepoID", err) + return + } // This section doesn't require repo_name/RepoName to be set in the form, don't show it // as an error on the UI for this action ctx.Data["Err_RepoName"] = nil @@ -209,15 +218,15 @@ func SettingsPost(ctx *context.Context) { return } - ctx.Repo.Mirror.EnablePrune = form.EnablePrune - ctx.Repo.Mirror.Interval = interval - ctx.Repo.Mirror.ScheduleNextUpdate() - if err := repo_model.UpdateMirror(ctx, ctx.Repo.Mirror); err != nil { + pullMirror.EnablePrune = form.EnablePrune + pullMirror.Interval = interval + pullMirror.ScheduleNextUpdate() + if err := repo_model.UpdateMirror(ctx, pullMirror); err != nil { ctx.ServerError("UpdateMirror", err) return } - u, err := git.GetRemoteURL(ctx, ctx.Repo.Repository.RepoPath(), ctx.Repo.Mirror.GetRemoteName()) + u, err := git.GetRemoteURL(ctx, ctx.Repo.Repository.RepoPath(), pullMirror.GetRemoteName()) if err != nil { ctx.Data["Err_MirrorAddress"] = true handleSettingRemoteAddrError(ctx, err, form) @@ -237,7 +246,7 @@ func SettingsPost(ctx *context.Context) { return } - if err := mirror_service.UpdateAddress(ctx, ctx.Repo.Mirror, address); err != nil { + if err := mirror_service.UpdateAddress(ctx, pullMirror, address); err != nil { ctx.ServerError("UpdateAddress", err) return } @@ -259,9 +268,9 @@ func SettingsPost(ctx *context.Context) { } } - ctx.Repo.Mirror.LFS = form.LFS - ctx.Repo.Mirror.LFSEndpoint = form.LFSEndpoint - if err := repo_model.UpdateMirror(ctx, ctx.Repo.Mirror); err != nil { + pullMirror.LFS = form.LFS + pullMirror.LFSEndpoint = form.LFSEndpoint + if err := repo_model.UpdateMirror(ctx, pullMirror); err != nil { ctx.ServerError("UpdateMirror", err) return } diff --git a/services/convert/repository.go b/services/convert/repository.go index f470fd1656..54a61efe43 100644 --- a/services/convert/repository.go +++ b/services/convert/repository.go @@ -124,11 +124,10 @@ func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.Acc mirrorInterval := "" var mirrorUpdated time.Time if repo.IsMirror { - var err error - repo.Mirror, err = repo_model.GetMirrorByRepoID(ctx, repo.ID) + pullMirror, err := repo_model.GetMirrorByRepoID(ctx, repo.ID) if err == nil { - mirrorInterval = repo.Mirror.Interval.String() - mirrorUpdated = repo.Mirror.UpdatedUnix.AsTime() + mirrorInterval = pullMirror.Interval.String() + mirrorUpdated = pullMirror.UpdatedUnix.AsTime() } } diff --git a/templates/repo/header.tmpl b/templates/repo/header.tmpl index 03f5a2e78f..33791fd512 100644 --- a/templates/repo/header.tmpl +++ b/templates/repo/header.tmpl @@ -39,8 +39,8 @@ {{svg "octicon-rss" 18}} {{end}} - {{if $.IsPullMirror}} - {{$address := MirrorRemoteAddress $.Context . $.Mirror.GetRemoteName false}} + {{if $.PullMirror}} + {{$address := MirrorRemoteAddress $.Context . $.PullMirror.GetRemoteName false}}
{{$.locale.Tr "repo.mirror_from"}} {{$address.Address}}
{{end}} {{if .IsFork}}
{{$.locale.Tr "repo.forked_from"}} {{.BaseRepo.FullName}}
{{end}} diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index b26656bf28..efcdaceb37 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -84,12 +84,21 @@ {{end}} - {{if .Repository.IsMirror}} + {{if and .Repository.IsMirror (not .PullMirror)}} + {{/* even if a repo is a pull mirror (IsMirror=true), the PullMirror might still be nil if the mirror migration is broken */}} + + + + {{$.locale.Tr "repo.settings.mirror_settings.direction.pull"}}: {{$.locale.Tr "error.occurred"}} + + + + {{else if .PullMirror}} - {{(MirrorRemoteAddress $.Context .Repository .Mirror.GetRemoteName false).Address}} + {{(MirrorRemoteAddress $.Context .Repository .PullMirror.GetRemoteName false).Address}} {{$.locale.Tr "repo.settings.mirror_settings.direction.pull"}} - {{DateTime "full" .Mirror.UpdatedUnix}} + {{DateTime "full" .PullMirror.UpdatedUnix}}
{{.CsrfTokenHtml}} @@ -107,15 +116,15 @@
- +
- +
- {{$address := MirrorRemoteAddress $.Context .Repository .Mirror.GetRemoteName false}} + {{$address := MirrorRemoteAddress $.Context .Repository .PullMirror.GetRemoteName false}}
@@ -142,13 +151,13 @@
- +
- +

{{.locale.Tr "repo.mirror_lfs_endpoint_desc" "https://github.com/git-lfs/git-lfs/blob/main/docs/api/server-discovery.md#server-discovery" | Str2html}}

{{end}} @@ -160,7 +169,7 @@ - {{end}} + {{end}}{{/* end if: IsMirror */}} {{range .PushMirrors}}