diff --git a/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormat.java b/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormat.java index 3fad1f32..884693aa 100644 --- a/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormat.java +++ b/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormat.java @@ -14,6 +14,8 @@ import com.github.koraktor.mavanagaiata.git.GitCommit; import com.github.koraktor.mavanagaiata.git.GitTag; +import org.apache.commons.lang3.StringUtils; + import static com.github.koraktor.mavanagaiata.mojo.AbstractGitOutputMojo.unescapeFormatNewlines; import static org.apache.commons.text.StringEscapeUtils.escapeHtml4; @@ -173,12 +175,17 @@ void printBranch(String branchName) { * Print a single line for a commit * * @param currentCommit The commit to print + * @param trimTrailingWhitespace Trim the trailing whitespace from commit */ - void printCommit(GitCommit currentCommit) { + void printCommit(GitCommit currentCommit, boolean trimTrailingWhitespace) { String commitMessage = escapeHtml ? escapeHtml4(currentCommit.getMessageSubject()) : currentCommit.getMessageSubject(); + if (trimTrailingWhitespace) { + commitMessage = StringUtils.stripEnd(commitMessage, null); + } + printStream.println(commitPrefix + commitMessage); } diff --git a/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogMojo.java b/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogMojo.java index f5d35e8c..6dfdfbb5 100644 --- a/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogMojo.java +++ b/src/main/java/com/github/koraktor/mavanagaiata/mojo/ChangelogMojo.java @@ -143,6 +143,16 @@ enum LinkToBaseUrl { @Parameter(property = "mavanagaiata.changelog.skipCommitsMatching") String skipCommitsMatching; + /** + * Whether to trim trailing whitespace from commits. + *
+ * This is useful for cases where auto formatting used and changelog checked in. + * + * @since 1.1.0 + */ + @Parameter(property = "mavanagaiata.changelog.trimTrailingWhitespace") + boolean trimTrailingWhitespace; + private Pattern skipCommitsPattern; /** @@ -255,7 +265,7 @@ protected void run() throws GitRepositoryException { format.printBranch(repository.getBranch()); } - format.printCommit(currentCommit); + format.printCommit(currentCommit, trimTrailingWhitespace); } } diff --git a/src/test/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormatTest.java b/src/test/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormatTest.java index 0afea3d4..419f1bb5 100644 --- a/src/test/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormatTest.java +++ b/src/test/java/com/github/koraktor/mavanagaiata/mojo/ChangelogFormatTest.java @@ -138,7 +138,17 @@ public boolean isLoaded() { @DisplayName("should print a commit with the given format") @Test void testPrintCommit() throws IOException { - format.printCommit(commit()); + format.printCommit(commit(), false); + + assertOutputLine(""); + assertOutputLine("- Commit > message"); + assertOutputLine(null); + } + + @DisplayName("should print a commit with the given format trimming whitespace") + @Test + void testPrintCommitTrim() throws IOException { + format.printCommit(commitTrailingSpace(), true); assertOutputLine(""); assertOutputLine("- Commit > message"); @@ -149,7 +159,7 @@ void testPrintCommit() throws IOException { @Test void testPrintCommitEscapeHtml() throws IOException { format.escapeHtml = true; - format.printCommit(commit()); + format.printCommit(commit(), false); assertOutputLine(""); assertOutputLine("- Commit > message"); @@ -274,4 +284,68 @@ public boolean isMergeCommit() { } }; } + + private GitCommit commitTrailingSpace() { + return new GitCommit() { + @Override + public Date getAuthorDate() { + return null; + } + + @Override + public String getAuthorEmailAddress() { + return null; + } + + @Override + public String getAuthorName() { + return null; + } + + @Override + public TimeZone getAuthorTimeZone() { + return null; + } + + @Override + public Date getCommitterDate() { + return null; + } + + @Override + public String getCommitterEmailAddress() { + return null; + } + + @Override + public String getCommitterName() { + return null; + } + + @Override + public TimeZone getCommitterTimeZone() { + return null; + } + + @Override + public String getId() { + return null; + } + + @Override + public String getMessage() { + return null; + } + + @Override + public String getMessageSubject() { + return "Commit > message "; + } + + @Override + public boolean isMergeCommit() { + return false; + } + }; + } }