Skip to content
Permalink
Browse files
vcs: add ReadOnlyRepository.commitMetadataFor
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 16, 2021
1 parent b39d50b commit 902698ad68a4e4c519dc8f0f92b86582b2a49419
@@ -185,6 +185,10 @@ public List<CommitMetadata> commitMetadata() throws IOException {
return List.of();
}

public List<CommitMetadata> commitMetadataFor(List<Branch> branches) throws IOException {
return List.of();
}

public Path root() throws IOException {
return null;
}
@@ -70,6 +70,10 @@ default Optional<CommitMetadata> commitMetadata(Hash hash) throws IOException {
List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths) throws IOException;
List<CommitMetadata> commitMetadata(String range, List<Path> paths, boolean reverse) throws IOException;
List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths, boolean reverse) throws IOException;

// Can't overload on both List<Path> and List<Branch>
List<CommitMetadata> commitMetadataFor(List<Branch> branches) throws IOException;

String range(Hash h);
String rangeInclusive(Hash from, Hash to);
String rangeExclusive(Hash from, Hash to);
@@ -254,6 +254,19 @@ public List<CommitMetadata> commitMetadata(String range, List<Path> paths, boole
return readMetadata(args, "commit ");
}

@Override
public List<CommitMetadata> commitMetadataFor(List<Branch> branches) throws IOException {
var args = new ArrayList<String>();
args.addAll(List.of("git", "rev-list",
"--format=" + GitCommitMetadata.FORMAT,
"--topo-order",
"--no-abbrev",
"--no-color"));
args.addAll(branches.stream().map(Branch::name).collect(Collectors.toList()));
args.add("--");
return readMetadata(args, "commit ");
}

@Override
public List<CommitMetadata> commitMetadata(Hash from, Hash to, List<Path> paths, boolean reverse) throws IOException {
return commitMetadata(from.hex() + ".." + to.hex(), paths, reverse);
@@ -307,6 +307,11 @@ public List<CommitMetadata> commitMetadata(String range, List<Path> paths, boole
return readMetadata(cmd);
}

@Override
public List<CommitMetadata> commitMetadataFor(List<Branch> branches) throws IOException {
throw new RuntimeException("Not implemented yet");
}

@Override
public List<CommitMetadata> commitMetadata(String range) throws IOException {
return commitMetadata(range, List.of(), false);
@@ -2933,4 +2933,49 @@ void testTagPush() throws IOException {
assertEquals("v1.0", tags.get(0).name());
}
}

@Test
void testCommitMetadataWithBranchesWithGit() throws IOException {
try (var dir = new TemporaryDirectory()) {
var r = Repository.init(dir.path(), VCS.GIT);

var readme = dir.path().resolve("README");
Files.write(readme, List.of("Hello, world!"));
r.add(readme);
var first = r.commit("Added README", "duke", "duke@openjdk.java.net");

var b1 = r.branch(first, "b1");
r.checkout(b1);
Files.write(readme, List.of("One more line"), WRITE, APPEND);
r.add(readme);
var second = r.commit("Modified README", "duke", "duke@openjdk.java.net");

r.checkout(r.defaultBranch());
var b2 = r.branch(first, "b2");
r.checkout(b2);
Files.write(readme, List.of("An additional line"), WRITE, APPEND);
r.add(readme);
var third = r.commit("Additional line added to README", "duke", "duke@openjdk.java.net");

var metadata = r.commitMetadataFor(List.of(r.defaultBranch()));
assertEquals(1, metadata.size());
assertEquals(first, metadata.get(0).hash());

metadata = r.commitMetadataFor(List.of(r.defaultBranch(), b1));
assertEquals(2, metadata.size());
assertEquals(second, metadata.get(0).hash());
assertEquals(first, metadata.get(1).hash());

metadata = r.commitMetadataFor(List.of(r.defaultBranch(), b2));
assertEquals(2, metadata.size());
assertEquals(third, metadata.get(0).hash());
assertEquals(first, metadata.get(1).hash());

metadata = r.commitMetadataFor(List.of(r.defaultBranch(), b1, b2));
assertEquals(3, metadata.size());
assertEquals(second, metadata.get(0).hash());
assertEquals(third, metadata.get(1).hash());
assertEquals(first, metadata.get(2).hash());
}
}
}

1 comment on commit 902698a

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on 902698a Mar 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.