mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-06 23:46:13 +02:00
Only add commit prefix if branch name matches regex pattern (#3703)
- **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
This commit is contained in:
commit
e0377f2bce
@ -152,12 +152,16 @@ func (self *WorkingTreeHelper) HandleCommitPress() error {
|
|||||||
if commitPrefixConfig != nil {
|
if commitPrefixConfig != nil {
|
||||||
prefixPattern := commitPrefixConfig.Pattern
|
prefixPattern := commitPrefixConfig.Pattern
|
||||||
prefixReplace := commitPrefixConfig.Replace
|
prefixReplace := commitPrefixConfig.Replace
|
||||||
|
branchName := self.refHelper.GetCheckedOutRef().Name
|
||||||
rgx, err := regexp.Compile(prefixPattern)
|
rgx, err := regexp.Compile(prefixPattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("%s: %s", self.c.Tr.CommitPrefixPatternError, err.Error())
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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(""))
|
||||||
|
},
|
||||||
|
})
|
@ -80,6 +80,7 @@ var tests = []*components.IntegrationTest{
|
|||||||
commit.CommitSwitchToEditor,
|
commit.CommitSwitchToEditor,
|
||||||
commit.CommitWipWithPrefix,
|
commit.CommitWipWithPrefix,
|
||||||
commit.CommitWithGlobalPrefix,
|
commit.CommitWithGlobalPrefix,
|
||||||
|
commit.CommitWithNonMatchingBranchName,
|
||||||
commit.CommitWithPrefix,
|
commit.CommitWithPrefix,
|
||||||
commit.CreateAmendCommit,
|
commit.CreateAmendCommit,
|
||||||
commit.CreateTag,
|
commit.CreateTag,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user