From 7f69566cf8b4cff86be48a4ac1241ab477910c24 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sat, 9 Apr 2016 14:50:37 +0200 Subject: [PATCH] Traversing commit tree by parents #23 * To find all commits in all tags. --- .travis.yml | 4 +- CHANGELOG.md | 4 +- .../gitchangelog/api/GitChangelogApi.java | 8 +- .../bjurr/gitchangelog/api/model/Commit.java | 6 +- .../gitchangelog/internal/git/GitRepo.java | 365 +++++++++++++----- .../internal/git/GitRepoData.java | 12 +- .../internal/git/GitRepoDataHelper.java | 2 +- .../gitchangelog/api/GitChangelogApiTest.java | 11 +- .../internal/git/GitRepoTest.java | 86 +++-- .../gitchangelog/test/LibPerformanceTest.java | 2 - .../assertions/testAuthorsCommits.md | 18 +- src/test/resources/assertions/testCommits.md | 18 +- .../assertions/testCommitsVariables.md | 32 +- .../resources/assertions/testTagsCommits.md | 8 +- 14 files changed, 383 insertions(+), 193 deletions(-) diff --git a/.travis.yml b/.travis.yml index 16a201b2..707e67a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ sudo: false language: java +env: + - secure: "f78upMWhPViLh2W9jLjkjikZgSJCd9LuJVZF227JFSLGV8+DC0YbHQi6pYmpejhTT9H1OTqXyQ5c16QEQzutx6O63iyWdvWtIHtuct66jWPUp1NL3EAlK9F4NdG/9sbdEcElgrQxEArjYB5w+UZ+1uJucc1oMqIWbWQ089tvdx1VYZYRdP3pdLOoqgAqi4/GOxj8vRO9lIY7kc9IhEDDAfzNrGV8/LKao9f1pEOI+XcVDMOovRcstkMJZ5kqAep7xZTURGScKE6dMm58IEPDQAfbbGxAaJTO1HiNhFt6u2n+Z/oXZS8zlD/mcTA2acE6Ip7AMplSrac3bkzhwULwVu1PeDhteS9cwhpwJ/B3dR5sDAA+U/4WkfDV5VyyWVn8Z7Jy5nzDyUrtZBiPCzUyKDEaKc0mlLjWJjGiboq3ACnn62CE+VWQz9J0vnjxrwwqLcZbAtPzbFn3myYNbrf2qHt7n9eL3l9c9mkzYCfC6hz1oqsa35cimL6NXzjbRPu8ruyZdDcOdPBlVcdQ/g8ZhTslo5SCi4yukGQ0NbdPF0bJ4JXmBT49KUGbPOrCqdzoe+or04uJ6KkbLsO+bSGtVcidMuUeeylhfUBMtZs8XBKB2o3WWpZKjCUlKDl/vfDmlP7G+1Bd6lR11F1Z5jmtPLBzKlURrAMPVSk4EwFLNeQ=" jdk: - oraclejdk8 - oraclejdk7 @@ -12,7 +14,7 @@ before_script: - git pull - git log --graph --full-history --all --color --date=short --pretty=format:"%Cred%x09%h %Creset%ad%Cgreen%d %Creset %s %C(bold)(%an)%Creset" script: - - ./gradlew build --stacktrace -i + - ./gradlew build -DGITHUB_OAUTH2TOKEN=$GITHUB_OAUTH2TOKEN --stacktrace -i after_script: - find -name TEST* -exec cat {} \; notifications: diff --git a/CHANGELOG.md b/CHANGELOG.md index 94ba3192..e6fe9337 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,14 @@ Changelog of Git Changelog. -## Next release +## 1.40 ### GitHub [#23](https://github.com/tomasbjerre/git-changelog-lib/issues/23) changelog is generating incorrect order of commits/Issues **Adding feature to ignore tags by regexp** * Also testing tag in feature branch -[19aa174570e2d5e](https://github.com/tomasbjerre/git-changelog-lib/commit/19aa174570e2d5e) Tomas Bjerre *2016-04-06 20:34:01* +[8166ec7dfd456df](https://github.com/tomasbjerre/git-changelog-lib/commit/8166ec7dfd456df) Tomas Bjerre *2016-04-06 20:39:27* ### Other changes diff --git a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java index a82d196c..3e1c5748 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java +++ b/src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java @@ -349,7 +349,11 @@ public void toMediaWiki(String username, String password, String url, String tit * @throws GitChangelogRepositoryException */ public Changelog getChangelog() throws GitChangelogRepositoryException { - return getChangelog(new GitRepo(new File(settings.getFromRepo()))); + try (GitRepo gitRepo = new GitRepo(new File(settings.getFromRepo()))) { + return getChangelog(gitRepo); + } catch (IOException e) { + throw new GitChangelogRepositoryException("", e); + } } /** @@ -368,7 +372,7 @@ public String render() throws GitChangelogRepositoryException { new Object[] { this.getChangelog(), settings.getExtendedVariables() } // ).flush(); return writer.toString(); - } catch (IOException e) { + } catch (Exception e) { // Should be impossible! throw propagate(e); } diff --git a/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java b/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java index 0a3c4559..2c264dd4 100644 --- a/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java +++ b/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java @@ -23,7 +23,7 @@ public Commit(String authorName, String authorEmailAddress, String commitTime, L this.message = checkNotNull(message, "message").trim(); this.commitTime = checkNotNull(commitTime, "commitTime"); this.commitTimeLong = checkNotNull(commitTimeLong, "commitTimeLong"); - this.hash = checkNotNull(hash, "hash"); + this.hash = toHash(checkNotNull(hash, "hash")); } public String getHash() { @@ -119,4 +119,8 @@ static private List toNoEmptyStringsList(String message) { public String toString() { return "hash: " + hash + " message: " + message; } + + private static String toHash(String input) { + return input.substring(0, 15); + } } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java index f604e63d..6233bfe5 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java @@ -1,60 +1,54 @@ package se.bjurr.gitchangelog.internal.git; -import static com.google.common.collect.Iterables.transform; +import static com.google.common.base.Throwables.propagate; import static com.google.common.collect.Iterators.getLast; 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 com.google.common.collect.Sets.newHashSet; 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; +import java.io.Closeable; import java.io.File; import java.io.IOException; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.TreeSet; import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import se.bjurr.gitchangelog.api.GitChangelogApiConstants; import se.bjurr.gitchangelog.api.exceptions.GitChangelogRepositoryException; import se.bjurr.gitchangelog.internal.git.model.GitCommit; 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 TO_GITCOMMIT = new Function() { - @Override - public GitCommit apply(RevCommit input) { - return new GitCommit( // - input.getAuthorIdent().getName(),// - input.getAuthorIdent().getEmailAddress(),// - new Date(input.getCommitTime() * 1000L),// - input.getFullMessage(),// - toHash(input.getId().getName())); - } - }; - - private static String toHash(String input) { - return input.substring(0, 15); - } - +public class GitRepo implements Closeable { + private static final Logger LOG = LoggerFactory.getLogger(GitRepo.class); private final Repository repository; + private final RevWalk revWalk; + private Git git; public GitRepo() { this.repository = null; + this.revWalk = null; } public GitRepo(File repo) throws GitChangelogRepositoryException { @@ -71,6 +65,8 @@ public GitRepo(File repo) throws GitChangelogRepositoryException { throw new GitChangelogRepositoryException("Did not find a GIT repo in " + repo.getAbsolutePath()); } this.repository = builder.build(); + this.revWalk = new RevWalk(repository); + this.git = new Git(repository); } catch (IOException e) { throw new GitChangelogRepositoryException("Could not use GIT repo in " + repo.getAbsolutePath(), e); } @@ -86,118 +82,270 @@ public GitRepo(File repo) throws GitChangelogRepositoryException { */ public GitRepoData getGitRepoData(ObjectId from, ObjectId to, String untaggedName, Optional ignoreTagsIfNameMatches) throws GitChangelogRepositoryException { - Git git = null; try { - git = new Git(repository); - List gitCommits = getGitCommits(git, from, to); - return new GitRepoData(gitCommits, gitTags(git, gitCommits, untaggedName, ignoreTagsIfNameMatches)); + return new GitRepoData(gitTags(from, to, untaggedName, ignoreTagsIfNameMatches)); } catch (Exception e) { throw new GitChangelogRepositoryException(toString(), e); - } finally { - git.close(); } } - private List gitTags(Git git, List gitCommits, String untaggedName, - Optional ignoreTagsIfNameMatches) throws GitAPIException { - List refs = newArrayList(); - List refList = git.tagList().call(); - Map refsPerCommit = newHashMap(); - for (Ref ref : refList) { - if (ignoreTagsIfNameMatches.isPresent()) { - if (compile(ignoreTagsIfNameMatches.get()).matcher(ref.getName()).matches()) { - continue; + public ObjectId getRef(String fromRef) { + try { + for (Ref foundRef : getAllRefs().values()) { + if (foundRef.getName().endsWith(fromRef)) { + Ref ref = getAllRefs().get(foundRef.getName()); + Ref peeledRef = repository.peel(ref); + if (peeledRef.getPeeledObjectId() != null) { + return peeledRef.getPeeledObjectId(); + } else { + return ref.getObjectId(); + } } } - refsPerCommit.put(toHash(getPeeled(ref).getName()), ref); + throw new RuntimeException(fromRef + " not found in:\n" + toString()); + } catch (Exception e) { + throw new RuntimeException("", e); } + } - String currentTagName = untaggedName; - List gitCommitsInCurrentTag = newArrayList(); - for (GitCommit gitCommit : gitCommits) { - if (refsPerCommit.containsKey(gitCommit.getHash())) { - if (!gitCommitsInCurrentTag.isEmpty()) { - GitTag newTag = new GitTag(currentTagName, gitCommitsInCurrentTag); - refs.add(newTag); - gitCommitsInCurrentTag = newArrayList(); - } - if (refsPerCommit.containsKey(gitCommit.getHash())) { - currentTagName = refsPerCommit.get(gitCommit.getHash()).getName(); - } else { - currentTagName = untaggedName; - } - } - gitCommitsInCurrentTag.add(gitCommit); + public ObjectId getCommit(String fromCommit) { + if (fromCommit.startsWith(ZERO_COMMIT)) { + return firstCommit(); } - if (!gitCommitsInCurrentTag.isEmpty()) { - GitTag newTag = new GitTag(currentTagName, gitCommitsInCurrentTag); - refs.add(newTag); + return fromString(fromCommit); + } + + private List gitTags(ObjectId from, ObjectId to, String untaggedName, Optional ignoreTagsIfNameMatches) + throws Exception { + List tagList = tagsBetweenFromAndTo(from, to); + /** + * What: Contains only the commits that are directly referred to by tags.
+ * Why: To know if a new tag was found when walking up through the parents. + */ + Map tagPerCommitHash = getTagPerCommitHash(ignoreTagsIfNameMatches, tagList); + /** + * What: Populated with all included commits, referring to there tags.
+ * Why: To know if a commit is already mapped to a tag, or not. + */ + Map tagPerCommitsHash = newHashMap(); + /** + * What: Commits per tag.
+ * Why: Its what we are here for! =) + */ + Map> commitsPerTag = newHashMap(); + + populateComitPerTag(from, to, tagPerCommitHash, tagPerCommitsHash, commitsPerTag, null); + populateComitPerTag(from, to, tagPerCommitHash, tagPerCommitsHash, commitsPerTag, untaggedName); + + List tags = newArrayList(); + addToTags(commitsPerTag, tags, untaggedName); + List tagCommitHashSortedByCommitTime = tagCommitHashSortedByCommitTime(tagPerCommitsHash.keySet()); + for (String tagCommitHash : tagCommitHashSortedByCommitTime) { + if (tagPerCommitHash.containsKey(tagCommitHash)) { + Ref tag = tagPerCommitHash.get(tagCommitHash); + addToTags(commitsPerTag, tags, tag.getName()); + } } + return tags; + } - return refs; + /** + * This can be done recursively but will result in {@link StackOverflowError} + * for large repos. + */ + private void populateComitPerTag(ObjectId from, ObjectId to, Map tagPerCommitHash, + Map tagPerCommitsHash, Map> commitsPerTag, String startingTagName) + throws Exception { + Set moreWork = populateCommitPerTag(from, to, commitsPerTag, tagPerCommitHash, tagPerCommitsHash, + startingTagName); + do { + Set evenMoreWork = newHashSet(); + for (TraversalWork tw : newArrayList(moreWork)) { + moreWork.remove(tw); + evenMoreWork.addAll(populateCommitPerTag(from, tw.getTo(), commitsPerTag, tagPerCommitHash, tagPerCommitsHash, + tw.getCurrentTagName())); + } + moreWork.addAll(evenMoreWork); + LOG.debug("Work left: " + moreWork.size()); + } while (!moreWork.isEmpty()); } - private List getGitCommits(Git git, ObjectId from, ObjectId to) throws GitChangelogRepositoryException { - try { - if (from.name().equals(firstCommit().name())) { - final List toList = newArrayList(git.log().add(to).call()); - return newArrayList(transform(toList, TO_GITCOMMIT)); + private List tagsBetweenFromAndTo(ObjectId from, ObjectId to) throws Exception { + List tagList = git.tagList().call(); + List icludedCommits = newArrayList(git.log().addRange(from, to).call()); + List includedTags = newArrayList(); + for (Ref tag : tagList) { + ObjectId peeledTag = getPeeled(tag); + if (icludedCommits.contains(peeledTag)) { + includedTags.add(tag); } + } + return includedTags; + } - Iterable itr = git // - .log() // - .addRange(from, to) // - .call(); + private List tagCommitHashSortedByCommitTime(Set commitHashes) { + List sorted = newArrayList(commitHashes); + Collections.sort(sorted, new Comparator() { + @Override + public int compare(String hash1, String hash2) { + try { + RevCommit revCommit1 = revWalk.lookupCommit(getCommit(hash1)); + revWalk.parseHeaders(revCommit1); + RevCommit revCommit2 = revWalk.lookupCommit(getCommit(hash2)); + revWalk.parseHeaders(revCommit2); + return toGitCommit(revCommit1).compareTo(toGitCommit(revCommit2)); + } catch (IOException e) { + throw propagate(e); + } + } + }); + return sorted; + } - return newArrayList(transform(itr, TO_GITCOMMIT)); - } catch (Exception e) { - throw new GitChangelogRepositoryException(from.name() + " -> " + to.name(), e); + private void addToTags(Map> commitsPerTag, List addTo, String tagName) { + if (commitsPerTag.containsKey(tagName)) { + Set gitCommits = commitsPerTag.get(tagName); + GitTag gitTag = new GitTag(tagName, newArrayList(gitCommits)); + addTo.add(gitTag); } } - private ObjectId getPeeled(Ref ref) { - Ref peeledRef = repository.peel(ref); - if (peeledRef.getPeeledObjectId() != null) { - return peeledRef.getPeeledObjectId(); - } else { - return ref.getObjectId(); + private Set populateCommitPerTag(ObjectId from, ObjectId to, + Map> commitsPerTagName, Map tagPerCommitHash, + Map tagPerCommitsHash, String currentTagName) throws Exception { + String thisCommitHash = to.getName(); + if (isNotMappedToAnotherTag(tagPerCommitsHash, thisCommitHash)) { + RevCommit thisCommit = revWalk.lookupCommit(to); + revWalk.parseHeaders(thisCommit); + if (thisIsANewTag(tagPerCommitHash, thisCommitHash)) { + currentTagName = getTagName(tagPerCommitHash, thisCommitHash); + } + if (currentTagName != null) { + addCommitToCurrentTag(commitsPerTagName, currentTagName, thisCommit); + noteThatTheCommitWasMapped(tagPerCommitsHash, currentTagName, thisCommitHash); + } + if (notFirstIncludedCommit(from, to)) { + Set work = newHashSet(); + for (RevCommit parent : thisCommit.getParents()) { + work.add(new TraversalWork(parent, currentTagName)); + } + return work; + } } + return newHashSet(); } - public ObjectId getRef(String fromRef) { - try { - for (Ref foundRef : getAllRefs().values()) { - if (foundRef.getName().endsWith(fromRef)) { - Ref ref = getAllRefs().get(foundRef.getName()); - Git git = null; - try { - git = new Git(repository); - Ref peeledRef = repository.peel(ref); - if (peeledRef.getPeeledObjectId() != null) { - return peeledRef.getPeeledObjectId(); - } else { - return ref.getObjectId(); - } - } finally { - git.close(); - } + private static class TraversalWork { + private final RevCommit to; + private final String currentTagName; + + public TraversalWork(RevCommit to, String currentTagName) { + this.to = to; + this.currentTagName = currentTagName; + } + + public String getCurrentTagName() { + return currentTagName; + } + + public RevCommit getTo() { + return to; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((currentTagName == null) ? 0 : currentTagName.hashCode()); + result = prime * result + ((to == null) ? 0 : to.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + TraversalWork other = (TraversalWork) obj; + if (currentTagName == null) { + if (other.currentTagName != null) { + return false; } + } else if (!currentTagName.equals(other.currentTagName)) { + return false; } - throw new RuntimeException(fromRef + " not found in:\n" + toString()); - } catch (Exception e) { - throw new RuntimeException("", e); + if (to == null) { + if (other.to != null) { + return false; + } + } else if (!to.equals(other.to)) { + return false; + } + return true; } } - private Map getAllRefs() { - return repository.getAllRefs(); + private boolean thisIsANewTag(Map tagsPerCommitHash, String thisCommitHash) { + return tagsPerCommitHash.containsKey(thisCommitHash); } - public ObjectId getCommit(String fromCommit) { - if (fromCommit.startsWith(ZERO_COMMIT)) { - return firstCommit(); + private String getTagName(Map tagPerCommitHash, String thisCommitHash) { + return tagPerCommitHash.get(thisCommitHash).getName(); + } + + private boolean notFirstIncludedCommit(ObjectId from, ObjectId to) { + return !from.getName().equals(to.getName()); + } + + private void addCommitToCurrentTag(Map> commitsPerTagName, String currentTagName, + RevCommit thisCommit) { + GitCommit gitCommit = toGitCommit(thisCommit); + if (!commitsPerTagName.containsKey(currentTagName)) { + commitsPerTagName.put(currentTagName, new TreeSet()); } - return fromString(fromCommit); + Set gitCommitsInCurrentTag = commitsPerTagName.get(currentTagName); + gitCommitsInCurrentTag.add(gitCommit); + } + + private String noteThatTheCommitWasMapped(Map tagPerCommitsHash, String currentTagName, + String thisCommitHash) { + return tagPerCommitsHash.put(thisCommitHash, currentTagName); + } + + private boolean isNotMappedToAnotherTag(Map tagPerCommitsHash, String thisCommitHash) { + return !tagPerCommitsHash.containsKey(thisCommitHash); + } + + private Map getTagPerCommitHash(Optional ignoreTagsIfNameMatches, List tagList) { + Map tagPerCommit = newHashMap(); + for (Ref tag : tagList) { + if (ignoreTagsIfNameMatches.isPresent()) { + if (compile(ignoreTagsIfNameMatches.get()).matcher(tag.getName()).matches()) { + continue; + } + } + tagPerCommit.put(getPeeled(tag).getName(), tag); + } + return tagPerCommit; + } + + private ObjectId getPeeled(Ref tag) { + Ref peeledTag = repository.peel(tag); + if (peeledTag.getPeeledObjectId() != null) { + return peeledTag.getPeeledObjectId(); + } else { + return tag.getObjectId(); + } + } + + private Map getAllRefs() { + return repository.getAllRefs(); } private RevCommit firstCommit() { @@ -214,8 +362,23 @@ private RevCommit firstCommit() { } } + private GitCommit toGitCommit(RevCommit revCommit) { + return new GitCommit( // + revCommit.getAuthorIdent().getName(),// + revCommit.getAuthorIdent().getEmailAddress(),// + new Date(revCommit.getCommitTime() * 1000L),// + revCommit.getFullMessage(),// + revCommit.getId().getName()); + } + @Override public String toString() { return "Repo: " + repository; } + + @Override + public void close() throws IOException { + git.close(); + repository.close(); + } } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoData.java b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoData.java index 14f6f69e..d0bc41b6 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoData.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoData.java @@ -1,6 +1,10 @@ package se.bjurr.gitchangelog.internal.git; +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.collect.Sets.newTreeSet; + import java.util.List; +import java.util.Set; import se.bjurr.gitchangelog.internal.git.model.GitCommit; import se.bjurr.gitchangelog.internal.git.model.GitTag; @@ -9,8 +13,12 @@ public class GitRepoData { private final List gitCommits; private final List gitTags; - public GitRepoData(List gitCommits, List gitTags) { - this.gitCommits = gitCommits; + public GitRepoData(List gitTags) { + Set gitCommitsSorted = newTreeSet(); + for (GitTag gitTag : gitTags) { + gitCommitsSorted.addAll(gitTag.getGitCommits()); + } + this.gitCommits = newArrayList(gitCommitsSorted); this.gitTags = gitTags; } diff --git a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java index 00252f22..8aca129d 100644 --- a/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java +++ b/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoDataHelper.java @@ -34,7 +34,7 @@ public static GitRepoData removeCommitsWithoutIssue(List allParsedI } } - return new GitRepoData(reducedGitCommits, reducedGitTags); + return new GitRepoData(reducedGitTags); } } diff --git a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java index 6c75a3a8..ada85c38 100644 --- a/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java +++ b/src/test/java/se/bjurr/gitchangelog/api/GitChangelogApiTest.java @@ -150,13 +150,14 @@ public void testThatReadableGroupMustExist() throws Exception { assertThat(actual)// .as("Should never happen! But nice to see what was rendered, if it does not crash as expected.")// .isEqualTo(""); - } catch (RuntimeException e) { - assertThat(e.getMessage())// - .isEqualTo("Pattern: \"[0-9]+?\" did not match any group in: \"refs/tags/test-lightweight-2\""); + } catch (Exception e) { + if (!e.getMessage().equals("Pattern: \"[0-9]+?\" did not match any group in: \"refs/tags/test-lightweight-2\"")) { + throw new AssertionError("Got: \"" + e.getMessage() + "\"", e); + } } } - @Test() + @Test public void testThatReadableGroupCanBeSet() throws Exception { URL settingsFile = getResource("settings/git-changelog-test-settings.json").toURI().toURL(); String templatePath = "templates/testIssuesCommits.mustache"; @@ -171,7 +172,7 @@ public void testThatReadableGroupCanBeSet() throws Exception { .render(); } - @Test() + @Test public void testThatCustomVariablesCanBeUsed() throws Exception { String expected = Resources.toString(getResource("assertions/testAuthorsCommitsExtended.md"), UTF_8).trim(); diff --git a/src/test/java/se/bjurr/gitchangelog/internal/git/GitRepoTest.java b/src/test/java/se/bjurr/gitchangelog/internal/git/GitRepoTest.java index 4553e957..8fb75447 100644 --- a/src/test/java/se/bjurr/gitchangelog/internal/git/GitRepoTest.java +++ b/src/test/java/se/bjurr/gitchangelog/internal/git/GitRepoTest.java @@ -1,7 +1,8 @@ package se.bjurr.gitchangelog.internal.git; +import static com.google.common.collect.Lists.newArrayList; import static com.google.common.collect.Lists.reverse; -import static com.google.common.collect.Maps.newHashMap; +import static com.google.common.collect.Maps.newTreeMap; 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; @@ -62,7 +63,7 @@ public void testThatCommitsCanBeRetrieved() throws Exception { assertThat(gitRepo.getCommit(TAG_1_0_HASH).name()).isEqualTo(TAG_1_0_HASH); } - // @Test + @Test public void testThatTagInFeatureBranchDoesNotIncludeCommitsInItsMainBranch() throws Exception { GitRepo gitRepo = getGitRepo(); ObjectId from = gitRepo.getCommit("87c0d72888961712d4d63dd6298c24c1133a6b51"); @@ -70,30 +71,25 @@ public void testThatTagInFeatureBranchDoesNotIncludeCommitsInItsMainBranch() thr GitRepoData gitRepoData = gitRepo.getGitRepoData(from, to, "No tag", Optional. absent()); Map perTag = perTag(gitRepoData.getGitTags()); - assertThat(gitRepoData.getGitTags())// - .hasSize(2); - assertThat(gitRepoData.getGitTags())// - .hasSize(2); assertThat(perTag.keySet())// .hasSize(2)// - .contains("No tag"); + .containsExactly(// + "No tag",// + "refs/tags/tag-in-test-feature"); GitTag noTagTag = perTag.get("No tag"); + List noTagNames = messages(noTagTag.getGitCommits()); + assertThat(noTagNames)// + .containsExactly(// + "Some stuff in test again",// + "Merge branch 'test-feature' into test",// + "some stuff in test branch"); - 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"); - - // TODO: Its a bit random with 1 or 2 commits here! 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"); - assertThat(testFeatureTag.getGitCommits().get(1).getMessage().trim())// - .isEqualTo("some stuff in test branch"); + List testFeatureTagMessages = messages(testFeatureTag.getGitCommits()); + assertThat(testFeatureTagMessages)// + .containsExactly(// + "Some stuff in test-feature",// + "Multiple issues #25 #20"); } @Test @@ -113,16 +109,14 @@ public void testThatTagCanBeIgnored() throws Exception { .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"); + List noTagTagMessages = messages(noTagTag.getGitCommits()); + assertThat(noTagTagMessages)// + .containsExactly(// + "Some stuff in test again",// + "Merge branch 'test-feature' into test",// + "Some stuff in test-feature",// + "some stuff in test branch",// + "Multiple issues #25 #20"); } @Test @@ -155,16 +149,24 @@ public void testThatCommitsSecondReleaseCommitCanBeListed() throws Exception { ObjectId secondRelease = gitRepo.getRef("refs/tags/1.1"); List diff = gitRepo.getGitRepoData(firstRelease, secondRelease, "No tag", Optional.of(".*tag-in-test-feature$")).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)); + assertThat(diff)// + .as("Commits in second release from 1.0.")// + .hasSize(9); + assertThat(diff.get(8).getHash())// + .startsWith("014"); + 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.of(".*tag-in-test-feature$")) .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)); + 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 @@ -184,13 +186,21 @@ public void testThatCommitsBetweenCommitAndCommitCanBeListed() throws Exception } private Map perTag(List gitTags) { - Map map = newHashMap(); + Map map = newTreeMap(); for (GitTag gitTag : gitTags) { map.put(gitTag.getName(), gitTag); } return map; } + private List messages(List gitCommits) { + List messages = newArrayList(); + for (GitCommit gc : gitCommits) { + messages.add(gc.getMessage().trim()); + } + return messages; + } + private GitRepo getGitRepo() throws Exception { return new GitRepo(gitRepoFile); } diff --git a/src/test/java/se/bjurr/gitchangelog/test/LibPerformanceTest.java b/src/test/java/se/bjurr/gitchangelog/test/LibPerformanceTest.java index c913e6e1..0431d2eb 100644 --- a/src/test/java/se/bjurr/gitchangelog/test/LibPerformanceTest.java +++ b/src/test/java/se/bjurr/gitchangelog/test/LibPerformanceTest.java @@ -70,8 +70,6 @@ public void testThatGimmitsBetweenTagsCanBeFound() throws Exception { || to.getName().equals(UNTAGGED_NAME)) { continue; } - String str = from.getName() + " -> " + to.getName(); - LOG.info(str); gitChangelogApiBuilder// .withFromRef(from.getName())// .withToRef(to.getName())// diff --git a/src/test/resources/assertions/testAuthorsCommits.md b/src/test/resources/assertions/testAuthorsCommits.md index e27db31d..63e55c12 100644 --- a/src/test/resources/assertions/testAuthorsCommits.md +++ b/src/test/resources/assertions/testAuthorsCommits.md @@ -29,15 +29,14 @@ Multiple issues More stuff tagged with twice -[cc0fbbd8bc63955](https://server/cc0fbbd8bc63955) Tomas Bjerre *2016-02-15 16:30:35* +[d50a3e332f9fcba](https://server/d50a3e332f9fcba) Tomas Bjerre *2016-02-15 16:30:35* -Adding stuff with a jira +Adding stuff gh 12 -[5607ea23fa8aaf7](https://server/5607ea23fa8aaf7) Tomas Bjerre *2016-02-15 16:30:35* +[cc0fbbd8bc63955](https://server/cc0fbbd8bc63955) Tomas Bjerre *2016-02-15 16:30:35* -Adding stuff - with gh again +Adding stuff with a jira [a9bd03b34b255ff](https://server/a9bd03b34b255ff) Tomas Bjerre *2016-02-15 16:30:35* @@ -45,14 +44,15 @@ Adding stuff Adding cq stuff with CQ -[3f62b35d1317311](https://server/3f62b35d1317311) Tomas Bjerre *2016-02-15 16:30:35* +[5607ea23fa8aaf7](https://server/5607ea23fa8aaf7) Tomas Bjerre *2016-02-15 16:30:35* -This is 1.0 tagged commit +Adding stuff + with gh again -[d50a3e332f9fcba](https://server/d50a3e332f9fcba) Tomas Bjerre *2016-02-15 16:30:35* +[3f62b35d1317311](https://server/3f62b35d1317311) Tomas Bjerre *2016-02-15 16:30:35* -Adding stuff gh 12 +This is 1.0 tagged commit [071a14f29020758](https://server/071a14f29020758) Tomas Bjerre *2016-02-15 16:12:02* diff --git a/src/test/resources/assertions/testCommits.md b/src/test/resources/assertions/testCommits.md index b55de56c..eab0327c 100644 --- a/src/test/resources/assertions/testCommits.md +++ b/src/test/resources/assertions/testCommits.md @@ -28,15 +28,14 @@ Multiple issues More stuff tagged with twice ## Tomas Bjerre - 2016-02-15 16:30:35 -[cc0fbbd8bc63955](https://server/cc0fbbd8bc63955) +[d50a3e332f9fcba](https://server/d50a3e332f9fcba) -Adding stuff with a jira +Adding stuff gh 12 ## Tomas Bjerre - 2016-02-15 16:30:35 -[5607ea23fa8aaf7](https://server/5607ea23fa8aaf7) +[cc0fbbd8bc63955](https://server/cc0fbbd8bc63955) -Adding stuff - with gh again +Adding stuff with a jira ## Tomas Bjerre - 2016-02-15 16:30:35 [a9bd03b34b255ff](https://server/a9bd03b34b255ff) @@ -44,14 +43,15 @@ Adding stuff Adding cq stuff with CQ ## Tomas Bjerre - 2016-02-15 16:30:35 -[3f62b35d1317311](https://server/3f62b35d1317311) +[5607ea23fa8aaf7](https://server/5607ea23fa8aaf7) -This is 1.0 tagged commit +Adding stuff + with gh again ## Tomas Bjerre - 2016-02-15 16:30:35 -[d50a3e332f9fcba](https://server/d50a3e332f9fcba) +[3f62b35d1317311](https://server/3f62b35d1317311) -Adding stuff gh 12 +This is 1.0 tagged commit ## T B - 2016-02-15 16:30:25 [71d845c18924504](https://server/71d845c18924504) diff --git a/src/test/resources/assertions/testCommitsVariables.md b/src/test/resources/assertions/testCommitsVariables.md index b1cef9b6..ffff64d0 100644 --- a/src/test/resources/assertions/testCommitsVariables.md +++ b/src/test/resources/assertions/testCommitsVariables.md @@ -42,22 +42,20 @@ Changelog of Git Changelog. Message Body: -## Commit cc0fbbd8bc63955 - Message: Adding stuff with a jira +## Commit d50a3e332f9fcba + Message: Adding stuff gh 12 - Message Title: Adding stuff with a jira + Message Title: Adding stuff gh 12 Message Body: -## Commit 5607ea23fa8aaf7 - Message: Adding stuff with gh again +## Commit cc0fbbd8bc63955 + Message: Adding stuff with a jira - Message Title: Adding stuff + Message Title: Adding stuff with a jira - Message Body: with gh again - - Item: with gh again + Message Body: ## Commit a9bd03b34b255ff @@ -68,18 +66,20 @@ Changelog of Git Changelog. Message Body: -## Commit 3f62b35d1317311 - Message: This is 1.0 tagged commit +## Commit 5607ea23fa8aaf7 + Message: Adding stuff with gh again - Message Title: This is 1.0 tagged commit + Message Title: Adding stuff - Message Body: + Message Body: with gh again + + Item: with gh again -## Commit d50a3e332f9fcba - Message: Adding stuff gh 12 +## Commit 3f62b35d1317311 + Message: This is 1.0 tagged commit - Message Title: Adding stuff gh 12 + Message Title: This is 1.0 tagged commit Message Body: diff --git a/src/test/resources/assertions/testTagsCommits.md b/src/test/resources/assertions/testTagsCommits.md index 670de34b..9a4b225c 100644 --- a/src/test/resources/assertions/testTagsCommits.md +++ b/src/test/resources/assertions/testTagsCommits.md @@ -47,14 +47,14 @@ Adding cq stuff with CQ ## test-1.0 ### Tomas Bjerre - 2016-02-15 16:30:35 -[3f62b35d1317311](https://server/3f62b35d1317311) +[d50a3e332f9fcba](https://server/d50a3e332f9fcba) -This is 1.0 tagged commit +Adding stuff gh 12 ### Tomas Bjerre - 2016-02-15 16:30:35 -[d50a3e332f9fcba](https://server/d50a3e332f9fcba) +[3f62b35d1317311](https://server/3f62b35d1317311) -Adding stuff gh 12 +This is 1.0 tagged commit ### T B - 2016-02-15 16:30:25 [71d845c18924504](https://server/71d845c18924504)