diff --git a/docs/Config.md b/docs/Config.md index d63987f065f..4e31914de0f 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -333,6 +333,9 @@ git: # If true, do not allow force pushes disableForcePushing: false + # If true, do not allow pushes to main branches + disableMainBranchPushing: false + # See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix commitPrefix: # pattern to match on. E.g. for 'feature/AB-123' to match on the AB-123 use "^\\w+\\/(\\w+-\\w+).*" diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index b02a959f531..60ee090309b 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -246,6 +246,8 @@ type GitConfig struct { OverrideGpg bool `yaml:"overrideGpg"` // If true, do not allow force pushes DisableForcePushing bool `yaml:"disableForcePushing"` + // If true, do not allow pushes to main branches + DisableMainBranchPushing bool `yaml:"disableMainBranchPushing"` // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix CommitPrefix *CommitPrefixConfig `yaml:"commitPrefix"` // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix @@ -771,6 +773,7 @@ func GetDefaultConfig() *UserConfig { BranchLogCmd: "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --", AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium", DisableForcePushing: false, + DisableMainBranchPushing: false, CommitPrefixes: map[string]CommitPrefixConfig(nil), BranchPrefix: "", ParseEmoji: false, diff --git a/pkg/gui/controllers/sync_controller.go b/pkg/gui/controllers/sync_controller.go index a6140d9d015..8cae7c71458 100644 --- a/pkg/gui/controllers/sync_controller.go +++ b/pkg/gui/controllers/sync_controller.go @@ -87,6 +87,16 @@ func (self *SyncController) branchCheckedOut(f func(*models.Branch) error) func( } func (self *SyncController) push(currentBranch *models.Branch) error { + mainBranchPushDisabled := self.c.UserConfig().Git.DisableMainBranchPushing + mainBranches := self.c.UserConfig().Git.MainBranches + if mainBranchPushDisabled { + for _, branch := range mainBranches { + if currentBranch.Name == branch { + return errors.New(self.c.Tr.MainBranchPushDisabled) + } + } + } + // if we are behind our upstream branch we'll ask if the user wants to force push if currentBranch.IsTrackingRemote() { opts := pushOpts{remoteBranchStoredLocally: currentBranch.RemoteBranchStoredLocally()} diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index e141a614ccb..67583e91daf 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -208,6 +208,7 @@ type TranslationSet struct { ForcePushDisabled string UpdatesRejected string UpdatesRejectedAndForcePushDisabled string + MainBranchPushDisabled string CheckForUpdate string CheckingForUpdates string UpdateAvailableTitle string @@ -1190,6 +1191,7 @@ func EnglishTranslationSet() *TranslationSet { ForcePushDisabled: "Your branch has diverged from the remote branch and you've disabled force pushing", UpdatesRejected: "Updates were rejected. Please fetch and examine the remote changes before pushing again.", UpdatesRejectedAndForcePushDisabled: "Updates were rejected and you have disabled force pushing", + MainBranchPushDisabled: "Push to main branches has been disabled", CheckForUpdate: "Check for update", CheckingForUpdates: "Checking for updates...", UpdateAvailableTitle: "Update available!", diff --git a/schema/config.json b/schema/config.json index 876a5bab9b5..6923e72f618 100644 --- a/schema/config.json +++ b/schema/config.json @@ -620,6 +620,11 @@ "description": "If true, do not allow force pushes", "default": false }, + "disableMainBranchPushing": { + "type": "boolean", + "description": "If true, do not allow pushes to main branches", + "default": false + }, "commitPrefix": { "properties": { "pattern": {