Convert skipHookPrefix to skipHookPrefixes (string to []string)

This commit is contained in:
Alexander Slavschik 2026-03-04 09:33:07 +00:00
parent c183f1ea8c
commit 9462b881f6
10 changed files with 322 additions and 85 deletions

View file

@ -396,9 +396,12 @@ git:
- master
- main
# Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks
# will be skipped when the commit message starts with 'WIP'
skipHookPrefix: WIP
# Prefixes to use when skipping hooks. E.g. if set to ['WIP'], then pre-commit
# hooks will be skipped when the commit message starts with 'WIP'. The first
# entry in the array will be used for the "Commit changes without pre-commit
# hook" command.
skipHookPrefixes:
- WIP
# If true, periodically fetch from remote
autoFetch: true

View file

@ -6,6 +6,7 @@ import (
"github.com/go-errors/errors"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/samber/lo"
)
var ErrInvalidCommitIndex = errors.New("invalid commit index")
@ -85,11 +86,16 @@ func (self *CommitCommands) ResetToCommit(hash string, strength string, envVars
Run()
}
func (self *CommitCommands) hasSkipHookPrefix(subject string) bool {
return lo.SomeBy(self.UserConfig().Git.SkipHookPrefixes, func(prefix string) bool {
return strings.HasPrefix(subject, prefix)
})
}
func (self *CommitCommands) CommitCmdObj(summary string, description string, forceSkipHooks bool) *oscommands.CmdObj {
messageArgs := self.commitMessageArgs(summary, description)
skipHookPrefix := self.UserConfig().Git.SkipHookPrefix
cmdArgs := NewGitCmd("commit").
ArgIf(forceSkipHooks || (skipHookPrefix != "" && strings.HasPrefix(summary, skipHookPrefix)), "--no-verify").
ArgIf(forceSkipHooks || self.hasSkipHookPrefix(summary), "--no-verify").
ArgIf(self.signoffFlag() != "", self.signoffFlag()).
Arg(messageArgs...).
ToArgv()
@ -287,7 +293,10 @@ func (self *CommitCommands) Revert(hashes []string, isMerge bool) error {
// CreateFixupCommit creates a commit that fixes up a previous commit
func (self *CommitCommands) CreateFixupCommit(hash string) error {
cmdArgs := NewGitCmd("commit").Arg("--fixup=" + hash).ToArgv()
cmdArgs := NewGitCmd("commit").
ArgIf(self.hasSkipHookPrefix("fixup! "), "--no-verify").
Arg("--fixup=" + hash).
ToArgv()
return self.cmd.New(cmdArgs).Run()
}
@ -299,6 +308,7 @@ func (self *CommitCommands) CreateAmendCommit(originalSubject, newSubject, newDe
description += "\n\n" + newDescription
}
cmdArgs := NewGitCmd("commit").
ArgIf(self.hasSkipHookPrefix("amend! "), "--no-verify").
Arg("-m", "amend! "+originalSubject).
Arg("-m", description).
ArgIf(!includeFileChanges, "--only", "--allow-empty").

View file

@ -51,72 +51,80 @@ func TestCommitResetToCommit(t *testing.T) {
func TestCommitCommitCmdObj(t *testing.T) {
type scenario struct {
testName string
summary string
forceSkipHooks bool
description string
configSignoff bool
configSkipHookPrefix string
expectedArgs []string
testName string
summary string
forceSkipHooks bool
description string
configSignoff bool
configSkipHookPrefixes []string
expectedArgs []string
}
scenarios := []scenario{
{
testName: "Commit",
summary: "test",
forceSkipHooks: false,
configSignoff: false,
configSkipHookPrefix: "",
expectedArgs: []string{"commit", "-m", "test"},
testName: "Commit",
summary: "test",
forceSkipHooks: false,
configSignoff: false,
configSkipHookPrefixes: nil,
expectedArgs: []string{"commit", "-m", "test"},
},
{
testName: "Commit with --no-verify flag < only prefix",
summary: "WIP: test",
forceSkipHooks: false,
configSignoff: false,
configSkipHookPrefix: "WIP",
expectedArgs: []string{"commit", "--no-verify", "-m", "WIP: test"},
testName: "Commit with --no-verify flag < only prefix",
summary: "WIP: test",
forceSkipHooks: false,
configSignoff: false,
configSkipHookPrefixes: []string{"WIP"},
expectedArgs: []string{"commit", "--no-verify", "-m", "WIP: test"},
},
{
testName: "Commit with --no-verify flag < skip flag and prefix",
summary: "WIP: test",
forceSkipHooks: true,
configSignoff: false,
configSkipHookPrefix: "WIP",
expectedArgs: []string{"commit", "--no-verify", "-m", "WIP: test"},
testName: "Commit with --no-verify flag < skip flag and prefix",
summary: "WIP: test",
forceSkipHooks: true,
configSignoff: false,
configSkipHookPrefixes: []string{"WIP"},
expectedArgs: []string{"commit", "--no-verify", "-m", "WIP: test"},
},
{
testName: "Commit with --no-verify flag < skip flag no prefix",
summary: "test",
forceSkipHooks: true,
configSignoff: false,
configSkipHookPrefix: "WIP",
expectedArgs: []string{"commit", "--no-verify", "-m", "test"},
testName: "Commit with --no-verify flag < skip flag no prefix",
summary: "test",
forceSkipHooks: true,
configSignoff: false,
configSkipHookPrefixes: []string{"WIP"},
expectedArgs: []string{"commit", "--no-verify", "-m", "test"},
},
{
testName: "Commit with multiline message",
summary: "line1",
forceSkipHooks: false,
description: "line2",
configSignoff: false,
configSkipHookPrefix: "",
expectedArgs: []string{"commit", "-m", "line1", "-m", "line2"},
testName: "Commit with multiline message",
summary: "line1",
forceSkipHooks: false,
description: "line2",
configSignoff: false,
configSkipHookPrefixes: nil,
expectedArgs: []string{"commit", "-m", "line1", "-m", "line2"},
},
{
testName: "Commit with signoff",
summary: "test",
forceSkipHooks: false,
configSignoff: true,
configSkipHookPrefix: "",
expectedArgs: []string{"commit", "--signoff", "-m", "test"},
testName: "Commit with signoff",
summary: "test",
forceSkipHooks: false,
configSignoff: true,
configSkipHookPrefixes: nil,
expectedArgs: []string{"commit", "--signoff", "-m", "test"},
},
{
testName: "Commit with signoff and no-verify",
summary: "WIP: test",
forceSkipHooks: true,
configSignoff: true,
configSkipHookPrefix: "WIP",
expectedArgs: []string{"commit", "--no-verify", "--signoff", "-m", "WIP: test"},
testName: "Commit with signoff and no-verify",
summary: "WIP: test",
forceSkipHooks: true,
configSignoff: true,
configSkipHookPrefixes: []string{"WIP"},
expectedArgs: []string{"commit", "--no-verify", "--signoff", "-m", "WIP: test"},
},
{
testName: "Commit with multiple prefixes, second matches",
summary: "fixup! some commit",
forceSkipHooks: false,
configSignoff: false,
configSkipHookPrefixes: []string{"WIP", "fixup!", "squash!"},
expectedArgs: []string{"commit", "--no-verify", "-m", "fixup! some commit"},
},
}
@ -124,7 +132,7 @@ func TestCommitCommitCmdObj(t *testing.T) {
t.Run(s.testName, func(t *testing.T) {
userConfig := config.GetDefaultConfig()
userConfig.Git.Commit.SignOff = s.configSignoff
userConfig.Git.SkipHookPrefix = s.configSkipHookPrefix
userConfig.Git.SkipHookPrefixes = s.configSkipHookPrefixes
runner := oscommands.NewFakeRunner(t).ExpectGitArgs(s.expectedArgs, "", nil)
instance := buildCommitCommands(commonDeps{userConfig: userConfig, runner: runner})
@ -171,10 +179,11 @@ func TestCommitCommitEditorCmdObj(t *testing.T) {
func TestCommitCreateFixupCommit(t *testing.T) {
type scenario struct {
testName string
hash string
runner *oscommands.FakeCmdObjRunner
test func(error)
testName string
hash string
userConfig *config.UserConfig
runner *oscommands.FakeCmdObjRunner
test func(error)
}
scenarios := []scenario{
@ -187,11 +196,47 @@ func TestCommitCreateFixupCommit(t *testing.T) {
assert.NoError(t, err)
},
},
{
testName: "with matching skipHookPrefixes",
hash: "12345",
userConfig: &config.UserConfig{
Git: config.GitConfig{SkipHookPrefixes: []string{"fixup!"}},
},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "--no-verify", "--fixup=12345"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
testName: "with non-matching skipHookPrefixes",
hash: "12345",
userConfig: &config.UserConfig{
Git: config.GitConfig{SkipHookPrefixes: []string{"WIP"}},
},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "--fixup=12345"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
{
testName: "with multiple prefixes including fixup!",
hash: "12345",
userConfig: &config.UserConfig{
Git: config.GitConfig{SkipHookPrefixes: []string{"WIP", "fixup!"}},
},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "--no-verify", "--fixup=12345"}, "", nil),
test: func(err error) {
assert.NoError(t, err)
},
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
instance := buildCommitCommands(commonDeps{runner: s.runner})
instance := buildCommitCommands(commonDeps{runner: s.runner, userConfig: s.userConfig})
s.test(instance.CreateFixupCommit(s.hash))
s.runner.CheckForMissingCalls()
})
@ -205,6 +250,7 @@ func TestCommitCreateAmendCommit(t *testing.T) {
newSubject string
newDescription string
includeFileChanges bool
userConfig *config.UserConfig
runner *oscommands.FakeCmdObjRunner
}
@ -236,11 +282,47 @@ func TestCommitCreateAmendCommit(t *testing.T) {
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "-m", "amend! original subject", "-m", "new subject", "--only", "--allow-empty"}, "", nil),
},
{
testName: "with matching skipHookPrefixes",
originalSubject: "original subject",
newSubject: "new subject",
newDescription: "",
includeFileChanges: true,
userConfig: &config.UserConfig{
Git: config.GitConfig{SkipHookPrefixes: []string{"amend!"}},
},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "--no-verify", "-m", "amend! original subject", "-m", "new subject"}, "", nil),
},
{
testName: "with non-matching skipHookPrefixes",
originalSubject: "original subject",
newSubject: "new subject",
newDescription: "",
includeFileChanges: true,
userConfig: &config.UserConfig{
Git: config.GitConfig{SkipHookPrefixes: []string{"WIP"}},
},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "-m", "amend! original subject", "-m", "new subject"}, "", nil),
},
{
testName: "with multiple prefixes including amend!",
originalSubject: "original subject",
newSubject: "new subject",
newDescription: "",
includeFileChanges: true,
userConfig: &config.UserConfig{
Git: config.GitConfig{SkipHookPrefixes: []string{"WIP", "amend!"}},
},
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"commit", "--no-verify", "-m", "amend! original subject", "-m", "new subject"}, "", nil),
},
}
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
instance := buildCommitCommands(commonDeps{runner: s.runner})
instance := buildCommitCommands(commonDeps{runner: s.runner, userConfig: s.userConfig})
err := instance.CreateAmendCommit(s.originalSubject, s.newSubject, s.newDescription, s.includeFileChanges)
assert.NoError(t, err)
s.runner.CheckForMissingCalls()

View file

@ -315,6 +315,11 @@ func computeMigratedConfig(path string, content []byte, changes *ChangesSet) ([]
return nil, false, fmt.Errorf("Couldn't migrate config file at `%s`: %w", path, err)
}
err = migrateSkipHookPrefix(&rootNode, changes)
if err != nil {
return nil, false, fmt.Errorf("Couldn't migrate config file at `%s`: %w", path, err)
}
// Add more migrations here...
if reflect.DeepEqual(rootNode, originalCopy) {
@ -505,6 +510,39 @@ func migratePagers(rootNode *yaml.Node, changes *ChangesSet) error {
})
}
func migrateSkipHookPrefix(rootNode *yaml.Node, changes *ChangesSet) error {
return yaml_utils.TransformNode(rootNode, []string{"git"}, func(gitNode *yaml.Node) error {
keyNode, valueNode := yaml_utils.LookupKey(gitNode, "skipHookPrefix")
if keyNode == nil || valueNode.Kind != yaml.ScalarNode || valueNode.Tag != "!!str" {
return nil
}
_, existingValueNode := yaml_utils.LookupKey(gitNode, "skipHookPrefixes")
if existingValueNode != nil {
return nil
}
newKeyNode := &yaml.Node{Kind: yaml.ScalarNode, Value: "skipHookPrefixes", Tag: "!!str"}
newValueNodeContent := []*yaml.Node{}
if valueNode.Value != "" {
newValueNodeContent = []*yaml.Node{
{Kind: yaml.ScalarNode, Value: valueNode.Value, Tag: "!!str"},
}
}
newValueNode := &yaml.Node{
Kind: yaml.SequenceNode,
Tag: "!!seq",
Content: newValueNodeContent,
}
gitNode.Content = append(gitNode.Content, newKeyNode, newValueNode)
_, _ = yaml_utils.RemoveKey(gitNode, "skipHookPrefix")
changes.Add("Moved git.skipHookPrefix string to git.skipHookPrefixes array")
return nil
})
}
func (c *AppConfig) GetDebug() bool {
return c.debug
}

View file

@ -645,8 +645,9 @@ git:
- master
- main
# Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'
skipHookPrefix: WIP
# Prefixes to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'
skipHookPrefixes:
- WIP
# If true, periodically fetch from remote
autoFetch: true
@ -1184,3 +1185,94 @@ func TestPagerMigration(t *testing.T) {
})
}
}
func TestSkipHookPrefixMigration(t *testing.T) {
scenarios := []struct {
name string
input string
expected string
expectedDidChange bool
expectedChanges []string
}{
{
name: "Empty Input",
input: "",
expectedDidChange: false,
expectedChanges: []string{},
},
{
name: "Empty string",
input: `git:
skipHookPrefix: ""
`,
expected: `git:
skipHookPrefixes: []
`,
expectedDidChange: true,
expectedChanges: []string{
"Moved git.skipHookPrefix string to git.skipHookPrefixes array",
},
},
{
name: "Old skipHookPrefix migrated to skipHookPrefixes",
input: `git:
skipHookPrefix: WIP
`,
expected: `git:
skipHookPrefixes:
- WIP
`,
expectedDidChange: true,
expectedChanges: []string{
"Moved git.skipHookPrefix string to git.skipHookPrefixes array",
},
},
{
name: "skipHookPrefix is not a string: do nothing",
input: `git:
skipHookPrefix: 5
`,
expectedDidChange: false,
expectedChanges: []string{},
},
{
name: "New skipHookPrefixes already present: do nothing",
input: `git:
skipHookPrefix: WIP
skipHookPrefixes:
- WIP
- fixup!
`,
expectedDidChange: false,
expectedChanges: []string{},
},
{
name: "Only new skipHookPrefixes, no migration needed",
input: `git:
skipHookPrefixes:
- WIP
`,
expectedDidChange: false,
expectedChanges: []string{},
},
{
name: "Neither present",
input: "git:",
expectedDidChange: false,
expectedChanges: []string{},
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
changes := NewChangesSet()
actual, didChange, err := computeMigratedConfig("path doesn't matter", []byte(s.input), changes)
assert.NoError(t, err)
assert.Equal(t, s.expectedDidChange, didChange)
if didChange {
assert.Equal(t, s.expected, string(actual))
}
assert.Equal(t, s.expectedChanges, changes.ToSliceFromOldest())
})
}
}

View file

@ -282,8 +282,8 @@ type GitConfig struct {
Merging MergingConfig `yaml:"merging"`
// list of branches that are considered 'main' branches, used when displaying commits
MainBranches []string `yaml:"mainBranches" jsonschema:"uniqueItems=true"`
// Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'
SkipHookPrefix string `yaml:"skipHookPrefix"`
// Prefixes to use when skipping hooks. E.g. if set to ['WIP'], then pre-commit hooks will be skipped when the commit message starts with 'WIP'. The first entry in the array will be used for the "Commit changes without pre-commit hook" command.
SkipHookPrefixes []string `yaml:"skipHookPrefixes" jsonschema:"uniqueItems=true"`
// If true, periodically fetch from remote
AutoFetch bool `yaml:"autoFetch"`
// If true, periodically refresh files and submodules
@ -859,7 +859,7 @@ func GetDefaultConfig() *UserConfig {
},
LocalBranchSortOrder: "date",
RemoteBranchSortOrder: "date",
SkipHookPrefix: "WIP",
SkipHookPrefixes: []string{"WIP"},
MainBranches: []string{"master", "main"},
AutoFetch: true,
AutoRefresh: true,

View file

@ -9,6 +9,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/keybindings"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
"github.com/spf13/afero"
)
@ -43,8 +44,8 @@ type CommitMessageViewModel struct {
onSwitchToEditor func(string) error
// the following two fields are used for the display of the "hooks disabled" subtitle
forceSkipHooks bool
skipHooksPrefix string
forceSkipHooks bool
skipHooksPrefixes []string
// The message typed in before cycling through history
// We store this separately to 'preservedMessage' because 'preservedMessage'
@ -153,7 +154,7 @@ func (self *CommitMessageContext) SetPanelState(
onConfirm func(string, string) error,
onSwitchToEditor func(string) error,
forceSkipHooks bool,
skipHooksPrefix string,
skipHooksPrefixes []string,
) {
self.viewModel.selectedindex = index
self.viewModel.preserveMessage = preserveMessage
@ -161,7 +162,7 @@ func (self *CommitMessageContext) SetPanelState(
self.viewModel.onConfirm = onConfirm
self.viewModel.onSwitchToEditor = onSwitchToEditor
self.viewModel.forceSkipHooks = forceSkipHooks
self.viewModel.skipHooksPrefix = skipHooksPrefix
self.viewModel.skipHooksPrefixes = skipHooksPrefixes
self.GetView().Title = summaryTitle
self.c.Views().CommitDescription.Title = descriptionTitle
@ -175,10 +176,12 @@ func (self *CommitMessageContext) SetPanelState(
}
func (self *CommitMessageContext) RenderSubtitle() {
skipHookPrefix := self.viewModel.skipHooksPrefix
subject := self.c.Views().CommitMessage.TextArea.GetContent()
var subtitle string
if self.viewModel.forceSkipHooks || (skipHookPrefix != "" && strings.HasPrefix(subject, skipHookPrefix)) {
hooksSkipped := self.viewModel.forceSkipHooks || lo.SomeBy(self.viewModel.skipHooksPrefixes, func(prefix string) bool {
return strings.HasPrefix(subject, prefix)
})
if hooksSkipped {
subtitle = self.c.Tr.CommitHooksDisabledSubTitle
}
if self.c.UserConfig().Gui.CommitLength.Show {

View file

@ -126,8 +126,8 @@ type OpenCommitMessagePanelOpts struct {
// the actual behavior; make sure what you are passing in matches that.
// Leave unassigned if the concept of skipping hooks doesn't make sense for
// what you are doing, e.g. when creating a tag.
ForceSkipHooks bool
SkipHooksPrefix string
ForceSkipHooks bool
SkipHooksPrefixes []string
}
func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOpts) {
@ -146,7 +146,7 @@ func (self *CommitsHelper) OpenCommitMessagePanel(opts *OpenCommitMessagePanelOp
onConfirm,
opts.OnSwitchToEditor,
opts.ForceSkipHooks,
opts.SkipHooksPrefix,
opts.SkipHooksPrefixes,
)
self.UpdateCommitPanelView(opts.InitialMessage)

View file

@ -135,8 +135,8 @@ func (self *WorkingTreeHelper) HandleCommitPressWithMessage(initialMessage strin
OnSwitchToEditor: func(filepath string) error {
return self.switchFromCommitMessagePanelToEditor(filepath, forceSkipHooks)
},
ForceSkipHooks: forceSkipHooks,
SkipHooksPrefix: self.c.UserConfig().Git.SkipHookPrefix,
ForceSkipHooks: forceSkipHooks,
SkipHooksPrefixes: self.c.UserConfig().Git.SkipHookPrefixes,
},
)
@ -188,8 +188,11 @@ func (self *WorkingTreeHelper) HandleWIPCommitPress() error {
var initialMessage string
preservedMessage := self.c.Contexts().CommitMessage.GetPreservedMessageAndLogError()
if preservedMessage == "" {
// Use the skipHook prefix only if we don't have a preserved message
initialMessage = self.c.UserConfig().Git.SkipHookPrefix
// Use the first skipHook prefix only if we don't have a preserved message
prefixes := self.c.UserConfig().Git.SkipHookPrefixes
if len(prefixes) > 0 {
initialMessage = prefixes[0]
}
}
return self.HandleCommitPressWithMessage(initialMessage, true)
}

View file

@ -323,10 +323,16 @@
"main"
]
},
"skipHookPrefix": {
"type": "string",
"description": "Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'",
"default": "WIP"
"skipHookPrefixes": {
"items": {
"type": "string"
},
"type": "array",
"uniqueItems": true,
"description": "Prefixes to use when skipping hooks. E.g. if set to ['WIP'], then pre-commit hooks will be skipped when the commit message starts with 'WIP'. The first entry in the array will be used for the \"Commit changes without pre-commit hook\" command.",
"default": [
"WIP"
]
},
"autoFetch": {
"type": "boolean",