-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove hard line breaks when rewording commits
... and when recalling a commit message from an old commit by pressing up-arrow. This is necessary because committing turns our soft line breaks into real ones, but when rewording we want to turn them back into soft ones again, so that it's possible to insert words at the beginning of a paragraph and have everything rewrap nicely. This is only a best effort; the algorithm only removes those hard line breaks that can be removed without changing the way the message looks. This works well when the previous commit message was wrapped at the same width, which for most users should be the most common case; but if it wasn't, the result is not great. Specifically, if the old wrap width was smaller, some hard line breaks just won't be removed; if it was wider though, you'll get an unpleasant comb effect with alternating long and short lines. In such a case it's best to switch to the editor and use whatever wrapping features you have there (e.g. alt-Q).
- Loading branch information
1 parent
dc494cd
commit 85a9dac
Showing
4 changed files
with
76 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTryRemoveHardLineBreaks(t *testing.T) { | ||
scenarios := []struct { | ||
name string | ||
message string | ||
autoWrapWidth int | ||
expectedResult string | ||
}{ | ||
{ | ||
name: "empty", | ||
message: "", | ||
autoWrapWidth: 7, | ||
expectedResult: "", | ||
}, | ||
{ | ||
name: "all line breaks are needed", | ||
message: "abc\ndef\n\nxyz", | ||
autoWrapWidth: 7, | ||
expectedResult: "abc\ndef\n\nxyz", | ||
}, | ||
{ | ||
name: "some can be unwrapped", | ||
message: "123\nabc def\nghi jkl\nmno\n456\n", | ||
autoWrapWidth: 7, | ||
expectedResult: "123\nabc def ghi jkl mno\n456\n", | ||
}, | ||
} | ||
for _, s := range scenarios { | ||
t.Run(s.name, func(t *testing.T) { | ||
actualResult := TryRemoveHardLineBreaks(s.message, s.autoWrapWidth) | ||
assert.Equal(t, s.expectedResult, actualResult) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters