diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 9738186d9..1df585567 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -343,6 +343,8 @@ type GitConfig struct { CommitPrefixes map[string][]CommitPrefixConfig `yaml:"commitPrefixes"` // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix BranchPrefix string `yaml:"branchPrefix"` + // Character(s) used to replace whitespace when sanitizing branch names. + BranchWhitespaceChar string `yaml:"branchWhitespaceChar"` // If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀 // (This should really be under 'gui', not 'git') ParseEmoji bool `yaml:"parseEmoji"` @@ -969,6 +971,7 @@ func GetDefaultConfigForPlatform(platform string) *UserConfig { DisableForcePushing: false, CommitPrefixes: map[string][]CommitPrefixConfig(nil), BranchPrefix: "", + BranchWhitespaceChar: "-", ParseEmoji: false, TruncateCopiedCommitHashesTo: 12, }, diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index cfc46b503..56d963ab7 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -736,7 +736,7 @@ func (self *BranchesController) rename(branch *models.Branch) error { InitialContent: branch.Name, HandleConfirm: func(newBranchName string) error { self.c.LogAction(self.c.Tr.Actions.RenameBranch) - if err := self.c.Git().Branch.Rename(branch.Name, helpers.SanitizedBranchName(newBranchName)); err != nil { + if err := self.c.Git().Branch.Rename(branch.Name, helpers.SanitizedBranchName(newBranchName, self.c.UserConfig().Git.BranchWhitespaceChar)); err != nil { return err } diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index 0fcbeace3..80a1e7062 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -390,7 +390,7 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest InitialContent: suggestedBranchName, HandleConfirm: func(response string) error { self.c.LogAction(self.c.Tr.Actions.CreateBranch) - newBranchName := SanitizedBranchName(response) + newBranchName := SanitizedBranchName(response, self.c.UserConfig().Git.BranchWhitespaceChar) newBranchFunc := self.c.Git().Branch.New if newBranchName != suggestedBranchName { newBranchFunc = self.c.Git().Branch.NewWithoutTracking @@ -465,7 +465,7 @@ func (self *RefsHelper) MoveCommitsToNewBranch() error { InitialContent: suggestedBranchName, HandleConfirm: func(response string) error { self.c.LogAction(self.c.Tr.MoveCommitsToNewBranch) - newBranchName := SanitizedBranchName(response) + newBranchName := SanitizedBranchName(response, self.c.UserConfig().Git.BranchWhitespaceChar) return self.c.WithWaitingStatus(self.c.Tr.MovingCommitsToNewBranchStatus, func(gocui.Task) error { return f(newBranchName) }) @@ -619,10 +619,13 @@ func (self *RefsHelper) CanMoveCommitsToNewBranch() *types.DisabledReason { return nil } -// SanitizedBranchName will remove all spaces in favor of a dash "-" to meet -// git's branch naming requirement. -func SanitizedBranchName(input string) string { - return strings.ReplaceAll(input, " ", "-") +// SanitizedBranchName will replace all spaces with the configured replacement +// string to meet git's branch naming requirement. +func SanitizedBranchName(input, whitespaceReplacement string) string { + if whitespaceReplacement == "" { + whitespaceReplacement = "-" + } + return strings.ReplaceAll(input, " ", whitespaceReplacement) } // Checks if the given branch name is a remote branch, and returns the name of diff --git a/pkg/gui/controllers/helpers/refs_helper_test.go b/pkg/gui/controllers/helpers/refs_helper_test.go new file mode 100644 index 000000000..b25a93ea0 --- /dev/null +++ b/pkg/gui/controllers/helpers/refs_helper_test.go @@ -0,0 +1,25 @@ +package helpers + +import "testing" + +func TestSanitizedBranchName(t *testing.T) { + tests := []struct { + name string + input string + whitespaceReplacement string + expected string + }{ + {name: "default fallback", input: "feature new branch", whitespaceReplacement: "", expected: "feature-new-branch"}, + {name: "custom replacement", input: "feature new branch", whitespaceReplacement: "_", expected: "feature_new_branch"}, + {name: "multiple characters", input: "feature new branch", whitespaceReplacement: "--", expected: "feature--new--branch"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := SanitizedBranchName(tt.input, tt.whitespaceReplacement) + if got != tt.expected { + t.Fatalf("expected %q, got %q", tt.expected, got) + } + }) + } +}