Skip to content

Commit 828ceb0

Browse files
committed
Add merge style fast-forward-only
With this option, it is possible to require a linear commit history with the following benefits over the next best option Rebase+fast-forward: the original commits continue existing, with the original signatures continuing to stay valid instead of being rewritten, there is no merge commit, and reverting commits becomes easier. Closes #24906
1 parent 34633d8 commit 828ceb0

File tree

23 files changed

+96
-11
lines changed

23 files changed

+96
-11
lines changed

custom/conf/app.example.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ LEVEL = Info
10441044
;; List of keywords used in Pull Request comments to automatically reopen a related issue
10451045
;REOPEN_KEYWORDS = reopen,reopens,reopened
10461046
;;
1047-
;; Set default merge style for repository creating, valid options: merge, rebase, rebase-merge, squash
1047+
;; Set default merge style for repository creating, valid options: merge, rebase, rebase-merge, squash, fast-forward-only
10481048
;DEFAULT_MERGE_STYLE = merge
10491049
;;
10501050
;; In the default merge message for squash commits include at most this many commits

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ In addition, there is _`StaticRootPath`_ which can be set as a built-in at build
126126
keywords used in Pull Request comments to automatically close a related issue
127127
- `REOPEN_KEYWORDS`: **reopen**, **reopens**, **reopened**: List of keywords used in Pull Request comments to automatically reopen
128128
a related issue
129-
- `DEFAULT_MERGE_STYLE`: **merge**: Set default merge style for repository creating, valid options: `merge`, `rebase`, `rebase-merge`, `squash`
129+
- `DEFAULT_MERGE_STYLE`: **merge**: Set default merge style for repository creating, valid options: `merge`, `rebase`, `rebase-merge`, `squash`, `fast-forward-only`
130130
- `DEFAULT_MERGE_MESSAGE_COMMITS_LIMIT`: **50**: In the default merge message for squash commits include at most this many commits. Set to `-1` to include all commits
131131
- `DEFAULT_MERGE_MESSAGE_SIZE`: **5120**: In the default merge message for squash commits limit the size of the commit messages. Set to `-1` to have no limit. Only used if `POPULATE_SQUASH_COMMENT_WITH_COMMIT_MESSAGES` is `true`.
132132
- `DEFAULT_MERGE_MESSAGE_ALL_AUTHORS`: **false**: In the default merge message for squash commits walk all commits to include all authors in the Co-authored-by otherwise just use those in the limited list

docs/content/administration/config-cheat-sheet.zh-cn.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ menu:
125125
- `CLOSE_KEYWORDS`: **close**, **closes**, **closed**, **fix**, **fixes**, **fixed**, **resolve**, **resolves**, **resolved**: 在拉取请求评论中用于自动关闭相关问题的关键词列表。
126126
- `REOPEN_KEYWORDS`: **reopen**, **reopens**, **reopened**: 在拉取请求评论中用于自动重新打开相关问题的
127127
关键词列表。
128-
- `DEFAULT_MERGE_STYLE`: **merge**: 设置创建仓库的默认合并方式,可选: `merge`, `rebase`, `rebase-merge`, `squash`
128+
- `DEFAULT_MERGE_STYLE`: **merge**: 设置创建仓库的默认合并方式,可选: `merge`, `rebase`, `rebase-merge`, `squash`, `fast-forward-only`
129129
- `DEFAULT_MERGE_MESSAGE_COMMITS_LIMIT`: **50**: 在默认合并消息中,对于`squash`提交,最多包括此数量的提交。设置为 -1 以包括所有提交。
130130
- `DEFAULT_MERGE_MESSAGE_SIZE`: **5120**: 在默认的合并消息中,对于`squash`提交,限制提交消息的大小。设置为 `-1`以取消限制。仅在`POPULATE_SQUASH_COMMENT_WITH_COMMIT_MESSAGES``true`时使用。
131131
- `DEFAULT_MERGE_MESSAGE_ALL_AUTHORS`: **false**: 在默认合并消息中,对于`squash`提交,遍历所有提交以包括所有作者的`Co-authored-by`,否则仅使用限定列表中的作者。

models/repo/git.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
MergeStyleRebaseMerge MergeStyle = "rebase-merge"
2222
// MergeStyleSquash squash commits into single commit before merging
2323
MergeStyleSquash MergeStyle = "squash"
24+
// MergeStyleFastForwardOnly fast-forward merge if possible, otherwise fail
25+
MergeStyleFastForwardOnly MergeStyle = "fast-forward-only"
2426
// MergeStyleManuallyMerged pr has been merged manually, just mark it as merged directly
2527
MergeStyleManuallyMerged MergeStyle = "manually-merged"
2628
// MergeStyleRebaseUpdate not a merge style, used to update pull head by rebase

models/repo/repo_unit.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ type PullRequestsConfig struct {
122122
AllowRebase bool
123123
AllowRebaseMerge bool
124124
AllowSquash bool
125+
AllowFastForwardOnly bool
125126
AllowManualMerge bool
126127
AutodetectManualMerge bool
127128
AllowRebaseUpdate bool
@@ -148,6 +149,7 @@ func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool {
148149
mergeStyle == MergeStyleRebase && cfg.AllowRebase ||
149150
mergeStyle == MergeStyleRebaseMerge && cfg.AllowRebaseMerge ||
150151
mergeStyle == MergeStyleSquash && cfg.AllowSquash ||
152+
mergeStyle == MergeStyleFastForwardOnly && cfg.AllowFastForwardOnly ||
151153
mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge
152154
}
153155

modules/git/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (err ErrBranchNotExist) Unwrap() error {
9696
return util.ErrNotExist
9797
}
9898

99-
// ErrPushOutOfDate represents an error if merging fails due to unrelated histories
99+
// ErrPushOutOfDate represents an error if merging fails due to the base branch being updated
100100
type ErrPushOutOfDate struct {
101101
StdOut string
102102
StdErr string

modules/repository/create.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, re
8787
units = append(units, repo_model.RepoUnit{
8888
RepoID: repo.ID,
8989
Type: tp,
90-
Config: &repo_model.PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, DefaultMergeStyle: repo_model.MergeStyle(setting.Repository.PullRequest.DefaultMergeStyle), AllowRebaseUpdate: true},
90+
Config: &repo_model.PullRequestsConfig{
91+
AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, AllowFastForwardOnly: true,
92+
DefaultMergeStyle: repo_model.MergeStyle(setting.Repository.PullRequest.DefaultMergeStyle),
93+
AllowRebaseUpdate: true,
94+
},
9195
})
9296
} else {
9397
units = append(units, repo_model.RepoUnit{

modules/structs/repo.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type Repository struct {
9898
AllowRebase bool `json:"allow_rebase"`
9999
AllowRebaseMerge bool `json:"allow_rebase_explicit"`
100100
AllowSquash bool `json:"allow_squash_merge"`
101+
AllowFastForwardOnly bool `json:"allow_fast_forward_only_merge"`
101102
AllowRebaseUpdate bool `json:"allow_rebase_update"`
102103
DefaultDeleteBranchAfterMerge bool `json:"default_delete_branch_after_merge"`
103104
DefaultMergeStyle string `json:"default_merge_style"`
@@ -195,6 +196,8 @@ type EditRepoOption struct {
195196
AllowRebaseMerge *bool `json:"allow_rebase_explicit,omitempty"`
196197
// either `true` to allow squash-merging pull requests, or `false` to prevent squash-merging.
197198
AllowSquash *bool `json:"allow_squash_merge,omitempty"`
199+
// either `true` to allow fast-forward-only merging pull requests, or `false` to prevent fast-forward-only merging.
200+
AllowFastForwardOnly *bool `json:"allow_fast_forward_only_merge,omitempty"`
198201
// either `true` to allow mark pr as merged manually, or `false` to prevent it.
199202
AllowManualMerge *bool `json:"allow_manual_merge,omitempty"`
200203
// either `true` to enable AutodetectManualMerge, or `false` to prevent it. Note: In some special cases, misjudgments can occur.
@@ -203,7 +206,7 @@ type EditRepoOption struct {
203206
AllowRebaseUpdate *bool `json:"allow_rebase_update,omitempty"`
204207
// set to `true` to delete pr branch after merge by default
205208
DefaultDeleteBranchAfterMerge *bool `json:"default_delete_branch_after_merge,omitempty"`
206-
// set to a merge style to be used by this repository: "merge", "rebase", "rebase-merge", or "squash".
209+
// set to a merge style to be used by this repository: "merge", "rebase", "rebase-merge", "squash", or "fast-forward-only".
207210
DefaultMergeStyle *string `json:"default_merge_style,omitempty"`
208211
// set to `true` to allow edits from maintainers by default
209212
DefaultAllowMaintainerEdit *bool `json:"default_allow_maintainer_edit,omitempty"`

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,7 @@ pulls.merge_pull_request = Create merge commit
17751775
pulls.rebase_merge_pull_request = Rebase then fast-forward
17761776
pulls.rebase_merge_commit_pull_request = Rebase then create merge commit
17771777
pulls.squash_merge_pull_request = Create squash commit
1778+
pulls.fast_forward_only_merge_pull_request = Fast-forward
17781779
pulls.merge_manually = Manually merged
17791780
pulls.merge_commit_id = The merge commit ID
17801781
pulls.require_signed_wont_sign = The branch requires signed commits but this merge will not be signed

routers/api/v1/repo/repo.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
884884
AllowRebase: true,
885885
AllowRebaseMerge: true,
886886
AllowSquash: true,
887+
AllowFastForwardOnly: true,
887888
AllowManualMerge: true,
888889
AutodetectManualMerge: false,
889890
AllowRebaseUpdate: true,
@@ -910,6 +911,9 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
910911
if opts.AllowSquash != nil {
911912
config.AllowSquash = *opts.AllowSquash
912913
}
914+
if opts.AllowFastForwardOnly != nil {
915+
config.AllowFastForwardOnly = *opts.AllowFastForwardOnly
916+
}
913917
if opts.AllowManualMerge != nil {
914918
config.AllowManualMerge = *opts.AllowManualMerge
915919
}

0 commit comments

Comments
 (0)