Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse JGit instance and avoid deprecated methods #44943

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading