Skip to content

Commit cda97a7

Browse files
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 <[email protected]>
1 parent e6a059a commit cda97a7

File tree

8 files changed

+97
-52
lines changed

8 files changed

+97
-52
lines changed

models/repo/git.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
package repo
55

6-
import "code.gitea.io/gitea/models/db"
6+
import (
7+
"code.gitea.io/gitea/models/db"
8+
)
79

810
// MergeStyle represents the approach to merge commits into base branch.
911
type MergeStyle string

modules/indexer/code/git.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ func getRepoChanges(ctx context.Context, repo *repo_model.Repository, revision s
3030
return nil, err
3131
}
3232

33-
if len(status.CommitSha) == 0 {
33+
needGenesis := len(status.CommitSha) == 0
34+
if !needGenesis {
35+
hasAncestorCmd := git.NewCommand(ctx, "merge-base").AddDynamicArguments(repo.CodeIndexerStatus.CommitSha, revision)
36+
stdout, _, _ := hasAncestorCmd.RunStdString(&git.RunOpts{Dir: repo.RepoPath()})
37+
needGenesis = len(stdout) == 0
38+
}
39+
40+
if needGenesis {
3441
return genesisChanges(ctx, repo, revision)
3542
}
3643
return nonGenesisChanges(ctx, repo, revision)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package setting
5+
6+
import (
7+
"net/http"
8+
9+
repo_model "code.gitea.io/gitea/models/repo"
10+
"code.gitea.io/gitea/modules/context"
11+
"code.gitea.io/gitea/modules/git"
12+
"code.gitea.io/gitea/modules/log"
13+
"code.gitea.io/gitea/modules/setting"
14+
"code.gitea.io/gitea/routers/web/repo"
15+
notify_service "code.gitea.io/gitea/services/notify"
16+
)
17+
18+
// SetDefaultBranchPost set default branch
19+
func SetDefaultBranchPost(ctx *context.Context) {
20+
ctx.Data["Title"] = ctx.Tr("repo.settings.branches.update_default_branch")
21+
ctx.Data["PageIsSettingsBranches"] = true
22+
23+
repo.PrepareBranchList(ctx)
24+
if ctx.Written() {
25+
return
26+
}
27+
28+
repo := ctx.Repo.Repository
29+
30+
switch ctx.FormString("action") {
31+
case "default_branch":
32+
if ctx.HasError() {
33+
ctx.HTML(http.StatusOK, tplBranches)
34+
return
35+
}
36+
37+
branch := ctx.FormString("branch")
38+
if !ctx.Repo.GitRepo.IsBranchExist(branch) {
39+
ctx.Status(http.StatusNotFound)
40+
return
41+
} else if repo.DefaultBranch != branch {
42+
repo.DefaultBranch = branch
43+
if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
44+
if !git.IsErrUnsupportedVersion(err) {
45+
ctx.ServerError("SetDefaultBranch", err)
46+
return
47+
}
48+
}
49+
if err := repo_model.UpdateDefaultBranch(repo); err != nil {
50+
ctx.ServerError("SetDefaultBranch", err)
51+
return
52+
}
53+
54+
notify_service.ChangeDefaultBranch(ctx, repo)
55+
}
56+
57+
log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
58+
59+
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
60+
ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath())
61+
default:
62+
ctx.NotFound("", nil)
63+
}
64+
}

routers/web/repo/setting/protected_branch.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ import (
1414
"code.gitea.io/gitea/models/organization"
1515
"code.gitea.io/gitea/models/perm"
1616
access_model "code.gitea.io/gitea/models/perm/access"
17-
repo_model "code.gitea.io/gitea/models/repo"
1817
"code.gitea.io/gitea/modules/base"
1918
"code.gitea.io/gitea/modules/context"
20-
"code.gitea.io/gitea/modules/git"
21-
"code.gitea.io/gitea/modules/log"
22-
"code.gitea.io/gitea/modules/setting"
2319
"code.gitea.io/gitea/modules/web"
2420
"code.gitea.io/gitea/routers/web/repo"
2521
"code.gitea.io/gitea/services/forms"
@@ -53,52 +49,6 @@ func ProtectedBranchRules(ctx *context.Context) {
5349
ctx.HTML(http.StatusOK, tplBranches)
5450
}
5551

56-
// SetDefaultBranchPost set default branch
57-
func SetDefaultBranchPost(ctx *context.Context) {
58-
ctx.Data["Title"] = ctx.Tr("repo.settings.branches.update_default_branch")
59-
ctx.Data["PageIsSettingsBranches"] = true
60-
61-
repo.PrepareBranchList(ctx)
62-
if ctx.Written() {
63-
return
64-
}
65-
66-
repo := ctx.Repo.Repository
67-
68-
switch ctx.FormString("action") {
69-
case "default_branch":
70-
if ctx.HasError() {
71-
ctx.HTML(http.StatusOK, tplBranches)
72-
return
73-
}
74-
75-
branch := ctx.FormString("branch")
76-
if !ctx.Repo.GitRepo.IsBranchExist(branch) {
77-
ctx.Status(http.StatusNotFound)
78-
return
79-
} else if repo.DefaultBranch != branch {
80-
repo.DefaultBranch = branch
81-
if err := ctx.Repo.GitRepo.SetDefaultBranch(branch); err != nil {
82-
if !git.IsErrUnsupportedVersion(err) {
83-
ctx.ServerError("SetDefaultBranch", err)
84-
return
85-
}
86-
}
87-
if err := repo_model.UpdateDefaultBranch(repo); err != nil {
88-
ctx.ServerError("SetDefaultBranch", err)
89-
return
90-
}
91-
}
92-
93-
log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
94-
95-
ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
96-
ctx.Redirect(setting.AppSubURL + ctx.Req.URL.EscapedPath())
97-
default:
98-
ctx.NotFound("", nil)
99-
}
100-
}
101-
10252
// SettingsProtectedBranch renders the protected branch setting page
10353
func SettingsProtectedBranch(c *context.Context) {
10454
ruleName := c.FormString("rule_name")

services/indexer/notify.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ func (r *indexerNotifier) SyncPushCommits(ctx context.Context, pusher *user_mode
110110
}
111111
}
112112

113+
func (r *indexerNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
114+
if setting.Indexer.RepoIndexerEnabled && !repo.IsEmpty {
115+
code_indexer.UpdateRepoIndexer(repo)
116+
}
117+
if err := stats_indexer.UpdateRepoIndexer(repo); err != nil {
118+
log.Error("stats_indexer.UpdateRepoIndexer(%d) failed: %v", repo.ID, err)
119+
}
120+
}
121+
113122
func (r *indexerNotifier) IssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) {
114123
issue_indexer.UpdateIssueIndexer(issue.ID)
115124
}

services/notify/notifier.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,6 @@ type Notifier interface {
7272

7373
PackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor)
7474
PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor)
75+
76+
ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository)
7577
}

services/notify/notify.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,10 @@ func PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_mode
360360
notifier.PackageDelete(ctx, doer, pd)
361361
}
362362
}
363+
364+
// ChangeDefaultBranch notifies change default branch to notifiers
365+
func ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
366+
for _, notifier := range notifiers {
367+
notifier.ChangeDefaultBranch(ctx, repo)
368+
}
369+
}

services/notify/null.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,7 @@ func (*NullNotifier) PackageCreate(ctx context.Context, doer *user_model.User, p
204204
// PackageDelete places a place holder function
205205
func (*NullNotifier) PackageDelete(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) {
206206
}
207+
208+
// ChangeDefaultBranch places a place holder function
209+
func (*NullNotifier) ChangeDefaultBranch(ctx context.Context, repo *repo_model.Repository) {
210+
}

0 commit comments

Comments
 (0)