Skip to content

Commit

Permalink
feat: fall back on HEAD and MASTER if no toRef is given (refs #127)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Mar 17, 2022
1 parent c19d1ea commit 9c05c8e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/se/bjurr/gitchangelog/api/GitChangelogApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.REF_HEAD;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.REF_MASTER;
import static se.bjurr.gitchangelog.api.GitChangelogApiConstants.ZERO_COMMIT;
import static se.bjurr.gitchangelog.internal.git.GitRepoDataHelper.removeCommitsWithoutIssue;
Expand Down Expand Up @@ -628,7 +629,7 @@ private Changelog getChangelog(final GitRepo gitRepo, final boolean useIntegrati
if (toIdOpt.isPresent()) {
toId = toIdOpt.get();
} else {
toId = gitRepo.getRef(REF_MASTER);
toId = gitRepo.findRef(REF_HEAD).orElse(gitRepo.getRef(REF_MASTER));
}
GitRepoData gitRepoData =
gitRepo.getGitRepoData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public final class GitChangelogApiConstants {

public static final String ZERO_COMMIT = "0000000000000000000000000000000000000000";
public static final String REF_MASTER = "master";
public static final String REF_HEAD = "head";
public static final boolean DEFAULT_REMOVE_ISSUE = false;
public static final String DEFAULT_TIMEZONE = "UTC";
public static final String DEFAULT_DATEFORMAT = "yyyy-MM-dd HH:mm:ss";
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/se/bjurr/gitchangelog/internal/git/GitRepo.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ public ObjectId getRef(final String fromRef) throws GitChangelogRepositoryExcept
throw new GitChangelogRepositoryException(fromRef + " not found in:\n" + this.toString());
}

public Optional<ObjectId> findRef(final String ref) {
try {
return Optional.of(this.getRef(ref));
} catch (final Exception e) {
return Optional.empty();
}
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 9c05c8e

Please # to comment.