tikhomirov@534: /* tikhomirov@534: * Copyright (c) 2013 TMate Software Ltd tikhomirov@534: * tikhomirov@534: * This program is free software; you can redistribute it and/or modify tikhomirov@534: * it under the terms of the GNU General Public License as published by tikhomirov@534: * the Free Software Foundation; version 2 of the License. tikhomirov@534: * tikhomirov@534: * This program is distributed in the hope that it will be useful, tikhomirov@534: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@534: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@534: * GNU General Public License for more details. tikhomirov@534: * tikhomirov@534: * For information on how to redistribute this software under tikhomirov@534: * the terms of a license other than GNU General Public License tikhomirov@534: * contact TMate Software at support@hg4j.com tikhomirov@534: */ tikhomirov@534: package org.tmatesoft.hg.test; tikhomirov@534: tikhomirov@559: import static org.junit.Assert.*; tikhomirov@559: import static org.tmatesoft.hg.repo.HgRepository.*; tikhomirov@539: tikhomirov@538: import java.io.File; tikhomirov@559: import java.io.IOException; tikhomirov@559: import java.util.List; tikhomirov@538: tikhomirov@588: import org.junit.Rule; tikhomirov@534: import org.junit.Test; tikhomirov@559: import org.tmatesoft.hg.core.HgAddRemoveCommand; tikhomirov@559: import org.tmatesoft.hg.core.HgCatCommand; tikhomirov@559: import org.tmatesoft.hg.core.HgChangeset; tikhomirov@586: import org.tmatesoft.hg.core.HgCommitCommand; tikhomirov@559: import org.tmatesoft.hg.core.HgLogCommand; tikhomirov@586: import org.tmatesoft.hg.core.HgStatus.Kind; tikhomirov@586: import org.tmatesoft.hg.core.HgStatusCommand; tikhomirov@559: import org.tmatesoft.hg.core.Nodeid; tikhomirov@617: import org.tmatesoft.hg.core.SessionContext; tikhomirov@559: import org.tmatesoft.hg.internal.ByteArrayChannel; tikhomirov@617: import org.tmatesoft.hg.internal.COWTransaction; tikhomirov@591: import org.tmatesoft.hg.internal.CommitFacility; tikhomirov@628: import org.tmatesoft.hg.internal.DataSerializer.ByteArrayDataSource; tikhomirov@621: import org.tmatesoft.hg.internal.DirstateReader; tikhomirov@586: import org.tmatesoft.hg.internal.FileContentSupplier; tikhomirov@591: import org.tmatesoft.hg.internal.Internals; tikhomirov@617: import org.tmatesoft.hg.internal.Transaction; tikhomirov@559: import org.tmatesoft.hg.repo.HgDataFile; tikhomirov@538: import org.tmatesoft.hg.repo.HgLookup; tikhomirov@652: import org.tmatesoft.hg.repo.HgPhase; tikhomirov@538: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@652: import org.tmatesoft.hg.repo.HgRepositoryFiles; tikhomirov@586: import org.tmatesoft.hg.util.Outcome; tikhomirov@559: import org.tmatesoft.hg.util.Path; tikhomirov@534: tikhomirov@534: /** tikhomirov@559: * Handy for debug to see patch content: tikhomirov@559: * ...RevlogDump /tmp/test-commit2non-empty/.hg/ store/data/file1.i dumpData tikhomirov@534: * tikhomirov@534: * @author Artem Tikhomirov tikhomirov@534: * @author TMate Software Ltd. tikhomirov@534: */ tikhomirov@534: public class TestCommit { tikhomirov@534: tikhomirov@588: @Rule tikhomirov@588: public ErrorCollectorExt errorCollector = new ErrorCollectorExt(); tikhomirov@617: tikhomirov@617: private final Transaction.Factory trFactory = new COWTransaction.Factory(); tikhomirov@617: // { tikhomirov@617: // public Transaction create(Source ctxSource) { tikhomirov@617: // return new Transaction.NoRollback(); tikhomirov@617: // } tikhomirov@617: // }; tikhomirov@617: tikhomirov@534: @Test tikhomirov@538: public void testCommitToNonEmpty() throws Exception { tikhomirov@538: File repoLoc = RepoUtils.initEmptyTempRepo("test-commit2non-empty"); tikhomirov@559: RepoUtils.createFile(new File(repoLoc, "file1"), "hello\n"); tikhomirov@559: new ExecHelper(new OutputParser.Stub(), repoLoc).run("hg", "commit", "--addremove", "-m", "FIRST"); tikhomirov@538: // tikhomirov@538: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@591: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), 0); tikhomirov@559: HgDataFile df = hgRepo.getFileNode("file1"); tikhomirov@618: cf.add(df, new ByteArrayDataSource("hello\nworld".getBytes())); tikhomirov@617: Transaction tr = newTransaction(hgRepo); tikhomirov@617: Nodeid secondRev = cf.commit("SECOND", tr); tikhomirov@617: tr.commit(); tikhomirov@559: // tikhomirov@559: List commits = new HgLogCommand(hgRepo).execute(); tikhomirov@588: errorCollector.assertEquals(2, commits.size()); tikhomirov@559: HgChangeset c1 = commits.get(0); tikhomirov@559: HgChangeset c2 = commits.get(1); tikhomirov@588: errorCollector.assertEquals("FIRST", c1.getComment()); tikhomirov@588: errorCollector.assertEquals("SECOND", c2.getComment()); tikhomirov@588: errorCollector.assertEquals(df.getPath(), c2.getAffectedFiles().get(0)); tikhomirov@588: errorCollector.assertEquals(c1.getNodeid(), c2.getFirstParentRevision()); tikhomirov@588: errorCollector.assertEquals(Nodeid.NULL, c2.getSecondParentRevision()); tikhomirov@588: errorCollector.assertEquals(secondRev, c2.getNodeid()); tikhomirov@539: } tikhomirov@539: tikhomirov@539: @Test tikhomirov@539: public void testCommitToEmpty() throws Exception { tikhomirov@539: File repoLoc = RepoUtils.initEmptyTempRepo("test-commit2empty"); tikhomirov@559: String fname = "file1"; tikhomirov@559: RepoUtils.createFile(new File(repoLoc, fname), null); tikhomirov@559: new ExecHelper(new OutputParser.Stub(), repoLoc).run("hg", "add", fname); tikhomirov@539: // tikhomirov@539: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@559: assertEquals("[sanity]", 0, new HgLogCommand(hgRepo).execute().size()); tikhomirov@591: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), NO_REVISION); tikhomirov@559: HgDataFile df = hgRepo.getFileNode(fname); tikhomirov@559: final byte[] initialContent = "hello\nworld".getBytes(); tikhomirov@618: cf.add(df, new ByteArrayDataSource(initialContent)); tikhomirov@559: String comment = "commit 1"; tikhomirov@617: Transaction tr = newTransaction(hgRepo); tikhomirov@617: Nodeid c1Rev = cf.commit(comment, tr); tikhomirov@617: tr.commit(); tikhomirov@559: List commits = new HgLogCommand(hgRepo).execute(); tikhomirov@588: errorCollector.assertEquals(1, commits.size()); tikhomirov@559: HgChangeset c1 = commits.get(0); tikhomirov@588: errorCollector.assertEquals(1, c1.getAffectedFiles().size()); tikhomirov@588: errorCollector.assertEquals(df.getPath(), c1.getAffectedFiles().get(0)); tikhomirov@588: errorCollector.assertEquals(0, c1.getRevisionIndex()); tikhomirov@588: errorCollector.assertEquals(Nodeid.NULL, c1.getFirstParentRevision()); tikhomirov@588: errorCollector.assertEquals(Nodeid.NULL, c1.getSecondParentRevision()); tikhomirov@588: errorCollector.assertEquals(HgRepository.DEFAULT_BRANCH_NAME, c1.getBranch()); tikhomirov@588: errorCollector.assertEquals(comment, c1.getComment()); tikhomirov@588: errorCollector.assertEquals(c1Rev, c1.getNodeid()); tikhomirov@559: ByteArrayChannel bac = new ByteArrayChannel(); tikhomirov@559: new HgCatCommand(hgRepo).file(df.getPath()).execute(bac); tikhomirov@559: assertArrayEquals(initialContent, bac.toArray()); tikhomirov@559: } tikhomirov@559: tikhomirov@559: @Test tikhomirov@559: public void testCommitIntoBranch() throws Exception { tikhomirov@613: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-commit2branch", false); tikhomirov@559: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@559: HgDataFile dfD = hgRepo.getFileNode("d"); tikhomirov@559: assertTrue("[sanity]", dfD.exists()); tikhomirov@559: File fileD = new File(repoLoc, "d"); tikhomirov@559: assertTrue("[sanity]", fileD.canRead()); tikhomirov@559: final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision(); tikhomirov@559: HgChangeset parentCset = new HgLogCommand(hgRepo).range(parentCsetRevIndex, parentCsetRevIndex).execute().get(0); tikhomirov@559: assertEquals("[sanity]", DEFAULT_BRANCH_NAME, parentCset.getBranch()); tikhomirov@621: assertEquals("[sanity]", DEFAULT_BRANCH_NAME, hgRepo.getWorkingCopyBranchName()); tikhomirov@559: // tikhomirov@559: RepoUtils.modifyFileAppend(fileD, "A CHANGE\n"); tikhomirov@591: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex); tikhomirov@618: FileContentSupplier contentProvider = new FileContentSupplier(hgRepo, fileD); tikhomirov@559: cf.add(dfD, contentProvider); tikhomirov@559: cf.branch("branch1"); tikhomirov@617: Transaction tr = newTransaction(hgRepo); tikhomirov@617: Nodeid commitRev1 = cf.commit("FIRST", tr); tikhomirov@617: tr.commit(); tikhomirov@559: // tikhomirov@559: List commits = new HgLogCommand(hgRepo).range(parentCsetRevIndex+1, TIP).execute(); tikhomirov@559: assertEquals(1, commits.size()); tikhomirov@559: HgChangeset c1 = commits.get(0); tikhomirov@588: errorCollector.assertEquals(c1.getNodeid(), commitRev1); tikhomirov@588: errorCollector.assertEquals("branch1", c1.getBranch()); tikhomirov@588: errorCollector.assertEquals("FIRST", c1.getComment()); tikhomirov@559: // tikhomirov@621: // check if cached value in hgRepo got updated tikhomirov@621: errorCollector.assertEquals("branch1", hgRepo.getWorkingCopyBranchName()); tikhomirov@621: // tikhomirov@559: assertHgVerifyOk(repoLoc); tikhomirov@559: } tikhomirov@559: tikhomirov@559: /** tikhomirov@559: * use own add and remove commands and then commit tikhomirov@559: */ tikhomirov@559: @Test tikhomirov@559: public void testCommitWithAddRemove() throws Exception { tikhomirov@559: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-add-remove-commit", false); tikhomirov@559: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@559: assertTrue("[sanity]", hgRepo.getFileNode("d").exists()); tikhomirov@559: assertTrue("[sanity]", new File(repoLoc, "d").canRead()); tikhomirov@559: RepoUtils.createFile(new File(repoLoc, "xx"), "xyz"); tikhomirov@559: new HgAddRemoveCommand(hgRepo).add(Path.create("xx")).remove(Path.create("d")).execute(); tikhomirov@591: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), hgRepo.getChangelog().getLastRevision()); tikhomirov@618: FileContentSupplier contentProvider = new FileContentSupplier(hgRepo, new File(repoLoc, "xx")); tikhomirov@559: cf.add(hgRepo.getFileNode("xx"), contentProvider); tikhomirov@559: cf.forget(hgRepo.getFileNode("d")); tikhomirov@617: Transaction tr = newTransaction(hgRepo); tikhomirov@617: Nodeid commitRev = cf.commit("Commit with add/remove cmd", tr); tikhomirov@617: tr.commit(); tikhomirov@559: // tikhomirov@559: List commits = new HgLogCommand(hgRepo).changeset(commitRev).execute(); tikhomirov@559: HgChangeset cmt = commits.get(0); tikhomirov@588: errorCollector.assertEquals(1, cmt.getAddedFiles().size()); tikhomirov@588: errorCollector.assertEquals("xx", cmt.getAddedFiles().get(0).getPath().toString()); tikhomirov@588: errorCollector.assertEquals(1, cmt.getRemovedFiles().size()); tikhomirov@588: errorCollector.assertEquals("d", cmt.getRemovedFiles().get(0).toString()); tikhomirov@559: ByteArrayChannel sink = new ByteArrayChannel(); tikhomirov@559: new HgCatCommand(hgRepo).file(Path.create("xx")).changeset(commitRev).execute(sink); tikhomirov@559: assertArrayEquals("xyz".getBytes(), sink.toArray()); tikhomirov@559: // tikhomirov@559: assertHgVerifyOk(repoLoc); tikhomirov@559: } tikhomirov@559: /** tikhomirov@559: * perform few commits one by one, into different branches tikhomirov@559: */ tikhomirov@559: @Test tikhomirov@559: public void testSequentialCommits() throws Exception { tikhomirov@613: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-sequential-commits", false); tikhomirov@559: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@559: HgDataFile dfD = hgRepo.getFileNode("d"); tikhomirov@559: assertTrue("[sanity]", dfD.exists()); tikhomirov@559: File fileD = new File(repoLoc, "d"); tikhomirov@559: assertTrue("[sanity]", fileD.canRead()); tikhomirov@559: // tikhomirov@559: RepoUtils.modifyFileAppend(fileD, " 1 \n"); tikhomirov@559: final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision(); tikhomirov@591: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex); tikhomirov@618: FileContentSupplier contentProvider = new FileContentSupplier(hgRepo, fileD); tikhomirov@559: cf.add(dfD, contentProvider); tikhomirov@559: cf.branch("branch1"); tikhomirov@617: Transaction tr = newTransaction(hgRepo); tikhomirov@617: Nodeid commitRev1 = cf.commit("FIRST", tr); tikhomirov@559: // tikhomirov@559: RepoUtils.modifyFileAppend(fileD, " 2 \n"); tikhomirov@618: cf.add(dfD, contentProvider = new FileContentSupplier(hgRepo, fileD)); tikhomirov@559: cf.branch("branch2"); tikhomirov@617: Nodeid commitRev2 = cf.commit("SECOND", tr); tikhomirov@559: // tikhomirov@559: RepoUtils.modifyFileAppend(fileD, " 2 \n"); tikhomirov@618: cf.add(dfD, contentProvider = new FileContentSupplier(hgRepo, fileD)); tikhomirov@559: cf.branch(DEFAULT_BRANCH_NAME); tikhomirov@617: Nodeid commitRev3 = cf.commit("THIRD", tr); tikhomirov@617: tr.commit(); tikhomirov@559: // tikhomirov@559: List commits = new HgLogCommand(hgRepo).range(parentCsetRevIndex+1, TIP).execute(); tikhomirov@559: assertEquals(3, commits.size()); tikhomirov@559: HgChangeset c1 = commits.get(0); tikhomirov@559: HgChangeset c2 = commits.get(1); tikhomirov@559: HgChangeset c3 = commits.get(2); tikhomirov@588: errorCollector.assertEquals(c1.getNodeid(), commitRev1); tikhomirov@588: errorCollector.assertEquals(c2.getNodeid(), commitRev2); tikhomirov@588: errorCollector.assertEquals(c3.getNodeid(), commitRev3); tikhomirov@588: errorCollector.assertEquals("branch1", c1.getBranch()); tikhomirov@588: errorCollector.assertEquals("branch2", c2.getBranch()); tikhomirov@588: errorCollector.assertEquals(DEFAULT_BRANCH_NAME, c3.getBranch()); tikhomirov@588: errorCollector.assertEquals("FIRST", c1.getComment()); tikhomirov@588: errorCollector.assertEquals("SECOND", c2.getComment()); tikhomirov@588: errorCollector.assertEquals("THIRD", c3.getComment()); tikhomirov@559: assertHgVerifyOk(repoLoc); tikhomirov@559: } tikhomirov@559: tikhomirov@586: @Test tikhomirov@586: public void testCommandBasics() throws Exception { tikhomirov@586: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-commit-cmd", false); tikhomirov@652: // PhasesHelper relies on file existence to tell phase enablement tikhomirov@652: RepoUtils.createFile(new File(repoLoc, HgRepositoryFiles.Phaseroots.getPath()), ""); tikhomirov@586: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@586: HgDataFile dfB = hgRepo.getFileNode("b"); tikhomirov@586: assertTrue("[sanity]", dfB.exists()); tikhomirov@586: File fileB = new File(repoLoc, "b"); tikhomirov@586: assertTrue("[sanity]", fileB.canRead()); tikhomirov@586: RepoUtils.modifyFileAppend(fileB, " 1 \n"); tikhomirov@586: tikhomirov@586: HgCommitCommand cmd = new HgCommitCommand(hgRepo); tikhomirov@586: assertFalse(cmd.isMergeCommit()); tikhomirov@586: Outcome r = cmd.message("FIRST").execute(); tikhomirov@588: errorCollector.assertTrue(r.isOk()); tikhomirov@586: Nodeid c1 = cmd.getCommittedRevision(); tikhomirov@586: tikhomirov@588: // check that modified files are no longer reported as such tikhomirov@586: TestStatus.StatusCollector status = new TestStatus.StatusCollector(); tikhomirov@588: new HgStatusCommand(hgRepo).all().execute(status); tikhomirov@588: errorCollector.assertTrue(status.getErrors().isEmpty()); tikhomirov@588: errorCollector.assertTrue(status.get(Kind.Modified).isEmpty()); tikhomirov@588: errorCollector.assertEquals(1, status.get(dfB.getPath()).size()); tikhomirov@588: errorCollector.assertTrue(status.get(dfB.getPath()).contains(Kind.Clean)); tikhomirov@586: tikhomirov@586: HgDataFile dfD = hgRepo.getFileNode("d"); tikhomirov@586: assertTrue("[sanity]", dfD.exists()); tikhomirov@586: File fileD = new File(repoLoc, "d"); tikhomirov@586: assertTrue("[sanity]", fileD.canRead()); tikhomirov@586: // tikhomirov@586: RepoUtils.modifyFileAppend(fileD, " 1 \n"); tikhomirov@586: cmd = new HgCommitCommand(hgRepo); tikhomirov@586: assertFalse(cmd.isMergeCommit()); tikhomirov@586: r = cmd.message("SECOND").execute(); tikhomirov@588: errorCollector.assertTrue(r.isOk()); tikhomirov@586: Nodeid c2 = cmd.getCommittedRevision(); tikhomirov@586: // tikhomirov@636: errorCollector.assertEquals("SECOND", hgRepo.getCommitLastMessage()); tikhomirov@636: // tikhomirov@586: int lastRev = hgRepo.getChangelog().getLastRevision(); tikhomirov@586: List csets = new HgLogCommand(hgRepo).range(lastRev-1, lastRev).execute(); tikhomirov@588: errorCollector.assertEquals(csets.get(0).getNodeid(), c1); tikhomirov@588: errorCollector.assertEquals(csets.get(1).getNodeid(), c2); tikhomirov@588: errorCollector.assertEquals(csets.get(0).getComment(), "FIRST"); tikhomirov@588: errorCollector.assertEquals(csets.get(1).getComment(), "SECOND"); tikhomirov@586: assertHgVerifyOk(repoLoc); tikhomirov@652: // new commits are drafts by default, check our commit respects this tikhomirov@652: // TODO more tests with children of changesets with draft, secret or public phases (latter - tikhomirov@652: // new commit is child of public, but there are other commits with draft/secret phases - ensure they are intact) tikhomirov@652: assertEquals(HgPhase.Draft, HgPhase.parse(hgRepo.getConfiguration().getStringValue("phases", "new-commit", HgPhase.Draft.mercurialString()))); tikhomirov@652: errorCollector.assertEquals(HgPhase.Draft, csets.get(0).getPhase()); tikhomirov@652: errorCollector.assertEquals(HgPhase.Draft, csets.get(1).getPhase()); tikhomirov@586: } tikhomirov@586: tikhomirov@605: @Test tikhomirov@605: public void testUpdateActiveBookmark() throws Exception { tikhomirov@612: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-commit-bookmark-update", false); tikhomirov@605: ExecHelper eh = new ExecHelper(new OutputParser.Stub(), repoLoc); tikhomirov@605: String activeBookmark = "bm1"; tikhomirov@605: eh.run("hg", "bookmarks", activeBookmark); tikhomirov@605: tikhomirov@605: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@605: assertEquals("[sanity]", activeBookmark, hgRepo.getBookmarks().getActiveBookmarkName()); tikhomirov@605: Nodeid activeBookmarkRevision = hgRepo.getBookmarks().getRevision(activeBookmark); tikhomirov@605: assertEquals("[sanity]", activeBookmarkRevision, hgRepo.getWorkingCopyParents().first()); tikhomirov@605: tikhomirov@605: HgDataFile dfD = hgRepo.getFileNode("d"); tikhomirov@605: File fileD = new File(repoLoc, "d"); tikhomirov@605: assertTrue("[sanity]", dfD.exists()); tikhomirov@605: assertTrue("[sanity]", fileD.canRead()); tikhomirov@605: tikhomirov@605: RepoUtils.modifyFileAppend(fileD, " 1 \n"); tikhomirov@605: HgCommitCommand cmd = new HgCommitCommand(hgRepo).message("FIRST"); tikhomirov@605: Outcome r = cmd.execute(); tikhomirov@605: errorCollector.assertTrue(r.isOk()); tikhomirov@605: Nodeid c = cmd.getCommittedRevision(); tikhomirov@605: tikhomirov@605: errorCollector.assertEquals(activeBookmark, hgRepo.getBookmarks().getActiveBookmarkName()); tikhomirov@605: errorCollector.assertEquals(c, hgRepo.getBookmarks().getRevision(activeBookmark)); tikhomirov@605: // reload repo, and repeat the check tikhomirov@605: hgRepo = new HgLookup().detect(repoLoc); tikhomirov@605: errorCollector.assertEquals(activeBookmark, hgRepo.getBookmarks().getActiveBookmarkName()); tikhomirov@605: errorCollector.assertEquals(c, hgRepo.getBookmarks().getRevision(activeBookmark)); tikhomirov@605: } tikhomirov@612: tikhomirov@624: /** tikhomirov@624: * from the wiki: tikhomirov@624: * "active bookmarks are automatically updated when committing to the changeset they are pointing to" tikhomirov@624: * Synopsis: commit 1 (c1), hg bookmark active (points to commit1), make commit 2, hg bookmark -f -r c1 active, commit 3, check active still points to c1 tikhomirov@624: */ tikhomirov@624: @Test tikhomirov@624: public void testNoBookmarkUpdate() throws Exception { tikhomirov@624: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-no-bookmark-upd", false); tikhomirov@624: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@624: assertNull("[sanity]", hgRepo.getBookmarks().getActiveBookmarkName()); tikhomirov@624: ExecHelper eh = new ExecHelper(new OutputParser.Stub(), repoLoc); tikhomirov@624: String activeBookmark = "bm1"; tikhomirov@624: eh.run("hg", "bookmarks", activeBookmark); tikhomirov@624: assertEquals("Bookmarks has to reload", activeBookmark, hgRepo.getBookmarks().getActiveBookmarkName()); tikhomirov@624: Nodeid initialBookmarkRevision = hgRepo.getBookmarks().getRevision(activeBookmark); // c1 tikhomirov@624: assertEquals("[sanity]", initialBookmarkRevision, hgRepo.getWorkingCopyParents().first()); tikhomirov@624: tikhomirov@624: File fileD = new File(repoLoc, "d"); tikhomirov@624: assertTrue("[sanity]", fileD.canRead()); tikhomirov@624: RepoUtils.modifyFileAppend(fileD, " 1 \n"); tikhomirov@624: HgCommitCommand cmd = new HgCommitCommand(hgRepo).message("FIRST"); tikhomirov@624: Outcome r = cmd.execute(); tikhomirov@624: errorCollector.assertTrue(r.isOk()); tikhomirov@624: Nodeid c2 = cmd.getCommittedRevision(); tikhomirov@624: errorCollector.assertEquals(c2, hgRepo.getBookmarks().getRevision(activeBookmark)); tikhomirov@624: // tikhomirov@626: if (!Internals.runningOnWindows()) { tikhomirov@626: // need change to happen not the same moment as the last commit (and read of bookmark file) tikhomirov@626: Thread.sleep(1000); // XXX remove once better file change detection in place tikhomirov@626: } tikhomirov@624: eh.run("hg", "bookmark", activeBookmark, "--force", "--rev", initialBookmarkRevision.toString()); tikhomirov@624: // tikhomirov@624: RepoUtils.modifyFileAppend(fileD, " 2 \n"); tikhomirov@624: cmd = new HgCommitCommand(hgRepo).message("SECOND"); tikhomirov@624: r = cmd.execute(); tikhomirov@624: errorCollector.assertTrue(r.isOk()); tikhomirov@624: //Nodeid c3 = cmd.getCommittedRevision(); tikhomirov@624: errorCollector.assertEquals(initialBookmarkRevision, hgRepo.getBookmarks().getRevision(activeBookmark)); tikhomirov@624: } tikhomirov@624: tikhomirov@612: @Test tikhomirov@612: public void testRefreshTagsAndBranches() throws Exception { tikhomirov@612: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-branches", "test-refresh-after-commit", false); tikhomirov@612: final String tag = "tag.refresh", branch = "branch-refresh"; tikhomirov@612: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@612: assertFalse(hgRepo.getTags().getAllTags().containsKey(tag)); tikhomirov@612: assertNull(hgRepo.getBranches().getBranch(branch)); tikhomirov@612: RepoUtils.modifyFileAppend(new File(repoLoc, "a"), "whatever"); tikhomirov@612: // tikhomirov@612: final int parentCsetRevIndex = hgRepo.getChangelog().getLastRevision(); tikhomirov@612: // HgCommitCommand can't do branch yet tikhomirov@612: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), parentCsetRevIndex); tikhomirov@618: cf.add(hgRepo.getFileNode("a"), new FileContentSupplier(hgRepo, new File(repoLoc, "a"))); tikhomirov@612: cf.branch(branch); tikhomirov@617: Transaction tr = newTransaction(hgRepo); tikhomirov@617: Nodeid commit = cf.commit("FIRST", tr); tikhomirov@617: tr.commit(); tikhomirov@612: errorCollector.assertEquals("commit with branch shall update WC", branch, hgRepo.getWorkingCopyBranchName()); tikhomirov@612: tikhomirov@612: ExecHelper eh = new ExecHelper(new OutputParser.Stub(), repoLoc); tikhomirov@612: eh.run("hg", "tag", tag); tikhomirov@612: assertEquals("[sanity]", 0, eh.getExitValue()); tikhomirov@612: tikhomirov@612: errorCollector.assertTrue(hgRepo.getTags().getAllTags().containsKey(tag)); tikhomirov@612: errorCollector.assertFalse(hgRepo.getBranches().getBranch(branch) == null); tikhomirov@612: errorCollector.assertTrue(hgRepo.getTags().tagged(tag).contains(commit)); tikhomirov@612: errorCollector.assertTrue(hgRepo.getTags().tags(commit).contains(tag)); tikhomirov@612: } tikhomirov@605: tikhomirov@621: @Test tikhomirov@621: public void testAddedFilesGetStream() throws Exception { tikhomirov@621: File repoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-commit-addfile-stream", false); tikhomirov@621: final File newFile = new File(repoLoc, "xx"); tikhomirov@621: final byte[] newFileContent = "xyz".getBytes(); tikhomirov@621: RepoUtils.createFile(newFile, newFileContent); tikhomirov@621: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@621: new HgAddRemoveCommand(hgRepo).add(Path.create("xx")).execute(); tikhomirov@621: // save the reference to HgDataFile without valid RevlogStream (entry in the dirstate tikhomirov@621: // doesn't make it valid) tikhomirov@621: final HgDataFile newFileNode = hgRepo.getFileNode("xx"); tikhomirov@621: assertFalse(newFileNode.exists()); tikhomirov@621: HgCommitCommand cmd = new HgCommitCommand(hgRepo).message("FIRST"); tikhomirov@621: Outcome r = cmd.execute(); tikhomirov@621: errorCollector.assertTrue(r.isOk()); tikhomirov@621: TestStatus.StatusCollector status = new TestStatus.StatusCollector(); tikhomirov@621: new HgStatusCommand(hgRepo).all().execute(status); tikhomirov@621: errorCollector.assertTrue(status.getErrors().isEmpty()); tikhomirov@621: errorCollector.assertTrue(status.get(Kind.Added).isEmpty()); tikhomirov@621: errorCollector.assertTrue(status.get(newFileNode.getPath()).contains(Kind.Clean)); tikhomirov@621: // tikhomirov@621: errorCollector.assertTrue(newFileNode.exists()); tikhomirov@621: final ByteArrayChannel read1 = new ByteArrayChannel(); tikhomirov@621: newFileNode.content(0, read1); tikhomirov@621: errorCollector.assertEquals("Read from existing HgDataFile instance", newFileContent, read1.toArray()); tikhomirov@621: final ByteArrayChannel read2 = new ByteArrayChannel(); tikhomirov@621: hgRepo.getFileNode(newFileNode.getPath()).content(0, read2); tikhomirov@621: errorCollector.assertEquals("Read from fresh HgDataFile instance", newFileContent, read2.toArray()); tikhomirov@621: } tikhomirov@621: tikhomirov@621: @Test tikhomirov@621: public void testRollback() throws Exception { tikhomirov@623: // Important: copy, not a clone of a repo to ensure old timestamps tikhomirov@623: // on repository files. Otherwise, there're chances transacition.rollback() tikhomirov@623: // would happen the very second (when fs timestamp granularity is second) tikhomirov@623: // repository got cloned, and RevlogChangeMonitor won't notice the file change tikhomirov@623: // (timestamp is the same, file size increased (CommitFacility) and decreased tikhomirov@623: // on rollback back to memorized value), and subsequent hgRepo access would fail tikhomirov@623: // trying to read more (due to Revlog#revisionAdded) revisions than there are in tikhomirov@623: // the store file. tikhomirov@623: // With copy and original timestamps we pretend commit happens to an existing repository tikhomirov@623: // in a regular manner (it's unlikely to have commits within the same second in a real life) tikhomirov@623: // XXX Note, once we have more robust method to detect file changes (e.g. Java7), this tikhomirov@623: // approach shall be abandoned. tikhomirov@623: File repoLoc = RepoUtils.copyRepoToTempLocation("log-1", "test-commit-rollback"); tikhomirov@621: final Path newFilePath = Path.create("xx"); tikhomirov@621: final File newFile = new File(repoLoc, newFilePath.toString()); tikhomirov@621: RepoUtils.createFile(newFile, "xyz"); tikhomirov@621: HgRepository hgRepo = new HgLookup().detect(repoLoc); tikhomirov@621: HgDataFile dfB = hgRepo.getFileNode("b"); tikhomirov@621: HgDataFile dfD = hgRepo.getFileNode("d"); tikhomirov@621: assertTrue("[sanity]", dfB.exists()); tikhomirov@621: assertTrue("[sanity]", dfD.exists()); tikhomirov@621: final File modifiedFile = new File(repoLoc, "b"); tikhomirov@621: RepoUtils.modifyFileAppend(modifiedFile, " 1 \n"); tikhomirov@621: // tikhomirov@621: new HgAddRemoveCommand(hgRepo).add(newFilePath).remove(dfD.getPath()).execute(); tikhomirov@621: // tikhomirov@621: TestStatus.StatusCollector status = new TestStatus.StatusCollector(); tikhomirov@621: new HgStatusCommand(hgRepo).all().execute(status); tikhomirov@621: assertTrue(status.getErrors().isEmpty()); tikhomirov@621: assertTrue(status.get(Kind.Added).contains(newFilePath)); tikhomirov@621: assertTrue(status.get(Kind.Modified).contains(dfB.getPath())); tikhomirov@621: assertTrue(status.get(Kind.Removed).contains(dfD.getPath())); tikhomirov@621: assertEquals(DEFAULT_BRANCH_NAME, hgRepo.getWorkingCopyBranchName()); tikhomirov@621: // tikhomirov@621: final int lastClogRevision = hgRepo.getChangelog().getLastRevision(); tikhomirov@621: final int lastManifestRev = hgRepo.getManifest().getLastRevision(); tikhomirov@621: CommitFacility cf = new CommitFacility(Internals.getInstance(hgRepo), lastClogRevision); tikhomirov@621: cf.add(hgRepo.getFileNode("xx"), new FileContentSupplier(hgRepo, newFile)); tikhomirov@621: cf.add(dfB, new FileContentSupplier(hgRepo, modifiedFile)); tikhomirov@621: cf.forget(dfD); tikhomirov@621: cf.branch("another-branch"); tikhomirov@621: Transaction tr = newTransaction(hgRepo); tikhomirov@621: Nodeid commitRev = cf.commit("Commit to fail", tr); tikhomirov@621: tr.rollback(); tikhomirov@621: // tikhomirov@621: errorCollector.assertEquals(lastClogRevision, hgRepo.getChangelog().getLastRevision()); tikhomirov@621: errorCollector.assertEquals(lastManifestRev, hgRepo.getManifest().getLastRevision()); tikhomirov@621: errorCollector.assertEquals(DEFAULT_BRANCH_NAME, DirstateReader.readBranch(Internals.getInstance(hgRepo))); tikhomirov@621: errorCollector.assertFalse(hgRepo.getChangelog().isKnown(commitRev)); tikhomirov@621: errorCollector.assertFalse(hgRepo.getFileNode("xx").exists()); tikhomirov@621: // check dirstate tikhomirov@621: status = new TestStatus.StatusCollector(); tikhomirov@621: new HgStatusCommand(hgRepo).all().execute(status); tikhomirov@621: errorCollector.assertTrue(status.getErrors().isEmpty()); tikhomirov@621: errorCollector.assertTrue(status.get(Kind.Added).contains(newFilePath)); tikhomirov@621: errorCollector.assertTrue(status.get(Kind.Modified).contains(dfB.getPath())); tikhomirov@621: errorCollector.assertTrue(status.get(Kind.Removed).contains(dfD.getPath())); tikhomirov@621: tikhomirov@621: assertHgVerifyOk(repoLoc); tikhomirov@621: } tikhomirov@621: tikhomirov@559: private void assertHgVerifyOk(File repoLoc) throws InterruptedException, IOException { tikhomirov@559: ExecHelper verifyRun = new ExecHelper(new OutputParser.Stub(), repoLoc); tikhomirov@559: verifyRun.run("hg", "verify"); tikhomirov@588: errorCollector.assertEquals("hg verify", 0, verifyRun.getExitValue()); tikhomirov@538: } tikhomirov@617: tikhomirov@617: private Transaction newTransaction(SessionContext.Source ctxSource) { tikhomirov@617: return trFactory.create(ctxSource); tikhomirov@617: } tikhomirov@539: tikhomirov@538: public static void main(String[] args) throws Exception { tikhomirov@539: new TestCommit().testCommitToEmpty(); tikhomirov@534: } tikhomirov@534: }