Skip to content

Commit

Permalink
Only add commit prefix if branch name matches regex pattern (#3703)
Browse files Browse the repository at this point in the history
- **PR Description**
Currently if a branch name does not match a regex pattern defined in the
config.yaml (commitPrefix/es) the commit message box is populated with
the branch name as is - this does not match expectations. A prefix
should only be added if there is a match on the regex pattern.

This PR seeks to change that by checking for a match before calling
ReplaceAllString - see Issue #3695

- **Please check if the PR fulfills these requirements**

* [x] Cheatsheets are up-to-date (run `go generate ./...`)
* [x] Code has been formatted (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting))
* [x] Tests have been added/updated (see
[here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md)
for the integration test guide)
* [-] Text is internationalised (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation))
* [-] Docs have been updated if necessary
* [x] You've read through your own file changes for silly mistakes etc
  • Loading branch information
stefanhaller committed Jul 10, 2024
2 parents b004b2e + 968060a commit e0377f2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/gui/controllers/helpers/working_tree_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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(""))
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var tests = []*components.IntegrationTest{
commit.CommitSwitchToEditor,
commit.CommitWipWithPrefix,
commit.CommitWithGlobalPrefix,
commit.CommitWithNonMatchingBranchName,
commit.CommitWithPrefix,
commit.CreateAmendCommit,
commit.CreateTag,
Expand Down

0 comments on commit e0377f2

Please sign in to comment.