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

Compact GitProvenance.Committer representation #4096

Merged
merged 9 commits into from
Jan 27, 2025
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 @@ -17,10 +17,8 @@


import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Value;
import lombok.With;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.Nullable;
import org.openrewrite.GitRemote;
Expand All @@ -44,6 +42,8 @@
import java.util.*;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.openrewrite.Tree.randomId;

@Value
Expand Down Expand Up @@ -187,7 +187,7 @@ public GitProvenance(UUID id,
*/
public static @Nullable GitProvenance fromProjectDirectory(Path projectDir,
@Nullable BuildEnvironment environment,
GitRemote.@Nullable Parser gitRemoteParser) {
GitRemote.@Nullable Parser gitRemoteParser) {
if (gitRemoteParser == null) {
gitRemoteParser = new GitRemote.Parser();
}
Expand Down Expand Up @@ -371,16 +371,22 @@ private static List<Committer> getCommitters(Repository repository) {
head = headRef.getObjectId();
}

Map<String, Committer> committers = new TreeMap<>();
Map<String, String> committerName = new HashMap<>();
Map<String, NavigableMap<LocalDate, Integer>> commitMap = new HashMap<>();
for (RevCommit commit : git.log().add(head).call()) {
PersonIdent who = commit.getAuthorIdent();
Committer committer = committers.computeIfAbsent(who.getEmailAddress(),
email -> new Committer(who.getName(), email, new TreeMap<>()));
committer.getCommitsByDay().compute(who.getWhen().toInstant().atZone(who.getTimeZone().toZoneId())
committerName.putIfAbsent(who.getEmailAddress(), who.getName());
commitMap.computeIfAbsent(who.getEmailAddress(),
email -> new TreeMap<>()).compute(who.getWhen().toInstant().atZone(who.getTimeZone().toZoneId())
.toLocalDate(),
(day, count) -> count == null ? 1 : count + 1);
}
return new ArrayList<>(committers.values());
return committerName.entrySet().stream()
.map(c -> new Committer(
c.getValue(),
c.getKey(),
commitMap.get(c.getKey()))
).collect(toList());
} catch (IOException | GitAPIException e) {
return emptyList();
}
Expand Down Expand Up @@ -419,10 +425,47 @@ public enum EOL {
}

@Value
@With
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static class Committer {

@With
String name;

@With
String email;
NavigableMap<LocalDate, Integer> commitsByDay;

@Getter(AccessLevel.PRIVATE)
int[] data;

@JsonCreator
static Committer from(
@JsonProperty("name") String name, @JsonProperty("email") String email,
@JsonProperty("dates") int @Nullable [] data,
@Nullable @JsonProperty("commitsByDay") NavigableMap<LocalDate, Integer> commitsByDay) {
if (commitsByDay != null) {
return new Committer(name, email, commitsByDay);
} else {
return new Committer(name, email, requireNonNull(data));
}
}

public Committer(String name, String email, NavigableMap<LocalDate, Integer> commitsByDay) {
this.name = name;
this.email = email;
this.data = new int[commitsByDay.size() * 2];
int i = 0;
for (Map.Entry<LocalDate, Integer> entry : commitsByDay.entrySet()) {
data[i++] = (int) entry.getKey().toEpochDay();
data[i++] = entry.getValue();
}
}

public NavigableMap<LocalDate, Integer> getCommitsByDay() {
TreeMap<LocalDate, Integer> commitsByDay = new TreeMap<>();
for (int i = 0; i < data.length; ) {
commitsByDay.put(LocalDate.ofEpochDay(data[i++]), data[i++]);
}
return commitsByDay;
}
}
}
Loading