Skip to content

Commit fd69e99

Browse files
authored
Merge branch 'main' into try-remote-prune-in-mirror-sync
2 parents ce4f8f0 + ff9a8a2 commit fd69e99

File tree

18 files changed

+398
-2
lines changed

18 files changed

+398
-2
lines changed

CHANGELOG.md

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ This changelog goes through all the changes that have been made in each release
44
without substantial changes to our git log; to see the highlights of what has
55
been added to each release, please refer to the [blog](https://blog.gitea.io).
66

7+
## [1.15.4](https://github.com/go-gitea/gitea/releases/tag/v1.15.4) - 2021-10-08
8+
* BUGFIXES
9+
* Raw file API: don't try to interpret 40char filenames as commit SHA (#17185) (#17272)
10+
* Don't allow merged PRs to be reopened (#17192) (#17271)
11+
* Fix incorrect repository count on organization tab of dashboard (#17256) (#17266)
12+
* Fix unwanted team review request deletion (#17257) (#17264)
13+
* Fix broken Activities link in team dashboard (#17255) (#17258)
14+
* API pull's head/base have correct permission(#17214) (#17245)
15+
* Fix stange behavior of DownloadPullDiffOrPatch in incorect index (#17223) (#17227)
16+
* Upgrade xorm to v1.2.5 (#17177) (#17188)
17+
* Fix missing repo link in issue/pull assigned emails (#17183) (#17184)
18+
* Fix bug of get context user (#17169) (#17172)
19+
* Nicely handle missing user in collaborations (#17049) (#17166)
20+
* Add Horizontal scrollbar to inner menu on Chrome (#17086) (#17164)
21+
* Fix wrong i18n keys (#17150) (#17153)
22+
* Fix Archive Creation: correct transaction ending (#17151)
23+
* Prevent panic in Org mode HighlightCodeBlock (#17140) (#17141)
24+
* Create doctor command to fix repo_units broken by dumps from 1.14.3-1.14.6 (#17136) (#17137)
25+
* ENHANCEMENT
26+
* Check user instead of organization when creating a repo from a template via API (#16346) (#17195)
27+
* TRANSLATION
28+
* v1.15 fix Sprintf format 'verbs' in locale files (#17187)
29+
730
## [1.15.3](https://github.com/go-gitea/gitea/releases/tag/v1.15.3) - 2021-09-19
831

932
* ENHANCEMENTS

docs/config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ params:
1818
description: Git with a cup of tea
1919
author: The Gitea Authors
2020
website: https://docs.gitea.io
21-
version: 1.15.3
21+
version: 1.15.4
2222
minGoVersion: 1.16
2323
goVersion: 1.17
2424
minNodeVersion: 12.17

integrations/rename_branch_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/models/db"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestRenameBranch(t *testing.T) {
17+
// get branch setting page
18+
session := loginUser(t, "user2")
19+
req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
20+
resp := session.MakeRequest(t, req, http.StatusOK)
21+
htmlDoc := NewHTMLParser(t, resp.Body)
22+
23+
postData := map[string]string{
24+
"_csrf": htmlDoc.GetCSRF(),
25+
"from": "master",
26+
"to": "main",
27+
}
28+
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", postData)
29+
session.MakeRequest(t, req, http.StatusFound)
30+
31+
// check new branch link
32+
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", postData)
33+
session.MakeRequest(t, req, http.StatusOK)
34+
35+
// check old branch link
36+
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", postData)
37+
resp = session.MakeRequest(t, req, http.StatusFound)
38+
location := resp.HeaderMap.Get("Location")
39+
assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location)
40+
41+
// check db
42+
repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
43+
assert.Equal(t, "main", repo1.DefaultBranch)
44+
}

models/branches.go

+81
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type ProtectedBranch struct {
5353
func init() {
5454
db.RegisterModel(new(ProtectedBranch))
5555
db.RegisterModel(new(DeletedBranch))
56+
db.RegisterModel(new(RenamedBranch))
5657
}
5758

5859
// IsProtected returns if the branch is protected
@@ -588,3 +589,83 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
588589
log.Error("DeletedBranchesCleanup: %v", err)
589590
}
590591
}
592+
593+
// RenamedBranch provide renamed branch log
594+
// will check it when a branch can't be found
595+
type RenamedBranch struct {
596+
ID int64 `xorm:"pk autoincr"`
597+
RepoID int64 `xorm:"INDEX NOT NULL"`
598+
From string
599+
To string
600+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
601+
}
602+
603+
// FindRenamedBranch check if a branch was renamed
604+
func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist bool, err error) {
605+
branch = &RenamedBranch{
606+
RepoID: repoID,
607+
From: from,
608+
}
609+
exist, err = db.GetEngine(db.DefaultContext).Get(branch)
610+
611+
return
612+
}
613+
614+
// RenameBranch rename a branch
615+
func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) {
616+
sess := db.NewSession(db.DefaultContext)
617+
defer sess.Close()
618+
if err := sess.Begin(); err != nil {
619+
return err
620+
}
621+
622+
// 1. update default branch if needed
623+
isDefault := repo.DefaultBranch == from
624+
if isDefault {
625+
repo.DefaultBranch = to
626+
_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo)
627+
if err != nil {
628+
return err
629+
}
630+
}
631+
632+
// 2. Update protected branch if needed
633+
protectedBranch, err := getProtectedBranchBy(sess, repo.ID, from)
634+
if err != nil {
635+
return err
636+
}
637+
638+
if protectedBranch != nil {
639+
protectedBranch.BranchName = to
640+
_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch)
641+
if err != nil {
642+
return err
643+
}
644+
}
645+
646+
// 3. Update all not merged pull request base branch name
647+
_, err = sess.Table(new(PullRequest)).Where("base_repo_id=? AND base_branch=? AND has_merged=?",
648+
repo.ID, from, false).
649+
Update(map[string]interface{}{"base_branch": to})
650+
if err != nil {
651+
return err
652+
}
653+
654+
// 4. do git action
655+
if err = gitAction(isDefault); err != nil {
656+
return err
657+
}
658+
659+
// 5. insert renamed branch record
660+
renamedBranch := &RenamedBranch{
661+
RepoID: repo.ID,
662+
From: from,
663+
To: to,
664+
}
665+
_, err = sess.Insert(renamedBranch)
666+
if err != nil {
667+
return err
668+
}
669+
670+
return sess.Commit()
671+
}

models/branches_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,52 @@ func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {
7979

8080
return deletedBranch
8181
}
82+
83+
func TestFindRenamedBranch(t *testing.T) {
84+
assert.NoError(t, db.PrepareTestDatabase())
85+
branch, exist, err := FindRenamedBranch(1, "dev")
86+
assert.NoError(t, err)
87+
assert.Equal(t, true, exist)
88+
assert.Equal(t, "master", branch.To)
89+
90+
_, exist, err = FindRenamedBranch(1, "unknow")
91+
assert.NoError(t, err)
92+
assert.Equal(t, false, exist)
93+
}
94+
95+
func TestRenameBranch(t *testing.T) {
96+
assert.NoError(t, db.PrepareTestDatabase())
97+
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
98+
_isDefault := false
99+
100+
err := UpdateProtectBranch(repo1, &ProtectedBranch{
101+
RepoID: repo1.ID,
102+
BranchName: "master",
103+
}, WhitelistOptions{})
104+
assert.NoError(t, err)
105+
106+
assert.NoError(t, repo1.RenameBranch("master", "main", func(isDefault bool) error {
107+
_isDefault = isDefault
108+
return nil
109+
}))
110+
111+
assert.Equal(t, true, _isDefault)
112+
repo1 = db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
113+
assert.Equal(t, "main", repo1.DefaultBranch)
114+
115+
pull := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged
116+
assert.Equal(t, "master", pull.BaseBranch)
117+
118+
pull = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open
119+
assert.Equal(t, "main", pull.BaseBranch)
120+
121+
renamedBranch := db.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch)
122+
assert.Equal(t, "master", renamedBranch.From)
123+
assert.Equal(t, "main", renamedBranch.To)
124+
assert.Equal(t, int64(1), renamedBranch.RepoID)
125+
126+
db.AssertExistsAndLoadBean(t, &ProtectedBranch{
127+
RepoID: repo1.ID,
128+
BranchName: "main",
129+
})
130+
}

models/fixtures/renamed_branch.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-
2+
id: 1
3+
repo_id: 1
4+
from: dev
5+
to: master

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ var migrations = []Migration{
346346
NewMigration("Add table commit_status_index", addTableCommitStatusIndex),
347347
// v196 -> v197
348348
NewMigration("Add Color to ProjectBoard table", addColorColToProjectBoard),
349+
// v197 -> v198
350+
NewMigration("Add renamed_branch table", addRenamedBranchTable),
349351
}
350352

351353
// GetCurrentDBVersion returns the current db version

models/migrations/v197.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func addRenamedBranchTable(x *xorm.Engine) error {
12+
type RenamedBranch struct {
13+
ID int64 `xorm:"pk autoincr"`
14+
RepoID int64 `xorm:"INDEX NOT NULL"`
15+
From string
16+
To string
17+
CreatedUnix int64 `xorm:"created"`
18+
}
19+
return x.Sync2(new(RenamedBranch))
20+
}

modules/context/repo.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,28 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
705705
ctx.Repo.TreePath = path
706706
return ctx.Repo.Repository.DefaultBranch
707707
case RepoRefBranch:
708-
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBranchExist)
708+
ref := getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBranchExist)
709+
if len(ref) == 0 {
710+
// maybe it's a renamed branch
711+
return getRefNameFromPath(ctx, path, func(s string) bool {
712+
b, exist, err := models.FindRenamedBranch(ctx.Repo.Repository.ID, s)
713+
if err != nil {
714+
log.Error("FindRenamedBranch", err)
715+
return false
716+
}
717+
718+
if !exist {
719+
return false
720+
}
721+
722+
ctx.Data["IsRenamedBranch"] = true
723+
ctx.Data["RenamedBranchName"] = b.To
724+
725+
return true
726+
})
727+
}
728+
729+
return ref
709730
case RepoRefTag:
710731
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
711732
case RepoRefCommit:
@@ -784,6 +805,15 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
784805
} else {
785806
refName = getRefName(ctx, refType)
786807
ctx.Repo.BranchName = refName
808+
isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)
809+
if isRenamedBranch && has {
810+
renamedBranchName := ctx.Data["RenamedBranchName"].(string)
811+
ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refName, renamedBranchName))
812+
link := strings.Replace(ctx.Req.RequestURI, refName, renamedBranchName, 1)
813+
ctx.Redirect(link)
814+
return
815+
}
816+
787817
if refType.RefTypeIncludesBranches() && ctx.Repo.GitRepo.IsBranchExist(refName) {
788818
ctx.Repo.IsViewBranch = true
789819

modules/git/repo_branch.go

+6
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,9 @@ func (repo *Repository) RemoveRemote(name string) error {
164164
func (branch *Branch) GetCommit() (*Commit, error) {
165165
return branch.gitRepo.GetBranchCommit(branch.Name)
166166
}
167+
168+
// RenameBranch rename a branch
169+
func (repo *Repository) RenameBranch(from, to string) error {
170+
_, err := NewCommand("branch", "-m", from, to).RunInDir(repo.Path)
171+
return err
172+
}

options/locale/locale_en-US.ini

+7
Original file line numberDiff line numberDiff line change
@@ -1985,6 +1985,12 @@ settings.lfs_pointers.inRepo=In Repo
19851985
settings.lfs_pointers.exists=Exists in store
19861986
settings.lfs_pointers.accessible=Accessible to User
19871987
settings.lfs_pointers.associateAccessible=Associate accessible %d OIDs
1988+
settings.rename_branch_failed_exist=Cannot rename branch because target branch %s exists.
1989+
settings.rename_branch_failed_not_exist=Cannot rename branch %s because it does not exist.
1990+
settings.rename_branch_success =Branch %s was successfully renamed to %s.
1991+
settings.rename_branch_from=old branch name
1992+
settings.rename_branch_to=new branch name
1993+
settings.rename_branch=Rename branch
19881994

19891995
diff.browse_source = Browse Source
19901996
diff.parent = parent
@@ -2106,6 +2112,7 @@ branch.create_new_branch = Create branch from branch:
21062112
branch.confirm_create_branch = Create branch
21072113
branch.new_branch = Create new branch
21082114
branch.new_branch_from = Create new branch from '%s'
2115+
branch.renamed = Branch %s was renamed to %s.
21092116

21102117
tag.create_tag = Create tag <strong>%s</strong>
21112118
tag.create_success = Tag '%s' has been created.

options/locale/locale_ru-RU.ini

+5
Original file line numberDiff line numberDiff line change
@@ -1982,6 +1982,10 @@ settings.lfs_pointers.inRepo=В репозитории
19821982
settings.lfs_pointers.exists=Существуют в хранилище
19831983
settings.lfs_pointers.accessible=Доступно для пользователя
19841984
settings.lfs_pointers.associateAccessible=Связать доступные %d OID
1985+
settings.rename_branch_success=Ветка %s была успешно переименована в %s.
1986+
settings.rename_branch_from=старое название ветки
1987+
settings.rename_branch_to=новое название ветки
1988+
settings.rename_branch=Переименовать ветку
19851989

19861990
diff.browse_source=Просмотр исходного кода
19871991
diff.parent=Родитель
@@ -2103,6 +2107,7 @@ branch.create_new_branch=Создать ветку из ветви:
21032107
branch.confirm_create_branch=Создать ветку
21042108
branch.new_branch=Создать новую ветку
21052109
branch.new_branch_from=Создать новую ветку из '%s'
2110+
branch.renamed=Ветка %s была переименована в %s.
21062111

21072112
tag.create_tag=Создать тег <strong>%s</strong>
21082113
tag.create_success=Тег '%s' был создан.

0 commit comments

Comments
 (0)