Skip to content

Commit

Permalink
Merge branch 'enevoldsen11-feature/issue-173-multiple-path-filters'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 30, 2023
2 parents 0ce4489 + 3b52d87 commit 9a9d335
Show file tree
Hide file tree
Showing 9 changed files with 21,439 additions and 37 deletions.
14 changes: 8 additions & 6 deletions src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -689,13 +690,14 @@ public GitChangelogApi withToCommit(final String toCommit) {
public GitChangelogApi withToRef(final String toBranch) {
return this.withToRevision(toBranch);
}

/**
* Filter commits using the provided path filter, analogous to using the cli command git log --
* 'pathFilter'
* 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 withPathFilter(final String pathFilter) {
this.settings.setPathFilter(pathFilter);
public GitChangelogApi withPathFilters(final String... pathFilters) {
this.settings.setPathFilters(Arrays.asList(pathFilters));
return this;
}

Expand All @@ -711,7 +713,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.getPathFilters());
final RevisionBoundary<ObjectId> fromId = this.getFrom(gitRepo, this.settings);
final RevisionBoundary<ObjectId> toId = this.getTo(gitRepo, this.settings);
GitRepoData gitRepoData =
Expand Down
21 changes: 10 additions & 11 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 @@ -651,11 +653,8 @@ private GitCommit toGitCommit(final RevCommit revCommit) {
merge);
}

/**
* @param pathFilter use when filtering commits
*/
public void setTreeFilter(final String pathFilter) {
this.pathFilter = pathFilter == null ? "" : pathFilter;
public void setPathFilters(List<String> pathFilters) {
this.pathFilters = pathFilters;
}

public List<String> getTags(
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/se/bjurr/gitchangelog/internal/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,8 @@ public class Settings implements Serializable {
/** Integrate with services to get more details about issues. */
private boolean useIntegrations;

public String getSubDirFilter() {
return ofNullable(this.subDirFilter).orElse("");
}

public void setPathFilter(final String subDirFilter) {
this.subDirFilter = subDirFilter;
}

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

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

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

public void setPathFilters(final 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 @@ -768,7 +769,7 @@ public int hashCode() {
this.semanticMajorPattern,
this.semanticMinorPattern,
this.semanticPatchPattern,
this.subDirFilter,
this.pathFilters,
this.templateBaseDir,
this.templatePath,
this.templateSuffix,
Expand Down Expand Up @@ -832,7 +833,7 @@ public boolean equals(final Object obj) {
&& Objects.equals(this.semanticMajorPattern, other.semanticMajorPattern)
&& 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 @@ -941,8 +942,8 @@ public String toString() {
+ this.semanticPatchPattern
+ ", useIntegrations="
+ this.useIntegrations
+ ", subDirFilter="
+ this.subDirFilter
+ ", pathFilters="
+ this.pathFilters
+ ", encoding="
+ this.encoding
+ "]";
Expand Down
24 changes: 21 additions & 3 deletions src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,25 @@ public void testPathFilterCanBeSpecified() throws Exception {
.withFromCommit(ZERO_COMMIT) //
.withToRef("1.71") //
.withTemplatePath(templatePath) //
.withPathFilter("src");
.withPathFilters("src");

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("src");

ApprovalsWrapper.verify(given);
}
Expand Down Expand Up @@ -347,7 +365,7 @@ public void testThatOnlyGithubIssuesCanBeParsed() throws Exception {
.withUseIntegrations(true)
.withFromCommit(ZERO_COMMIT) //
.withToRef("1.71") //
.withPathFilter("src")
.withPathFilters("src")
.withIgnoreCommitsWithoutIssue(true);

ApprovalsWrapper.verify(given);
Expand All @@ -360,7 +378,7 @@ public void testThatOnlyJiraIssuesCanBeParsed() throws Exception {
.withJiraEnabled(true)
.withFromCommit(ZERO_COMMIT) //
.withToRef("1.71") //
.withPathFilter("src")
.withPathFilters("src")
.withIgnoreCommitsWithoutIssue(true);

ApprovalsWrapper.verify(given);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ settings:
"gitLabEnabled": true,
"gitLabProjectName": "tomasbjerre",
"useIntegrations": false,
"subDirFilter": "src",
"pathFilters": [
"src"
],
"encoding": "UTF-8"
}

Expand Down
Loading

0 comments on commit 9a9d335

Please sign in to comment.