diff --git a/integrations/pull_merge_test.go b/integrations/pull_merge_test.go index b375a55f5..f466515eb 100644 --- a/integrations/pull_merge_test.go +++ b/integrations/pull_merge_test.go @@ -77,6 +77,19 @@ func TestPullRebase(t *testing.T) { testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleRebase) } +func TestPullRebaseMerge(t *testing.T) { + prepareTestEnv(t) + session := loginUser(t, "user1") + testRepoFork(t, session, "user2", "repo1", "user1", "repo1") + testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n") + + resp := testPullCreate(t, session, "user1", "repo1", "master", "This is a pull title") + + elem := strings.Split(test.RedirectURL(resp), "/") + assert.EqualValues(t, "pulls", elem[3]) + testPullMerge(t, session, elem[1], elem[2], elem[4], models.MergeStyleRebaseMerge) +} + func TestPullSquash(t *testing.T) { prepareTestEnv(t) session := loginUser(t, "user1") diff --git a/models/fixtures/repo_unit.yml b/models/fixtures/repo_unit.yml index 24d77b999..f494cdd1b 100644 --- a/models/fixtures/repo_unit.yml +++ b/models/fixtures/repo_unit.yml @@ -30,7 +30,7 @@ id: 5 repo_id: 1 type: 3 - config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowSquash\":true}" + config: "{\"IgnoreWhitespaceConflicts\":false,\"AllowMerge\":true,\"AllowRebase\":true,\"AllowRebaseMerge\":true,\"AllowSquash\":true}" created_unix: 946684810 - @@ -51,7 +51,7 @@ id: 8 repo_id: 3 type: 3 - config: "{\"IgnoreWhitespaceConflicts\":true,\"AllowMerge\":true,\"AllowRebase\":false,\"AllowSquash\":false}" + config: "{\"IgnoreWhitespaceConflicts\":true,\"AllowMerge\":true,\"AllowRebase\":false,\"AllowRebaseMerge\":true,\"AllowSquash\":false}" created_unix: 946684810 - diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 5a9c80a82..1fa94c42a 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -204,6 +204,8 @@ var migrations = []Migration{ NewMigration("add approval whitelists to protected branches", addApprovalWhitelistsToProtectedBranches), // v75 -> v76 NewMigration("clear nonused data which not deleted when user was deleted", clearNonusedData), + // v76 -> v77 + NewMigration("add pull request rebase with merge commit", addPullRequestRebaseWithMerge), } // Migrate database to current version diff --git a/models/migrations/v76.go b/models/migrations/v76.go new file mode 100644 index 000000000..efab7e0cf --- /dev/null +++ b/models/migrations/v76.go @@ -0,0 +1,63 @@ +// Copyright 2018 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 ( + "fmt" + + "code.gitea.io/gitea/modules/util" + + "github.com/go-xorm/xorm" +) + +func addPullRequestRebaseWithMerge(x *xorm.Engine) error { + // RepoUnit describes all units of a repository + type RepoUnit struct { + ID int64 + RepoID int64 `xorm:"INDEX(s)"` + Type int `xorm:"INDEX(s)"` + Config map[string]interface{} `xorm:"JSON"` + CreatedUnix util.TimeStamp `xorm:"INDEX CREATED"` + } + + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + //Updating existing issue units + units := make([]*RepoUnit, 0, 100) + if err := sess.Where("`type` = ?", V16UnitTypePRs).Find(&units); err != nil { + return fmt.Errorf("Query repo units: %v", err) + } + for _, unit := range units { + if unit.Config == nil { + unit.Config = make(map[string]interface{}) + } + // Allow the new merge style if all other merge styles are allowed + allowMergeRebase := true + + if allowMerge, ok := unit.Config["AllowMerge"]; ok { + allowMergeRebase = allowMergeRebase && allowMerge.(bool) + } + + if allowRebase, ok := unit.Config["AllowRebase"]; ok { + allowMergeRebase = allowMergeRebase && allowRebase.(bool) + } + + if allowSquash, ok := unit.Config["AllowSquash"]; ok { + allowMergeRebase = allowMergeRebase && allowSquash.(bool) + } + + if _, ok := unit.Config["AllowRebaseMerge"]; !ok { + unit.Config["AllowRebaseMerge"] = allowMergeRebase + } + if _, err := sess.ID(unit.ID).Cols("config").Update(unit); err != nil { + return err + } + } + return sess.Commit() +} diff --git a/models/pull.go b/models/pull.go index 4c4c3f3e1..cd21e494a 100644 --- a/models/pull.go +++ b/models/pull.go @@ -297,6 +297,8 @@ const ( MergeStyleMerge MergeStyle = "merge" // MergeStyleRebase rebase before merging MergeStyleRebase MergeStyle = "rebase" + // MergeStyleRebaseMerge rebase before merging with merge commit (--no-ff) + MergeStyleRebaseMerge MergeStyle = "rebase-merge" // MergeStyleSquash squash commits into single commit before merging MergeStyleSquash MergeStyle = "squash" ) @@ -434,6 +436,41 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle "git", "merge", "--ff-only", "-q", "head_repo_"+pr.HeadBranch); err != nil { return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) } + case MergeStyleRebaseMerge: + // Checkout head branch + if _, stderr, err = process.GetManager().ExecDir(-1, tmpBasePath, + fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath), + "git", "checkout", "-b", "head_repo_"+pr.HeadBranch, "head_repo/"+pr.HeadBranch); err != nil { + return fmt.Errorf("git checkout: %s", stderr) + } + // Rebase before merging + if _, stderr, err = process.GetManager().ExecDir(-1, tmpBasePath, + fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath), + "git", "rebase", "-q", pr.BaseBranch); err != nil { + return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) + } + // Checkout base branch again + if _, stderr, err = process.GetManager().ExecDir(-1, tmpBasePath, + fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath), + "git", "checkout", pr.BaseBranch); err != nil { + return fmt.Errorf("git checkout: %s", stderr) + } + // Prepare merge with commit + if _, stderr, err = process.GetManager().ExecDir(-1, tmpBasePath, + fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath), + "git", "merge", "--no-ff", "--no-commit", "-q", "head_repo_"+pr.HeadBranch); err != nil { + return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) + } + + // Set custom message and author and create merge commit + sig := doer.NewGitSig() + if _, stderr, err = process.GetManager().ExecDir(-1, tmpBasePath, + fmt.Sprintf("PullRequest.Merge (git commit): %s", tmpBasePath), + "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), + "-m", message); err != nil { + return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr) + } + case MergeStyleSquash: // Merge with squash if _, stderr, err = process.GetManager().ExecDir(-1, tmpBasePath, diff --git a/models/repo.go b/models/repo.go index 564790e57..cd861d750 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1298,7 +1298,7 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err units = append(units, RepoUnit{ RepoID: repo.ID, Type: tp, - Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowSquash: true}, + Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true}, }) } else { units = append(units, RepoUnit{ diff --git a/models/repo_unit.go b/models/repo_unit.go index 9eaec884b..430f5a242 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -91,6 +91,7 @@ type PullRequestsConfig struct { IgnoreWhitespaceConflicts bool AllowMerge bool AllowRebase bool + AllowRebaseMerge bool AllowSquash bool } @@ -108,6 +109,7 @@ func (cfg *PullRequestsConfig) ToDB() ([]byte, error) { func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool { return mergeStyle == MergeStyleMerge && cfg.AllowMerge || mergeStyle == MergeStyleRebase && cfg.AllowRebase || + mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge || mergeStyle == MergeStyleSquash && cfg.AllowSquash } diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index f86506137..a600ecc8f 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -112,6 +112,7 @@ type RepoSettingForm struct { PullsIgnoreWhitespace bool PullsAllowMerge bool PullsAllowRebase bool + PullsAllowRebaseMerge bool PullsAllowSquash bool EnableTimetracker bool AllowOnlyContributorsToTrackTime bool @@ -363,7 +364,7 @@ func (f *InitializeLabelsForm) Validate(ctx *macaron.Context, errs binding.Error // MergePullRequestForm form for merging Pull Request type MergePullRequestForm struct { - Do string `binding:"Required;In(merge,rebase,squash)"` + Do string `binding:"Required;In(merge,rebase,rebase-merge,squash)"` MergeTitleField string MergeMessageField string } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 1a748e8b2..5013e0a94 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -868,6 +868,7 @@ pulls.no_merge_helper = Enable merge options in the repository settings or merge pulls.no_merge_wip = This pull request can not be merged because it is marked as being a work in progress. pulls.merge_pull_request = Merge Pull Request pulls.rebase_merge_pull_request = Rebase and Merge +pulls.rebase_merge_commit_pull_request = Rebase and Merge (--no-ff) pulls.squash_merge_pull_request = Squash and Merge pulls.invalid_merge_option = You cannot use this merge option for this pull request. pulls.open_unmerged_pull_exists = `You cannot perform a reopen operation because there is a pending pull request (#%d) with identical properties.` @@ -1013,6 +1014,7 @@ settings.pulls_desc = Enable Repository Pull Requests settings.pulls.ignore_whitespace = Ignore Whitespace for Conflicts settings.pulls.allow_merge_commits = Enable Commit Merging settings.pulls.allow_rebase_merge = Enable Rebasing to Merge Commits +settings.pulls.allow_rebase_merge_commit = Enable Rebasing with explicit merge commits (--no-ff) settings.pulls.allow_squash_commits = Enable Squashing to Merge Commits settings.admin_settings = Administrator Settings settings.admin_enable_health_check = Enable Repository Health Checks (git fsck) diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 3600120fb..f025cc388 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -843,6 +843,8 @@ func ViewIssue(ctx *context.Context) { ctx.Data["MergeStyle"] = models.MergeStyleMerge } else if prConfig.AllowRebase { ctx.Data["MergeStyle"] = models.MergeStyleRebase + } else if prConfig.AllowRebaseMerge { + ctx.Data["MergeStyle"] = models.MergeStyleRebaseMerge } else if prConfig.AllowSquash { ctx.Data["MergeStyle"] = models.MergeStyleSquash } else { diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 0d5cec802..b38ba3bf5 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -552,6 +552,9 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) { if models.MergeStyle(form.Do) == models.MergeStyleMerge { message = pr.GetDefaultMergeMessage() } + if models.MergeStyle(form.Do) == models.MergeStyleRebaseMerge { + message = pr.GetDefaultMergeMessage() + } if models.MergeStyle(form.Do) == models.MergeStyleSquash { message = pr.GetDefaultSquashMessage() } diff --git a/routers/repo/setting.go b/routers/repo/setting.go index f815171ac..2bb57a0e4 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -227,6 +227,7 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) { IgnoreWhitespaceConflicts: form.PullsIgnoreWhitespace, AllowMerge: form.PullsAllowMerge, AllowRebase: form.PullsAllowRebase, + AllowRebaseMerge: form.PullsAllowRebaseMerge, AllowSquash: form.PullsAllowSquash, }, }) diff --git a/templates/repo/issue/view_content/pull.tmpl b/templates/repo/issue/view_content/pull.tmpl index 5edde9051..72808d089 100644 --- a/templates/repo/issue/view_content/pull.tmpl +++ b/templates/repo/issue/view_content/pull.tmpl @@ -86,7 +86,7 @@ {{if .AllowMerge}} {{$prUnit := .Repository.MustGetUnit $.UnitTypePullRequests}} - {{if or $prUnit.PullRequestsConfig.AllowMerge $prUnit.PullRequestsConfig.AllowRebase $prUnit.PullRequestsConfig.AllowSquash}} + {{if or $prUnit.PullRequestsConfig.AllowMerge $prUnit.PullRequestsConfig.AllowRebase $prUnit.PullRequestsConfig.AllowRebaseMerge $prUnit.PullRequestsConfig.AllowSquash}}
{{if $prUnit.PullRequestsConfig.AllowMerge}} {{end}} + {{if $prUnit.PullRequestsConfig.AllowRebaseMerge}} + + {{end}} {{if $prUnit.PullRequestsConfig.AllowSquash}} +