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
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions rewrite-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,17 @@ tasks.named<Test>("test").configure {
dependsOn(shadowJar)
classpath = files(shadowJar, sourceSets.test.get().output, configurations.testRuntimeClasspath)
}

tasks.withType<Javadoc> {
// generated ANTLR sources violate doclint
(options as StandardJavadocDocletOptions).addStringOption("Xdoclint:none", "-quiet")

// Items besides JavaParser due to lombok error which looks similar to this:
// openrewrite/rewrite/rewrite-java/src/main/java/org/openrewrite/java/OrderImports.java:42: error: cannot find symbol
// @AllArgsConstructor(onConstructor_=@JsonCreator)
// ^
// symbol: method onConstructor_()
// location: @interface AllArgsConstructor
// 1 error
exclude("**/GitProvenance.java")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package org.openrewrite.marker;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.With;
import org.eclipse.jgit.api.Git;
Expand All @@ -41,6 +44,7 @@
import java.util.*;

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

@Value
Expand Down Expand Up @@ -330,16 +334,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 -> Committer.from(
c.getValue(),
c.getKey(),
commitMap.get(c.getKey()))
).collect(toList());
} catch (IOException | GitAPIException e) {
return emptyList();
}
Expand Down Expand Up @@ -380,9 +390,32 @@ public enum EOL {

@Value
@With
@RequiredArgsConstructor(onConstructor_ = {@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)})
public static class Committer {
String name;
String email;
NavigableMap<LocalDate, Integer> commitsByDay;
int[] dates;
int[] commits;

@JsonCreator
public static Committer from(
@JsonProperty("name") String name,
@JsonProperty("email") String email,
@JsonProperty("commitsByDay") NavigableMap<LocalDate, Integer> commitsByDay) {
return new Committer(
name,
email,
commitsByDay.keySet().stream().mapToInt(localDate -> (int) localDate.toEpochDay()).toArray(),
commitsByDay.values().stream().mapToInt(Integer::intValue).toArray()
);
}

public NavigableMap<LocalDate, Integer> getCommitsByDay() {
TreeMap<LocalDate, Integer> commitsByDay = new TreeMap<>();
for (int i = 0; i < dates.length; i++) {
commitsByDay.put(LocalDate.ofEpochDay(dates[i]), commits[i]);
}
return commitsByDay;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.LocalDate;
import java.util.Collection;
import java.util.Collections;
import java.util.NavigableMap;
import java.util.concurrent.atomic.AtomicReference;

import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -90,15 +91,16 @@ public Collection<? extends SourceFile> generate(AtomicReference<GitProvenance>
if (gitProvenance != null) {
LocalDate from = StringUtils.isBlank(fromDate) ? null : LocalDate.parse(fromDate).minusDays(1);
for (GitProvenance.Committer committer : requireNonNull(gitProvenance.getCommitters())) {
if (from == null || committer.getCommitsByDay().keySet().stream().anyMatch(day -> day.isAfter(from))) {
NavigableMap<LocalDate, Integer> committerCommits = committer.getCommitsByDay();
if (from == null || committerCommits.keySet().stream().anyMatch(day -> day.isAfter(from))) {
committers.insertRow(ctx, new DistinctCommitters.Row(
committer.getName(),
committer.getEmail(),
committer.getCommitsByDay().lastKey(),
committer.getCommitsByDay().values().stream().mapToInt(Integer::intValue).sum()
committerCommits.lastKey(),
committerCommits.values().stream().mapToInt(Integer::intValue).sum()
));

committer.getCommitsByDay().forEach((day, commits) -> commitsByDay.insertRow(ctx, new CommitsByDay.Row(
committerCommits.forEach((day, commits) -> commitsByDay.insertRow(ctx, new CommitsByDay.Row(
committer.getName(),
committer.getEmail(),
day,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void defaults(RecipeSpec spec) {
void findCommitters() {
GitProvenance git = new GitProvenance(
randomId(), "github.com", "main", "123", null, null,
List.of(new GitProvenance.Committer("Jon", "[email protected]",
List.of(GitProvenance.Committer.from("Jon", "[email protected]",
new TreeMap<>() {{
put(LocalDate.now().minusDays(5), 5);
put(LocalDate.now(), 5);
Expand All @@ -64,12 +64,12 @@ void findCommitters() {
void findCommittersFromDate() {
GitProvenance git = new GitProvenance(
randomId(), "github.com", "main", "123", null, null,
List.of(new GitProvenance.Committer("Jon", "[email protected]",
List.of(GitProvenance.Committer.from("Jon", "[email protected]",
new TreeMap<>() {{
put(LocalDate.of(2023, 1, 9), 5);
put(LocalDate.of(2023, 1, 1), 5);
}}),
new GitProvenance.Committer("Peter", "[email protected]",
GitProvenance.Committer.from("Peter", "[email protected]",
new TreeMap<>() {{
put(LocalDate.of(2023, 1, 10), 5);
}}))
Expand All @@ -93,12 +93,12 @@ void findCommittersFromDate() {
void findCommittersFromDateEmpty() {
GitProvenance git = new GitProvenance(
randomId(), "github.com", "main", "123", null, null,
List.of(new GitProvenance.Committer("Jon", "[email protected]",
List.of(GitProvenance.Committer.from("Jon", "[email protected]",
new TreeMap<>() {{
put(LocalDate.of(2023, 1, 9), 5);
put(LocalDate.of(2023, 1, 1), 5);
}}),
new GitProvenance.Committer("Peter", "[email protected]",
GitProvenance.Committer.from("Peter", "[email protected]",
new TreeMap<>() {{
put(LocalDate.of(2023, 1, 10), 5);
}}))
Expand Down