diff --git a/pkg/commands/loaders/stash.go b/pkg/commands/loaders/stash.go index 66cfeaa3e..4c9d16a23 100644 --- a/pkg/commands/loaders/stash.go +++ b/pkg/commands/loaders/stash.go @@ -65,8 +65,8 @@ outer: } func (self *StashLoader) getUnfilteredStashEntries() []*models.StashEntry { - rawString, _ := self.cmd.New("git stash list --pretty='%gs'").DontLog().RunWithOutput() - return slices.MapWithIndex(utils.SplitLines(rawString), func(line string, index int) *models.StashEntry { + rawString, _ := self.cmd.New("git stash list -z --pretty='%gs'").DontLog().RunWithOutput() + return slices.MapWithIndex(utils.SplitNul(rawString), func(line string, index int) *models.StashEntry { return self.stashEntryFromLine(line, index) }) } diff --git a/pkg/commands/loaders/stash_test.go b/pkg/commands/loaders/stash_test.go index e7999e861..707be090e 100644 --- a/pkg/commands/loaders/stash_test.go +++ b/pkg/commands/loaders/stash_test.go @@ -22,7 +22,7 @@ func TestGetStashEntries(t *testing.T) { "No stash entries found", "", oscommands.NewFakeRunner(t). - Expect(`git stash list --pretty='%gs'`, "", nil), + Expect(`git stash list -z --pretty='%gs'`, "", nil), []*models.StashEntry{}, }, { @@ -30,8 +30,8 @@ func TestGetStashEntries(t *testing.T) { "", oscommands.NewFakeRunner(t). Expect( - `git stash list --pretty='%gs'`, - "WIP on add-pkg-commands-test: 55c6af2 increase parallel build\nWIP on master: bb86a3f update github template", + `git stash list -z --pretty='%gs'`, + "WIP on add-pkg-commands-test: 55c6af2 increase parallel build\x00WIP on master: bb86a3f update github template\x00", nil, ), []*models.StashEntry{ diff --git a/pkg/utils/lines.go b/pkg/utils/lines.go index 47d33e939..662ba2f9b 100644 --- a/pkg/utils/lines.go +++ b/pkg/utils/lines.go @@ -17,6 +17,14 @@ func SplitLines(multilineString string) []string { return lines } +func SplitNul(str string) []string { + if str == "" { + return make([]string, 0) + } + str = strings.TrimSuffix(str, "\x00") + return strings.Split(str, "\x00") +} + // NormalizeLinefeeds - Removes all Windows and Mac style line feeds func NormalizeLinefeeds(str string) string { str = strings.Replace(str, "\r\n", "\n", -1) diff --git a/pkg/utils/lines_test.go b/pkg/utils/lines_test.go index 361f0a510..e7171022b 100644 --- a/pkg/utils/lines_test.go +++ b/pkg/utils/lines_test.go @@ -36,6 +36,37 @@ func TestSplitLines(t *testing.T) { } } +func TestSplitNul(t *testing.T) { + type scenario struct { + multilineString string + expected []string + } + + scenarios := []scenario{ + { + "", + []string{}, + }, + { + "\x00", + []string{ + "", + }, + }, + { + "hello world !\x00hello universe !\x00", + []string{ + "hello world !", + "hello universe !", + }, + }, + } + + for _, s := range scenarios { + assert.EqualValues(t, s.expected, SplitNul(s.multilineString)) + } +} + // TestNormalizeLinefeeds is a function. func TestNormalizeLinefeeds(t *testing.T) { type scenario struct {