Skip to content

Commit ea10dd8

Browse files
feat: wrap commit message to 72 characters
It seems a hard wrap at 72 characters is the de-facto standard.
1 parent 4c4f3e2 commit ea10dd8

File tree

4 files changed

+39
-38
lines changed

4 files changed

+39
-38
lines changed

src/com/leroymerlin/commit/CommitMessage.java

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.leroymerlin.commit;
22

3+
import org.apache.commons.lang.WordUtils;
4+
35
import static org.apache.commons.lang.StringUtils.isNotBlank;
46

57
/**
68
* @author Damien Arrachequesne <damien.arrachequesne@gmail.com>
79
*/
810
class CommitMessage {
11+
private static final int MAX_LINE_LENGTH = 72; // https://stackoverflow.com/a/2120040/5138796
912
private final String content;
1013

1114
CommitMessage(ChangeType changeType, String changeScope, String shortDescription, String longDescription, String closedIssues, String breakingChanges) {
@@ -26,14 +29,13 @@ private String buildContent(ChangeType changeType, String changeScope, String sh
2629
.append(shortDescription)
2730
.append(System.lineSeparator())
2831
.append(System.lineSeparator())
29-
.append(breakLines(longDescription, 100));
32+
.append(WordUtils.wrap(longDescription, MAX_LINE_LENGTH));
3033

3134
if (isNotBlank(breakingChanges)) {
3235
builder
3336
.append(System.lineSeparator())
3437
.append(System.lineSeparator())
35-
.append("BREAKING CHANGE: ")
36-
.append(breakingChanges);
38+
.append(WordUtils.wrap("BREAKING CHANGE: " + breakingChanges, MAX_LINE_LENGTH));
3739
}
3840

3941
if (isNotBlank(closedIssues)) {
@@ -49,31 +51,6 @@ private String buildContent(ChangeType changeType, String changeScope, String sh
4951
return builder.toString();
5052
}
5153

52-
private static String breakLines(String input, int maxLineLength) {
53-
String[] tokens = input.split("\\s+");
54-
StringBuilder output = new StringBuilder(input.length());
55-
int lineLength = 0;
56-
for (int i = 0; i < tokens.length; i++) {
57-
String word = tokens[i];
58-
59-
boolean shouldAddNewLine = lineLength + (" " + word).length() > maxLineLength;
60-
if (shouldAddNewLine) {
61-
if (i > 0) {
62-
output.append(System.lineSeparator());
63-
}
64-
lineLength = 0;
65-
}
66-
boolean shouldAddSpace = i < tokens.length - 1 &&
67-
(lineLength + (word + " ").length() + tokens[i + 1].length() <= maxLineLength);
68-
if (shouldAddSpace) {
69-
word += " ";
70-
}
71-
output.append(word);
72-
lineLength += word.length();
73-
}
74-
return output.toString();
75-
}
76-
7754
@Override
7855
public String toString() {
7956
return content;

src/com/leroymerlin/commit/CommitPanel.form

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@
7070
<preferred-size width="150" height="50"/>
7171
</grid>
7272
</constraints>
73-
<properties/>
73+
<properties>
74+
<lineWrap value="true"/>
75+
</properties>
7476
</component>
7577
<component id="83b17" class="javax.swing.JLabel">
7678
<constraints>
@@ -104,13 +106,15 @@
104106
<text value="Breaking changes"/>
105107
</properties>
106108
</component>
107-
<component id="d09c0" class="javax.swing.JTextField" binding="breakingChanges">
109+
<component id="15a33" class="javax.swing.JTextArea" binding="breakingChanges">
108110
<constraints>
109111
<grid row="4" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
110112
<preferred-size width="150" height="-1"/>
111113
</grid>
112114
</constraints>
113-
<properties/>
115+
<properties>
116+
<lineWrap value="true"/>
117+
</properties>
114118
</component>
115119
</children>
116120
</grid>

src/com/leroymerlin/commit/CommitPanel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class CommitPanel {
1616
private JTextField shortDescription;
1717
private JTextArea longDescription;
1818
private JTextField closedIssues;
19-
private JTextField breakingChanges;
19+
private JTextArea breakingChanges;
2020

2121
CommitPanel(Project project) {
2222
for (ChangeType type : ChangeType.values()) {

tests/com/leroymerlin/commit/CommitMessageTest.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,20 @@ public void testFormatCommit() {
1414
"#16709", "");
1515
String expected = "fix(ngStyle): skip setting empty value when new style has the property\n" +
1616
"\n" +
17-
"Previously, all the properties in oldStyles are set to empty value once. Using AngularJS with \n" +
18-
"jQuery 3.3.1, this disables the CSS transition as reported in jquery/jquery#4185.\n" +
17+
"Previously, all the properties in oldStyles are set to empty value once.\n" +
18+
"Using AngularJS with jQuery 3.3.1, this disables the CSS transition as\n" +
19+
"reported in jquery/jquery#4185.\n" +
1920
"\n" +
2021
"Closes #16709";
21-
assertEquals(commitMessage.toString(), expected);
22+
assertEquals(expected, commitMessage.toString());
2223
}
2324

2425
@Test
2526
public void testFormatCommit_withoutScope() {
2627
CommitMessage commitMessage = new CommitMessage(ChangeType.STYLE, "",
2728
"fix eslint error", "", "", "");
2829
String expected = "style: fix eslint error\n\n";
29-
assertEquals(commitMessage.toString(), expected);
30+
assertEquals(expected, commitMessage.toString());
3031
}
3132

3233
@Test
@@ -37,10 +38,29 @@ public void testFormatCommit_withMultipleClosedIssues() {
3738
"#7925,#15002", "");
3839
String expected = "feat($route): add support for the `reloadOnUrl` configuration option\n" +
3940
"\n" +
40-
"Enables users to specify that a particular route should not be reloaded after a URL change.\n" +
41+
"Enables users to specify that a particular route should not be reloaded\n" +
42+
"after a URL change.\n" +
4143
"\n" +
4244
"Closes #7925\n" +
4345
"Closes #15002";
44-
assertEquals(commitMessage.toString(), expected);
46+
assertEquals(expected, commitMessage.toString());
47+
}
48+
49+
@Test
50+
public void testFormatCommit_withLongBreakingChange() {
51+
CommitMessage commitMessage = new CommitMessage(ChangeType.FEAT, "", "break everything","", "",
52+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
53+
String expected = "feat: break everything\n" +
54+
"\n" +
55+
"\n" +
56+
"\n" +
57+
"BREAKING CHANGE: Lorem ipsum dolor sit amet, consectetur adipiscing\n" +
58+
"elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" +
59+
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi\n" +
60+
"ut aliquip ex ea commodo consequat. Duis aute irure dolor in\n" +
61+
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla\n" +
62+
"pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa\n" +
63+
"qui officia deserunt mollit anim id est laborum.";
64+
assertEquals(expected, commitMessage.toString());
4565
}
4666
}

0 commit comments

Comments
 (0)