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

vcs: lookup does not handle non-existing commits #898

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
@@ -179,8 +179,19 @@ public Commits commits(String range, int n, boolean reverse) throws IOException
return new GitCommits(dir, range, reverse, n);
}

private boolean exists(Hash h) throws IOException {
try (var p = capture("git", "cat-file", "-e", h.hex())) {
var res = p.await();
return res.status() == 0;
}
}

@Override
public Optional<Commit> lookup(Hash h) throws IOException {
if (!exists(h)) {
return Optional.empty();
}

var commits = commits(h.hex(), 1).asList();
if (commits.size() != 1) {
return Optional.empty();
@@ -235,8 +235,18 @@ public Commits commits(String range, int n, boolean reverse) throws IOException
return new HgCommits(dir, range, ext, reverse, n);
}

private boolean exists(Hash h) throws IOException {
try (var p = capture("hg", "log", "--rev=" + h.hex(), "--template={node}\n")) {
var res = p.await();
return res.status() == 0;
}
}

@Override
public Optional<Commit> lookup(Hash h) throws IOException {
if (!exists(h)) {
return Optional.empty();
}
var commits = commits(h.hex()).asList();
if (commits.size() != 1) {
return Optional.empty();
@@ -2726,4 +2726,21 @@ void testPull(VCS vcs) throws IOException {
}
}
}

@ParameterizedTest
@EnumSource(VCS.class)
void testNonExistingLookup(VCS vcs) throws IOException {
try (var dir = new TemporaryDirectory()) {
var r = Repository.init(dir.path(), vcs);
assertTrue(r.isClean());

var readme = dir.path().resolve("README.md");
Files.writeString(readme, "Hello world\n");
r.add(readme);
var hash = r.commit("Added readme", "duke", "duke@openjdk.java.net");

var nonExisting = r.lookup(new Hash("0123456789012345678901234567890123456789"));
assertEquals(Optional.empty(), nonExisting);
}
}
}