-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathCommitMessage.java
More file actions
177 lines (144 loc) · 5.98 KB
/
CommitMessage.java
File metadata and controls
177 lines (144 loc) · 5.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.leroymerlin.commit;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.apache.commons.lang.StringUtils.isNotBlank;
/**
* @author Damien Arrachequesne <damien.arrachequesne@gmail.com>
*/
class CommitMessage {
private static final int MAX_LINE_LENGTH = 72; // https://stackoverflow.com/a/2120040/5138796
public static final Pattern COMMIT_FIRST_LINE_FORMAT = Pattern.compile("^(.+?)(\\((.+)\\))?: (.+)");
public static final Pattern COMMIT_CLOSES_FORMAT = Pattern.compile("Closes (.+)");
private ChangeType changeType;
private boolean english = true;
private String changeScope, shortDescription, longDescription, breakingChanges, closedIssues;
private boolean wrapText = true;
private boolean skipCI = false;
private CommitMessage() {
this.longDescription = "";
this.breakingChanges = "";
this.closedIssues = "";
}
public CommitMessage(ChangeType changeType, boolean english, String changeScope, String shortDescription, String longDescription,
String breakingChanges, String closedIssues, boolean wrapText, boolean skipCI) {
this.changeType = changeType;
this.english = english;
this.changeScope = changeScope;
this.shortDescription = shortDescription;
this.longDescription = longDescription;
this.breakingChanges = breakingChanges;
this.closedIssues = closedIssues;
this.wrapText = wrapText;
this.skipCI = skipCI;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(changeType.label(english));
if (isNotBlank(changeScope)) {
builder
.append('(')
.append(changeScope)
.append(')');
}
builder
.append(": ")
.append(shortDescription);
if (isNotBlank(longDescription)) {
builder
.append(System.lineSeparator())
.append(System.lineSeparator())
.append(wrapText ? WordUtils.wrap(longDescription, MAX_LINE_LENGTH) : longDescription);
}
if (isNotBlank(breakingChanges)) {
String content = "BREAKING CHANGE: " + breakingChanges;
builder
.append(System.lineSeparator())
.append(System.lineSeparator())
.append(wrapText ? WordUtils.wrap(content, MAX_LINE_LENGTH) : content);
}
if (isNotBlank(closedIssues)) {
builder.append(System.lineSeparator());
for (String closedIssue : closedIssues.split(",")) {
builder
.append(System.lineSeparator())
.append("Closes ")
.append(formatClosedIssue(closedIssue));
}
}
if (skipCI) {
builder
.append(System.lineSeparator())
.append(System.lineSeparator())
.append("[skip ci]");
}
return builder.toString();
}
private String formatClosedIssue(String closedIssue) {
String trimmed = closedIssue.trim();
return (StringUtils.isNumeric(trimmed) ? "#" : "") + trimmed;
}
public static CommitMessage parse(String message) {
CommitMessage commitMessage = new CommitMessage();
try {
Matcher matcher = COMMIT_FIRST_LINE_FORMAT.matcher(message);
if (!matcher.find()) return commitMessage;
commitMessage.changeType = ChangeType.lookup(matcher.group(1));
commitMessage.changeScope = matcher.group(3);
commitMessage.shortDescription = matcher.group(4);
String[] strings = message.split("\n");
if (strings.length < 2) return commitMessage;
int pos = 1;
StringBuilder stringBuilder;
stringBuilder = new StringBuilder();
for (; pos < strings.length; pos++) {
String lineString = strings[pos];
if (lineString.startsWith("BREAKING") || lineString.startsWith("Closes") || lineString.equalsIgnoreCase("[skip ci]")) break;
stringBuilder.append(lineString).append('\n');
}
commitMessage.longDescription = stringBuilder.toString().trim();
stringBuilder = new StringBuilder();
for (; pos < strings.length; pos++) {
String lineString = strings[pos];
if (lineString.startsWith("Closes") || lineString.equalsIgnoreCase("[skip ci]")) break;
stringBuilder.append(lineString).append('\n');
}
commitMessage.breakingChanges = stringBuilder.toString().trim().replace("BREAKING CHANGE: ", "");
matcher = COMMIT_CLOSES_FORMAT.matcher(message);
stringBuilder = new StringBuilder();
while (matcher.find()) {
stringBuilder.append(matcher.group(1)).append(',');
}
if (stringBuilder.length() > 0) stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
commitMessage.closedIssues = stringBuilder.toString();
commitMessage.skipCI = message.contains("[skip ci]");
} catch (RuntimeException e) {}
return commitMessage;
}
public ChangeType getChangeType() {
return changeType;
}
public String getChangeScope() {
return changeScope;
}
public String getShortDescription() {
return shortDescription;
}
public String getLongDescription() {
return longDescription;
}
public String getBreakingChanges() {
return breakingChanges;
}
public String getClosedIssues() {
return closedIssues;
}
public boolean isSkipCI() {
return skipCI;
}
public boolean isEnglish() {
return english;
}
}