Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Rosenberg2011-09-30 21:29:10 +0000
committerRobin Rosenberg2011-11-13 19:36:49 +0000
commit57b79abd8f486af5f95db42435c374404fa40513 (patch)
tree8be1360a948189b3e247665b73940b6758edcfe0 /org.eclipse.egit.core.test
parentf332331f4c3b5e69be2846213908acc12b3cd109 (diff)
downloadegit-57b79abd8f486af5f95db42435c374404fa40513.tar.gz
egit-57b79abd8f486af5f95db42435c374404fa40513.tar.xz
egit-57b79abd8f486af5f95db42435c374404fa40513.zip
Implement project rename and move for some cases and add tests
We can now move and rename projects within a repository for a number of cases. Moving a project to a subdirectory does not work and since Eclipse actually destroys the project, we simply forbid it for now (Platform bug 307140). The bug is only worked around in Eclipse 3.7, i.e. forbidden in the UI. This also adds tests for move and delete of folders and files. We are quite inconsistent with respect to how we handle the index, i.e. sometime we update the index to reflect the change and sometimes we do not, so the current behavior may not be the final behavior. This version of the patch does not try to evade bug 307140 Requires JGit patch: I7425da91c0752ae82484e3c29d21b57402d30c61 Bug: 334112 Bug: 358703 Change-Id: I634d5b44bc7fc6a7453b5d718733c9f8f3542840 Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Diffstat (limited to 'org.eclipse.egit.core.test')
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/GitMoveDeleteHookTest.java577
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java63
-rw-r--r--org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java18
3 files changed, 647 insertions, 11 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/GitMoveDeleteHookTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/GitMoveDeleteHookTest.java
new file mode 100644
index 0000000000..43af672e62
--- /dev/null
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/GitMoveDeleteHookTest.java
@@ -0,0 +1,577 @@
+/*******************************************************************************
+ * Copyright (C) 2011, Robin Rosenberg
+ *
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.filesystem.URIUtil;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.egit.core.op.AddToIndexOperation;
+import org.eclipse.egit.core.project.RepositoryMapping;
+import org.eclipse.egit.core.test.TestProject;
+import org.eclipse.egit.core.test.TestRepository;
+import org.eclipse.egit.core.test.TestUtils;
+import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.errors.CorruptObjectException;
+import org.eclipse.jgit.junit.MockSystemReader;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepository;
+import org.eclipse.jgit.util.FS;
+import org.eclipse.jgit.util.FileUtils;
+import org.eclipse.jgit.util.SystemReader;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * All sorts of interesting cases
+ */
+public class GitMoveDeleteHookTest {
+
+ TestUtils testUtils = new TestUtils();
+
+ TestRepository testRepository;
+
+ Repository repository;
+
+ List<File> gitDirs = new ArrayList<File>();
+
+ File workspaceSupplement;
+
+ File workspace;
+
+ @Before
+ public void setUp() throws Exception {
+ Activator.getDefault().getRepositoryCache().clear();
+ MockSystemReader mockSystemReader = new MockSystemReader();
+ SystemReader.setInstance(mockSystemReader);
+ mockSystemReader.setProperty(Constants.GIT_CEILING_DIRECTORIES_KEY,
+ ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile()
+ .getAbsoluteFile().toString());
+ workspaceSupplement = testUtils.createTempDir("wssupplement");
+ workspace = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsoluteFile();
+ }
+
+ @After
+ public void tearDown() throws IOException {
+ if (testRepository != null)
+ testRepository.dispose();
+ repository = null;
+ for (File d : gitDirs)
+ if (d.exists())
+ FileUtils.delete(d, FileUtils.RECURSIVE | FileUtils.RETRY);
+ }
+
+ private TestProject initRepoInsideProjectInsideWorkspace() throws IOException,
+ CoreException {
+ TestProject project = new TestProject(true, "Project-1", true, workspaceSupplement);
+ File gitDir = new File(project.getProject().getLocationURI().getPath(),
+ Constants.DOT_GIT);
+ gitDirs.add(gitDir);
+ testRepository = new TestRepository(gitDir);
+ repository = testRepository.getRepository();
+ testRepository.connect(project.getProject());
+ return project;
+ }
+
+ private TestProject initRepoInsideProjectOutsideWorkspace()
+ throws IOException, CoreException {
+ TestProject project = new TestProject(true, "Project-1", false,
+ workspaceSupplement);
+ File gitDir = new File(project.getProject().getLocationURI().getPath(),
+ Constants.DOT_GIT);
+ gitDirs.add(gitDir);
+ testRepository = new TestRepository(gitDir);
+ repository = testRepository.getRepository();
+ testRepository.connect(project.getProject());
+ return project;
+ }
+
+ private TestProject initRepoAboveProjectInsideWs(String srcParent, String d)
+ throws IOException, CoreException {
+ return initRepoAboveProject(srcParent, d, true);
+ }
+
+ private TestProject initRepoAboveProject(String srcParent, String d, boolean insidews)
+ throws IOException, CoreException {
+ TestProject project = new TestProject(true, srcParent + "Project-1", insidews, workspaceSupplement);
+ File gd = new File(insidews?workspace:workspaceSupplement, d);
+
+ File gitDir = new File(gd, Constants.DOT_GIT);
+ gitDirs.add(gitDir);
+ testRepository = new TestRepository(gitDir);
+ repository = testRepository.getRepository();
+ testRepository.connect(project.getProject());
+ return project;
+ }
+
+ @Test
+ public void testDeleteFile() throws Exception {
+ TestProject project = initRepoInsideProjectInsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ testUtils.addFileToProject(project.getProject(), "file2.txt",
+ "some more text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt"),
+ project.getProject().getFile("file2.txt") });
+ addToIndexOperation.execute(null);
+
+ // Validate pre-conditions
+ DirCache dirCache = DirCache.read(repository.getIndexFile(),
+ FS.DETECTED);
+ assertEquals(2, dirCache.getEntryCount());
+ assertNotNull(dirCache.getEntry("file.txt"));
+ assertNotNull(dirCache.getEntry("file2.txt"));
+ // Modify the content before the move
+ testUtils.changeContentOfFile(project.getProject(), project
+ .getProject().getFile("file.txt"), "other text");
+ project.getProject().getFile("file.txt").delete(true, null);
+
+ // Check index for the deleted file
+ dirCache.read();
+ assertEquals(1, dirCache.getEntryCount());
+ assertNull(dirCache.getEntry("file.txt"));
+ assertNotNull(dirCache.getEntry("file2.txt"));
+ // Actual file is deleted
+ assertFalse(project.getProject().getFile("file.txt").exists());
+ // But a non-affected file remains
+ assertTrue(project.getProject().getFile("file2.txt").exists());
+ }
+
+ @Test
+ public void testDeleteFolder() throws Exception {
+ TestProject project = initRepoInsideProjectInsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "folder/file.txt",
+ "some text");
+ testUtils.addFileToProject(project.getProject(), "folder2/file.txt",
+ "some other text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] {
+ project.getProject().getFile("folder/file.txt"),
+ project.getProject().getFile("folder2/file.txt") });
+ addToIndexOperation.execute(null);
+
+ DirCache dirCache = DirCache.read(repository.getIndexFile(),
+ FS.DETECTED);
+ assertNotNull(dirCache.getEntry("folder/file.txt"));
+ assertNotNull(dirCache.getEntry("folder2/file.txt"));
+ // Modify the content before the move
+ testUtils.changeContentOfFile(project.getProject(), project
+ .getProject().getFile("folder/file.txt"), "other text");
+ project.getProject().getFolder("folder").delete(true, null);
+
+ dirCache.read();
+ // Unlike delete file, dircache is untouched... pretty illogical
+ // TODO: Change the behavior of the hook.
+ assertNotNull(dirCache.getEntry("folder/file.txt"));
+ // Not moved file still there
+ assertNotNull(dirCache.getEntry("folder2/file.txt"));
+ }
+
+ @Test
+ public void testDeleteProject() throws Exception {
+ TestProject project = initRepoAboveProjectInsideWs("P/", "");
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt") });
+ addToIndexOperation.execute(null);
+
+ RepositoryMapping mapping = RepositoryMapping.getMapping(project
+ .getProject());
+ IPath gitDirAbsolutePath = mapping.getGitDirAbsolutePath();
+ Repository db = new FileRepository(gitDirAbsolutePath.toFile());
+ DirCache index = DirCache.read(db.getIndexFile(), db.getFS());
+ assertNotNull(index.getEntry("P/Project-1/file.txt"));
+ db.close();
+ db = null;
+ project.getProject().delete(true, null);
+ assertNull(RepositoryMapping.getMapping(project.getProject()));
+ // Check that the repo is still there. Being a bit paranoid we look for
+ // a file
+ assertTrue(gitDirAbsolutePath.toString(),
+ gitDirAbsolutePath.append("HEAD").toFile().exists());
+
+ db = new FileRepository(gitDirAbsolutePath.toFile());
+ index = DirCache.read(db.getIndexFile(), db.getFS());
+ // FIXME: Shouldn't we unstage deleted projects?
+ assertNotNull(index.getEntry("P/Project-1/file.txt"));
+ db.close();
+ }
+
+ @Test
+ public void testMoveFile() throws Exception {
+ TestProject project = initRepoInsideProjectInsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ testUtils.addFileToProject(project.getProject(), "file2.txt",
+ "some more text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt"),
+ project.getProject().getFile("file2.txt") });
+ addToIndexOperation.execute(null);
+
+ // Validate pre-conditions
+ DirCache dirCache = DirCache.read(repository.getIndexFile(),
+ FS.DETECTED);
+ assertNotNull(dirCache.getEntry("file.txt"));
+ assertNotNull(dirCache.getEntry("file2.txt"));
+ assertNull(dirCache.getEntry("data.txt"));
+ assertFalse(project.getProject().getFile("data.txt").exists());
+ ObjectId oldContentId = dirCache.getEntry("file.txt").getObjectId();
+ // Modify the content before the move
+ testUtils.changeContentOfFile(project.getProject(), project
+ .getProject().getFile("file.txt"), "other text");
+ project.getProject()
+ .getFile("file.txt")
+ .move(project.getProject().getFile("data.txt").getFullPath(),
+ false, null);
+
+ dirCache.read();
+ assertTrue(project.getProject().getFile("data.txt").exists());
+ assertNotNull(dirCache.getEntry("data.txt"));
+ // Same content in index as before the move
+ assertEquals(oldContentId, dirCache.getEntry("data.txt").getObjectId());
+
+ // Not moved file still in its old place
+ assertNotNull(dirCache.getEntry("file2.txt"));
+ }
+
+ /**
+ * Rename "folder" to "dir".
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testMoveFolder() throws Exception {
+ TestProject project = initRepoInsideProjectInsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "folder/file.txt",
+ "some text");
+ testUtils.addFileToProject(project.getProject(), "folder2/file.txt",
+ "some other text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] {
+ project.getProject().getFile("folder/file.txt"),
+ project.getProject().getFile("folder2/file.txt") });
+ addToIndexOperation.execute(null);
+
+ DirCache dirCache = DirCache.read(repository.getIndexFile(),
+ FS.DETECTED);
+ assertNotNull(dirCache.getEntry("folder/file.txt"));
+ assertNotNull(dirCache.getEntry("folder2/file.txt"));
+ assertNull(dirCache.getEntry("dir/file.txt"));
+ assertFalse(project.getProject().getFile("dir/file.txt").exists());
+ ObjectId oldContentId = dirCache.getEntry("folder/file.txt")
+ .getObjectId();
+ // Modify the content before the move
+ testUtils.changeContentOfFile(project.getProject(), project
+ .getProject().getFile("folder/file.txt"), "other text");
+ project.getProject()
+ .getFolder("folder")
+ .move(project.getProject().getFolder("dir").getFullPath(),
+ false, null);
+
+ dirCache.read();
+ assertTrue(project.getProject().getFile("dir/file.txt").exists());
+ assertNull(dirCache.getEntry("folder/file.txt"));
+ assertNotNull(dirCache.getEntry("dir/file.txt"));
+ // Same content in index as before the move
+ assertEquals(oldContentId, dirCache.getEntry("dir/file.txt")
+ .getObjectId());
+ // Not moved file still there
+ assertNotNull(dirCache.getEntry("folder2/file.txt"));
+ }
+
+ /**
+ * Rename and move a project in the workspace containing a Git repository.
+ * <p>
+ * The repository will be moved with the project.
+ * Note that there is no way to rename a project in the workspace without
+ * moving it. See https://bugs.eclipse.org/358828 for a discussion.
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testMoveAndRenameProjectContainingGitRepo() throws Exception {
+ ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").delete(true, null);
+ ResourcesPlugin.getWorkspace().getRoot().getProject("P2").delete(true, null);
+
+ TestProject project = initRepoInsideProjectInsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt") });
+ addToIndexOperation.execute(null);
+ IProjectDescription description = project.getProject().getDescription();
+ description.setName("P2");
+ project.getProject().move(description,
+ IResource.FORCE | IResource.SHALLOW, null);
+ IProject project2 = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject("P2");
+ assertNotNull(RepositoryMapping.getMapping(project2.getProject()));
+ Repository movedRepo = RepositoryMapping.getMapping(project2)
+ .getRepository();
+ assertEquals("P2",
+ movedRepo.getDirectory().getParentFile()
+ .getName());
+ DirCache dc = movedRepo.readDirCache();
+ assertEquals(1, dc.getEntryCount());
+ assertEquals("file.txt", dc.getEntry(0).getPathString());
+
+ assertFalse(ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").exists());
+ }
+
+ /**
+ * Rename a project outside the workspace containing a Git repository.
+ * <p>
+ * Note the similarity of the code with {@link #testMoveAndRenameProjectContainingGitRepo()}
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testRenameProjectOutsideWorkspaceContainingGitRepo() throws Exception {
+ ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").delete(true, null);
+ ResourcesPlugin.getWorkspace().getRoot().getProject("P2").delete(true, null);
+ TestProject project = initRepoInsideProjectOutsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt") });
+ addToIndexOperation.execute(null);
+ IProjectDescription description = project.getProject().getDescription();
+ description.setName("P2");
+ project.getProject().move(description,
+ IResource.FORCE | IResource.SHALLOW, null);
+ IProject project2 = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject("P2");
+ assertNotNull(RepositoryMapping.getMapping(project2.getProject()));
+ Repository movedRepo = RepositoryMapping.getMapping(project2)
+ .getRepository();
+ assertEquals("Project-1",
+ movedRepo.getDirectory().getParentFile()
+ .getName());
+ DirCache dc = movedRepo.readDirCache();
+ assertEquals(1, dc.getEntryCount());
+ assertEquals("file.txt", dc.getEntry(0).getPathString());
+
+ assertFalse(ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").exists());
+ }
+
+ /**
+ * Move a project outside the workspace containing a Git repository, but do not rename it.
+ * <p>
+ * Note the similarity of the code with {@link #testMoveAndRenameProjectContainingGitRepo()}
+ *
+ * @throws Exception
+ */
+ @Test
+ public void testMoveButDoNotRenameProjectOutsideWorkspaceContainingGitRepo() throws Exception {
+ ResourcesPlugin.getWorkspace().getRoot().getProject("Project-1").delete(true, null);
+ ResourcesPlugin.getWorkspace().getRoot().getProject("P2").delete(true, null);
+ TestProject project = initRepoInsideProjectOutsideWorkspace();
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt") });
+ addToIndexOperation.execute(null);
+ IProjectDescription description = project.getProject().getDescription();
+ description.setLocationURI(URIUtil.toURI(new Path(new File(project.getWorkspaceSupplement(), "P2").getAbsolutePath())));
+ project.getProject().move(description,
+ IResource.FORCE | IResource.SHALLOW, null);
+ IProject project2 = ResourcesPlugin.getWorkspace().getRoot()
+ .getProject("Project-1"); // same name
+ assertNotNull(RepositoryMapping.getMapping(project2.getProject()));
+ Repository movedRepo = RepositoryMapping.getMapping(project2)
+ .getRepository();
+ assertEquals("P2",
+ movedRepo.getDirectory().getParentFile()
+ .getName());
+ DirCache dc = movedRepo.readDirCache();
+ assertEquals(1, dc.getEntryCount());
+ assertEquals("file.txt", dc.getEntry(0).getPathString());
+
+ assertFalse(ResourcesPlugin.getWorkspace().getRoot().getProject("P2").exists());
+ }
+
+
+ @Test
+ public void testMoveProjectWithinGitRepoMoveAtSameTopLevel()
+ throws Exception {
+ dotestMoveProjectWithinRepoWithinWorkspace("", "Project-1", "", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitRepoMoveFromTopOneLevelDown()
+ throws Exception {
+ dotestMoveProjectWithinRepoWithinWorkspace("", "Project-1", "X/", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitRepoMoveFromOneLevelDownToTop()
+ throws Exception {
+ dotestMoveProjectWithinRepoWithinWorkspace("P/", "Project-1", "", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitRepoMoveFromOneLevelDownToSameDepth()
+ throws Exception {
+ dotestMoveProjectWithinRepoWithinWorkspace("P/", "Project-1", "X/", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitRepoMoveFromOneLevelDownOutsideTheRepo()
+ throws Exception {
+ dotestMoveProjectWithinRepoWithinWorkspace("P/", "Project-1", "P/", "P2", "P/");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitOutsideWorkspaceRepoMoveAtSameTopLevel()
+ throws Exception {
+ dotestMoveProjectWithinRepoOutsideWorkspace("", "Project-1", "", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitOutsideWorkspaceRepoMoveFromTopOneLevelDown()
+ throws Exception {
+ dotestMoveProjectWithinRepoOutsideWorkspace("", "Project-1", "X/", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitOutsideWorkspaceRepoMoveFromOneLevelDownToTop()
+ throws Exception {
+ dotestMoveProjectWithinRepoOutsideWorkspace("P/", "Project-1", "", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitOutsideWorkspaceRepoMoveFromOneLevelDownToSameDepth()
+ throws Exception {
+ dotestMoveProjectWithinRepoOutsideWorkspace("P/", "Project-1", "X/", "P2", "");
+ }
+
+ @Test
+ public void testMoveProjectWithinGitOutsideWorkspaceRepoMoveFromOneLevelDownOutsideTheRepo()
+ throws Exception {
+ dotestMoveProjectWithinRepoOutsideWorkspace("P/", "Project-1", "P/", "P2", "P/");
+ }
+
+
+ @Test
+ public void testMoveProjectWithinGitRepoMoveFromLevelZeroDownOne()
+ throws Exception {
+ // In this case we'd expect the project to move, but not the repository
+ // TODO: Eclipse cannot do this even without the Git plugin either,
+ // TODO: See Bug 307140)
+ try {
+ dotestMoveProjectWithinRepoWithinWorkspace("P/", "Project-1",
+ "P/Project-1/", "P2", "P/Project-1/");
+ if (!"true".equals(System.getProperty("egit.assume_307140_fixed")))
+ fail("ResourceException expected, core functionality dangerously broken and therefore forbidden");
+ } catch (CoreException e) {
+ if ("true".equals(System.getProperty("egit.assume_307140_fixed")))
+ throw e;
+ }
+ }
+
+ private void dotestMoveProjectWithinRepoWithinWorkspace(String srcParent,
+ String srcProjectName, String dstParent, String dstProjecName,
+ String gitDir) throws CoreException, IOException, Exception,
+ CorruptObjectException {
+ dotestMoveProjectWithinRepo(srcParent, srcProjectName, dstParent, dstProjecName, gitDir, true);
+ }
+
+ private void dotestMoveProjectWithinRepoOutsideWorkspace(String srcParent,
+ String srcProjectName, String dstParent, String dstProjecName,
+ String gitDir) throws CoreException, IOException, Exception,
+ CorruptObjectException {
+ dotestMoveProjectWithinRepo(srcParent, srcProjectName, dstParent, dstProjecName, gitDir, false);
+ }
+
+ private void dotestMoveProjectWithinRepo(String srcParent,
+ String srcProjectName, String dstParent, String dstProjecName,
+ String gitDir, boolean sourceInsideWs) throws IOException, CoreException {
+ String gdRelativeSrcParent = srcParent + srcProjectName + "/";
+ if (gdRelativeSrcParent.startsWith(gitDir))
+ gdRelativeSrcParent = gdRelativeSrcParent
+ .substring(gitDir.length());
+ String gdRelativeDstParent = dstParent + dstProjecName + "/";
+ if (gdRelativeDstParent.startsWith(gitDir))
+ gdRelativeDstParent = gdRelativeDstParent
+ .substring(gitDir.length());
+
+ // Old cruft may be laying around
+ TestProject project = initRepoAboveProject(srcParent, gitDir, sourceInsideWs);
+ IProject project0 = project.getProject().getWorkspace().getRoot()
+ .getProject(dstProjecName);
+ project0.delete(true, null);
+
+ testUtils.addFileToProject(project.getProject(), "file.txt",
+ "some text");
+ AddToIndexOperation addToIndexOperation = new AddToIndexOperation(
+ new IResource[] { project.getProject().getFile("file.txt") });
+ addToIndexOperation.execute(null);
+
+ // Check condition before move
+ DirCache dirCache = DirCache.read(repository.getIndexFile(),
+ FS.DETECTED);
+ assertNotNull(dirCache.getEntry(gdRelativeSrcParent + "file.txt"));
+ ObjectId oldContentId = dirCache.getEntry(
+ gdRelativeSrcParent + "file.txt").getObjectId();
+
+ // Modify the content before the move, we want to see the staged content
+ // as it was before the move in the index
+ testUtils.changeContentOfFile(project.getProject(), project
+ .getProject().getFile("file.txt"), "other text");
+ IProjectDescription description = project.getProject().getDescription();
+ description.setName(dstProjecName);
+ if (sourceInsideWs)
+ if (dstParent.length() > 0)
+ description.setLocationURI(URIUtil.toURI(project.getProject()
+ .getWorkspace().getRoot().getLocation()
+ .append(dstParent + dstProjecName)));
+ else
+ description.setLocationURI(null);
+ else
+ description.setLocationURI(URIUtil.toURI(new Path(workspaceSupplement + "/" + dstParent + "/" + dstProjecName)));
+ project.getProject().move(description,
+ IResource.FORCE | IResource.SHALLOW, null);
+ IProject project2 = project.getProject().getWorkspace().getRoot()
+ .getProject(dstProjecName);
+ assertTrue(project2.exists());
+ assertNotNull(RepositoryMapping.getMapping(project2));
+
+ // Check that our file exists on disk has a new location in the index
+ dirCache.read();
+ assertTrue(project2.getFile("file.txt").exists());
+ assertNotNull(dirCache.getEntry(gdRelativeDstParent + "file.txt"));
+
+ // Same content in index as before the move, i.e. not same as on disk
+ assertEquals(oldContentId,
+ dirCache.getEntry(gdRelativeDstParent + "file.txt")
+ .getObjectId());
+ }
+}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java
index b91adb709c..1468b1c8e5 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestProject.java
@@ -13,8 +13,10 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.net.URI;
import java.net.URL;
+import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
@@ -46,6 +48,8 @@ public class TestProject {
private String location;
private TestUtils testUtils = new TestUtils();
+ private final File workspaceSupplement;
+
/**
* @throws CoreException
* If project already exists
@@ -61,17 +65,35 @@ public class TestProject {
/**
* @param remove
* should project be removed if already exists
- * @param projectName
+ * @param path
+ * @throws CoreException
+ */
+ public TestProject(final boolean remove, String path) throws CoreException {
+ this(remove, path, true, null);
+ }
+
+ /**
+ * @param remove
+ * should project be removed if already exists
+ * @param path
+ * @param insidews set false to create in temp
+ * @param workspaceSupplement
* @throws CoreException
*/
- public TestProject(final boolean remove, String projectName) throws CoreException {
+ public TestProject(final boolean remove, String path, boolean insidews, File workspaceSupplement) throws CoreException {
+ this.workspaceSupplement = workspaceSupplement;
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
- project = root.getProject(projectName);
+ IProjectDescription description = createDescription(remove, path,
+ insidews, root, workspaceSupplement);
+ project = root.getProject(description.getName());
if (remove)
project.delete(true, null);
- project.create(null);
+ IPath locationBefore = description.getLocation();
+ if (locationBefore == null)
+ locationBefore = root.getRawLocation().append(path);
+ location = locationBefore.toOSString();
+ project.create(description, null);
project.open(null);
- location = project.getLocation().toOSString();
javaProject = JavaCore.create(project);
IFolder binFolder = createBinFolder();
setJavaNature();
@@ -80,6 +102,33 @@ public class TestProject {
addSystemLibraries();
}
+ public File getWorkspaceSupplement() {
+ return workspaceSupplement;
+ }
+
+ private IProjectDescription createDescription(final boolean remove,
+ String path, boolean insidews, IWorkspaceRoot root, File workspaceSupplement) {
+ Path ppath = new Path(path);
+ String projectName = ppath.lastSegment();
+ URI locationURI;
+ URI top;
+ if (insidews) {
+ top = root.getRawLocationURI();
+ } else {
+ top = URIUtil.toURI(workspaceSupplement.getAbsolutePath());
+ }
+ if (!insidews || !ppath.lastSegment().equals(path)) {
+ locationURI = URIUtil.toURI(URIUtil.toPath(top).append(path));
+ } else
+ locationURI = null;
+ IProjectDescription description = ResourcesPlugin.getWorkspace()
+ .newProjectDescription(projectName);
+
+ description.setName(projectName);
+ description.setLocationURI(locationURI);
+ return description;
+ }
+
public IProject getProject() {
return project;
}
@@ -225,4 +274,8 @@ public class TestProject {
public void setSourceFolder(IPackageFragmentRoot sourceFolder) {
this.sourceFolder = sourceFolder;
}
+
+ public String getLocation() {
+ return location;
+ }
}
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java
index c9970d4c36..702675260b 100644
--- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java
+++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/TestUtils.java
@@ -17,6 +17,7 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
@@ -27,6 +28,7 @@ import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jgit.lib.ObjectId;
@@ -101,11 +103,11 @@ public class TestUtils {
* @param content
* the contents
* @return the file
- * @throws Exception
+ * @throws CoreException
* if the file can not be created
+ * @throws UnsupportedEncodingException
*/
- public IFile addFileToProject(IProject project, String path, String content)
- throws Exception {
+ public IFile addFileToProject(IProject project, String path, String content) throws CoreException, UnsupportedEncodingException {
IPath filePath = new Path(path);
IFolder folder = null;
for (int i = 0; i < filePath.segmentCount() - 1; i++) {
@@ -130,10 +132,10 @@ public class TestUtils {
* @param file
* @param newContent
* @return the file
- * @throws Exception
+ * @throws CoreException
+ * @throws UnsupportedEncodingException
*/
- public IFile changeContentOfFile(IProject project, IFile file, String newContent)
- throws Exception {
+ public IFile changeContentOfFile(IProject project, IFile file, String newContent) throws UnsupportedEncodingException, CoreException {
file.setContents(new ByteArrayInputStream(newContent.getBytes(project
.getDefaultCharset())), 0, null);
return file;
@@ -253,4 +255,8 @@ public class TestUtils {
return map;
}
+ File getWorkspaceSupplement() throws IOException {
+ return createTempDir("wssupplement");
+ }
+
}

Back to the top