Skip to content

Commit

Permalink
Reuse JGit instance and avoid deprecated methods
Browse files Browse the repository at this point in the history
JGit 7.1.0 deprecated some methods. This uses the recommended methods.
  • Loading branch information
gastaldi committed Dec 5, 2024
1 parent f2e2c1b commit be3ac86
Showing 1 changed file with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import java.io.File;
import java.net.InetAddress;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -70,11 +70,10 @@ void gitInfo(InfoBuildTimeConfig config,
log.debug("Project is not checked in to git");
return;
}
try (Repository repository = repositoryBuilder.build()) {
try (Repository repository = repositoryBuilder.build();
Git git = Git.wrap(repository)) {

RevCommit latestCommit = new Git(repository).log().setMaxCount(1).call().iterator().next();
Date commitDate = new Date(latestCommit.getCommitTime() * 1000L);
TimeZone commitTimeZone = TimeZone.getDefault();
RevCommit latestCommit = git.log().setMaxCount(1).call().iterator().next();

boolean addFullInfo = config.git().mode() == InfoBuildTimeConfig.Git.Mode.FULL;

Expand All @@ -85,16 +84,17 @@ void gitInfo(InfoBuildTimeConfig config,
Map<String, Object> commit = new LinkedHashMap<>();
String latestCommitId = latestCommit.getName();
commit.put("id", latestCommitId);
String latestCommitTime = formatDate(commitDate, commitTimeZone);
String latestCommitTime = formatDate(Instant.ofEpochMilli(latestCommit.getCommitTime()), ZoneId.systemDefault());
commit.put("time", latestCommitTime);

if (addFullInfo) {

PersonIdent authorIdent = latestCommit.getAuthorIdent();
commit.put("author", Map.of("time", formatDate(authorIdent.getWhen(), authorIdent.getTimeZone())));
commit.put("author", Map.of("time", formatDate(authorIdent.getWhenAsInstant(), authorIdent.getZoneId())));

PersonIdent committerIdent = latestCommit.getCommitterIdent();
commit.put("committer", Map.of("time", formatDate(committerIdent.getWhen(), committerIdent.getTimeZone())));
commit.put("committer",
Map.of("time", formatDate(committerIdent.getWhenAsInstant(), committerIdent.getZoneId())));

Map<String, String> user = new LinkedHashMap<>();
user.put("email", authorIdent.getEmailAddress());
Expand All @@ -111,7 +111,7 @@ void gitInfo(InfoBuildTimeConfig config,

commit.put("id", id);

data.put("tags", getTags(repository, latestCommit));
data.put("tags", getTags(git, latestCommit));
}

data.put("commit", commit);
Expand All @@ -130,9 +130,8 @@ void gitInfo(InfoBuildTimeConfig config,
}
}

private String formatDate(Date date, TimeZone timeZone) {
return ISO_OFFSET_DATE_TIME.format(
OffsetDateTime.ofInstant(date.toInstant(), timeZone.toZoneId()));
private String formatDate(Instant instant, ZoneId zoneId) {
return ISO_OFFSET_DATE_TIME.format(OffsetDateTime.ofInstant(instant, zoneId));
}

private Map<String, Object> obtainBuildInfo(CurateOutcomeBuildItem curateOutcomeBuildItem,
Expand Down Expand Up @@ -160,13 +159,11 @@ private Map<String, Object> obtainBuildInfo(CurateOutcomeBuildItem curateOutcome
return build;
}

public Collection<String> getTags(Repository repo, final ObjectId objectId) throws GitAPIException {
try (Git git = Git.wrap(repo)) {
try (RevWalk walk = new RevWalk(repo)) {
Collection<String> tags = getTags(git, objectId, walk);
walk.dispose();
return tags;
}
public Collection<String> getTags(final Git git, final ObjectId objectId) throws GitAPIException {
try (RevWalk walk = new RevWalk(git.getRepository())) {
Collection<String> tags = getTags(git, objectId, walk);
walk.dispose();
return tags;
}
}

Expand Down

0 comments on commit be3ac86

Please sign in to comment.