aboutsummaryrefslogtreecommitdiffstats
path: root/models/git
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2024-04-12 18:11:16 +0800
committerGitHub <noreply@github.com>2024-04-12 13:11:16 +0300
commit9466fec879f4f2c88c7c1e7a5cffba319282ab66 (patch)
treeb51fbda349aea5a39afe4c1286556dc4313cb135 /models/git
parentf9fdac9809335729b2ac3227b2a5f71a62fc64ad (diff)
downloadgitea-9466fec879f4f2c88c7c1e7a5cffba319282ab66.tar.gz
gitea-9466fec879f4f2c88c7c1e7a5cffba319282ab66.zip
Fix rename branch 500 when the target branch is deleted but exist in database (#30430)
Fix #30428
Diffstat (limited to 'models/git')
-rw-r--r--models/git/branch.go31
1 files changed, 25 insertions, 6 deletions
diff --git a/models/git/branch.go b/models/git/branch.go
index fa0781fed1..2979dff3d2 100644
--- a/models/git/branch.go
+++ b/models/git/branch.go
@@ -297,6 +297,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
sess := db.GetEngine(ctx)
+ // check whether from branch exist
var branch Branch
exist, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, from).Get(&branch)
if err != nil {
@@ -308,6 +309,24 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
}
}
+ // check whether to branch exist or is_deleted
+ var dstBranch Branch
+ exist, err = db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, to).Get(&dstBranch)
+ if err != nil {
+ return err
+ }
+ if exist {
+ if !dstBranch.IsDeleted {
+ return ErrBranchAlreadyExists{
+ BranchName: to,
+ }
+ }
+
+ if _, err := db.GetEngine(ctx).ID(dstBranch.ID).NoAutoCondition().Delete(&dstBranch); err != nil {
+ return err
+ }
+ }
+
// 1. update branch in database
if n, err := sess.Where("repo_id=? AND name=?", repo.ID, from).Update(&Branch{
Name: to,
@@ -362,12 +381,7 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
return err
}
- // 5. do git action
- if err = gitAction(ctx, isDefault); err != nil {
- return err
- }
-
- // 6. insert renamed branch record
+ // 5. insert renamed branch record
renamedBranch := &RenamedBranch{
RepoID: repo.ID,
From: from,
@@ -378,6 +392,11 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
return err
}
+ // 6. do git action
+ if err = gitAction(ctx, isDefault); err != nil {
+ return err
+ }
+
return committer.Commit()
}