diff --git a/pkg/gui/controllers/helpers/working_tree_helper.go b/pkg/gui/controllers/helpers/working_tree_helper.go index a97639795d2..51a6bc553a9 100644 --- a/pkg/gui/controllers/helpers/working_tree_helper.go +++ b/pkg/gui/controllers/helpers/working_tree_helper.go @@ -152,12 +152,16 @@ func (self *WorkingTreeHelper) HandleCommitPress() error { if commitPrefixConfig != nil { prefixPattern := commitPrefixConfig.Pattern prefixReplace := commitPrefixConfig.Replace + branchName := self.refHelper.GetCheckedOutRef().Name rgx, err := regexp.Compile(prefixPattern) if err != nil { return fmt.Errorf("%s: %s", self.c.Tr.CommitPrefixPatternError, err.Error()) } - prefix := rgx.ReplaceAllString(self.refHelper.GetCheckedOutRef().Name, prefixReplace) - message = prefix + + if rgx.MatchString(branchName) { + prefix := rgx.ReplaceAllString(branchName, prefixReplace) + message = prefix + } } } diff --git a/pkg/integration/tests/commit/commit_with_non_matching_branch_name.go b/pkg/integration/tests/commit/commit_with_non_matching_branch_name.go new file mode 100644 index 00000000000..1075c7bb168 --- /dev/null +++ b/pkg/integration/tests/commit/commit_with_non_matching_branch_name.go @@ -0,0 +1,35 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CommitWithNonMatchingBranchName = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Commit with defined config commitPrefixes", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(testConfig *config.AppConfig) { + testConfig.UserConfig.Git.CommitPrefix = &config.CommitPrefixConfig{ + Pattern: "^\\w+\\/(\\w+-\\w+).*", + Replace: "[$1]: ", + } + }, + SetupRepo: func(shell *Shell) { + shell.NewBranch("branchnomatch") + shell.CreateFile("test-commit-prefix", "This is foo bar") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + IsEmpty() + + t.Views().Files(). + IsFocused(). + PressPrimaryAction(). + Press(keys.Files.CommitChanges) + + t.ExpectPopup().CommitMessagePanel(). + Title(Equals("Commit summary")). + InitialText(Equals("")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index ec24b9f5a18..fcc0b74bbbf 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -80,6 +80,7 @@ var tests = []*components.IntegrationTest{ commit.CommitSwitchToEditor, commit.CommitWipWithPrefix, commit.CommitWithGlobalPrefix, + commit.CommitWithNonMatchingBranchName, commit.CommitWithPrefix, commit.CreateAmendCommit, commit.CreateTag,