File repoLoc = RepoUtils.copyRepoToTempLocation("log-1", "test-commit-rollback");
final Path newFilePath = Path.create("xx");
final File newFile = new File(repoLoc, newFilePath.toString());
RepoUtils.createFile(newFile, "xyz");
HgRepository hgRepo = new HgLookup().detect(repoLoc);
HgDataFile dfB = hgRepo.getFileNode("b");
HgDataFile dfD = hgRepo.getFileNode("d");
assertTrue("[sanity]", dfB.exists());
assertTrue("[sanity]", dfD.exists());
final File modifiedFile = new File(repoLoc, "b");
RepoUtils.modifyFileAppend(modifiedFile, " 1 \n");
//
new HgAddRemoveCommand(hgRepo).add(newFilePath).remove(dfD.getPath()).execute();
//
TestStatus.StatusCollector status = new TestStatus.StatusCollector();
new HgStatusCommand(hgRepo).all().execute(status);
assertTrue(status.getErrors().isEmpty());
assertTrue(status.get(Kind.Added).contains(newFilePath));
assertTrue(status.get(Kind.Modified).contains(dfB.getPath()));
assertTrue(status.get(Kind.Removed).contains(dfD.getPath()));
assertEquals(DEFAULT_BRANCH_NAME, hgRepo.getWorkingCopyBranchName());
//
final int lastClogRevision = hgRepo.getChangelog().getLastRevision();
final int lastManifestRev = hgRepo.getManifest().getLastRevision();
CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), lastClogRevision);
cf.add(hgRepo.getFileNode("xx"), new FileContentSupplier(hgRepo, newFile));
cf.add(dfB, new FileContentSupplier(hgRepo, modifiedFile));
cf.forget(dfD);
cf.branch("another-branch");
Transaction tr = newTransaction(hgRepo);
Nodeid commitRev = cf.commit("Commit to fail", tr);
tr.rollback();
//
errorCollector.assertEquals(lastClogRevision, hgRepo.getChangelog().getLastRevision());
errorCollector.assertEquals(lastManifestRev, hgRepo.getManifest().getLastRevision());
errorCollector.assertEquals(DEFAULT_BRANCH_NAME, DirstateReader.readBranch(Internals.getInstance(hgRepo)));
errorCollector.assertFalse(hgRepo.getChangelog().isKnown(commitRev));
errorCollector.assertFalse(hgRepo.getFileNode("xx").exists());
// check dirstate
status = new TestStatus.StatusCollector();
new HgStatusCommand(hgRepo).all().execute(status);
errorCollector.assertTrue(status.getErrors().isEmpty());
errorCollector.assertTrue(status.get(Kind.Added).contains(newFilePath));
errorCollector.assertTrue(status.get(Kind.Modified).contains(dfB.getPath()));
errorCollector.assertTrue(status.get(Kind.Removed).contains(dfD.getPath()));
RepoUtils.assertHgVerifyOk(errorCollector, repoLoc);
}