Skip to content

Commit

Permalink
feat: enable support for filtering commits with multiple path filters…
Browse files Browse the repository at this point in the history
… (refs #173)
  • Loading branch information
Ulrik Enevoldsen committed Oct 27, 2023
1 parent 0ce4489 commit f6c2b3f
Show file tree
Hide file tree
Showing 6 changed files with 21,448 additions and 11 deletions.
13 changes: 12 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,17 @@ public GitChangelogApi withPathFilter(final String pathFilter) {
return this;
}

/**
* Filter commits using the provided path filters, analogous to using the cli command git log --
* git log <path>...
*
* @param pathFilters the path filters to be used for filtering.
*/
public GitChangelogApi withPathFilters(final List<String> pathFilters) {
this.settings.setPathFilters(pathFilters);
return this;
}

/**
* Some commits may not be included in any tag. Commits that not released yet may not be tagged.
* This is a "virtual tag", added to {@link Changelog#getTags()}, that includes those commits. A
Expand All @@ -711,7 +722,7 @@ public GitChangelogApi withUntaggedName(final String untaggedName) {

private Changelog getChangelog(final GitRepo gitRepo, final boolean useIntegrations)
throws GitChangelogRepositoryException {
gitRepo.setTreeFilter(this.settings.getSubDirFilter());
gitRepo.setPathFilters(this.settings.getSubDirFilter(), this.settings.getPathFilters());
final RevisionBoundary<ObjectId> fromId = this.getFrom(gitRepo, this.settings);
final RevisionBoundary<ObjectId> toId = this.getTo(gitRepo, this.settings);
GitRepoData gitRepoData =
Expand Down
33 changes: 24 additions & 9 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class GitRepo implements Closeable {
private Git git;
private final Repository repository;
private final RevWalk revWalk;
private String pathFilter = "";
private List<String> pathFilters = new ArrayList<>();

public GitRepo() {
this.repository = null;
Expand Down Expand Up @@ -288,13 +288,15 @@ private List<RevCommit> getCommitList(
final RevWalk revWalk,
final RevisionBoundary<RevCommit> fromBoundary,
final RevisionBoundary<RevCommit> toBoundary,
final String pathFilterParam)
final List<String> pathFilters)
throws Exception {
final RevCommit from = fromBoundary.getRevision();
final RevCommit to = toBoundary.getRevision();
final LogCommand logCommand = this.git.log().addRange(from, to);
if (pathFilterParam != null && !pathFilterParam.isEmpty()) {
logCommand.addPath(pathFilterParam);
if (pathFilters != null && !pathFilters.isEmpty()) {
for (String pathFilter : pathFilters) {
logCommand.addPath(pathFilter);
}
}
final List<RevCommit> list = new ArrayList<>();

Expand All @@ -319,7 +321,7 @@ private List<RevCommit> getCommitList(
}

private boolean hasPathFilter() {
return this.pathFilter != null && !this.pathFilter.isEmpty();
return this.pathFilters != null && !this.pathFilters.isEmpty();
}

private ObjectId getPeeled(final Ref tag) {
Expand Down Expand Up @@ -392,7 +394,7 @@ private List<GitTag> gitTags(
final RevisionBoundary<RevCommit> from = this.toRevCommit(fromObjectId);
final RevisionBoundary<RevCommit> to = this.toRevCommit(toObjectId);

this.commitsToInclude = this.getCommitList(this.revWalk, from, to, this.pathFilter);
this.commitsToInclude = this.getCommitList(this.revWalk, from, to, this.pathFilters);

final List<Ref> tagList = this.tagsBetweenFromAndTo(from, to);
/**
Expand Down Expand Up @@ -652,10 +654,23 @@ private GitCommit toGitCommit(final RevCommit revCommit) {
}

/**
* @param pathFilter use when filtering commits
* Sets the pathFilters to be used when filtering commits
*
* @param pathFilter used when filtering commits from single path (kept to ensure backwards
* compatibility)
* @param pathFilters used when filtering commits from multiple paths
*/
public void setTreeFilter(final String pathFilter) {
this.pathFilter = pathFilter == null ? "" : pathFilter;
public void setPathFilters(final String pathFilter, List<String> pathFilters) {
if (pathFilters == null) {
if (pathFilter != null && !pathFilter.isEmpty()) {
this.pathFilters.add(pathFilter);
}
} else {
if (pathFilter != null && !pathFilter.isEmpty()) {
pathFilters.add(pathFilter);
}
this.pathFilters = pathFilters;
}
}

public List<String> getTags(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ public void setPathFilter(final String subDirFilter) {

private String subDirFilter;

/** Path filters to use for filtering commits */
private List<String> pathFilters;

private String encoding = StandardCharsets.UTF_8.name();

public Settings() {}
Expand Down Expand Up @@ -705,6 +708,14 @@ public Charset getEncoding() {
return Charset.forName(this.encoding);
}

public void setPathFilters(List<String> pathFilters) {
this.pathFilters = pathFilters;
}

public List<String> getPathFilters() {
return ofNullable(this.pathFilters).orElse(new ArrayList<>());
}

public List<String> getJiraIssueAdditionalFields() {
if (this.jiraIssueAdditionalFields == null) {
return new ArrayList<>();
Expand Down Expand Up @@ -769,6 +780,7 @@ public int hashCode() {
this.semanticMinorPattern,
this.semanticPatchPattern,
this.subDirFilter,
this.pathFilters,
this.templateBaseDir,
this.templatePath,
this.templateSuffix,
Expand Down Expand Up @@ -833,6 +845,7 @@ public boolean equals(final Object obj) {
&& Objects.equals(this.semanticMinorPattern, other.semanticMinorPattern)
&& Objects.equals(this.semanticPatchPattern, other.semanticPatchPattern)
&& Objects.equals(this.subDirFilter, other.subDirFilter)
&& Objects.equals(this.pathFilters, other.pathFilters)
&& Objects.equals(this.templateBaseDir, other.templateBaseDir)
&& Objects.equals(this.templatePath, other.templatePath)
&& Objects.equals(this.templateSuffix, other.templateSuffix)
Expand Down Expand Up @@ -943,6 +956,8 @@ public String toString() {
+ this.useIntegrations
+ ", subDirFilter="
+ this.subDirFilter
+ ", pathFilters="
+ this.pathFilters
+ ", encoding="
+ this.encoding
+ "]";
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
Expand Down Expand Up @@ -149,6 +151,24 @@ public void testPathFilterCanBeSpecified() throws Exception {
ApprovalsWrapper.verify(given);
}

@Test
public void testPathFiltersCanBeSpecified() throws Exception {
final String templatePath = "templatetest/testAuthorsCommitsExtended.mustache";

final GitChangelogApi given =
gitChangelogApiBuilder() //
.withJiraEnabled(true)
.withGitHubEnabled(true)
.withGitLabEnabled(true)
.withRedmineEnabled(true)
.withFromRevision(ZERO_COMMIT)
.withToRevision("1.71") //
.withTemplatePath(templatePath) //
.withPathFilters(new ArrayList<>(Arrays.asList("src")));

ApprovalsWrapper.verify(given);
}

@Test
public void testThatIssuesCanBeRemoved() throws Exception {

Expand Down
Loading

0 comments on commit f6c2b3f

Please sign in to comment.