Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ gui:
showIcons: false
commandLogSize: 8
splitDiff: 'auto' # one of 'auto' | 'always'
branchWhitespaceChar: '-' # to replace whitespace in new branch names
git:
paging:
colorArg: always
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type GuiConfig struct {
ShowIcons bool `yaml:"showIcons"`
CommandLogSize int `yaml:"commandLogSize"`
SplitDiff string `yaml:"splitDiff"`
BranchWhitespaceChar string `yaml:"branchWhitespaceChar"`
}

type ThemeConfig struct {
Expand Down Expand Up @@ -376,6 +377,7 @@ func GetDefaultConfig() *UserConfig {
ShowIcons: false,
CommandLogSize: 8,
SplitDiff: "auto",
BranchWhitespaceChar: "-",
},
Git: GitConfig{
Paging: PagingConfig{
Expand Down
11 changes: 7 additions & 4 deletions pkg/gui/controllers/helpers/refs_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helpers

import (
"fmt"
"regexp"
"strings"

"github.com/jesseduffield/generics/slices"
Expand Down Expand Up @@ -177,7 +178,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)
if err := self.git.Branch.New(sanitizedBranchName(response), from); err != nil {
if err := self.git.Branch.New(self.sanitizedBranchName(response), from); err != nil {
return err
}

Expand All @@ -195,8 +196,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
})
}

// sanitizedBranchName will remove all spaces in favor of a dash "-" to meet
// sanitizedBranchName will remove all spaces in favor of a replacement char to meet
// git's branch naming requirement.
func sanitizedBranchName(input string) string {
return strings.Replace(input, " ", "-", -1)
func (self *RefsHelper) sanitizedBranchName(input string) string {
reg1 := regexp.MustCompile(`^-+`)
reg2 := regexp.MustCompile(`\.|\/\.|\.\.|~|\^|:|\/$|\\|\*|\s|^\s*$|\.$|\[|\]$`)
return reg2.ReplaceAllString(reg1.ReplaceAllString(strings.TrimSpace(input), ""), self.c.UserConfig.Gui.BranchWhitespaceChar)
}
39 changes: 39 additions & 0 deletions pkg/integration/tests/commit/new_sanitized_branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var NewSanitizedBranch = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Creating a new sanitized branch from a commit",
ExtraCmdArgs: "",
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.
EmptyCommit("commit 1").
EmptyCommit("commit 2").
EmptyCommit("commit 3")
},
Run: func(shell *Shell, input *Input, assert *Assert, keys config.KeybindingConfig) {
assert.CommitCount(3)

input.SwitchToCommitsWindow()
assert.CurrentViewName("commits")
input.NextItem()

input.PressKeys(keys.Universal.New)

assert.CurrentViewName("confirmation")

branchName := "my branch name"
branchSanName := "my-branch-name"
input.Type(branchName)
input.Confirm()

assert.CommitCount(2)
assert.MatchHeadCommitMessage(Contains("commit 2"))
assert.CurrentBranchName(branchSanName)
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
var tests = []*components.IntegrationTest{
commit.Commit,
commit.NewBranch,
commit.NewSanitizedBranch,
branch.Suggestions,
branch.Delete,
branch.Rebase,
Expand Down