Skip to content

Commit

Permalink
Adding annotation to context of tag #36
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 22, 2016
1 parent 4ab05cc commit ecd852c
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 35 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
Changelog of Git Changelog.

## Next release
### GitHub [#36](https://github.com/tomasbjerre/git-changelog-lib/issues/36) Annotated tags support

**Adding annotation to context of tag**


[e9db23559b58597](https://github.com/tomasbjerre/git-changelog-lib/commit/e9db23559b58597) Tomas Bjerre *2016-10-22 07:23:01*


## 1.57
### GitHub [#35](https://github.com/tomasbjerre/git-changelog-lib/issues/35) Git Change log show only Merges or have Merge as its own Tag

**Adding merge boolean to commits**


[96618d725d4b57f](https://github.com/tomasbjerre/git-changelog-lib/commit/96618d725d4b57f) Tomas Bjerre *2016-10-05 17:03:40*
[2d9189f421254f2](https://github.com/tomasbjerre/git-changelog-lib/commit/2d9189f421254f2) Tomas Bjerre *2016-10-05 17:04:13*


## 1.56
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ The template is supplied with a datastructure like:
* messageBodyItems (List of strings, the lines after the title)
* tags
- name
- annotation
* commits
- authorName
- authorEmailAddress
Expand Down
37 changes: 22 additions & 15 deletions src/main/java/se/bjurr/gitchangelog/api/model/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,56 @@

public class Tag implements ICommits, IAuthors, IIssues, Serializable {
private static final long serialVersionUID = 2140208294219785889L;
private final List<Commit> commits;
private final String annotation;
private final List<Author> authors;
private final List<Commit> commits;
private final List<Issue> issues;
private final List<IssueType> issueTypes;
private final String name;

public Tag(String name, List<Commit> commits, List<Author> authors, List<Issue> issues, List<IssueType> issueTypes) {
public Tag(String name, String annotation, List<Commit> commits, List<Author> authors, List<Issue> issues,
List<IssueType> issueTypes) {
this.commits = commits;
this.authors = authors;
this.issues = issues;
this.name = name;
this.annotation = annotation;
this.issueTypes = issueTypes;
}

@Override
public List<Issue> getIssues() {
return issues;
public String getAnnotation() {
return this.annotation;
}

@Override
public List<Author> getAuthors() {
return authors;
}

public String getName() {
return name;
return this.authors;
}

public Commit getCommit() {
return commits.get(0);
return this.commits.get(0);
}

@Override
public List<Commit> getCommits() {
return commits;
return this.commits;
}

@Override
public String toString() {
return "name: " + name;
public List<Issue> getIssues() {
return this.issues;
}

public List<IssueType> getIssueTypes() {
return issueTypes;
return this.issueTypes;
}

public String getName() {
return this.name;
}

@Override
public String toString() {
return "name: " + this.name;
}
}
44 changes: 40 additions & 4 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -148,10 +149,17 @@ private void addCommitToCurrentTag(Map<String, Set<GitCommit>> commitsPerTagName
gitCommitsInCurrentTag.add(gitCommit);
}

private void addToTags(Map<String, Set<GitCommit>> commitsPerTag, String tagName, List<GitTag> addTo) {
private void addToTags(Map<String, Set<GitCommit>> commitsPerTag, String tagName, List<GitTag> addTo,
Map<String, RevTag> annotatedTagPerTagName) {
if (commitsPerTag.containsKey(tagName)) {
Set<GitCommit> gitCommits = commitsPerTag.get(tagName);
GitTag gitTag = new GitTag(tagName, newArrayList(gitCommits));
boolean isAnnotated = annotatedTagPerTagName.containsKey(tagName);
String annotation = null;
if (isAnnotated) {
annotation = annotatedTagPerTagName.get(tagName).getFullMessage();
}

GitTag gitTag = new GitTag(tagName, annotation, newArrayList(gitCommits));
addTo.add(gitTag);
}
}
Expand All @@ -174,6 +182,27 @@ private Map<String, Ref> getAllRefs() {
return this.repository.getAllRefs();
}

private Map<String, RevTag> getAnnotatedTagPerTagName(Optional<String> ignoreTagsIfNameMatches, List<Ref> tagList) {
Map<String, RevTag> tagPerCommit = newHashMap();
for (Ref tag : tagList) {
if (ignoreTagsIfNameMatches.isPresent()) {
if (compile(ignoreTagsIfNameMatches.get()).matcher(tag.getName()).matches()) {
continue;
}
}
Ref peeledTag = this.repository.peel(tag);
if (peeledTag.getPeeledObjectId() != null) {
try {
RevTag revTag = RevTag.parse(this.repository.open(tag.getObjectId()).getBytes());
tagPerCommit.put(tag.getName(), revTag);
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
return tagPerCommit;
}

private List<RevCommit> getDiffingCommits(RevCommit from, RevCommit to) throws Exception {
RevCommit firstCommit = firstCommit();
List<RevCommit> allInFrom = newArrayList(this.git.log().addRange(firstCommit, from).call());
Expand Down Expand Up @@ -238,6 +267,13 @@ private List<GitTag> gitTags(ObjectId fromObjectId, ObjectId toObjectId, String
* Why: To know if a new tag was found when walking up through the parents.
*/
Map<String, Ref> tagPerCommitHash = getTagPerCommitHash(ignoreTagsIfNameMatches, tagList);

/**
* What: Contains only the tags that are annotated.<br>
* Why: To populate tag message, for annotated tags.
*/
Map<String, RevTag> annotatedTagPerTagName = getAnnotatedTagPerTagName(ignoreTagsIfNameMatches, tagList);

/**
* What: Populated with all included commits, referring to there tags.<br>
* Why: To know if a commit is already mapped to a tag, or not.
Expand All @@ -253,10 +289,10 @@ private List<GitTag> gitTags(ObjectId fromObjectId, ObjectId toObjectId, String
populateComitPerTag(from, to, tagPerCommitHash, tagPerCommitsHash, commitsPerTag, untaggedName);

List<GitTag> tags = newArrayList();
addToTags(commitsPerTag, untaggedName, tags);
addToTags(commitsPerTag, untaggedName, tags, annotatedTagPerTagName);
List<Ref> tagCommitHashSortedByCommitTime = getTagCommitHashSortedByCommitTime(tagPerCommitHash.values());
for (Ref tag : tagCommitHashSortedByCommitTime) {
addToTags(commitsPerTag, tag.getName(), tags);
addToTags(commitsPerTag, tag.getName(), tags, annotatedTagPerTagName);
}
return tags;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import se.bjurr.gitchangelog.internal.model.ParsedIssue;

public class GitRepoDataHelper {
private GitRepoDataHelper() {
}

public static GitRepoData removeCommitsWithoutIssue(List<ParsedIssue> allParsedIssues, GitRepoData gitRepoData) {
Set<GitCommit> commitsWithIssues = newTreeSet();
for (ParsedIssue parsedIssue : allParsedIssues) {
Expand All @@ -30,11 +27,15 @@ public static GitRepoData removeCommitsWithoutIssue(List<ParsedIssue> allParsedI
for (GitTag gitTag : gitRepoData.getGitTags()) {
Iterable<GitCommit> reducedCommitsInTag = filter(gitTag.getGitCommits(), in(reducedGitCommits));
if (!isEmpty(reducedCommitsInTag)) {
reducedGitTags.add(new GitTag(gitTag.getName(), newArrayList(reducedCommitsInTag)));
reducedGitTags
.add(new GitTag(gitTag.getName(), gitTag.findAnnotation().orNull(), newArrayList(reducedCommitsInTag)));
}
}

return new GitRepoData(reducedGitTags);
}

private GitRepoDataHelper() {
}

}
29 changes: 19 additions & 10 deletions src/main/java/se/bjurr/gitchangelog/internal/git/model/GitTag.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
package se.bjurr.gitchangelog.internal.git.model;

import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;

import com.google.common.base.Optional;

import se.bjurr.gitchangelog.internal.model.interfaces.IGitCommitReferer;

public class GitTag implements IGitCommitReferer {

private final String name;
private final String annotation;
private final List<GitCommit> gitCommits;
private final String name;

public GitTag(String name, List<GitCommit> gitCommits) {
public GitTag(String name, String annotation, List<GitCommit> gitCommits) {
checkArgument(!gitCommits.isEmpty(), "No commits in " + name);
this.name = name;
this.name = checkNotNull(name, "name");
this.annotation = annotation;
this.gitCommits = gitCommits;
}

@Override
public GitCommit getGitCommit() {
return checkNotNull(gitCommits.get(0), name);
public Optional<String> findAnnotation() {
return fromNullable(this.annotation);
}

@Override
public String getName() {
return name;
public GitCommit getGitCommit() {
return checkNotNull(this.gitCommits.get(0), this.name);
}

public List<GitCommit> getGitCommits() {
return gitCommits;
return this.gitCommits;
}

@Override
public String getName() {
return this.name;
}

@Override
public String toString() {
return "Tag: " + name + ": " + getGitCommit();
return "Tag: " + this.name + " Annotation: " + this.annotation + ", " + getGitCommit();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public Tag apply(GitTag input) {
List<Author> authors = toAuthors(gitCommits);
List<Issue> issues = toIssues(parsedIssues);
List<IssueType> issueTypes = toIssueTypes(parsedIssues);
return new Tag(toReadableTagName(input.getName()), commits, authors, issues, issueTypes);
return new Tag(toReadableTagName(input.getName()), input.findAnnotation().orNull(), commits, authors, issues,
issueTypes);
}

private List<ParsedIssue> reduceParsedIssuesToOnlyGitCommits(final List<ParsedIssue> allParsedIssues,
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/assertions/testTagsCommits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changelog of Git Changelog.

## No tag

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

Expand Down Expand Up @@ -40,12 +41,14 @@ Adding stuff
with gh again

## test-lightweight-2

### Tomas Bjerre - 2016-02-15 16:30:35
[a9bd03b34b255ff](https://server/a9bd03b34b255ff)

Adding cq stuff with CQ

## test-1.0
this is the tag for test-1.0&#10;
### Tomas Bjerre - 2016-02-15 16:30:35
[d50a3e332f9fcba](https://server/d50a3e332f9fcba)

Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/assertions/testTagsIssuesAuthorsCommits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changelog of Git Changelog.

## No tag

### Bugs Mixed bugs
* Tomas Bjerre
[20e333f8e108f77](https://server/20e333f8e108f77) *2016-03-19 20:32:19*
Expand Down Expand Up @@ -47,13 +48,15 @@ some stuff in test branch


## test-lightweight-2

### CQ [CQ1234](http://cq/1234) 1234
* Tomas Bjerre
[a9bd03b34b255ff](https://server/a9bd03b34b255ff) *2016-02-15 16:30:35*
Adding cq stuff with CQ


## test-1.0
this is the tag for test-1.0&#10;
### GitHub [#12](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/12) Strange chars in PULL_REQUEST_URL
* Tomas Bjerre
[d50a3e332f9fcba](https://server/d50a3e332f9fcba) *2016-02-15 16:30:35*
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/assertions/testTagsIssuesCommits.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Changelog of Git Changelog.

## No tag

### Bugs Mixed bugs
[20e333f8e108f77](https://server/20e333f8e108f77) 2016-03-19 20:32:19
More stuff tagged with twice
Expand Down Expand Up @@ -35,11 +36,13 @@ Some stuff in test-feature
some stuff in test branch

## test-lightweight-2

### CQ [CQ1234](http://cq/1234) 1234
[a9bd03b34b255ff](https://server/a9bd03b34b255ff) 2016-02-15 16:30:35
Adding cq stuff with CQ

## test-1.0
this is the tag for test-1.0&#10;
### GitHub [#12](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/12) Strange chars in PULL_REQUEST_URL
[d50a3e332f9fcba](https://server/d50a3e332f9fcba) 2016-02-15 16:30:35
Adding stuff gh 12
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/templates/testTagsCommits.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog of Git Changelog.

{{#tags}}
## {{name}}
{{annotation}}
{{#commits}}
### {{authorName}} - {{commitTime}}
[{{hash}}](https://server/{{hash}})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog of Git Changelog.

{{#tags}}
## {{name}}
{{annotation}}
{{#issues}}
{{#hasLink}}
### {{name}} [{{issue}}]({{link}}) {{title}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog of Git Changelog.

{{#tags}}
## {{name}}
{{annotation}}
{{#issues}}
{{#hasLink}}
### {{name}} [{{issue}}]({{link}}) {{title}}
Expand Down

0 comments on commit ecd852c

Please sign in to comment.