Skip to content

Commit

Permalink
Add config option to disable push to main branches
Browse files Browse the repository at this point in the history
  • Loading branch information
trskare authored and d33pc0re committed Nov 9, 2024
1 parent b0a766c commit 37d7441
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
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

0 comments on commit 37d7441

Please sign in to comment.