Skip to content

Commit aed2a0b

Browse files
committed
refactor: trim trailing whitespace from commit message
Ensures cleaner commit messages by stripping trailing spaces, tabs, and carriage returns from each line before processing. Added a helper function with corresponding unit tests to verify behavior. This was causing some glitches with bubbletea text component too.
1 parent 2ed32e2 commit aed2a0b

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

internal/ui/model.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
149149
return m, tea.Quit
150150
}
151151
}
152+
commitMessage = trimTrailingSpaces(commitMessage)
152153
commitMessage = strings.ReplaceAll(commitMessage, "\n\n\n", "\n\n")
153154

154155
if m.yolo {
@@ -377,3 +378,11 @@ func (m *Model) Action() Action {
377378
func (m *Model) CommitMessage() string {
378379
return m.commitMessage
379380
}
381+
382+
func trimTrailingSpaces(s string) string {
383+
lines := strings.Split(s, "\n")
384+
for i := range lines {
385+
lines[i] = strings.TrimRight(lines[i], " \t\r")
386+
}
387+
return strings.Join(lines, "\n")
388+
}

internal/ui/model_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func (m *MockGitClient) Commit(ctx context.Context, message string, skipCI, noVe
6161
args := m.Called(ctx, message, skipCI, noVerify)
6262
return args.Error(0)
6363
}
64+
6465
func (m *MockGitClient) RecentCommits(ctx context.Context, count int) ([]string, error) {
6566
args := m.Called(ctx, count)
6667
return args.Get(0).([]string), args.Error(1)
@@ -418,3 +419,23 @@ func (m *mockTeaModel) View() tea.View {
418419
}
419420
return tea.NewView("")
420421
}
422+
423+
func TestTrimTrailingSpaces(t *testing.T) {
424+
tests := []struct {
425+
name string
426+
input string
427+
expected string
428+
}{
429+
{"no trailing spaces", "hello\nworld", "hello\nworld"},
430+
{"trailing spaces on some lines", "hello \nworld \n ", "hello\nworld\n"},
431+
{"tabs and carriage returns", "line1\t\r\nline2 \n", "line1\nline2\n"},
432+
{"empty string", "", ""},
433+
{"single line with space", "hello ", "hello"},
434+
}
435+
436+
for _, tt := range tests {
437+
t.Run(tt.name, func(t *testing.T) {
438+
assert.Equal(t, tt.expected, trimTrailingSpaces(tt.input))
439+
})
440+
}
441+
}

0 commit comments

Comments
 (0)