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: handle nested repositories #496

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
@@ -135,11 +135,26 @@ static Repository init(Path p, VCS vcs) throws IOException {
}

static Optional<Repository> get(Path p) throws IOException {
var r = GitRepository.get(p);
if (r.isPresent()) {
return r;
var gitRepo = GitRepository.get(p);
var hgRepo = HgRepository.get(p);
if (gitRepo.isPresent() && hgRepo.isEmpty()) {
return gitRepo;
} else if (gitRepo.isEmpty() && hgRepo.isPresent()) {
return hgRepo;
} else if (gitRepo.isPresent() && hgRepo.isPresent()) {
// Nested repositories
var gitRoot = gitRepo.get().root();
var hgRoot = hgRepo.get().root();
if (gitRoot.equals(hgRoot)) {
throw new IOException(p.toString() + " contains both a hg and git repository");
}
if (hgRoot.startsWith(gitRoot)) {
return hgRepo;
} else {
return gitRepo;
}
}
return HgRepository.get(p);
return Optional.empty();
}

static boolean exists(Path p) throws IOException {
@@ -2224,4 +2224,79 @@ void testRangeExclusive(VCS vcs) throws IOException {
}
}
}

@Test
void testHgRepoNestedInGitRepo() throws IOException {
try (var gitDir = new TemporaryDirectory()) {
var gitRepo = Repository.init(gitDir.path(), VCS.GIT);
var gitFile = gitRepo.root().resolve("git-file.txt");
Files.write(gitFile, List.of("Hello, Git!"));
gitRepo.add(gitFile);
var gitHash = gitRepo.commit("Added git-file.txt", "duke", "duke@openjdk.java.net");

var hgDir = gitRepo.root().resolve("hg");
var hgRepo = Repository.init(hgDir, VCS.HG);
var hgFile = hgRepo.root().resolve("hg-file.txt");
Files.write(hgFile, List.of("Hello, Mercurial!"));
hgRepo.add(hgFile);
var hgHash = hgRepo.commit("Added hg-file.txt", "duke", "duke@openjdk.java.net");

var resolvedHgRepo = Repository.get(hgDir).orElseThrow();
var resolvedHgCommits = resolvedHgRepo.commits().asList();
assertEquals(1, resolvedHgCommits.size());
assertEquals(hgHash, resolvedHgCommits.get(0).hash());

var resolvedGitRepo = Repository.get(gitDir.path()).orElseThrow();
var resolvedGitCommits = resolvedGitRepo.commits().asList();
assertEquals(1, resolvedGitCommits.size());
assertEquals(gitHash, resolvedGitCommits.get(0).hash());
}
}

@Test
void testGitRepoNestedInHgRepo() throws IOException {
try (var hgDir = new TemporaryDirectory()) {
var hgRepo = Repository.init(hgDir.path(), VCS.HG);
var hgFile = hgRepo.root().resolve("hg-file.txt");
Files.write(hgFile, List.of("Hello, Mercurial!"));
hgRepo.add(hgFile);
var hgHash = hgRepo.commit("Added hg-file.txt", "duke", "duke@openjdk.java.net");

var gitDir = hgRepo.root().resolve("git");
var gitRepo = Repository.init(gitDir, VCS.GIT);
var gitFile = gitRepo.root().resolve("git-file.txt");
Files.write(gitFile, List.of("Hello, Git!"));
gitRepo.add(gitFile);
var gitHash = gitRepo.commit("Added git-file.txt", "duke", "duke@openjdk.java.net");

var resolvedHgRepo = Repository.get(hgDir.path()).orElseThrow();
var resolvedHgCommits = resolvedHgRepo.commits().asList();
assertEquals(1, resolvedHgCommits.size());
assertEquals(hgHash, resolvedHgCommits.get(0).hash());

var resolvedGitRepo = Repository.get(gitDir).orElseThrow();
var resolvedGitCommits = resolvedGitRepo.commits().asList();
assertEquals(1, resolvedGitCommits.size());
assertEquals(gitHash, resolvedGitCommits.get(0).hash());
}
}

@Test
void testGitAndHgRepoInSameDirectory() throws IOException {
try (var dir = new TemporaryDirectory()) {
var hgRepo = Repository.init(dir.path(), VCS.HG);
var hgFile = hgRepo.root().resolve("hg-file.txt");
Files.write(hgFile, List.of("Hello, Mercurial!"));
hgRepo.add(hgFile);
var hgHash = hgRepo.commit("Added hg-file.txt", "duke", "duke@openjdk.java.net");

var gitRepo = Repository.init(dir.path(), VCS.GIT);
var gitFile = gitRepo.root().resolve("git-file.txt");
Files.write(gitFile, List.of("Hello, Git!"));
gitRepo.add(gitFile);
var gitHash = gitRepo.commit("Added git-file.txt", "duke", "duke@openjdk.java.net");

assertThrows(IOException.class, () -> Repository.get(dir.path()));
}
}
}