Browse Source

repo/pull: allow rebase before merging (#4805)

pull/4762/merge
Unknwon 7 years ago
parent
commit
34c2e52bd5
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 5
      conf/locale/locale_en-US.ini
  2. 36
      models/pull.go
  3. 2
      models/repo.go
  4. 4
      pkg/bindata/bindata.go
  5. 2
      pkg/form/repo.go
  6. 6
      routes/repo/pull.go
  7. 2
      routes/repo/setting.go
  8. 33
      templates/repo/issue/view_content.tmpl
  9. 4
      templates/repo/settings/options.tmpl

5
conf/locale/locale_en-US.ini

@ -632,8 +632,9 @@ pulls.is_checking = The conflict checking is still in progress, please refresh p
pulls.can_auto_merge_desc = This pull request can be merged automatically. pulls.can_auto_merge_desc = This pull request can be merged automatically.
pulls.cannot_auto_merge_desc = This pull request can't be merged automatically because there are conflicts. pulls.cannot_auto_merge_desc = This pull request can't be merged automatically because there are conflicts.
pulls.cannot_auto_merge_helper = Please merge manually in order to resolve the conflicts. pulls.cannot_auto_merge_helper = Please merge manually in order to resolve the conflicts.
pulls.create_merge_commit = Create a merge commit
pulls.rebase_before_merging = Rebase before merging
pulls.merge_pull_request = Merge Pull Request pulls.merge_pull_request = Merge Pull Request
pulls.rebase_merge_pull_request = Rebase and Merge Pull Request
pulls.open_unmerged_pull_exists = `You can't perform reopen operation because there is already an open pull request (#%d) from same repository with same merge information and is waiting for merging.` pulls.open_unmerged_pull_exists = `You can't perform reopen operation because there is already an open pull request (#%d) from same repository with same merge information and is waiting for merging.`
pulls.delete_branch = Delete Branch pulls.delete_branch = Delete Branch
pulls.delete_branch_has_new_commits = Branch cannot be deleted because it has new commits after mergence. pulls.delete_branch_has_new_commits = Branch cannot be deleted because it has new commits after mergence.
@ -738,7 +739,7 @@ settings.tracker_issue_style.numeric = Numeric
settings.tracker_issue_style.alphanumeric = Alphanumeric settings.tracker_issue_style.alphanumeric = Alphanumeric
settings.tracker_url_format_desc = You can use placeholder <code>{user} {repo} {index}</code> for user name, repository name and issue index. settings.tracker_url_format_desc = You can use placeholder <code>{user} {repo} {index}</code> for user name, repository name and issue index.
settings.pulls_desc = Enable pull requests to accept public contributions settings.pulls_desc = Enable pull requests to accept public contributions
settings.use_rebase_desc = Use rebase to merge pull requests settings.pulls.allow_rebase_merge = Allow use rebase to merge commits
settings.danger_zone = Danger Zone settings.danger_zone = Danger Zone
settings.cannot_fork_to_same_owner = You cannot fork a repository to its original owner. settings.cannot_fork_to_same_owner = You cannot fork a repository to its original owner.
settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name. settings.new_owner_has_same_repo = The new owner already has a repository with same name. Please choose another name.

36
models/pull.go

@ -183,9 +183,17 @@ func (pr *PullRequest) CanAutoMerge() bool {
return pr.Status == PULL_REQUEST_STATUS_MERGEABLE return pr.Status == PULL_REQUEST_STATUS_MERGEABLE
} }
// MergeStyle represents the approach to merge commits into base branch.
type MergeStyle string
const (
MERGE_STYLE_REGULAR MergeStyle = "create_merge_commit"
MERGE_STYLE_REBASE MergeStyle = "rebase_before_merging"
)
// Merge merges pull request to base repository. // Merge merges pull request to base repository.
// FIXME: add repoWorkingPull make sure two merges does not happen at same time. // FIXME: add repoWorkingPull make sure two merges does not happen at same time.
func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error) { func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle MergeStyle) (err error) {
defer func() { defer func() {
go HookQueue.Add(pr.BaseRepo.ID) go HookQueue.Add(pr.BaseRepo.ID)
go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false) go AddTestPullRequestTask(doer, pr.BaseRepo.ID, pr.BaseBranch, false)
@ -239,15 +247,13 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
} }
if (pr.BaseRepo.PullUseRebase) { // Check if merge style is allowed, reset to default style if not
// Rebase. if mergeStyle == MERGE_STYLE_REBASE && !pr.BaseRepo.PullsAllowRebase {
if _, stderr, err = process.ExecDir(-1, tmpBasePath, mergeStyle = MERGE_STYLE_REGULAR
fmt.Sprintf("PullRequest.Rebase (git rebase): %s", tmpBasePath), }
"git", "rebase", "-q", pr.BaseBranch, "head_repo/"+pr.HeadBranch); err != nil {
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) switch mergeStyle {
} case MERGE_STYLE_REGULAR: // Create merge commit
} else {
// Merge commits.
if _, stderr, err = process.ExecDir(-1, tmpBasePath, if _, stderr, err = process.ExecDir(-1, tmpBasePath,
fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath), fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath),
"git", "merge", "--no-ff", "--no-commit", "head_repo/"+pr.HeadBranch); err != nil { "git", "merge", "--no-ff", "--no-commit", "head_repo/"+pr.HeadBranch); err != nil {
@ -261,6 +267,16 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error
"-m", fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.HeadUserName, pr.HeadRepo.Name, pr.BaseBranch)); err != nil { "-m", fmt.Sprintf("Merge branch '%s' of %s/%s into %s", pr.HeadBranch, pr.HeadUserName, pr.HeadRepo.Name, pr.BaseBranch)); err != nil {
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr) return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
} }
case MERGE_STYLE_REBASE: // Rebase before merging
if _, stderr, err = process.ExecDir(-1, tmpBasePath,
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
"git", "rebase", "-q", pr.BaseBranch, "head_repo/"+pr.HeadBranch); err != nil {
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
}
default:
return fmt.Errorf("unknown merge style: %s", mergeStyle)
} }
// Push back to upstream. // Push back to upstream.

2
models/repo.go

@ -184,7 +184,7 @@ type Repository struct {
ExternalTrackerStyle string ExternalTrackerStyle string
ExternalMetas map[string]string `xorm:"-"` ExternalMetas map[string]string `xorm:"-"`
EnablePulls bool `xorm:"NOT NULL DEFAULT true"` EnablePulls bool `xorm:"NOT NULL DEFAULT true"`
PullUseRebase bool `xorm:"NOT NULL DEFAULT false"` PullsAllowRebase bool `xorm:"NOT NULL DEFAULT false"`
IsFork bool `xorm:"NOT NULL DEFAULT false"` IsFork bool `xorm:"NOT NULL DEFAULT false"`
ForkID int64 ForkID int64

4
pkg/bindata/bindata.go

File diff suppressed because one or more lines are too long

2
pkg/form/repo.go

@ -102,7 +102,7 @@ type RepoSetting struct {
TrackerURLFormat string TrackerURLFormat string
TrackerIssueStyle string TrackerIssueStyle string
EnablePulls bool EnablePulls bool
PullUseRebase bool PullsAllowRebase bool
} }
func (f *RepoSetting) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { func (f *RepoSetting) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {

6
routes/repo/pull.go

@ -389,7 +389,7 @@ func MergePullRequest(c *context.Context) {
return return
} }
if issue.IsClosed { if issue.IsClosed {
c.Handle(404, "MergePullRequest", nil) c.NotFound()
return return
} }
@ -400,13 +400,13 @@ func MergePullRequest(c *context.Context) {
} }
if !pr.CanAutoMerge() || pr.HasMerged { if !pr.CanAutoMerge() || pr.HasMerged {
c.Handle(404, "MergePullRequest", nil) c.NotFound()
return return
} }
pr.Issue = issue pr.Issue = issue
pr.Issue.Repo = c.Repo.Repository pr.Issue.Repo = c.Repo.Repository
if err = pr.Merge(c.User, c.Repo.GitRepo); err != nil { if err = pr.Merge(c.User, c.Repo.GitRepo, models.MergeStyle(c.Query("merge_style"))); err != nil {
c.ServerError("Merge", err) c.ServerError("Merge", err)
return return
} }

2
routes/repo/setting.go

@ -147,7 +147,7 @@ func SettingsPost(c *context.Context, f form.RepoSetting) {
repo.ExternalTrackerFormat = f.TrackerURLFormat repo.ExternalTrackerFormat = f.TrackerURLFormat
repo.ExternalTrackerStyle = f.TrackerIssueStyle repo.ExternalTrackerStyle = f.TrackerIssueStyle
repo.EnablePulls = f.EnablePulls repo.EnablePulls = f.EnablePulls
repo.PullUseRebase = f.PullUseRebase repo.PullsAllowRebase = f.PullsAllowRebase
if err := models.UpdateRepository(repo, false); err != nil { if err := models.UpdateRepository(repo, false); err != nil {
c.ServerError("UpdateRepository", err) c.ServerError("UpdateRepository", err)

33
templates/repo/issue/view_content.tmpl

@ -190,20 +190,29 @@
<span class="octicon octicon-check"></span> <span class="octicon octicon-check"></span>
{{$.i18n.Tr "repo.pulls.can_auto_merge_desc"}} {{$.i18n.Tr "repo.pulls.can_auto_merge_desc"}}
</div> </div>
{{if .IsRepositoryWriter}} {{if .IsRepositoryWriter}}
<div class="ui divider"></div> <div class="ui divider"></div>
<div> <form class="ui form" action="{{.Link}}/merge" method="post">
<form class="ui form" action="{{.Link}}/merge" method="post"> {{.CSRFTokenHTML}}
{{.CSRFTokenHTML}} <div class="field">
<button class="ui green button"> <div class="ui radio checkbox">
{{if .Issue.Repo.PullUseRebase }} <input type="radio" name="merge_style" value="create_merge_commit" checked="checked">
<span class="octicon octicon-git-pull-request"></span> {{$.i18n.Tr "repo.pulls.rebase_merge_pull_request"}} <label>{{$.i18n.Tr "repo.pulls.create_merge_commit"}}</label>
{{else}} </div>
<span class="octicon octicon-git-merge"></span> {{$.i18n.Tr "repo.pulls.merge_pull_request"}} </div>
{{end}} {{if .Issue.Repo.PullsAllowRebase}}
</button> <div class="field">
</form> <div class="ui radio checkbox">
</div> <input type="radio" name="merge_style" value="rebase_before_merging">
<label>{{$.i18n.Tr "repo.pulls.rebase_before_merging"}}</label>
</div>
</div>
{{end}}
<button class="ui green button">
<span class="octicon octicon-git-merge"></span> {{$.i18n.Tr "repo.pulls.merge_pull_request"}}
</button>
</form>
{{end}} {{end}}
{{else}} {{else}}
<div class="item text red"> <div class="item text red">

4
templates/repo/settings/options.tmpl

@ -199,8 +199,8 @@
</div> </div>
<div class="ui segment field {{if not .Repository.EnablePulls}}disabled{{end}}" id="pull_box"> <div class="ui segment field {{if not .Repository.EnablePulls}}disabled{{end}}" id="pull_box">
<div class="ui checkbox"> <div class="ui checkbox">
<input name="pull_use_rebase" type="checkbox" {{if .Repository.PullUseRebase}}checked{{end}}> <input name="pulls_allow_rebase" type="checkbox" {{if .Repository.PullsAllowRebase}}checked{{end}}>
<label>{{.i18n.Tr "repo.settings.use_rebase_desc"}}</label> <label>{{.i18n.Tr "repo.settings.pulls.allow_rebase_merge"}}</label>
</div> </div>
</div> </div>
{{end}} {{end}}

Loading…
Cancel
Save