Skip to content
Permalink
Browse files
vcs: handle nested repositories
Reviewed-by: rwestberg
  • Loading branch information
edvbld committed Mar 5, 2020
1 parent eb54331 commit da335bd7983bed2970bd912faffd6f9f31c0c49a
Showing 2 changed files with 94 additions and 4 deletions.
@@ -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()));
}
}
}

0 comments on commit da335bd

Please sign in to comment.