Skip to content

Commit

Permalink
fix(changelog): Trim title/body before parsing commits
Browse files Browse the repository at this point in the history
Fixes #1646
  • Loading branch information
aalmiray committed May 1, 2024
1 parent d802383 commit 20d1ce6
Showing 1 changed file with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import static org.jreleaser.sdk.git.ChangelogProvider.storeIssues;
import static org.jreleaser.sdk.git.GitSdk.extractTagName;
import static org.jreleaser.util.ComparatorUtils.lessThan;
import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;
import static org.jreleaser.util.StringUtils.isTrue;
import static org.jreleaser.util.StringUtils.normalizeRegexPattern;
Expand Down Expand Up @@ -679,10 +680,10 @@ protected static class Commit {
protected Commit(RevCommit rc) {
fullHash = rc.getId().name();
shortHash = rc.getId().abbreviate(7).name();
body = rc.getFullMessage();
body = rc.getFullMessage().trim();
String[] lines = split(body);
if (lines.length > 0) {
title = lines[0];
title = lines[0].trim();
} else {
title = "";
}
Expand Down Expand Up @@ -759,7 +760,7 @@ static class ConventionalCommit extends Commit {
private ConventionalCommit(RevCommit rc) {
super(rc);
List<String> lines = new ArrayList<>(Arrays.asList(split(body)));
Matcher matcherFirstLine = FIRST_LINE_PATTERN.matcher(lines.get(0));
Matcher matcherFirstLine = FIRST_LINE_PATTERN.matcher(lines.get(0).trim());
if (matcherFirstLine.matches()) {
lines.remove(0); // consumed first line
if (null != matcherFirstLine.group("bang") && !matcherFirstLine.group("bang").isEmpty()) {
Expand All @@ -774,13 +775,13 @@ private ConventionalCommit(RevCommit rc) {
}

// drop any empty lines at the beginning
while (!lines.isEmpty() && "".equals(lines.get(0))) {
while (!lines.isEmpty() && isBlank(lines.get(0))) {
lines.remove(0);
}

// try to match trailers from the end
while (!lines.isEmpty()) {
Matcher matcherTrailer = TRAILER_PATTERN.matcher(lines.get(lines.size() - 1));
Matcher matcherTrailer = TRAILER_PATTERN.matcher(lines.get(lines.size() - 1).trim());
if (matcherTrailer.matches()) {
String token = matcherTrailer.group("token");
if ("BREAKING-CHANGE".equals(token)) break;
Expand All @@ -792,7 +793,7 @@ private ConventionalCommit(RevCommit rc) {
}

// drop any empty lines at the end
while (!lines.isEmpty() && "".equals(lines.get(lines.size() - 1))) {
while (!lines.isEmpty() && isBlank(lines.get(lines.size() - 1))) {
lines.remove(lines.size() - 1);
}

Expand All @@ -802,7 +803,7 @@ private ConventionalCommit(RevCommit rc) {
ccBreakingChangeContent = matcherBC.group("content");
// consume the breaking change
OptionalInt match = IntStream.range(0, lines.size())
.filter(i -> BREAKING_CHANGE_PATTERN.matcher(lines.get(i)).find())
.filter(i -> BREAKING_CHANGE_PATTERN.matcher(lines.get(i).trim()).find())
.findFirst();
if (match.isPresent() && lines.size() > match.getAsInt()) {
lines.subList(match.getAsInt(), lines.size()).clear();
Expand Down

0 comments on commit 20d1ce6

Please sign in to comment.