From be3ac865773afa44e8f3d07bce731a44c7012484 Mon Sep 17 00:00:00 2001 From: George Gastaldi Date: Thu, 5 Dec 2024 11:24:34 -0300 Subject: [PATCH] Reuse JGit instance and avoid deprecated methods JGit 7.1.0 deprecated some methods. This uses the recommended methods. --- .../info/deployment/InfoProcessor.java | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/extensions/info/deployment/src/main/java/io/quarkus/info/deployment/InfoProcessor.java b/extensions/info/deployment/src/main/java/io/quarkus/info/deployment/InfoProcessor.java index c31f9dae6c19d..1509ac3314abf 100644 --- a/extensions/info/deployment/src/main/java/io/quarkus/info/deployment/InfoProcessor.java +++ b/extensions/info/deployment/src/main/java/io/quarkus/info/deployment/InfoProcessor.java @@ -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; @@ -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; @@ -85,16 +84,17 @@ void gitInfo(InfoBuildTimeConfig config, Map 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 user = new LinkedHashMap<>(); user.put("email", authorIdent.getEmailAddress()); @@ -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); @@ -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 obtainBuildInfo(CurateOutcomeBuildItem curateOutcomeBuildItem, @@ -160,13 +159,11 @@ private Map obtainBuildInfo(CurateOutcomeBuildItem curateOutcome return build; } - public Collection getTags(Repository repo, final ObjectId objectId) throws GitAPIException { - try (Git git = Git.wrap(repo)) { - try (RevWalk walk = new RevWalk(repo)) { - Collection tags = getTags(git, objectId, walk); - walk.dispose(); - return tags; - } + public Collection getTags(final Git git, final ObjectId objectId) throws GitAPIException { + try (RevWalk walk = new RevWalk(git.getRepository())) { + Collection tags = getTags(git, objectId, walk); + walk.dispose(); + return tags; } }