From cda97a725347cdadd43af812749732b9d1ee6429 Mon Sep 17 00:00:00 2001 From: Nanguan Lin <70063547+lng2020@users.noreply.github.com> Date: Wed, 13 Sep 2023 12:43:31 +0800 Subject: [PATCH] Update status and code index after changing the default branch (#27018) Fix #26723 Add `ChangeDefaultBranch` to the `notifier` interface and implement it in `indexerNotifier`. So when changing the default branch, `indexerNotifier` sends a message to the `indexer queue` to update the index. --------- Co-authored-by: techknowlogick --- models/repo/git.go | 4 +- modules/indexer/code/git.go | 9 ++- routers/web/repo/setting/default_branch.go | 64 ++++++++++++++++++++ routers/web/repo/setting/protected_branch.go | 50 --------------- services/indexer/notify.go | 9 +++ services/notify/notifier.go | 2 + services/notify/notify.go | 7 +++ services/notify/null.go | 4 ++ 8 files changed, 97 insertions(+), 52 deletions(-) create mode 100644 routers/web/repo/setting/default_branch.go diff --git a/models/repo/git.go b/models/repo/git.go index c1af7ee960..2f71128b5a 100644 --- a/models/repo/git.go +++ b/models/repo/git.go @@ -3,7 +3,9 @@ package repo -import "code.gitea.io/gitea/models/db" +import ( + "code.gitea.io/gitea/models/db" +) // MergeStyle represents the approach to merge commits into base branch. type MergeStyle string diff --git a/modules/indexer/code/git.go b/modules/indexer/code/git.go index 1ba6b849d1..e4686fa01f 100644 --- a/modules/indexer/code/git.go +++ b/modules/indexer/code/git.go @@ -30,7 +30,14 @@ func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision s return nil, err } - if len(status.CommitSha) == 0 { + needGenesis := len(status.CommitSha) == 0 + if !needGenesis { + hasAncestorCmd := git.NewCommand(ctx, "merge-base").AddDynamicArguments(repo.CodeIndexerStatus.CommitSha, revision) + stdout, _, _ := hasAncestorCmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()}) + needGenesis = len(stdout) == 0 + } + + if needGenesis { return genesisChanges(ctx, repo, revision) } return nonGenesisChanges(ctx, repo, revision) diff --git a/routers/web/repo/setting/default_branch.go b/routers/web/repo/setting/default_branch.go new file mode 100644 index 0000000000..f0aa1a89e7 --- /dev/null +++ b/routers/web/repo/setting/default_branch.go @@ -0,0 +1,64 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package setting + +import ( + "net/http" + + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/routers/web/repo" + notify_service "code.gitea.io/gitea/services/notify" +) + +// SetDefaultBranchPost set default branch +func SetDefaultBranchPost(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("repo.settings.branches.update_default_branch") + ctx.Data["PageIsSettingsBranches"] = true + + repo.PrepareBranchList(ctx) + if ctx.Written() { + return + } + + repo := ctx.Repo.Repository + + switch ctx.FormString("action") { + case "default_branch": + if ctx.HasError() { + ctx.HTML(http.StatusOK, tplBranches) + return + } + + branch := ctx.FormString("branch") + if !ctx.Repo.GitRepo.IsBranchExist(branch) { + ctx.Status(http.StatusNotFound) + return + } else if repo.DefaultBranch != branch { + repo.DefaultBranch = branch + if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil { + if !git.IsErrUnsupportedVersion(err) { + ctx.ServerError("SetDefaultBranch", err) + return + } + } + if err := repo_model.UpdateDefaultBranch(repo); err != nil { + ctx.ServerError("SetDefaultBranch", err) + return + } + + notify_service.ChangeDefaultBranch(ctx, repo) + } + + log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name) + + ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success")) + ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath()) + default: + ctx.NotFound("", nil) + } +} diff --git a/routers/web/repo/setting/protected_branch.go b/routers/web/repo/setting/protected_branch.go index 5bfdb8f515..e0f2294a14 100644 --- a/routers/web/repo/setting/protected_branch.go +++ b/routers/web/repo/setting/protected_branch.go @@ -14,12 +14,8 @@ import ( "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/perm" access_model "code.gitea.io/gitea/models/perm/access" - repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/context" - "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/web/repo" "code.gitea.io/gitea/services/forms" @@ -53,52 +49,6 @@ func ProtectedBranchRules(ctx *context.Context) { ctx.HTML(http.StatusOK, tplBranches) } -// SetDefaultBranchPost set default branch -func SetDefaultBranchPost(ctx *context.Context) { - ctx.Data["Title"] = ctx.Tr("repo.settings.branches.update_default_branch") - ctx.Data["PageIsSettingsBranches"] = true - - repo.PrepareBranchList(ctx) - if ctx.Written() { - return - } - - repo := ctx.Repo.Repository - - switch ctx.FormString("action") { - case "default_branch": - if ctx.HasError() { - ctx.HTML(http.StatusOK, tplBranches) - return - } - - branch := ctx.FormString("branch") - if !ctx.Repo.GitRepo.IsBranchExist(branch) { - ctx.Status(http.StatusNotFound) - return - } else if repo.DefaultBranch != branch { - repo.DefaultBranch = branch - if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil { - if !git.IsErrUnsupportedVersion(err) { - ctx.ServerError("SetDefaultBranch", err) - return - } - } - if err := repo_model.UpdateDefaultBranch(repo); err != nil { - ctx.ServerError("SetDefaultBranch", err) - return - } - } - - log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name) - - ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success")) - ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath()) - default: - ctx.NotFound("", nil) - } -} - // SettingsProtectedBranch renders the protected branch setting page func SettingsProtectedBranch(c *context.Context) { ruleName := c.FormString("rule_name") diff --git a/services/indexer/notify.go b/services/indexer/notify.go index 22306c691b..a07bf38b06 100644 --- a/services/indexer/notify.go +++ b/services/indexer/notify.go @@ -110,6 +110,15 @@ func (r *indexerNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode } } +func (r *indexerNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) { + if setting.Indexer.RepoIndexerEnabled && !repo.IsEmpty { + code_indexer.UpdateRepoIndexer(repo) + } + if err := stats_indexer.UpdateRepoIndexer(repo); err != nil { + log.Error("stats_indexer.UpdateRepoIndexer(%d) failed: %v", repo.ID, err) + } +} + func (r *indexerNotifier) IssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) { issue_indexer.UpdateIssueIndexer(issue.ID) } diff --git a/services/notify/notifier.go b/services/notify/notifier.go index d1dbe44c11..ed053a812a 100644 --- a/services/notify/notifier.go +++ b/services/notify/notifier.go @@ -72,4 +72,6 @@ type Notifier interface { PackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) + + ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) } diff --git a/services/notify/notify.go b/services/notify/notify.go index 71bc1c7d58..16fbb6325d 100644 --- a/services/notify/notify.go +++ b/services/notify/notify.go @@ -360,3 +360,10 @@ func PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_mode notifier.PackageDelete(ctx, doer, pd) } } + +// ChangeDefaultBranch notifies change default branch to notifiers +func ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) { + for _, notifier := range notifiers { + notifier.ChangeDefaultBranch(ctx, repo) + } +} diff --git a/services/notify/null.go b/services/notify/null.go index c5b31f83d6..dddd421bef 100644 --- a/services/notify/null.go +++ b/services/notify/null.go @@ -204,3 +204,7 @@ func (*NullNotifier) PackageCreate(ctx context.Context, doer *user_model.User, p // PackageDelete places a place holder function func (*NullNotifier) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) { } + +// ChangeDefaultBranch places a place holder function +func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) { +}