diff options
author | a1012112796 <1012112796@qq.com> | 2021-10-09 01:03:04 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-08 19:03:04 +0200 |
commit | bb393596689ee7c33ecb041806ae2c9e8dc5dfab (patch) | |
tree | c6b71f73e53d5d5c1bb1da9e47e917f23538cd3f /models/branches.go | |
parent | 56d79301b9f212e7801cbced1475238cc61c0748 (diff) | |
download | gitea-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 'models/branches.go')
-rw-r--r-- | models/branches.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/models/branches.go b/models/branches.go index 8eaa4b6fd7..3c62c7a87b 100644 --- a/models/branches.go +++ b/models/branches.go @@ -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 @@ -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() +} |