diff options
3 files changed, 376 insertions, 63 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java new file mode 100644 index 0000000000..663b745fb2 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestRepository.java @@ -0,0 +1,205 @@ +/******************************************************************************* + * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.egit.core.test; + +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.core.resources.IFile; +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.ConcurrentRefUpdateException; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.JGitInternalException; +import org.eclipse.jgit.api.NoHeadException; +import org.eclipse.jgit.api.NoMessageException; +import org.eclipse.jgit.api.WrongRepositoryStateException; +import org.eclipse.jgit.errors.UnmergedPathException; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.GitIndex; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefUpdate; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.Tree; +import org.eclipse.jgit.lib.GitIndex.Entry; +import org.eclipse.jgit.revwalk.RevCommit; + +/** + * Helper class for creating and filling a test repository + * + */ +public class TestRepository { + + Repository repository; + String workdirPrefix; + + /** + * Creates a new test repository + * + * @param gitDir + * @throws IOException + */ + public TestRepository(File gitDir) throws IOException { + repository = new Repository(gitDir); + repository.create(); + try { + workdirPrefix = repository.getWorkDir().getCanonicalPath(); + } catch (IOException err) { + workdirPrefix = repository.getWorkDir().getAbsolutePath(); + } + workdirPrefix = workdirPrefix.replace('\\', '/'); + if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$ + workdirPrefix += "/"; //$NON-NLS-1$ + } + + /** + * @return the wrapped repository + */ + public Repository getRepository() { + return repository; + } + + /** + * create an initial commit containing a file "dummy" in the + * + * @param message + * commit message + * @return commit object + * @throws IOException + * @throws NoHeadException + * @throws NoMessageException + * @throws ConcurrentRefUpdateException + * @throws JGitInternalException + * @throws WrongRepositoryStateException + */ + public RevCommit createInitialCommit(String message) throws IOException, + NoHeadException, NoMessageException, ConcurrentRefUpdateException, + JGitInternalException, WrongRepositoryStateException { + String repoPath = repository.getWorkDir().getAbsolutePath(); + File file = new File(repoPath, "dummy"); + file.createNewFile(); + track(file); + return commit(message); + } + + /** + * Commits the current index + * + * @param message + * commit message + * @return commit object + * + * @throws NoHeadException + * @throws NoMessageException + * @throws UnmergedPathException + * @throws ConcurrentRefUpdateException + * @throws JGitInternalException + * @throws WrongRepositoryStateException + */ + public RevCommit commit(String message) throws NoHeadException, + NoMessageException, UnmergedPathException, + ConcurrentRefUpdateException, JGitInternalException, + WrongRepositoryStateException { + Git git = new Git(repository); + CommitCommand commitCommand = git.commit(); + commitCommand.setAuthor("J. Git", "j.git@egit.org"); + commitCommand.setCommitter(commitCommand.getAuthor()); + commitCommand.setMessage(message); + return commitCommand.call(); + } + + /** + * Adds file to version control + * + * @param file + * @throws IOException + */ + public void track(File file) throws IOException { + GitIndex index = repository.getIndex(); + Entry entry = index.add(repository.getWorkDir(), file); + entry.setAssumeValid(false); + index.write(); + } + + /** + * Creates a new branch + * + * @param refName + * starting point for the new branch + * @param newRefName + * @throws IOException + */ + public void createBranch(String refName, String newRefName) + throws IOException { + RefUpdate updateRef; + updateRef = repository.updateRef(newRefName); + Ref startRef = repository.getRef(refName); + ObjectId startAt = repository.resolve(refName); + String startBranch; + if (startRef != null) + startBranch = refName; + else + startBranch = startAt.name(); + startBranch = repository.shortenRefName(startBranch); + updateRef.setNewObjectId(startAt); + updateRef + .setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$ + updateRef.update(); + } + + /** + * Adds the given file to the index + * @param file + * @throws IOException + */ + public void addToIndex(IFile file) throws IOException { + GitIndex index = repository.getIndex(); + Entry entry = index.getEntry(getRepoRelativePath(file.getLocation().toOSString())); + assertNotNull(entry); + if (entry.isModified(repository.getWorkDir())) + entry.update(new File(repository.getWorkDir(), entry.getName())); + index.write(); + } + + /** + * Checks if a file with the given path exists in the HEAD tree + * @param path + * @return true if the file exists + * @throws IOException + */ + public boolean inHead(String path) throws IOException { + Tree headTree = repository.mapTree(Constants.HEAD); + String repoPath = getRepoRelativePath(path); + boolean headExists = headTree.existsBlob(repoPath); + return headExists; + } + + public boolean inIndex(String path) throws IOException { + String repoPath = getRepoRelativePath(path); + GitIndex index = repository.getIndex(); + return index.getEntry(repoPath) != null; + } + + public String getRepoRelativePath(String path) { + final int pfxLen = workdirPrefix.length(); + final int pLen = path.length(); + if (pLen > pfxLen) + return path.substring(pfxLen); + else if (path.length() == pfxLen - 1) + return ""; //$NON-NLS-1$ + return null; + } + + public void dispose() { + repository.close(); + repository = null; + } +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0004_BranchOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0004_BranchOperationTest.java index a40e73749f..5b11666095 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0004_BranchOperationTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0004_BranchOperationTest.java @@ -1,28 +1,23 @@ +/******************************************************************************* + * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ package org.eclipse.egit.core.test.op; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.IOException; import org.eclipse.egit.core.op.BranchOperation; import org.eclipse.egit.core.test.GitTestCase; -import org.eclipse.jgit.api.CommitCommand; -import org.eclipse.jgit.api.ConcurrentRefUpdateException; -import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.JGitInternalException; -import org.eclipse.jgit.api.NoHeadException; -import org.eclipse.jgit.api.NoMessageException; -import org.eclipse.jgit.api.WrongRepositoryStateException; -import org.eclipse.jgit.errors.UnmergedPathException; +import org.eclipse.egit.core.test.TestRepository; import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.GitIndex; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Ref; -import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.GitIndex.Entry; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -31,36 +26,36 @@ public class T0004_BranchOperationTest extends GitTestCase{ private static final String TEST = Constants.R_HEADS + "test"; private static final String MASTER = Constants.R_HEADS + "master"; + TestRepository testRepository; Repository repository; @Before public void setUp() throws Exception { super.setUp(); - repository = new Repository(gitDir); - repository.create(); + testRepository = new TestRepository(gitDir); + repository = testRepository.getRepository(); } @After public void tearDown() throws Exception { - repository.close(); + testRepository.dispose(); repository = null; super.tearDown(); } - @Test public void testBranchOperation() throws Exception { // create first commit containing a dummy file - createInitialCommit(); + testRepository.createInitialCommit("testBranchOperation\n\nfirst commit\n"); // create branch test and switch to branch test - createBranch(MASTER, TEST); + testRepository.createBranch(MASTER, TEST); new BranchOperation(repository, TEST).execute(null); assertTrue(repository.getFullBranch().equals(TEST)); // add .project to version control and commit String path = project.getProject().getLocation().append(".project").toOSString(); File file = new File(path); - track(file); - commit("Add .project file"); + testRepository.track(file); + testRepository.commit("Add .project file"); // switch back to master branch // .project must disappear, related Eclipse project must be deleted new BranchOperation(repository, MASTER).execute(null); @@ -71,45 +66,4 @@ public class T0004_BranchOperationTest extends GitTestCase{ new BranchOperation(repository, TEST).execute(null); assertTrue(file.exists()); } - - private void createInitialCommit() throws IOException, NoHeadException, NoMessageException, ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException { - String repoPath = project.getProject().getWorkspace().getRoot().getLocation().toOSString(); - File file = new File(repoPath, "dummy"); - file.createNewFile(); - track(file); - commit("testBranchOperation\n\nfirst commit\n"); - } - - private void createBranch(String refName, String newRefName) throws IOException { - RefUpdate updateRef; - updateRef = repository.updateRef(newRefName); - Ref startRef = repository.getRef(refName); - ObjectId startAt = repository.resolve(refName); - String startBranch; - if (startRef != null) - startBranch = refName; - else - startBranch = startAt.name(); - startBranch = repository.shortenRefName(startBranch); - updateRef.setNewObjectId(startAt); - updateRef.setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$ - updateRef.update(); - } - - private void track(File file) throws IOException { - GitIndex index = repository.getIndex(); - Entry entry = index.add(repository.getWorkDir(), file); - entry.setAssumeValid(false); - index.write(); - } - - private void commit(String message) throws NoHeadException, NoMessageException, UnmergedPathException, ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException { - Git git = new Git(repository); - CommitCommand commitCommand = git.commit(); - commitCommand.setAuthor("J. Git", "j.git@egit.org"); - commitCommand.setCommitter(commitCommand.getAuthor()); - commitCommand.setMessage(message); - commitCommand.call(); - } - } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0005_ResetOperationTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0005_ResetOperationTest.java new file mode 100644 index 0000000000..59adaaf23f --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/op/T0005_ResetOperationTest.java @@ -0,0 +1,154 @@ +/******************************************************************************* + * Copyright (C) 2010, Jens Baumgart <jens.baumgart@sap.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ +package org.eclipse.egit.core.test.op; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.InputStream; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.egit.core.op.ResetOperation; +import org.eclipse.egit.core.test.GitTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class T0005_ResetOperationTest extends GitTestCase { + + TestRepository testRepository; + + Repository repository; + + // members filled by setupRepository() + RevCommit initialCommit; + + File projectFile; + + IFile untrackedFile; + + IFile fileInIndex; + + @Before + public void setUp() throws Exception { + super.setUp(); + testRepository = new TestRepository(gitDir); + repository = testRepository.getRepository(); + } + + @After + public void tearDown() throws Exception { + testRepository.dispose(); + repository = null; + super.tearDown(); + } + + @Test + public void testHardReset() throws Exception { + setupRepository(); + String fileInIndexPath = fileInIndex.getLocation().toOSString(); + new ResetOperation(repository, initialCommit.getName(), + ResetOperation.ResetType.HARD).execute(null); + // .project must disappear, related Eclipse project must be deleted + assertFalse(projectFile.exists()); + assertFalse(project.getProject().exists()); + // check if HEAD points to initial commit now + assertTrue(repository.resolve("HEAD").equals(initialCommit)); + // check if files were removed + assertFalse(untrackedFile.exists()); + assertFalse(fileInIndex.exists()); + // fileInIndex must no longer be in HEAD and in the index + assertFalse(testRepository.inHead(fileInIndexPath)); + assertFalse(testRepository.inIndex(fileInIndexPath)); + } + + @Test + public void testSoftReset() throws Exception { + setupRepository(); + String fileInIndexPath = fileInIndex.getLocation().toOSString(); + new ResetOperation(repository, initialCommit.getName(), + ResetOperation.ResetType.SOFT).execute(null); + // .project must remain + assertTrue(projectFile.exists()); + assertTrue(project.getProject().exists()); + // check if HEAD points to initial commit now + assertTrue(repository.resolve("HEAD").equals(initialCommit)); + // untrackedFile and fileInIndex must still exist + assertTrue(untrackedFile.exists()); + assertTrue(fileInIndex.exists()); + // fileInIndex must no longer be in HEAD + assertFalse(testRepository.inHead(fileInIndexPath)); + // fileInIndex must exist in the index + assertTrue(testRepository.inIndex(fileInIndexPath)); + } + + @Test + public void testMixedReset() throws Exception { + setupRepository(); + String fileInIndexPath = fileInIndex.getLocation().toOSString(); + new ResetOperation(repository, initialCommit.getName(), + ResetOperation.ResetType.MIXED).execute(null); + // .project must remain + assertTrue(projectFile.exists()); + assertTrue(project.getProject().exists()); + // check if HEAD points to initial commit now + assertTrue(repository.resolve("HEAD").equals(initialCommit)); + // untrackedFile and fileInIndex must still exist + assertTrue(untrackedFile.exists()); + assertTrue(fileInIndex.exists()); + // fileInIndex must no longer be in HEAD + assertFalse(testRepository.inHead(fileInIndexPath)); + // fileInIndex must not in the index + assertFalse(testRepository.inIndex(fileInIndexPath)); + } + + private void setupRepository() throws Exception { + // create first commit containing a dummy file + initialCommit = testRepository + .createInitialCommit("testResetOperation\n\nfirst commit\n"); + // add .project to version control + String path = project.getProject().getLocation().append(".project") + .toOSString(); + projectFile = new File(path); + testRepository.track(projectFile); + // add fileInIndex to version control + fileInIndex = createFile("fileInIndex"); + testRepository.track(new File(fileInIndex.getLocation().toOSString())); + testRepository.commit("Add .project file"); + // modify fileInIndex and add it to the index + InputStream stream = new ByteArrayInputStream(new byte[] { 'I', 'n', + 'd', 'e', 'x' }); + fileInIndex.setContents(stream, 0, null); + testRepository.addToIndex(fileInIndex); + // create an untracked file + untrackedFile = createFile("untrackedFile"); + } + + /** + * create a file with the given name in the root folder of testproject + * + * @param name + * name of file + * @return new file + * @throws CoreException + */ + private IFile createFile(String name) throws CoreException { + IFile file = project.project.getFile(name); + file.create( + new ByteArrayInputStream(new byte[] { 'T', 'e', 's', 't' }), + true, null); + return file; + } +} |