From 07fe828f60b2c091e038d76075ac7ed08588a9c2 Mon Sep 17 00:00:00 2001 From: Luke Swan Date: Wed, 10 Jul 2024 00:47:22 +0300 Subject: [PATCH 1/2] Add initial test for non-matching branch name --- .../commit_with_non_matching_branch_name.go | 38 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 39 insertions(+) create mode 100644 pkg/integration/tests/commit/commit_with_non_matching_branch_name.go 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 000000000..490bbf9f8 --- /dev/null +++ b/pkg/integration/tests/commit/commit_with_non_matching_branch_name.go @@ -0,0 +1,38 @@ +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")). + /* EXPECTED: + InitialText(Equals("")) + ACTUAL: */ + InitialText(Equals("branchnomatch")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index ec24b9f5a..fcc0b74bb 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, From 968060a5ec13a7804301a1368899b756bcfb04b3 Mon Sep 17 00:00:00 2001 From: Luke Swan Date: Sun, 30 Jun 2024 01:07:15 +0000 Subject: [PATCH 2/2] Ensure branch name matches pattern before replace Amend test for non-matching branch name --- pkg/gui/controllers/helpers/working_tree_helper.go | 8 ++++++-- .../tests/commit/commit_with_non_matching_branch_name.go | 3 --- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/gui/controllers/helpers/working_tree_helper.go b/pkg/gui/controllers/helpers/working_tree_helper.go index a97639795..51a6bc553 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 index 490bbf9f8..1075c7bb1 100644 --- a/pkg/integration/tests/commit/commit_with_non_matching_branch_name.go +++ b/pkg/integration/tests/commit/commit_with_non_matching_branch_name.go @@ -30,9 +30,6 @@ var CommitWithNonMatchingBranchName = NewIntegrationTest(NewIntegrationTestArgs{ t.ExpectPopup().CommitMessagePanel(). Title(Equals("Commit summary")). - /* EXPECTED: InitialText(Equals("")) - ACTUAL: */ - InitialText(Equals("branchnomatch")) }, })