summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authora1012112796 <1012112796@qq.com>2021-10-09 01:03:04 +0800
committerGitHub <noreply@github.com>2021-10-08 19:03:04 +0200
commitbb393596689ee7c33ecb041806ae2c9e8dc5dfab (patch)
treec6b71f73e53d5d5c1bb1da9e47e917f23538cd3f /modules
parent56d79301b9f212e7801cbced1475238cc61c0748 (diff)
downloadgitea-bb393596689ee7c33ecb041806ae2c9e8dc5dfab.tar.gz
gitea-bb393596689ee7c33ecb041806ae2c9e8dc5dfab.zip
Add a simple way to rename branch like gh (#15870)
- Update default branch if needed - Update protected branch if needed - Update all not merged pull request base branch name - Rename git branch - Record this rename work and auto redirect for old branch on ui Signed-off-by: a1012112796 <1012112796@qq.com> Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'modules')
-rw-r--r--modules/context/repo.go32
-rw-r--r--modules/git/repo_branch.go6
2 files changed, 37 insertions, 1 deletions
diff --git a/modules/context/repo.go b/modules/context/repo.go
index eceefd9e59..8972cd28bc 100644
--- a/modules/context/repo.go
+++ b/modules/context/repo.go
@@ -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:
@@ -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
diff --git a/modules/git/repo_branch.go b/modules/git/repo_branch.go
index 7c30b1fb20..96f692826e 100644
--- a/modules/git/repo_branch.go
+++ b/modules/git/repo_branch.go
@@ -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
+}