Skip to content

Commit

Permalink
Merge branch 'main' into try-remote-prune-in-mirror-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
zeripath authored Oct 10, 2021
2 parents ce4f8f0 + ff9a8a2 commit fd69e99
Show file tree
Hide file tree
Showing 18 changed files with 398 additions and 2 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ This changelog goes through all the changes that have been made in each release
without substantial changes to our git log; to see the highlights of what has
been added to each release, please refer to the [blog](https://blog.gitea.io).

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

## [1.15.3](https://github.com/go-gitea/gitea/releases/tag/v1.15.3) - 2021-09-19

* ENHANCEMENTS
Expand Down
2 changes: 1 addition & 1 deletion docs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ params:
description: Git with a cup of tea
author: The Gitea Authors
website: https://docs.gitea.io
version: 1.15.3
version: 1.15.4
minGoVersion: 1.16
goVersion: 1.17
minNodeVersion: 12.17
Expand Down
44 changes: 44 additions & 0 deletions integrations/rename_branch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"net/http"
"testing"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
"github.com/stretchr/testify/assert"
)

func TestRenameBranch(t *testing.T) {
// get branch setting page
session := loginUser(t, "user2")
req := NewRequest(t, "GET", "/user2/repo1/settings/branches")
resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)

postData := map[string]string{
"_csrf": htmlDoc.GetCSRF(),
"from": "master",
"to": "main",
}
req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", postData)
session.MakeRequest(t, req, http.StatusFound)

// check new branch link
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", postData)
session.MakeRequest(t, req, http.StatusOK)

// check old branch link
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", postData)
resp = session.MakeRequest(t, req, http.StatusFound)
location := resp.HeaderMap.Get("Location")
assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location)

// check db
repo1 := db.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
assert.Equal(t, "main", repo1.DefaultBranch)
}
81 changes: 81 additions & 0 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ProtectedBranch struct {
func init() {
db.RegisterModel(new(ProtectedBranch))
db.RegisterModel(new(DeletedBranch))
db.RegisterModel(new(RenamedBranch))
}

// IsProtected returns if the branch is protected
Expand Down Expand Up @@ -588,3 +589,83 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
log.Error("DeletedBranchesCleanup: %v", err)
}
}

// RenamedBranch provide renamed branch log
// will check it when a branch can't be found
type RenamedBranch struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"INDEX NOT NULL"`
From string
To string
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

// FindRenamedBranch check if a branch was renamed
func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist bool, err error) {
branch = &RenamedBranch{
RepoID: repoID,
From: from,
}
exist, err = db.GetEngine(db.DefaultContext).Get(branch)

return
}

// RenameBranch rename a branch
func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) {
sess := db.NewSession(db.DefaultContext)
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

// 1. update default branch if needed
isDefault := repo.DefaultBranch == from
if isDefault {
repo.DefaultBranch = to
_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo)
if err != nil {
return err
}
}

// 2. Update protected branch if needed
protectedBranch, err := getProtectedBranchBy(sess, repo.ID, from)
if err != nil {
return err
}

if protectedBranch != nil {
protectedBranch.BranchName = to
_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch)
if err != nil {
return err
}
}

// 3. Update all not merged pull request base branch name
_, err = sess.Table(new(PullRequest)).Where("base_repo_id=? AND base_branch=? AND has_merged=?",
repo.ID, from, false).
Update(map[string]interface{}{"base_branch": to})
if err != nil {
return err
}

// 4. do git action
if err = gitAction(isDefault); err != nil {
return err
}

// 5. insert renamed branch record
renamedBranch := &RenamedBranch{
RepoID: repo.ID,
From: from,
To: to,
}
_, err = sess.Insert(renamedBranch)
if err != nil {
return err
}

return sess.Commit()
}
49 changes: 49 additions & 0 deletions models/branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,52 @@ func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {

return deletedBranch
}

func TestFindRenamedBranch(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
branch, exist, err := FindRenamedBranch(1, "dev")
assert.NoError(t, err)
assert.Equal(t, true, exist)
assert.Equal(t, "master", branch.To)

_, exist, err = FindRenamedBranch(1, "unknow")
assert.NoError(t, err)
assert.Equal(t, false, exist)
}

func TestRenameBranch(t *testing.T) {
assert.NoError(t, db.PrepareTestDatabase())
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
_isDefault := false

err := UpdateProtectBranch(repo1, &ProtectedBranch{
RepoID: repo1.ID,
BranchName: "master",
}, WhitelistOptions{})
assert.NoError(t, err)

assert.NoError(t, repo1.RenameBranch("master", "main", func(isDefault bool) error {
_isDefault = isDefault
return nil
}))

assert.Equal(t, true, _isDefault)
repo1 = db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
assert.Equal(t, "main", repo1.DefaultBranch)

pull := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged
assert.Equal(t, "master", pull.BaseBranch)

pull = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open
assert.Equal(t, "main", pull.BaseBranch)

renamedBranch := db.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch)
assert.Equal(t, "master", renamedBranch.From)
assert.Equal(t, "main", renamedBranch.To)
assert.Equal(t, int64(1), renamedBranch.RepoID)

db.AssertExistsAndLoadBean(t, &ProtectedBranch{
RepoID: repo1.ID,
BranchName: "main",
})
}
5 changes: 5 additions & 0 deletions models/fixtures/renamed_branch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-
id: 1
repo_id: 1
from: dev
to: master
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ var migrations = []Migration{
NewMigration("Add table commit_status_index", addTableCommitStatusIndex),
// v196 -> v197
NewMigration("Add Color to ProjectBoard table", addColorColToProjectBoard),
// v197 -> v198
NewMigration("Add renamed_branch table", addRenamedBranchTable),
}

// GetCurrentDBVersion returns the current db version
Expand Down
20 changes: 20 additions & 0 deletions models/migrations/v197.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/xorm"
)

func addRenamedBranchTable(x *xorm.Engine) error {
type RenamedBranch struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"INDEX NOT NULL"`
From string
To string
CreatedUnix int64 `xorm:"created"`
}
return x.Sync2(new(RenamedBranch))
}
32 changes: 31 additions & 1 deletion modules/context/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,28 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
ctx.Repo.TreePath = path
return ctx.Repo.Repository.DefaultBranch
case RepoRefBranch:
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBranchExist)
ref := getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsBranchExist)
if len(ref) == 0 {
// maybe it's a renamed branch
return getRefNameFromPath(ctx, path, func(s string) bool {
b, exist, err := models.FindRenamedBranch(ctx.Repo.Repository.ID, s)
if err != nil {
log.Error("FindRenamedBranch", err)
return false
}

if !exist {
return false
}

ctx.Data["IsRenamedBranch"] = true
ctx.Data["RenamedBranchName"] = b.To

return true
})
}

return ref
case RepoRefTag:
return getRefNameFromPath(ctx, path, ctx.Repo.GitRepo.IsTagExist)
case RepoRefCommit:
Expand Down Expand Up @@ -784,6 +805,15 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
} else {
refName = getRefName(ctx, refType)
ctx.Repo.BranchName = refName
isRenamedBranch, has := ctx.Data["IsRenamedBranch"].(bool)
if isRenamedBranch && has {
renamedBranchName := ctx.Data["RenamedBranchName"].(string)
ctx.Flash.Info(ctx.Tr("repo.branch.renamed", refName, renamedBranchName))
link := strings.Replace(ctx.Req.RequestURI, refName, renamedBranchName, 1)
ctx.Redirect(link)
return
}

if refType.RefTypeIncludesBranches() && ctx.Repo.GitRepo.IsBranchExist(refName) {
ctx.Repo.IsViewBranch = true

Expand Down
6 changes: 6 additions & 0 deletions modules/git/repo_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ func (repo *Repository) RemoveRemote(name string) error {
func (branch *Branch) GetCommit() (*Commit, error) {
return branch.gitRepo.GetBranchCommit(branch.Name)
}

// RenameBranch rename a branch
func (repo *Repository) RenameBranch(from, to string) error {
_, err := NewCommand("branch", "-m", from, to).RunInDir(repo.Path)
return err
}
7 changes: 7 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,12 @@ settings.lfs_pointers.inRepo=In Repo
settings.lfs_pointers.exists=Exists in store
settings.lfs_pointers.accessible=Accessible to User
settings.lfs_pointers.associateAccessible=Associate accessible %d OIDs
settings.rename_branch_failed_exist=Cannot rename branch because target branch %s exists.
settings.rename_branch_failed_not_exist=Cannot rename branch %s because it does not exist.
settings.rename_branch_success =Branch %s was successfully renamed to %s.
settings.rename_branch_from=old branch name
settings.rename_branch_to=new branch name
settings.rename_branch=Rename branch

diff.browse_source = Browse Source
diff.parent = parent
Expand Down Expand Up @@ -2106,6 +2112,7 @@ branch.create_new_branch = Create branch from branch:
branch.confirm_create_branch = Create branch
branch.new_branch = Create new branch
branch.new_branch_from = Create new branch from '%s'
branch.renamed = Branch %s was renamed to %s.

tag.create_tag = Create tag <strong>%s</strong>
tag.create_success = Tag '%s' has been created.
Expand Down
5 changes: 5 additions & 0 deletions options/locale/locale_ru-RU.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,10 @@ settings.lfs_pointers.inRepo=В репозитории
settings.lfs_pointers.exists=Существуют в хранилище
settings.lfs_pointers.accessible=Доступно для пользователя
settings.lfs_pointers.associateAccessible=Связать доступные %d OID
settings.rename_branch_success=Ветка %s была успешно переименована в %s.
settings.rename_branch_from=старое название ветки
settings.rename_branch_to=новое название ветки
settings.rename_branch=Переименовать ветку

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

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

0 comments on commit fd69e99

Please sign in to comment.