Skip to content

Update status and code index after changing the default branch #27018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
4 changes: 3 additions & 1 deletion models/repo/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion modules/indexer/code/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions routers/web/repo/setting/default_branch.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
50 changes: 0 additions & 50 deletions routers/web/repo/setting/protected_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions services/indexer/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions services/notify/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions services/notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 4 additions & 0 deletions services/notify/null.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}