diff options
author | mharinder <mharinder@yandex.com> | 2017-10-01 05:37:24 -0700 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2017-11-15 23:28:23 -0500 |
commit | 5cd1fdeb9ee1af1179a859dc5ed65561bfe5a6d6 (patch) | |
tree | f0c36bfd3a763cf7489da93d4d015c8c7fd252b6 /models | |
parent | dbe6de313efdff942d16b82b63f02fa10d8fc170 (diff) |
pull: add an option to use rebase for merging pull requests
For DVCS, either merge or rebase works for getting new code in a pull
request in the main branch.
The rebase workflow produces a linear history which is cleaner, and
more bisect-able.
This commit adds a repo-level option to enable the rebase workflow. Once
enabled, "Merge Pull Request" will be replaced by
"Rebase and Merge Pull Request" which does exactly what the user wants.
It's unlikely a project wants a mixed-use of both rebase and merge
workflows, therefore the feature is not implemented as a drop-down
button like what GitHub does
(https://github.com/blog/2243-rebase-and-merge-pull-requests).
Diffstat (limited to 'models')
-rw-r--r-- | models/pull.go | 35 | ||||
-rw-r--r-- | models/repo.go | 1 |
2 files changed, 23 insertions, 13 deletions
diff --git a/models/pull.go b/models/pull.go index 27a843d5..bc03c804 100644 --- a/models/pull.go +++ b/models/pull.go @@ -233,31 +233,40 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository) (err error return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) } - // Merge commits. if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git fetch): %s", tmpBasePath), "git", "fetch", "head_repo"); err != nil { return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr) } - if _, stderr, err = process.ExecDir(-1, 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 { - return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr) - } + if (pr.BaseRepo.PullUseRebase) { + // Rebase. + if _, stderr, err = process.ExecDir(-1, tmpBasePath, + 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) + } + } else { + // Merge commits. + if _, stderr, err = process.ExecDir(-1, 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 { + return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr) + } - sig := doer.NewGitSig() - if _, stderr, err = process.ExecDir(-1, tmpBasePath, - fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath), - "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), - "-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) + sig := doer.NewGitSig() + if _, stderr, err = process.ExecDir(-1, tmpBasePath, + fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath), + "git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), + "-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) + } } // Push back to upstream. if _, stderr, err = process.ExecDir(-1, tmpBasePath, fmt.Sprintf("PullRequest.Merge (git push): %s", tmpBasePath), - "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil { + "git", "push", "head_repo", "HEAD:"+pr.BaseBranch); err != nil { return fmt.Errorf("git push: %s", stderr) } diff --git a/models/repo.go b/models/repo.go index 998914d5..7e9bd916 100644 --- a/models/repo.go +++ b/models/repo.go @@ -184,6 +184,7 @@ type Repository struct { ExternalTrackerStyle string ExternalMetas map[string]string `xorm:"-"` EnablePulls bool `xorm:"NOT NULL DEFAULT true"` + PullUseRebase bool `xorm:"NOT NULL DEFAULT false"` IsFork bool `xorm:"NOT NULL DEFAULT false"` ForkID int64 |