diff --git a/models/repo.go b/models/repo.go index 329e63cc4..c807916dd 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1086,7 +1086,7 @@ func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, overwriteO units = append(units, RepoUnit{ RepoID: repo.ID, Type: tp, - Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true}, + Config: &PullRequestsConfig{AllowMerge: true, AllowRebase: true, AllowRebaseMerge: true, AllowSquash: true, DefaultMergeStyle: MergeStyleMerge}, }) } else { units = append(units, RepoUnit{ diff --git a/models/repo_unit.go b/models/repo_unit.go index 7702697ff..1d54579a6 100644 --- a/models/repo_unit.go +++ b/models/repo_unit.go @@ -102,6 +102,7 @@ type PullRequestsConfig struct { AllowSquash bool AllowManualMerge bool AutodetectManualMerge bool + DefaultMergeStyle MergeStyle } // FromDB fills up a PullRequestsConfig from serialized format. @@ -125,6 +126,15 @@ func (cfg *PullRequestsConfig) IsMergeStyleAllowed(mergeStyle MergeStyle) bool { mergeStyle == MergeStyleManuallyMerged && cfg.AllowManualMerge } +// GetDefaultMergeStyle returns the default merge style for this pull request +func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle { + if len(cfg.DefaultMergeStyle) != 0 { + return cfg.DefaultMergeStyle + } + + return MergeStyleMerge +} + // AllowedMergeStyleCount returns the total count of allowed merge styles for the PullRequestsConfig func (cfg *PullRequestsConfig) AllowedMergeStyleCount() int { count := 0 diff --git a/modules/convert/repository.go b/modules/convert/repository.go index 813201ca6..63a22bb04 100644 --- a/modules/convert/repository.go +++ b/modules/convert/repository.go @@ -71,6 +71,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) allowRebase := false allowRebaseMerge := false allowSquash := false + defaultMergeStyle := models.MergeStyleMerge if unit, err := repo.GetUnit(models.UnitTypePullRequests); err == nil { config := unit.PullRequestsConfig() hasPullRequests = true @@ -79,6 +80,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) allowRebase = config.AllowRebase allowRebaseMerge = config.AllowRebaseMerge allowSquash = config.AllowSquash + defaultMergeStyle = config.GetDefaultMergeStyle() } hasProjects := false if _, err := repo.GetUnit(models.UnitTypeProjects); err == nil { @@ -139,6 +141,7 @@ func innerToRepo(repo *models.Repository, mode models.AccessMode, isParent bool) AllowRebase: allowRebase, AllowRebaseMerge: allowRebaseMerge, AllowSquash: allowSquash, + DefaultMergeStyle: string(defaultMergeStyle), AvatarURL: repo.AvatarLink(), Internal: !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePrivate, MirrorInterval: mirrorInterval, diff --git a/modules/forms/repo_form.go b/modules/forms/repo_form.go index 6c7c9bea1..d9eb06d19 100644 --- a/modules/forms/repo_form.go +++ b/modules/forms/repo_form.go @@ -140,6 +140,7 @@ type RepoSettingForm struct { PullsAllowRebaseMerge bool PullsAllowSquash bool PullsAllowManualMerge bool + PullsDefaultMergeStyle string EnableAutodetectManualMerge bool EnableTimetracker bool AllowOnlyContributorsToTrackTime bool diff --git a/modules/structs/repo.go b/modules/structs/repo.go index c47700cd0..c23bd1033 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -89,6 +89,7 @@ type Repository struct { AllowRebase bool `json:"allow_rebase"` AllowRebaseMerge bool `json:"allow_rebase_explicit"` AllowSquash bool `json:"allow_squash_merge"` + DefaultMergeStyle string `json:"default_merge_style"` AvatarURL string `json:"avatar_url"` Internal bool `json:"internal"` MirrorInterval string `json:"mirror_interval"` @@ -171,6 +172,8 @@ type EditRepoOption struct { AllowManualMerge *bool `json:"allow_manual_merge,omitempty"` // either `true` to enable AutodetectManualMerge, or `false` to prevent it. `has_pull_requests` must be `true`, Note: In some special cases, misjudgments can occur. AutodetectManualMerge *bool `json:"autodetect_manual_merge,omitempty"` + // set to a merge style to be used by this repository: "merge", "rebase", "rebase-merge", or "squash". `has_pull_requests` must be `true`. + DefaultMergeStyle *string `json:"default_merge_style,omitempty"` // set to `true` to archive this repository. Archived *bool `json:"archived,omitempty"` // set to a string like `8h30m0s` to set the mirror interval time diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index e679e1e87..3a8783930 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1791,6 +1791,7 @@ settings.block_on_official_review_requests_desc = Merging will not be possible w settings.block_outdated_branch = Block merge if pull request is outdated settings.block_outdated_branch_desc = Merging will not be possible when head branch is behind base branch. settings.default_branch_desc = Select a default repository branch for pull requests and code commits: +settings.default_merge_style_desc = Default merge style for pull requests: settings.choose_branch = Choose a branch… settings.no_protected_branch = There are no protected branches. settings.edit_protected_branch = Edit diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index b7ed2951a..c422feb04 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -731,6 +731,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { AllowSquash: true, AllowManualMerge: true, AutodetectManualMerge: false, + DefaultMergeStyle: models.MergeStyleMerge, } } else { config = unit.PullRequestsConfig() @@ -757,6 +758,9 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error { if opts.AutodetectManualMerge != nil { config.AutodetectManualMerge = *opts.AutodetectManualMerge } + if opts.DefaultMergeStyle != nil { + config.DefaultMergeStyle = models.MergeStyle(*opts.DefaultMergeStyle) + } units = append(units, models.RepoUnit{ RepoID: repo.ID, diff --git a/routers/repo/issue.go b/routers/repo/issue.go index 15459cd0e..c2969ca4b 100644 --- a/routers/repo/issue.go +++ b/routers/repo/issue.go @@ -1484,7 +1484,10 @@ func ViewIssue(ctx *context.Context) { // Check correct values and select default if ms, ok := ctx.Data["MergeStyle"].(models.MergeStyle); !ok || !prConfig.IsMergeStyleAllowed(ms) { - if prConfig.AllowMerge { + defaultMergeStyle := prConfig.GetDefaultMergeStyle() + if prConfig.IsMergeStyleAllowed(defaultMergeStyle) && !ok { + ctx.Data["MergeStyle"] = defaultMergeStyle + } else if prConfig.AllowMerge { ctx.Data["MergeStyle"] = models.MergeStyleMerge } else if prConfig.AllowRebase { ctx.Data["MergeStyle"] = models.MergeStyleRebase diff --git a/routers/repo/setting.go b/routers/repo/setting.go index dc14b69b3..8349164d4 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -322,6 +322,7 @@ func SettingsPost(ctx *context.Context) { AllowSquash: form.PullsAllowSquash, AllowManualMerge: form.PullsAllowManualMerge, AutodetectManualMerge: form.EnableAutodetectManualMerge, + DefaultMergeStyle: models.MergeStyle(form.PullsDefaultMergeStyle), }, }) } else if !models.UnitTypePullRequests.UnitGlobalDisabled() { diff --git a/templates/repo/settings/options.tmpl b/templates/repo/settings/options.tmpl index f944eb8d8..514a2f5c0 100644 --- a/templates/repo/settings/options.tmpl +++ b/templates/repo/settings/options.tmpl @@ -346,6 +346,39 @@ +
+ {{.i18n.Tr "repo.settings.default_merge_style_desc"}} +
+