Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option to disable push to main branches #4054

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+).*"
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions pkg/gui/controllers/sync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down
2 changes: 2 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type TranslationSet struct {
ForcePushDisabled string
UpdatesRejected string
UpdatesRejectedAndForcePushDisabled string
MainBranchPushDisabled string
CheckForUpdate string
CheckingForUpdates string
UpdateAvailableTitle string
Expand Down Expand Up @@ -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!",
Expand Down
5 changes: 5 additions & 0 deletions schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down