Skip to content

Commit

Permalink
[enhance] Add support to trim trailing whitespace from commit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
hazendaz committed Sep 21, 2024
1 parent a584a61 commit e6438ed
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,21 @@ enum LinkToBaseUrl {
@Parameter(property = "mavanagaiata.changelog.skipCommitsMatching")
String skipCommitsMatching;

/**
* Whether to trim trailing whitespace from commits.
* <p>
* 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;

/** The Constant REMOVE_TRAILING_PATTERN. */
private static final Pattern REMOVE_TRAILING_PATTERN = Pattern.compile("\\p{Blank}+$", Pattern.MULTILINE);

/**
* Walks through the history of the currently checked out branch of the
* Git repository and builds a changelog from the commits contained in that
Expand Down Expand Up @@ -255,7 +268,7 @@ protected void run() throws GitRepositoryException {
format.printBranch(repository.getBranch());
}

format.printCommit(currentCommit);
format.printCommit(currentCommit, trimTrailingWhitespace);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 &gt; message");
Expand Down Expand Up @@ -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;
}
};
}
}

0 comments on commit e6438ed

Please sign in to comment.