-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add a simple way to rename branch like gh #15870
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
Changes from 8 commits
30ee9c7
b51c6a7
7059da8
83ab12d
6034e84
7d79bba
9e5d9aa
71983a2
c01d4d2
7216645
076c913
2be6c91
57fc738
ddae9e4
5408d40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// 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" | ||
"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) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// check new branch link | ||
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", postData) | ||
session.MakeRequest(t, req, http.StatusOK) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// check old branch link | ||
req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", postData) | ||
resp = session.MakeRequest(t, req, http.StatusFound) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
location := resp.HeaderMap.Get("Location") | ||
assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location) | ||
|
||
// check db | ||
repo1 := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) | ||
assert.Equal(t, "main", repo1.DefaultBranch) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -571,3 +571,83 @@ func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) { | |
log.Error("DeletedBranchesCleanup: %v", err) | ||
} | ||
} | ||
|
||
// RenamedBranch proivde renamed branch log | ||
// will check it when an branch can't be found | ||
a1012112796 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type RenamedBranch struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"INDEX NOT NULL"` | ||
From string | ||
To string | ||
CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
} | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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 = x.Get(branch) | ||
|
||
return | ||
} | ||
|
||
// RenameBranch rename a branch | ||
func (repo *Repository) RenameBranch(from, to string, gitAction func(isDefault bool) error) (err error) { | ||
sess := x.NewSession() | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I seeing this wrong, or is it a bad idea to automatically apply branch protection to a "new" branch without testing if that branch should be protected?
Furthermore, if I see that correctly, the type of branch protection also needs to be switched then if this branch is covered by another rule. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
sadly, gitea don't have the feature to protect more than one branches by one rule. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, thanks for the clarification. I did not need to set up branch protection on Gitea until now, I thought it was implemented just as in GitHub using a pattern for branches. Gitea's approach definitely has its advantages as well as its disadvantages. |
||
_, 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() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
- | ||
id: 1 | ||
repo_id: 1 | ||
from: dev | ||
to: master |
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)) | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -603,6 +603,7 @@ func RegisterRoutes(m *web.Route) { | |||||||||||||||||
m.Combo("/*").Get(repo.SettingsProtectedBranch). | ||||||||||||||||||
Post(bindIgnErr(forms.ProtectBranchForm{}), context.RepoMustNotBeArchived(), repo.SettingsProtectedBranchPost) | ||||||||||||||||||
}, repo.MustBeNotEmpty) | ||||||||||||||||||
m.Post("/rename_branch", bindIgnErr(forms.RenameBranchForm{}), context.RepoMustNotBeArchived(), repo.RenameBranchPost) | ||||||||||||||||||
Comment on lines
612
to
+615
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I have seen, the underscore (
Suggested change
Combined with a change inside the UI dialog. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well I dont mind since it's only a web route - if you like to group settings bit more refactor pulls are welcome :) |
||||||||||||||||||
|
||||||||||||||||||
m.Group("/tags", func() { | ||||||||||||||||||
m.Get("", repo.Tags) | ||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.