Skip to content

Commit

Permalink
Adding feature to ignore tags by regexp
Browse files Browse the repository at this point in the history
 * Also testing tag in feature branch #23
  • Loading branch information
tomasbjerre committed Apr 6, 2016
1 parent 96ee3bb commit 4b3463c
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 17 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

Changelog of Git Changelog.

## Next release
### GitHub [#23](https://github.com/tomasbjerre/git-changelog-lib/issues/23) changelog is generating incorrect order of commits/Issues

**Testing tag in feature branch**


[d9fb26da2290cc3](https://github.com/tomasbjerre/git-changelog-lib/commit/d9fb26da2290cc3) Tomas Bjerre *2016-04-06 19:35:19*


### Other changes

**Updating CHANGELOG.md**


[96ee3bb55e4da2d](https://github.com/tomasbjerre/git-changelog-lib/commit/96ee3bb55e4da2d) Tomas Bjerre *2016-03-20 13:25:18*


## 1.39
### Other changes

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ public GitChangelogApi withIgnoreCommitsWithMesssage(String ignoreCommitsIfMessa
return this;
}

/**
* A regular expression that is evaluated on each tag. If it matches, the tag
* will be filtered out and not included in the changelog.
*/
public GitChangelogApi withIgnoreTagsIfNameMatches(String ignoreTagsIfNameMatches) {
settings.setIgnoreTagsIfNameMatches(ignoreTagsIfNameMatches);
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
Expand Down Expand Up @@ -370,7 +379,8 @@ private Changelog getChangelog(GitRepo gitRepo) throws GitChangelogRepositoryExc
.or(gitRepo.getCommit(ZERO_COMMIT));
ObjectId toId = getId(gitRepo, settings.getToRef(), settings.getToCommit()) //
.or(gitRepo.getRef(REF_MASTER));
GitRepoData gitRepoData = gitRepo.getGitRepoData(fromId, toId, settings.getUntaggedName());
GitRepoData gitRepoData = gitRepo.getGitRepoData(fromId, toId, settings.getUntaggedName(),
settings.getIgnoreTagsIfNameMatches());
List<GitCommit> diff = gitRepoData.getGitCommits();
List<ParsedIssue> issues = new IssueParser(settings, diff).parseForIssues();
if (settings.ignoreCommitsWithoutIssue()) {
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.transform;
import static com.google.common.collect.Maps.newHashMap;
import static java.util.regex.Pattern.compile;
import static org.eclipse.jgit.lib.ObjectId.fromString;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.REF_MASTER;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.ZERO_COMMIT;
Expand All @@ -31,6 +32,7 @@
import se.bjurr.gitchangelog.internal.git.model.GitTag;

import com.google.common.base.Function;
import com.google.common.base.Optional;

public class GitRepo {
private static final Function<RevCommit, GitCommit> TO_GITCOMMIT = new Function<RevCommit, GitCommit>() {
Expand Down Expand Up @@ -81,28 +83,32 @@ public GitRepo(File repo) throws GitChangelogRepositoryException {
* {@link GitChangelogApiConstants#ZERO_COMMIT}, it is included.
* @param to
* To and including this commit.
* @param untaggedName
* @throws GitChangelogRepositoryException
*/
public GitRepoData getGitRepoData(ObjectId from, ObjectId to, String untaggedName)
throws GitChangelogRepositoryException {
public GitRepoData getGitRepoData(ObjectId from, ObjectId to, String untaggedName,
Optional<String> ignoreTagsIfNameMatches) throws GitChangelogRepositoryException {
Git git = null;
try {
git = new Git(repository);
List<GitCommit> gitCommits = getGitCommits(git, from, to);
return new GitRepoData(gitCommits, gitTags(git, gitCommits, untaggedName));
return new GitRepoData(gitCommits, gitTags(git, gitCommits, untaggedName, ignoreTagsIfNameMatches));
} catch (Exception e) {
throw new GitChangelogRepositoryException(toString(), e);
} finally {
git.close();
}
}

private List<GitTag> gitTags(Git git, List<GitCommit> gitCommits, String untaggedName) throws GitAPIException {
private List<GitTag> gitTags(Git git, List<GitCommit> gitCommits, String untaggedName,
Optional<String> ignoreTagsIfNameMatches) throws GitAPIException {
List<GitTag> refs = newArrayList();
List<Ref> refList = git.tagList().call();
Map<String, Ref> refsPerCommit = newHashMap();
for (Ref ref : refList) {
if (ignoreTagsIfNameMatches.isPresent()) {
if (compile(ignoreTagsIfNameMatches.get()).matcher(ref.getName()).matches()) {
continue;
}
}
refsPerCommit.put(toHash(getPeeled(ref).getName()), ref);
}

Expand Down Expand Up @@ -133,8 +139,8 @@ private List<GitTag> gitTags(Git git, List<GitCommit> gitCommits, String untagge

private List<GitCommit> getGitCommits(Git git, ObjectId from, ObjectId to) throws GitChangelogRepositoryException {
try {
final List<RevCommit> toList = newArrayList(git.log().add(to).call());
if (from.name().equals(firstCommit().name())) {
final List<RevCommit> toList = newArrayList(git.log().add(to).call());
return newArrayList(transform(toList, TO_GITCOMMIT));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public class Settings {
* Include all commits to here. Any commit hash.
*/
private String toCommit;
/**
* A regular expression that is evaluated on each tag. If it matches, the tag
* will be filtered out and not included in the changelog.
*/
private String ignoreTagsIfNameMatches;
/**
* A regular expression that is evaluated on the commit message of each commit.
* If it matches, the commit will be filtered out and not included in the
Expand Down Expand Up @@ -200,6 +205,10 @@ public void setIgnoreCommitsIfMessageMatches(String ignoreCommitsIfMessageMatche
this.ignoreCommitsIfMessageMatches = ignoreCommitsIfMessageMatches;
}

public void setIgnoreTagsIfNameMatches(String ignoreTagsIfNameMatches) {
this.ignoreTagsIfNameMatches = ignoreTagsIfNameMatches;
}

public void setJiraIssuePattern(String jiraIssuePattern) {
this.jiraIssuePattern = jiraIssuePattern;
}
Expand Down Expand Up @@ -259,6 +268,10 @@ public String getUntaggedName() {
return fromNullable(untaggedName).or(DEFAULT_UNTAGGED_NAME);
}

public Optional<String> getIgnoreTagsIfNameMatches() {
return fromNullable(ignoreTagsIfNameMatches);
}

public void setUntaggedName(String untaggedName) {
this.untaggedName = untaggedName;
}
Expand Down
101 changes: 93 additions & 8 deletions src/test/java/se/bjurr/gitchangelog/internal/git/GitRepoTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package se.bjurr.gitchangelog.internal.git;

import static com.google.common.collect.Lists.reverse;
import static com.google.common.collect.Maps.newHashMap;
import static org.assertj.core.api.Assertions.assertThat;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.REF_MASTER;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.ZERO_COMMIT;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.eclipse.jgit.lib.ObjectId;
import org.junit.Before;
import org.junit.Test;

import se.bjurr.gitchangelog.internal.git.model.GitCommit;
import se.bjurr.gitchangelog.internal.git.model.GitTag;

import com.google.common.base.Optional;
import com.google.common.io.Resources;

public class GitRepoTest {
Expand All @@ -36,8 +40,8 @@ public void testThatRepoCanBeFound() throws Exception {
@Test
public void testThatTagsCanBeListed() throws Exception {
GitRepo gitRepo = getGitRepo();
GitRepoData gitRepoData = gitRepo
.getGitRepoData(gitRepo.getCommit(ZERO_COMMIT), gitRepo.getRef(REF_MASTER), "No tag");
GitRepoData gitRepoData = gitRepo.getGitRepoData(gitRepo.getCommit(ZERO_COMMIT), gitRepo.getRef(REF_MASTER),
"No tag", Optional.<String> absent());
assertThat(gitRepoData.getGitTags()).isNotEmpty();
}

Expand All @@ -58,12 +62,76 @@ public void testThatCommitsCanBeRetrieved() throws Exception {
assertThat(gitRepo.getCommit(TAG_1_0_HASH).name()).isEqualTo(TAG_1_0_HASH);
}

@Test
public void testThatTagInFeatureBranchDoesNotIncludeCommitsInItsMainBranch() throws Exception {
GitRepo gitRepo = getGitRepo();
ObjectId from = gitRepo.getCommit("87c0d72888961712d4d63dd6298c24c1133a6b51");
ObjectId to = gitRepo.getRef("test");

GitRepoData gitRepoData = gitRepo.getGitRepoData(from, to, "No tag", Optional.of("asdasdasd"));
Map<String, GitTag> perTag = perTag(gitRepoData.getGitTags());
assertThat(gitRepoData.getGitTags())//
.hasSize(2);
assertThat(gitRepoData.getGitTags())//
.hasSize(2);
assertThat(perTag.keySet())//
.hasSize(2)//
.contains("No tag");
GitTag noTagTag = perTag.get("No tag");

assertThat(noTagTag.getGitCommits())//
.hasSize(2);
assertThat(noTagTag.getGitCommits().get(0).getMessage().trim())//
.isEqualTo("Some stuff in test again");
assertThat(noTagTag.getGitCommits().get(1).getMessage().trim())//
.isEqualTo("Merge branch 'test-feature' into test");

GitTag testFeatureTag = perTag.get("refs/tags/tag-in-test-feature");
assertThat(testFeatureTag.getGitCommits())//
.hasSize(2);
assertThat(testFeatureTag.getGitCommits().get(0).getMessage().trim())//
.isEqualTo("Some stuff in test-feature");
// TODO: This is a bug! #23
assertThat(testFeatureTag.getGitCommits().get(1).getMessage().trim())//
.isEqualTo("some stuff in test branch");
}

@Test
public void testThatTagCanBeIgnored() throws Exception {
GitRepo gitRepo = getGitRepo();
ObjectId from = gitRepo.getCommit("87c0d72888961712d4d63dd6298c24c1133a6b51");
ObjectId to = gitRepo.getRef("test");

GitRepoData gitRepoData = gitRepo.getGitRepoData(from, to, "No tag", Optional.of(".*tag-in-test-feature$"));
Map<String, GitTag> perTag = perTag(gitRepoData.getGitTags());
assertThat(gitRepoData.getGitTags())//
.hasSize(1);
assertThat(gitRepoData.getGitTags())//
.hasSize(1);
assertThat(perTag.keySet())//
.hasSize(1)//
.contains("No tag");
GitTag noTagTag = perTag.get("No tag");

assertThat(noTagTag.getGitCommits())//
.hasSize(4);
assertThat(noTagTag.getGitCommits().get(0).getMessage().trim())//
.isEqualTo("Some stuff in test again");
assertThat(noTagTag.getGitCommits().get(1).getMessage().trim())//
.isEqualTo("Merge branch 'test-feature' into test");
assertThat(noTagTag.getGitCommits().get(2).getMessage().trim())//
.isEqualTo("Some stuff in test-feature");
assertThat(noTagTag.getGitCommits().get(3).getMessage().trim())//
.isEqualTo("some stuff in test branch");
}

@Test
public void testThatCommitsBetweenCommitAndReferenceCanBeListed() throws Exception {
GitRepo gitRepo = getGitRepo();
ObjectId firstCommit = gitRepo.getCommit(ZERO_COMMIT);
ObjectId lastCommit = gitRepo.getRef(REF_MASTER);
List<GitCommit> diff = gitRepo.getGitRepoData(firstCommit, lastCommit, "No tag").getGitCommits();
List<GitCommit> diff = gitRepo.getGitRepoData(firstCommit, lastCommit, "No tag", Optional.<String> absent())
.getGitCommits();
assertThat(diff.size()).isGreaterThan(10);
assertThat(reverse(diff).get(0).getHash()).startsWith(FIRST_COMMIT_HASH);
}
Expand All @@ -73,7 +141,7 @@ public void testThatCommitsBetweenZeroCommitAndCommitCanBeListed() throws Except
GitRepo gitRepo = getGitRepo();
ObjectId firstCommit = gitRepo.getCommit(ZERO_COMMIT);
ObjectId lastCommit = gitRepo.getCommit(TAG_1_0_HASH);
GitRepoData gitRepoData = gitRepo.getGitRepoData(firstCommit, lastCommit, "No tag");
GitRepoData gitRepoData = gitRepo.getGitRepoData(firstCommit, lastCommit, "No tag", Optional.<String> absent());
assertThat(gitRepoData.getGitCommits()).as("Commits in first release.").hasSize(6);
assertThat(gitRepoData.getGitTags()).as("Tags in first release.").hasSize(1);
assertThat(reverse(gitRepoData.getGitCommits()).get(0).getHash()).startsWith(FIRST_COMMIT_HASH);
Expand All @@ -84,17 +152,26 @@ public void testThatCommitsSecondReleaseCommitCanBeListed() throws Exception {
GitRepo gitRepo = getGitRepo();
ObjectId firstRelease = gitRepo.getRef("refs/tags/1.0");
ObjectId secondRelease = gitRepo.getRef("refs/tags/1.1");
List<GitCommit> diff = gitRepo.getGitRepoData(firstRelease, secondRelease, "No tag").getGitCommits();
assertThat(diff).as("Commits in second release.").hasSize(8);
assertThat(reverse(diff).get(0).getHash()).startsWith("3950");
List<GitCommit> diff = gitRepo.getGitRepoData(firstRelease, secondRelease, "No tag", Optional.<String> absent())
.getGitCommits();
assertThat(diff).as("Commits in second release from 1.0.").hasSize(8);
assertThat(diff.get(7).getHash()).startsWith("3950");
assertThat(diff.get(0).getHash()).startsWith(secondRelease.getName().substring(0, 10));

ObjectId firstCommit = gitRepo.getCommit(ZERO_COMMIT);
diff = gitRepo.getGitRepoData(firstCommit, secondRelease, "No tag", Optional.<String> absent()).getGitCommits();
assertThat(diff).as("Commits in second release from zero commit.").hasSize(14);
assertThat(diff.get(7).getHash()).startsWith("3950");
assertThat(diff.get(0).getHash()).startsWith(secondRelease.getName().substring(0, 10));
}

@Test
public void testThatCommitsBetweenCommitAndCommitCanBeListed() throws Exception {
GitRepo gitRepo = getGitRepo();
ObjectId firstCommit = gitRepo.getCommit(FIRST_COMMIT_HASH_FULL);
ObjectId lastCommit = gitRepo.getCommit("e3766e2d4bc6d206475c5d2ed96b3f967a6e157e");
List<GitCommit> diff = gitRepo.getGitRepoData(firstCommit, lastCommit, "No tag").getGitCommits();
List<GitCommit> diff = gitRepo.getGitRepoData(firstCommit, lastCommit, "No tag", Optional.<String> absent())
.getGitCommits();
assertThat(diff).isNotEmpty();
assertThat(reverse(diff).get(0).getHash())//
.as("first")//
Expand All @@ -104,6 +181,14 @@ public void testThatCommitsBetweenCommitAndCommitCanBeListed() throws Exception
.startsWith("e3766e2d4bc6d20");
}

private Map<String, GitTag> perTag(List<GitTag> gitTags) {
Map<String, GitTag> map = newHashMap();
for (GitTag gitTag : gitTags) {
map.put(gitTag.getName(), gitTag);
}
return map;
}

private GitRepo getGitRepo() throws Exception {
return new GitRepo(gitRepoFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import se.bjurr.gitchangelog.internal.git.GitRepoData;
import se.bjurr.gitchangelog.internal.git.model.GitTag;

import com.google.common.base.Optional;
import com.google.common.base.Stopwatch;

public class LibPerformanceTest {
Expand Down Expand Up @@ -57,7 +58,7 @@ public void testThatGimmitsBetweenTagsCanBeFound() throws Exception {

ObjectId fromId = gitRepo.getCommit(ZERO_COMMIT);
ObjectId toId = gitRepo.getRef(REF_MASTER);
GitRepoData gitRepoData = gitRepo.getGitRepoData(fromId, toId, UNTAGGED_NAME);
GitRepoData gitRepoData = gitRepo.getGitRepoData(fromId, toId, UNTAGGED_NAME, Optional.<String> absent());
List<GitTag> allTags = gitRepoData.getGitTags();
int i = 0;
for (GitTag from : allTags) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/assertions/testAuthorsCommits.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ Changelog of Git Changelog.

* Tomas Bjerre

[8371342ad0d887d](https://server/8371342ad0d887d) Tomas Bjerre *2016-04-06 18:40:51*

Some stuff in test again


[090e7f4b11223c4](https://server/090e7f4b11223c4) Tomas Bjerre *2016-04-06 15:13:04*

Some stuff in test-feature


[c5d37f5e964afd5](https://server/c5d37f5e964afd5) Tomas Bjerre *2016-04-06 15:12:12*

some stuff in test branch


[87c0d7288896171](https://server/87c0d7288896171) Tomas Bjerre *2016-03-19 20:32:32*

Multiple issues
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/assertions/testCommits.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@

Changelog of Git Changelog.

## Tomas Bjerre - 2016-04-06 18:40:51
[8371342ad0d887d](https://server/8371342ad0d887d)

Some stuff in test again

## Tomas Bjerre - 2016-04-06 15:13:04
[090e7f4b11223c4](https://server/090e7f4b11223c4)

Some stuff in test-feature

## Tomas Bjerre - 2016-04-06 15:12:12
[c5d37f5e964afd5](https://server/c5d37f5e964afd5)

some stuff in test branch

## Tomas Bjerre - 2016-03-19 20:32:32
[87c0d7288896171](https://server/87c0d7288896171)

Expand Down
Loading

0 comments on commit 4b3463c

Please # to comment.