Skip to content

Commit

Permalink
fix: current version
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 24, 2023
1 parent b601ed2 commit 1f538aa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 54 deletions.
75 changes: 39 additions & 36 deletions src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,19 @@ public SemanticVersion getHighestSemanticVersion() throws GitChangelogRepository
return SemanticVersioning.getHighestVersion(tags);
}

public SemanticVersion getCurrentSemanticVersion() throws GitChangelogRepositoryException {
final Optional<String> toRevisionOpt = this.settings.getToRevision();
if (toRevisionOpt.isPresent()) {
try (GitRepo gitRepo = new GitRepo(new File(this.settings.getFromRepo()))) {
final List<String> tags =
gitRepo.getTags(toRevisionOpt.get(), this.settings.getToRevisionStrategy()).stream()
.map(it -> Transformer.toReadableTagName(it, this.settings.getReadableTagName()))
.collect(Collectors.toList());
if (!tags.isEmpty()) {
return SemanticVersioning.getHighestVersion(tags);
}
} catch (final IOException e) {
throw new GitChangelogRepositoryException("", e);
public SemanticVersion getCurrentSemanticVersion() throws Exception {
try (GitRepo gitRepo = new GitRepo(new File(this.settings.getFromRepo()))) {
final RevisionBoundary<ObjectId> from = this.getFrom(gitRepo, this.settings);
final RevisionBoundary<ObjectId> to = this.getTo(gitRepo, this.settings);
final List<String> tags =
gitRepo.getTags(from, to).stream()
.map(it -> Transformer.toReadableTagName(it, this.settings.getReadableTagName()))
.collect(Collectors.toList());
if (!tags.isEmpty()) {
return SemanticVersioning.getHighestVersion(tags);
}
} catch (final IOException e) {
throw new GitChangelogRepositoryException("", e);
}
return this.getNextSemanticVersion();
}
Expand Down Expand Up @@ -713,29 +712,8 @@ public GitChangelogApi withUntaggedName(final String untaggedName) {
private Changelog getChangelog(final GitRepo gitRepo, final boolean useIntegrations)
throws GitChangelogRepositoryException {
gitRepo.setTreeFilter(this.settings.getSubDirFilter());
final RevisionBoundary<ObjectId> fromId =
this.getId(
gitRepo,
this.settings.getFromRevision(),
this.settings.getFromRevisionStrategy()) //
.orElse(
new RevisionBoundary<ObjectId>(
gitRepo.getCommit(ZERO_COMMIT), InclusivenessStrategy.INCLUSIVE));
final Optional<RevisionBoundary<ObjectId>> toIdOpt =
this.getId(gitRepo, this.settings.getToRevision(), this.settings.getToRevisionStrategy());
RevisionBoundary<ObjectId> toId;
if (toIdOpt.isPresent()) {
toId = toIdOpt.get();
} else {
final Optional<ObjectId> headOpt = gitRepo.findRef(REF_HEAD);
if (headOpt.isPresent()) {
toId = new RevisionBoundary<ObjectId>(headOpt.get(), InclusivenessStrategy.INCLUSIVE);
} else {
toId =
new RevisionBoundary<ObjectId>(
gitRepo.getRef(REF_MASTER), InclusivenessStrategy.INCLUSIVE);
}
}
final RevisionBoundary<ObjectId> fromId = this.getFrom(gitRepo, this.settings);
final RevisionBoundary<ObjectId> toId = this.getTo(gitRepo, this.settings);
GitRepoData gitRepoData =
gitRepo.getGitRepoData(
fromId,
Expand Down Expand Up @@ -771,6 +749,31 @@ private Changelog getChangelog(final GitRepo gitRepo, final boolean useIntegrati
gitRepoData.getUrlPartsList());
}

private RevisionBoundary<ObjectId> getTo(final GitRepo gitRepo, final Settings settings)
throws GitChangelogRepositoryException {
final Optional<RevisionBoundary<ObjectId>> toIdOpt =
this.getId(gitRepo, settings.getToRevision(), settings.getToRevisionStrategy());
if (toIdOpt.isPresent()) {
return toIdOpt.get();
} else {
final Optional<ObjectId> headOpt = gitRepo.findRef(REF_HEAD);
if (headOpt.isPresent()) {
return new RevisionBoundary<ObjectId>(headOpt.get(), InclusivenessStrategy.INCLUSIVE);
} else {
return new RevisionBoundary<ObjectId>(
gitRepo.getRef(REF_MASTER), InclusivenessStrategy.INCLUSIVE);
}
}
}

private RevisionBoundary<ObjectId> getFrom(final GitRepo gitRepo, final Settings settings)
throws GitChangelogRepositoryException {
return this.getId(gitRepo, settings.getFromRevision(), settings.getFromRevisionStrategy()) //
.orElse(
new RevisionBoundary<ObjectId>(
gitRepo.getCommit(ZERO_COMMIT), InclusivenessStrategy.INCLUSIVE));
}

private Optional<RevisionBoundary<ObjectId>> getId(
final GitRepo gitRepo,
final Optional<String> revision,
Expand Down
36 changes: 18 additions & 18 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Set;
Expand Down Expand Up @@ -390,14 +389,8 @@ private List<GitTag> gitTags(
final String untaggedName,
final Optional<String> ignoreTagsIfNameMatches)
throws Exception {
final RevisionBoundary<RevCommit> from =
new RevisionBoundary<RevCommit>(
this.revWalk.lookupCommit(fromObjectId.getRevision()),
fromObjectId.getInclusivenessStrategy());
final RevisionBoundary<RevCommit> to =
new RevisionBoundary<RevCommit>(
this.revWalk.lookupCommit(toObjectId.getRevision()),
toObjectId.getInclusivenessStrategy());
final RevisionBoundary<RevCommit> from = this.toRevCommit(fromObjectId);
final RevisionBoundary<RevCommit> to = this.toRevCommit(toObjectId);

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

Expand Down Expand Up @@ -464,6 +457,12 @@ private List<GitTag> gitTags(
return tags;
}

private RevisionBoundary<RevCommit> toRevCommit(final RevisionBoundary<ObjectId> fromObjectId) {
return new RevisionBoundary<RevCommit>(
this.revWalk.lookupCommit(fromObjectId.getRevision()),
fromObjectId.getInclusivenessStrategy());
}

/**
* prunes commits that have been added in addition to the desired ones
*
Expand Down Expand Up @@ -660,16 +659,17 @@ public void setTreeFilter(final String pathFilter) {
}

public List<String> getTags(
final String revision, final InclusivenessStrategy inclusivenessStrategy)
throws GitChangelogRepositoryException {
final Optional<RevisionBoundary<ObjectId>> objectId =
this.findObjectId(revision, inclusivenessStrategy);
final String commitIdOfRevision = objectId.get().getRevision().getName();
final RevisionBoundary<ObjectId> fromObjectId, final RevisionBoundary<ObjectId> toObjectId)
throws Exception {

final RevisionBoundary<RevCommit> from = this.toRevCommit(fromObjectId);
final RevisionBoundary<RevCommit> to = this.toRevCommit(toObjectId);

final List<String> tags = new ArrayList<String>();
for (final Entry<String, Ref> entry : this.getAllRefs().entrySet()) {
final String commitIdOfRef = this.getPeeledObjectId(entry.getValue()).get().getName();
if (commitIdOfRef.equals(commitIdOfRevision)) {
tags.add(entry.getValue().getName());
for (final Ref tagRef : this.tagsBetweenFromAndTo(from, to)) {
final String commitIdOfRef = this.getPeeled(tagRef).name();
if (commitIdOfRef.equals(to.getRevision().getName())) {
tags.add(tagRef.getName());
}
}
return tags;
Expand Down

0 comments on commit 1f538aa

Please sign in to comment.