Skip to content

Commit 9e5d9aa

Browse files
committed
apply suggestions from code review
1 parent 7d79bba commit 9e5d9aa

File tree

5 files changed

+90
-77
lines changed

5 files changed

+90
-77
lines changed

models/branches.go

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -600,10 +600,11 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
600600
// RenamedBranch proivde renamed branch log
601601
// will check it when an branch can't be found
602602
type RenamedBranch struct {
603-
ID int64 `xorm:"pk autoincr"`
604-
RepoID int64 `xorm:"INDEX NOT NULL"`
605-
From string
606-
To string
603+
ID int64 `xorm:"pk autoincr"`
604+
RepoID int64 `xorm:"INDEX NOT NULL"`
605+
From string
606+
To string
607+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
607608
}
608609

609610
// FindRenamedBranch check if a branch was renamed
@@ -638,19 +639,13 @@ func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault b
638639
// 2. Update protected branch if needed
639640
protectedBranch, err := getProtectedBranchBy(sess, repo.ID, from)
640641
if err != nil {
641-
if err2 := sess.Rollback(); err2 != nil {
642-
log.Error("RenameBranch: sess.Rollback: %v", err2)
643-
}
644642
return err
645643
}
646644

647645
if protectedBranch != nil {
648646
protectedBranch.BranchName = to
649647
_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch)
650648
if err != nil {
651-
if err2 := sess.Rollback(); err2 != nil {
652-
log.Error("RenameBranch: sess.Rollback: %v", err2)
653-
}
654649
return err
655650
}
656651
}
@@ -660,18 +655,11 @@ func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault b
660655
repo.ID, from, false).
661656
Update(map[string]interface{}{"base_branch": to})
662657
if err != nil {
663-
if err2 := sess.Rollback(); err2 != nil {
664-
log.Error("RenameBranch: sess.Rollback: %v", err2)
665-
}
666658
return err
667659
}
668660

669661
// 4. do git action
670662
if err = gitAction(isDefault); err != nil {
671-
if err2 := sess.Rollback(); err2 != nil {
672-
log.Error("RenameBranch: sess.Rollback: %v", err2)
673-
}
674-
675663
return err
676664
}
677665

@@ -681,13 +669,8 @@ func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault b
681669
From: from,
682670
To: to,
683671
}
684-
685672
_, err = sess.Insert(renamedBranch)
686673
if err != nil {
687-
if err2 := sess.Rollback(); err2 != nil {
688-
log.Error("RenameBranch: sess.Rollback: %v", err2)
689-
}
690-
691674
return err
692675
}
693676

models/migrations/v180.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010

1111
func addRenamedBranchTable(x *xorm.Engine) error {
1212
type RenamedBranch struct {
13-
ID int64 `xorm:"pk autoincr"`
14-
RepoID int64 `xorm:"INDEX NOT NULL"`
15-
From string
16-
To string
13+
ID int64 `xorm:"pk autoincr"`
14+
RepoID int64 `xorm:"INDEX NOT NULL"`
15+
From string
16+
To string
17+
CreatedUnix int64 `xorm:"created"`
1718
}
1819
return x.Sync2(new(RenamedBranch))
1920
}

modules/context/repo.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,23 @@ type PullRequest struct {
4949
// Repository contains information to operate a repository
5050
type Repository struct {
5151
models.Permission
52-
IsWatching bool
53-
IsViewBranch bool
54-
IsViewTag bool
55-
IsViewCommit bool
56-
Repository *models.Repository
57-
Owner *models.User
58-
Commit *git.Commit
59-
Tag *git.Tag
60-
GitRepo *git.Repository
61-
BranchName string
62-
IsRenamedBranch bool
63-
RenamedBranchName string
64-
TagName string
65-
TreePath string
66-
CommitID string
67-
RepoLink string
68-
CloneLink models.CloneLink
69-
CommitsCount int64
70-
Mirror *models.Mirror
52+
IsWatching bool
53+
IsViewBranch bool
54+
IsViewTag bool
55+
IsViewCommit bool
56+
Repository *models.Repository
57+
Owner *models.User
58+
Commit *git.Commit
59+
Tag *git.Tag
60+
GitRepo *git.Repository
61+
BranchName string
62+
TagName string
63+
TreePath string
64+
CommitID string
65+
RepoLink string
66+
CloneLink models.CloneLink
67+
CommitsCount int64
68+
Mirror *models.Mirror
7169

7270
PullRequest *PullRequest
7371
}
@@ -717,8 +715,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
717715
return false
718716
}
719717

720-
ctx.Repo.IsRenamedBranch = true
721-
ctx.Repo.RenamedBranchName = b.To
718+
ctx.Data["IsRenamedBranch"] = true
719+
ctx.Data["RenamedBranchName"] = b.To
722720

723721
return true
724722
})
@@ -803,9 +801,11 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
803801
} else {
804802
refName = getRefName(ctx, refType)
805803
ctx.Repo.BranchName = refName
806-
if ctx.Repo.IsRenamedBranch {
807-
ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refName, ctx.Repo.RenamedBranchName))
808-
link := strings.Replace(ctx.Req.RequestURI, refName, ctx.Repo.RenamedBranchName, 1)
804+
isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)
805+
if isRenamedBranch && has {
806+
renamedBranchName := ctx.Data["RenamedBranchName"].(string)
807+
ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refName, renamedBranchName))
808+
link := strings.Replace(ctx.Req.RequestURI, refName, renamedBranchName, 1)
809809
ctx.Redirect(link)
810810
return
811811
}

routers/repo/setting_protected_branch.go

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
"code.gitea.io/gitea/modules/context"
1616
"code.gitea.io/gitea/modules/git"
1717
"code.gitea.io/gitea/modules/log"
18-
"code.gitea.io/gitea/modules/notification"
1918
"code.gitea.io/gitea/modules/setting"
2019
"code.gitea.io/gitea/modules/web"
2120
"code.gitea.io/gitea/services/forms"
2221
pull_service "code.gitea.io/gitea/services/pull"
22+
"code.gitea.io/gitea/services/repository"
2323
)
2424

2525
// ProtectedBranch render the page to protect the repository
@@ -301,44 +301,24 @@ func RenameBranchPost(ctx *context.Context) {
301301
return
302302
}
303303

304-
if form.From == form.To {
305-
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_exist", form.To))
306-
ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
304+
msg, err := repository.RenameBranch(ctx.Repo.Repository, ctx.User, ctx.Repo.GitRepo, form.From, form.To)
305+
if err != nil {
306+
ctx.ServerError("RenameBranch", err)
307307
return
308308
}
309309

310-
if ctx.Repo.GitRepo.IsBranchExist(form.To) {
310+
if msg == "target_exist" {
311311
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_exist", form.To))
312312
ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
313+
return
313314
}
314315

315-
if !ctx.Repo.GitRepo.IsBranchExist(form.From) {
316+
if msg == "from_not_exist" {
316317
ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_not_exist", form.From))
317318
ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
318-
}
319-
320-
if err := ctx.Repo.Repository.RenameBranch(form.From, form.To, func(isDefault bool) error {
321-
err2 := ctx.Repo.GitRepo.RenameBranch(form.From, form.To)
322-
if err2 != nil {
323-
return err2
324-
}
325-
326-
if isDefault {
327-
err2 = ctx.Repo.GitRepo.SetDefaultBranch(form.To)
328-
if err2 != nil {
329-
return err2
330-
}
331-
}
332-
333-
return nil
334-
}); err != nil {
335-
ctx.ServerError("RenameBranch", err)
336319
return
337320
}
338321

339-
notification.NotifyDeleteRef(ctx.User, ctx.Repo.Repository, "branch", "refs/heads/"+form.From)
340-
notification.NotifyCreateRef(ctx.User, ctx.Repo.Repository, "branch", "refs/heads/"+form.To)
341-
342322
ctx.Flash.Success(ctx.Tr("repo.settings.rename_branch_success", form.From, form.To))
343323
ctx.Redirect(fmt.Sprintf("%s/settings/branches", ctx.Repo.RepoLink))
344324
}

services/repository/branch.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repository
6+
7+
import (
8+
"code.gitea.io/gitea/models"
9+
"code.gitea.io/gitea/modules/git"
10+
"code.gitea.io/gitea/modules/notification"
11+
)
12+
13+
// RenameBranch rename a branch
14+
func RenameBranch(repo *models.Repository, doer *models.User, gitRepo *git.Repository, from, to string) (string, error) {
15+
if from == to {
16+
return "target_exist", nil
17+
}
18+
19+
if gitRepo.IsBranchExist(to) {
20+
return "target_exist", nil
21+
}
22+
23+
if !gitRepo.IsBranchExist(from) {
24+
return "from_not_exist", nil
25+
}
26+
27+
if err := repo.RenameBranch(from, to, func(isDefault bool) error {
28+
err2 := gitRepo.RenameBranch(from, to)
29+
if err2 != nil {
30+
return err2
31+
}
32+
33+
if isDefault {
34+
err2 = gitRepo.SetDefaultBranch(to)
35+
if err2 != nil {
36+
return err2
37+
}
38+
}
39+
40+
return nil
41+
}); err != nil {
42+
return "", err
43+
}
44+
45+
notification.NotifyDeleteRef(doer, repo, "branch", "refs/heads/"+from)
46+
notification.NotifyCreateRef(doer, repo, "branch", "refs/heads/"+to)
47+
48+
return "", nil
49+
}

0 commit comments

Comments
 (0)