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

webrev: refactor webrev stats #713

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -141,9 +141,10 @@ private static Optional<String> issueUrl(PullRequest pr, URI issueTracker, Strin
private static String stats(Repository localRepo, Hash base, Hash head) {
try {
var diff = localRepo.diff(base, head);
var inserted = diff.added();
var deleted = diff.removed();
var modified = diff.modified();
var diffStats = diff.totalStats();
var inserted = diffStats.added();
var deleted = diffStats.removed();
var modified = diffStats.modified();
var linesChanged = inserted + deleted + modified;
var filesChanged = diff.patches().size();
return String.format("%d line%s in %d file%s changed: %d ins; %d del; %d mod",
@@ -56,24 +56,19 @@ public List<Patch> patches() {
return patches;
}

public List<PatchStats> stats() {
public List<WebrevStats> stats() {
return patches().stream()
.filter(Patch::isTextual)
.map(Patch::asTextualPatch)
.map(TextualPatch::stats)
.collect(Collectors.toList());
}

public int added() {
return stats().stream().mapToInt(PatchStats::added).sum();
}

public int modified() {
return stats().stream().mapToInt(PatchStats::modified).sum();
}

public int removed() {
return stats().stream().mapToInt(PatchStats::removed).sum();
public WebrevStats totalStats() {
var added = stats().stream().mapToInt(WebrevStats::added).sum();
var removed = stats().stream().mapToInt(WebrevStats::removed).sum();
var modified = stats().stream().mapToInt(WebrevStats::modified).sum();
return new WebrevStats(added, removed, modified);
}

public void write(BufferedWriter w) throws IOException {
@@ -73,17 +73,14 @@ public Info target() {
return target;
}

public int modified() {
return Math.min(source.lines().size(), target.lines().size());
public WebrevStats stats() {
var modified = Math.min(source.lines().size(), target.lines().size());
var added = target.lines().size() - modified;
var removed = source.lines().size() - modified;
return new WebrevStats(added, removed, modified);
}

public int added() {
return target.lines().size() - modified();
}

public int removed() {
return source.lines().size() - modified();
}

public void write(BufferedWriter w) throws IOException {
w.append("@@ -");
@@ -49,18 +49,18 @@ public boolean isEmpty() {
return hunks.isEmpty();
}

public PatchStats stats() {
public WebrevStats stats() {
int added = 0;
int removed = 0;
int modified = 0;

for (var hunk : hunks()) {
added += hunk.added();
removed += hunk.removed();
modified += hunk.modified();
var stats = hunk.stats();
added += stats.added();
removed += stats.removed();
modified += stats.modified();
}

var path = target().path().isPresent() ? target().path().get() : source().path().get();
return new PatchStats(path, added, removed, modified);
return new WebrevStats(added, removed, modified);
}
}
@@ -24,24 +24,19 @@

import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;

public class PatchStats {
private final Path path;
public class WebrevStats {
private final int added;
private final int removed;
private final int modified;

public PatchStats(Path path, int added, int removed, int modified) {
this.path = path;
public WebrevStats(int added, int removed, int modified) {
this.added = added;
this.removed = removed;
this.modified = modified;
}

public Path path() {
return path;
}

public int added() {
return added;
}
@@ -56,7 +51,7 @@ public int modified() {

@Override
public int hashCode() {
return Objects.hash(path, added, removed, modified);
return Objects.hash(added, removed, modified);
}

@Override
@@ -65,13 +60,12 @@ public boolean equals(Object other) {
return true;
}

if (!(other instanceof PatchStats)) {
if (!(other instanceof WebrevStats)) {
return false;
}

var o = (PatchStats) other;
return Objects.equals(path, o.path) &&
Objects.equals(added, o.added) &&
var o = (WebrevStats) other;
return Objects.equals(added, o.added) &&
Objects.equals(removed, o.removed) &&
Objects.equals(modified, o.modified);
}
@@ -290,9 +290,10 @@ void testCommitListingWithSingleCommit(VCS vcs) throws IOException {
assertEquals(Hash.zero(), diff.from());
assertEquals(hash, diff.to());

assertEquals(0, diff.removed());
assertEquals(0, diff.modified());
assertEquals(1, diff.added());
var stats = diff.totalStats();
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
assertEquals(1, stats.added());

var patches = diff.patches();
assertEquals(1, patches.size());
@@ -368,9 +369,10 @@ void testCommitListingWithMultipleCommits(VCS vcs) throws IOException {
assertEquals(hash1, diff.from());
assertEquals(hash2, diff.to());

assertEquals(0, diff.removed());
assertEquals(0, diff.modified());
assertEquals(1, diff.added());
var stats = diff.totalStats();
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
assertEquals(1, stats.added());

var patches = diff.patches();
assertEquals(1, patches.size());
@@ -446,9 +448,10 @@ void testSquashDeletes(VCS vcs) throws IOException {
assertEquals(hash1, diff.from());
assertEquals(head.hash(), diff.to());

assertEquals(2, diff.removed());
assertEquals(0, diff.modified());
assertEquals(0, diff.added());
var stats = diff.totalStats();
assertEquals(2, stats.removed());
assertEquals(0, stats.modified());
assertEquals(0, stats.added());
}
}

@@ -500,9 +503,10 @@ void testSquash(VCS vcs) throws IOException {
assertEquals(hash1, diff.from());
assertEquals(head.hash(), diff.to());

assertEquals(0, diff.removed());
assertEquals(0, diff.modified());
assertEquals(2, diff.added());
var stats = diff.totalStats();
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
assertEquals(2, stats.added());

var patches = diff.patches();
assertEquals(1, patches.size());
@@ -607,9 +611,10 @@ void testRebase(VCS vcs) throws IOException {
assertEquals(1, diffs.size());
var diff = diffs.get(0);

assertEquals(0, diff.removed());
assertEquals(0, diff.modified());
assertEquals(1, diff.added());
var stats = diff.totalStats();
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
assertEquals(1, stats.added());

var patches = diff.patches();
assertEquals(1, patches.size());
@@ -899,9 +904,10 @@ void testDiffBetweenCommits(VCS vcs) throws IOException {
assertEquals(1, hunk.target().range().count());
assertLinesEquals(List.of("One more line"), hunk.target().lines());

assertEquals(1, hunk.added());
assertEquals(0, hunk.removed());
assertEquals(0, hunk.modified());
var stats = hunk.stats();
assertEquals(1, stats.added());
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
}
}

@@ -1016,9 +1022,10 @@ void testDiffBetweenCommitsWithMultipleHunks(VCS vcs) throws IOException {
assertEquals(2, hunk1.target().range().count());
assertLinesEquals(List.of("1", "2"), hunk1.target().lines());

assertEquals(1, hunk1.added());
assertEquals(0, hunk1.removed());
assertEquals(1, hunk1.modified());
var stats1 = hunk1.stats();
assertEquals(1, stats1.added());
assertEquals(0, stats1.removed());
assertEquals(1, stats1.modified());

var hunk2 = hunks.get(1);
assertEquals(3, hunk2.source().range().start());
@@ -1029,9 +1036,10 @@ void testDiffBetweenCommitsWithMultipleHunks(VCS vcs) throws IOException {
assertEquals(1, hunk2.target().range().count());
assertLinesEquals(List.of("3"), hunk2.target().lines());

assertEquals(0, hunk2.added());
assertEquals(0, hunk2.removed());
assertEquals(1, hunk2.modified());
var stats2 = hunk2.stats();
assertEquals(0, stats2.added());
assertEquals(0, stats2.removed());
assertEquals(1, stats2.modified());
}
}

@@ -1077,9 +1085,10 @@ void testDiffWithRemoval(VCS vcs) throws IOException {
assertEquals(0, hunk.target().range().count());
assertLinesEquals(List.of(), hunk.target().lines());

assertEquals(0, hunk.added());
assertEquals(1, hunk.removed());
assertEquals(0, hunk.modified());
var stats = hunk.stats();
assertEquals(0, stats.added());
assertEquals(1, stats.removed());
assertEquals(0, stats.modified());
}
}

@@ -1126,9 +1135,10 @@ void testDiffWithAddition(VCS vcs) throws IOException {
assertEquals(1, hunk.target().range().count());
assertLinesEquals(List.of("make"), hunk.target().lines());

assertEquals(1, hunk.added());
assertEquals(0, hunk.removed());
assertEquals(0, hunk.modified());
var stats = hunk.stats();
assertEquals(1, stats.added());
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
}
}

@@ -1172,9 +1182,10 @@ void testDiffWithWorkingDir(VCS vcs) throws IOException {
assertEquals(1, hunk.target().range().count());
assertLinesEquals(List.of("One more line"), hunk.target().lines());

assertEquals(1, hunk.added());
assertEquals(0, hunk.removed());
assertEquals(0, hunk.modified());
var stats = hunk.stats();
assertEquals(1, stats.added());
assertEquals(0, stats.removed());
assertEquals(0, stats.modified());
}
}

@@ -1785,7 +1796,7 @@ void testTrackLineEndings(VCS vcs) throws IOException, InterruptedException {
var commit = commits.get(0);
var diffs = commit.parentDiffs();
var diff = diffs.get(0);
assertEquals(2, diff.added());
assertEquals(2, diff.totalStats().added());

var patches = diff.patches();
assertEquals(1, patches.size());
@@ -2026,9 +2037,10 @@ void testDiffWithFileList(VCS vcs) throws IOException {
Files.writeString(contribute, "2. Run git commit", WRITE, APPEND);

var diff = repo.diff(first, List.of(Path.of("README")));
assertEquals(1, diff.added());
assertEquals(0, diff.modified());
assertEquals(0, diff.removed());
var diffStats = diff.totalStats();
assertEquals(1, diffStats.added());
assertEquals(0, diffStats.modified());
assertEquals(0, diffStats.removed());
var patches = diff.patches();
assertEquals(1, patches.size());
var patch = patches.get(0);
@@ -2042,9 +2054,10 @@ void testDiffWithFileList(VCS vcs) throws IOException {
var second = repo.commit("Updates to both README and CONTRIBUTE", "duke", "duke@openjdk.org");

diff = repo.diff(first, second, List.of(Path.of("CONTRIBUTE")));
assertEquals(1, diff.added());
assertEquals(0, diff.modified());
assertEquals(0, diff.removed());
diffStats = diff.totalStats();
assertEquals(1, diffStats.added());
assertEquals(0, diffStats.modified());
assertEquals(0, diffStats.removed());
patches = diff.patches();
assertEquals(1, patches.size());
patch = patches.get(0);
@@ -84,9 +84,11 @@ void assertCommitEquals(Commit gitCommit, Commit hgCommit) {
assertEquals(gitHunk.target().range(), hgHunk.target().range());
assertEquals(gitHunk.target().lines(), hgHunk.target().lines());

assertEquals(gitHunk.added(), hgHunk.added());
assertEquals(gitHunk.removed(), hgHunk.removed());
assertEquals(gitHunk.modified(), hgHunk.modified());
var hgStats = hgHunk.stats();
var gitStats = gitHunk.stats();
assertEquals(gitStats.added(), hgStats.added());
assertEquals(gitStats.removed(), hgStats.removed());
assertEquals(gitStats.modified(), hgStats.modified());
}
}
}
@@ -108,9 +108,11 @@ void convertOneCommit() throws IOException {
assertEquals(hgHunk.target().range(), gitHunk.target().range());
assertEquals(hgHunk.target().lines(), gitHunk.target().lines());

assertEquals(hgHunk.added(), gitHunk.added());
assertEquals(hgHunk.removed(), gitHunk.removed());
assertEquals(hgHunk.modified(), gitHunk.modified());
var hgStats = hgHunk.stats();
var gitStats = gitHunk.stats();
assertEquals(hgStats.added(), gitStats.added());
assertEquals(hgStats.removed(), gitStats.removed());
assertEquals(hgStats.modified(), gitStats.modified());
}
}