Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhance] Add support to trim trailing whitespace from commit messages #448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +143,16 @@ 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;

/**
Expand Down Expand Up @@ -255,7 +265,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;
}
};
}
}
Loading