diff options
| author | Dariusz Luksza | 2010-07-29 19:04:54 +0000 |
|---|---|---|
| committer | Chris Aniszczyk | 2010-08-05 19:29:01 +0000 |
| commit | 986e3823e1e3555bda426b3b09bd790d4595113a (patch) | |
| tree | f6bc423e63d485003eb3f8bfc30243b96956675b | |
| parent | 9d4a7f54b6d40ff2c523bc817e4cc57ff6def47d (diff) | |
| download | egit-986e3823e1e3555bda426b3b09bd790d4595113a.tar.gz egit-986e3823e1e3555bda426b3b09bd790d4595113a.tar.xz egit-986e3823e1e3555bda426b3b09bd790d4595113a.zip | |
Fixes obtaining common ancestor in Synchronize view
For obtaining common base ancestor I've used suggested RevWalk with
RevFilter.MERGE_BASE. For API compatibility it is also used for
obtaining remote variant. This patch also improves the
GitResourceVariant implementation. I replaced all dependencies to
the deprecated TreeEntry with the proper RevObject implementation.
Bug: 317934
CQ: 4390
Change-Id: I8f5352862d01e5a5475a74f222241970cd7960ee
Signed-off-by: Dariusz Luksza <dariusz@luksza.org>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
24 files changed, 1385 insertions, 1889 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java index 7c9de90157..4fdcca3a97 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparatorTest.java @@ -17,6 +17,7 @@ import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; import java.io.ByteArrayInputStream; +import java.io.File; import java.util.Arrays; import org.eclipse.core.resources.IContainer; @@ -26,14 +27,15 @@ import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.egit.core.op.ConnectProviderOperation; +import org.eclipse.core.runtime.Path; import org.eclipse.egit.core.project.RepositoryMapping; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; import org.eclipse.egit.core.test.GitTestCase; -import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.storage.file.FileRepository; +import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.team.core.variants.IResourceVariant; import org.junit.After; import org.junit.Before; @@ -43,27 +45,31 @@ public class GitResourceVariantComparatorTest extends GitTestCase { private Repository repo; + private IProject iProject; + + private TestRepository testRepo; + @Before public void setUp() throws Exception { super.setUp(); - IProject iProject = project.project; - if (!gitDir.exists()) - new FileRepository(gitDir).create(); - - new ConnectProviderOperation(iProject, gitDir).execute(null); + iProject = project.project; + testRepo = new TestRepository(gitDir); + testRepo.connect(iProject); repo = RepositoryMapping.getMapping(iProject).getRepository(); } @After - public void tearDown() throws Exception { - repo.close(); + public void clearGitResources() throws Exception { + testRepo.disconnect(iProject); + testRepo.dispose(); + repo = null; super.tearDown(); } - /*============================================ + /* ============================================ * compare(IResource, IResourceVariant) tests - *============================================*/ + * ============================================ */ /** * When remote variant wasn't found, compare method is called with null as @@ -74,7 +80,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { public void shouldReturnFalseWhenRemoteDoesNotExist() { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given IResource local = createMock(IResource.class); @@ -87,16 +93,16 @@ public class GitResourceVariantComparatorTest extends GitTestCase { } /** - * It is possible to have a local file that has same name as a remote folder. - * In some cases that two resources can be compared. In this case compare - * method should return false, because they aren't same resources + * It is possible to have a local file that has same name as a remote + * folder. In some cases that two resources can be compared. In this case + * compare method should return false, because they aren't same resources */ @Test @SuppressWarnings("boxing") public void shouldReturnFalseWhenComparingFileAndContainer() { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given IFile local = createMock(IFile.class); @@ -113,14 +119,17 @@ public class GitResourceVariantComparatorTest extends GitTestCase { } /** - * Comparing two folders that have different path should return false. + * Comparing two folders that have different path should return false. + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnFalseWhenComparingContainerAndContainer() { + public void shouldReturnFalseWhenComparingContainerAndContainer() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given IPath localPath = createMock(IPath.class); @@ -130,53 +139,59 @@ public class GitResourceVariantComparatorTest extends GitTestCase { expect(local.getFullPath()).andReturn(localPath); replay(local); - IPath remotePath = createMock(IPath.class); - replay(remotePath); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.getFullPath()).andReturn(remotePath); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); + File file = testRepo.createFile(iProject, "test" + File.separator + + "keep"); + RevCommit commit = testRepo.addAndCommit(iProject, file, + "initial commit"); + String path = Repository.stripWorkDir(repo.getWorkTree(), file); + + GitFolderResourceVariant remote = new GitFolderResourceVariant(repo, + commit, path); // then assertFalse(grvc.compare(local, remote)); - verify(local, localPath, remotePath, remoteResource); + verify(local, localPath); } /** * When comparing two folders that have same path, compare() method should * return true. + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnTrueWhenComparingContainerAndContainer() { + public void shouldReturnTrueWhenComparingContainerAndContainer() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IPath path = createMock(IPath.class); - replay(path); + File file = testRepo.createFile(iProject, "test" + File.separator + + "keep"); + RevCommit commit = testRepo.addAndCommit(iProject, file, + "initial commit"); + String path = Repository.stripWorkDir(repo.getWorkTree(), file); + IPath iPath = new Path(path); IContainer local = createMock(IContainer.class); expect(local.exists()).andReturn(true); - expect(local.getFullPath()).andReturn(path); + expect(local.getFullPath()).andReturn(iPath); replay(local); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.getFullPath()).andReturn(path); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); + GitFolderResourceVariant remote = new GitFolderResourceVariant(repo, + commit, path); // then assertTrue(grvc.compare(local, remote)); - verify(local, path, remoteResource); + verify(local); } /** * Compare() should return false when comparing two files with different * content length + * * @throws Exception */ @Test @@ -189,7 +204,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); GitResourceVariantComparator grvc = new GitResourceVariantComparator( - dataSet, null); + dataSet); // given IFile local = createMock(IFile.class); @@ -218,6 +233,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { /** * Comparing two files that have same content length but having small * difference inside content should return false. + * * @throws Exception */ @Test @@ -230,7 +246,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); GitResourceVariantComparator grvc = new GitResourceVariantComparator( - dataSet, null); + dataSet); // given IFile local = createMock(IFile.class); @@ -259,6 +275,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { /** * Comparing two 'large' files that have same length and almost identical * content should return false. + * * @throws Exception */ @Test @@ -273,7 +290,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); GitResourceVariantComparator grvc = new GitResourceVariantComparator( - dataSet, null); + dataSet); // given IFile local = createMock(IFile.class); @@ -306,6 +323,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { * This and previous three test cases cover almost the same functionality * but they are covering all return points in compare methods that can be * used when comparing files content + * * @throws Exception */ @Test @@ -320,7 +338,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); GitResourceVariantComparator grvc = new GitResourceVariantComparator( - dataSet, null); + dataSet); // given IFile local = createMock(IFile.class); @@ -349,6 +367,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { /** * Comparing two files that have the same content and content length should * return true + * * @throws Exception */ @Test @@ -360,7 +379,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); GitResourceVariantComparator grvc = new GitResourceVariantComparator( - dataSet, null); + dataSet); // given IFile local = createMock(IFile.class); @@ -389,6 +408,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { /** * Compare two 'large' files that have same content length and content * should return true. + * * @throws Exception */ @Test @@ -403,7 +423,7 @@ public class GitResourceVariantComparatorTest extends GitTestCase { GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); GitResourceVariantComparator grvc = new GitResourceVariantComparator( - dataSet, null); + dataSet); // given IFile local = createMock(IFile.class); @@ -429,252 +449,261 @@ public class GitResourceVariantComparatorTest extends GitTestCase { verify(local, remote, storage); } + /* ================================================== + * compare(IResourceVariant, IResourceVariant) tests + * ================================================== */ + /** - * When comparing locally not existing file with file that exists in remote, + * When comparing file that don't exist in base, but exists in remote * compare method should return false. + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnFalseWhenBaseDoesntExist() { + public void shouldReturnFalseWhenBaseDoesntExist() throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(false); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, ObjectId.zeroId(), null); - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, ObjectId.zeroId(), null); + RevCommit baseCommit = testRepo.createInitialCommit("initial commit"); + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + File file = testRepo.createFile(iProject, "test-file"); + RevCommit remoteCommit = testRepo.addAndCommit(iProject, file, + "second commit"); + String path = Repository.stripWorkDir(repo.getWorkTree(), file); + + GitBlobResourceVariant base = new GitBlobResourceVariant(repo, + baseCommit, path); + GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, + remoteCommit, path); // then assertFalse(grvc.compare(base, remote)); - verify(baseResource, remoteResource); } /** * Compare() should return false when remote file does not exists, but * equivalent local file exist. + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnFalseWhenRemoteVariantDoesntExist() { + public void shouldReturnFalseWhenRemoteVariantDoesntExist() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, ObjectId.zeroId(), null); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(false); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, ObjectId.zeroId(), null); + RevCommit remoteCommit = testRepo.createInitialCommit("initial commit"); + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + File file = testRepo.createFile(iProject, "test-file"); + RevCommit baseCommit = testRepo.addAndCommit(iProject, file, + "second commit"); + String path = Repository.stripWorkDir(repo.getWorkTree(), file); + + GitBlobResourceVariant base = new GitBlobResourceVariant(repo, + baseCommit, path); + GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, + remoteCommit, path); // then assertFalse(grvc.compare(base, remote)); - verify(baseResource, remoteResource); } - /*================================================== - * compare(IResourceVariant, IResourceVariant) tests - *==================================================*/ - /** * Return false when comparing incompatible types (file against folder) that * also maps onto different resources + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnFalseWhenComparingRemoteVariantFileWithContainer() { + public void shouldReturnFalseWhenComparingRemoteVariantFileWithContainer() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, ObjectId.zeroId(), null); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); + File file = testRepo.createFile(iProject, "test" + File.separator + + "keep"); + RevCommit commit = testRepo.addAndCommit(iProject, file, + "initial commit"); + String filePath = Repository.stripWorkDir(repo.getWorkTree(), file); + String folderPath = Repository.stripWorkDir(repo.getWorkTree(), + new File(file.getParent())); + GitBlobResourceVariant base = new GitBlobResourceVariant(repo, + commit, filePath); + GitFolderResourceVariant remote = new GitFolderResourceVariant(repo, + commit, folderPath); // then assertFalse(grvc.compare(base, remote)); - verify(baseResource, remoteResource); } /** * Return false when comparing incompatible types (folder against file) that * also map onto different resources + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnFalseWhenComparingRemoteVariantContainerWithFile() { + public void shouldReturnFalseWhenComparingRemoteVariantContainerWithFile() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, ObjectId.zeroId(), null); + File file = testRepo.createFile(iProject, "test" + File.separator + + "keep"); + RevCommit commit = testRepo.addAndCommit(iProject, file, + "initial commit"); + String filePath = Repository.stripWorkDir(repo.getWorkTree(), file); + String folderPath = Repository.stripWorkDir(repo.getWorkTree(), + new File(file.getParent())); + + GitFolderResourceVariant base = new GitFolderResourceVariant(repo, + commit, folderPath); + GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, + commit, filePath); // then assertFalse(grvc.compare(base, remote)); - verify(baseResource, remoteResource); } /** * When comparing two remote variants that have different path compare * method should return false + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnFalseWhenComparingRemoteVariantContainerWithContainer() { + public void shouldReturnFalseWhenComparingRemoteVariantContainerWithContainer() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IPath basePath = createMock(IPath.class); - replay(basePath); - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - expect(baseResource.getFullPath()).andReturn(basePath); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - IPath remotePath = createMock(IPath.class); - replay(remotePath); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - expect(remoteResource.getFullPath()).andReturn(remotePath); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); + File file1 = testRepo.createFile(iProject, "test1" + File.separator + + "keep1"); + File file2 = testRepo.createFile(iProject, "test2" + File.separator + + "keep2"); + testRepo.track(file1); + testRepo.track(file2); + testRepo.addToIndex(testRepo.getIFile(iProject, file1)); + testRepo.addToIndex(testRepo.getIFile(iProject, file2)); + RevCommit commit = testRepo.commit("initial commit"); + + String path1 = Repository.stripWorkDir(repo.getWorkTree(), new File( + file1.getParent())); + String path2 = Repository.stripWorkDir(repo.getWorkTree(), new File( + file2.getParent())); + + GitFolderResourceVariant base = new GitFolderResourceVariant(repo, + commit, path1); + GitFolderResourceVariant remote = new GitFolderResourceVariant(repo, + commit, path2); // then assertFalse(grvc.compare(base, remote)); - verify(baseResource, remoteResource, basePath, remotePath); } /** * Comparing two remote folders that have same path should return true + * + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnTrueWhenComparingRemoteVariantContainerWithContainer() { + public void shouldReturnTrueWhenComparingRemoteVariantContainerWithContainer() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IPath path = createMock(IPath.class); - replay(path); - - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - expect(baseResource.getFullPath()).andReturn(path); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - expect(remoteResource.getFullPath()).andReturn(path); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); + File file1 = testRepo.createFile(iProject, "test1" + File.separator + + "keep1"); + testRepo.track(file1); + testRepo.addToIndex(testRepo.getIFile(iProject, file1)); + RevCommit commit = testRepo.commit("initial commit"); + + String path1 = Repository.stripWorkDir(repo.getWorkTree(), new File( + file1.getParent())); + + GitFolderResourceVariant base = new GitFolderResourceVariant(repo, + commit, path1); + GitFolderResourceVariant remote = new GitFolderResourceVariant(repo, + commit, path1); // then assertTrue(grvc.compare(base, remote)); - verify(baseResource, remoteResource, path); } @Test @SuppressWarnings("boxing") /** * Comparing two remote files that have different git ObjectId should return false. + * + * @throws Exception */ - public void shouldReturnFalseWhenComparingRemoteVariantWithDifferentObjectId() { + public void shouldReturnFalseWhenComparingRemoteVariantWithDifferentObjectId() + throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant( - baseResource, - repo, - ObjectId.fromString("0123456789012345678901234567890123456789"), - null); + File file = testRepo.createFile(iProject, "test-file"); + RevCommit baseCommit = testRepo.appendContentAndCommit(iProject, file, + "a", "initial commit"); + RevCommit remoteCommit = testRepo.appendContentAndCommit(iProject, + file, "bc", "second commit"); + + String path = Repository.stripWorkDir(repo.getWorkTree(), file); + GitBlobResourceVariant base = new GitBlobResourceVariant(repo, + baseCommit, path); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, ObjectId.zeroId(), null); + GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, + remoteCommit, path); // then assertFalse(grvc.compare(base, remote)); - verify(baseResource, remoteResource); } /** * Comparing two remote files that have the same git ObjectId should return * true. + * @throws Exception */ @Test @SuppressWarnings("boxing") - public void shouldReturnTrueWhenComparingRemoteVariant() { + public void shouldReturnTrueWhenComparingRemoteVariant() throws Exception { // when GitResourceVariantComparator grvc = new GitResourceVariantComparator( - null, null); + null); // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant( - baseResource, - repo, - ObjectId.fromString("0123456789012345678901234567890123456789"), - null); + File file = testRepo.createFile(iProject, "test-file"); + RevCommit commit = testRepo.appendContentAndCommit(iProject, file, + "a", "initial commit"); - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, - repo, - ObjectId.fromString("0123456789012345678901234567890123456789"), - null); + String path = Repository.stripWorkDir(repo.getWorkTree(), file); + GitBlobResourceVariant base = new GitBlobResourceVariant(repo, commit, path); + + GitBlobResourceVariant remote = new GitBlobResourceVariant(repo, commit, path); // then assertTrue(grvc.compare(base, remote)); - verify(baseResource, remoteResource); } } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest.java new file mode 100644 index 0000000000..32ef364248 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest.java @@ -0,0 +1,213 @@ +/******************************************************************************* + * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org> + * + * 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.synchronize; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; +import org.eclipse.egit.core.test.GitTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.variants.IResourceVariant; +import org.eclipse.team.core.variants.IResourceVariantTree; +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +public class GitResourceVariantTreeSubscriberTest extends GitTestCase { + + private Repository repo; + + private IProject iProject; + + private TestRepository testRepo; + + @Before + public void setUp() throws Exception { + super.setUp(); + iProject = project.getProject(); + testRepo = new TestRepository(gitDir); + testRepo.connect(iProject); + repo = RepositoryMapping.getMapping(iProject).getRepository(); + } + + @After + public void clearGitResources() throws Exception { + testRepo.disconnect(iProject); + testRepo.dispose(); + repo = null; + super.tearDown(); + } + + /** + * This test simulates that user work and made some changes on branch 'test' + * and then try to synchronize "test" and 'master' branch. + * + * @throws Exception + */ + @Test + public void shouldReturnSrcBranchAsBase() throws Exception { + // when + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + RevCommit commit = testRepo.appendContentAndCommit(iProject, file, + "class Main {}", "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); + ObjectId fileId = findFileId(commit, mainJava); + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + testRepo.appendContentAndCommit(iProject, file, "// test1", + "secont commit"); + + // given + GitResourceVariantTreeSubscriber grvts = createGitResourceVariantTreeSubscriber( + Constants.HEAD, Constants.R_HEADS + Constants.MASTER); + grvts.getBaseTree(); + IResourceVariantTree baseTree = grvts.getBaseTree(); + + // then + IResourceVariant actual = commonAssertionsForBaseTree(baseTree, + mainJava); + assertEquals(fileId.getName(), actual.getContentIdentifier()); + } + + /** + * Both source and destination branches has some different commits but they + * has also common ancestor. This situation is described more detailed in + * bug #317934 + * + * This test passes when it is run as a stand alone test case, but it fails + * when it is run as part of test suite. It throws NPE when it try's to + * checkout master branch. + * + * @throws Exception + */ + @Test + @Ignore + public void shouldReturnCommonAncestorAsBase() throws Exception { + // when + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + RevCommit commit = testRepo.appendContentAndCommit(iProject, file, + "class Main {}", "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); + // this should be our common ancestor + ObjectId fileId = findFileId(commit, mainJava); + + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + testRepo.appendContentAndCommit(iProject, file, "// test 1", + "second commit"); + + testRepo.checkoutBranch(Constants.R_HEADS + Constants.MASTER); + testRepo.appendContentAndCommit(iProject, file, "// test 2", + "third commit"); + + // given + GitResourceVariantTreeSubscriber grvts = createGitResourceVariantTreeSubscriber( + Constants.HEAD, Constants.R_HEADS + "test"); + grvts.getBaseTree(); + IResourceVariantTree baseTree = grvts.getBaseTree(); + + // then + IResourceVariant actual = commonAssertionsForBaseTree(baseTree, + mainJava); + assertEquals(fileId.getName(), actual.getContentIdentifier()); + } + + /** + * This test passes when it is run as a stand alone test case, but it fails + * when it is run as part of test suite. It throws NPE when it try's to + * checkout master branch. + * + * @throws Exception + */ + @Test + @Ignore + public void shouldReturnRemoteTree() throws Exception { + // when + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + testRepo.appendContentAndCommit(iProject, file, + "class Main {}", "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); + + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + RevCommit commit = testRepo.appendContentAndCommit(iProject, file, "// test 1", + "second commit"); + ObjectId fileId = findFileId(commit, mainJava); + + // given + GitResourceVariantTreeSubscriber grvts = createGitResourceVariantTreeSubscriber( + Constants.HEAD, "test"); + grvts.getBaseTree(); + IResourceVariantTree remoteTree = grvts.getRemoteTree(); + + // then + assertNotNull(remoteTree); + assertTrue(remoteTree instanceof GitRemoteResourceVariantTree); + IResourceVariant resourceVariant = remoteTree + .getResourceVariant(mainJava); + assertNotNull(resourceVariant); + assertTrue(resourceVariant instanceof GitResourceVariant); + assertEquals(fileId.getName(), resourceVariant.getContentIdentifier()); + } + + private GitResourceVariantTreeSubscriber createGitResourceVariantTreeSubscriber( + String src, String dst) { + GitSynchronizeData gsd = new GitSynchronizeData(repo, src, dst, false); + GitSynchronizeDataSet gsds = new GitSynchronizeDataSet(gsd); + new GitResourceVariantTreeSubscriber(gsds); + return new GitResourceVariantTreeSubscriber(gsds); + } + + private ObjectId findFileId(RevCommit commit, IFile mainJava) + throws Exception { + TreeWalk tw = new TreeWalk(repo); + tw.reset(); + tw.setRecursive(true); + String path = Repository.stripWorkDir(repo.getWorkTree(), mainJava + .getLocation().toFile()); + tw.setFilter(PathFilter.create(path)); + int nth = tw.addTree(commit.getTree()); + tw.next(); + + return tw.getObjectId(nth); + } + + private IResourceVariant commonAssertionsForBaseTree( + IResourceVariantTree baseTree, IResource resource) + throws TeamException { + assertNotNull(baseTree); + assertTrue(baseTree instanceof GitBaseResourceVariantTree); + IResourceVariant resourceVariant = baseTree + .getResourceVariant(resource); + assertNotNull(resourceVariant); + assertTrue(resourceVariant instanceof GitResourceVariant); + return resourceVariant; + } + +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest1.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest1.java new file mode 100644 index 0000000000..dd4cb7db16 --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest1.java @@ -0,0 +1,129 @@ +/******************************************************************************* + * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org> + * + * 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.synchronize; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; +import org.eclipse.egit.core.test.GitTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.variants.IResourceVariant; +import org.eclipse.team.core.variants.IResourceVariantTree; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class GitResourceVariantTreeSubscriberTest1 extends GitTestCase { + + private Repository repo; + + private IProject iProject; + + private TestRepository testRepo; + + @Before + public void setUp() throws Exception { + super.setUp(); + iProject = project.getProject(); + testRepo = new TestRepository(gitDir); + testRepo.connect(iProject); + repo = RepositoryMapping.getMapping(iProject).getRepository(); + } + + @After + public void clearGitResources() throws Exception { + testRepo.disconnect(iProject); + testRepo.dispose(); + repo = null; + super.tearDown(); + } + + /** + * This test simulates that user work and made some changes on branch 'test' + * and then try to synchronize "test" and 'master' branch. + * + * @throws Exception + */ + @Test + public void shouldReturnSrcBranchAsBase() throws Exception { + // when + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + RevCommit commit = testRepo.appendContentAndCommit(iProject, file, + "class Main {}", "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); + ObjectId fileId = findFileId(commit, mainJava); + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + testRepo.appendContentAndCommit(iProject, file, "// test1", + "secont commit"); + + // given + GitResourceVariantTreeSubscriber grvts = createGitResourceVariantTreeSubscriber( + Constants.HEAD, Constants.R_HEADS + Constants.MASTER); + grvts.getBaseTree(); + IResourceVariantTree baseTree = grvts.getBaseTree(); + + // then + IResourceVariant actual = commonAssertionsForBaseTree(baseTree, + mainJava); + assertEquals(fileId.getName(), actual.getContentIdentifier()); + } + + private GitResourceVariantTreeSubscriber createGitResourceVariantTreeSubscriber( + String src, String dst) { + GitSynchronizeData gsd = new GitSynchronizeData(repo, src, dst, false); + GitSynchronizeDataSet gsds = new GitSynchronizeDataSet(gsd); + new GitResourceVariantTreeSubscriber(gsds); + return new GitResourceVariantTreeSubscriber(gsds); + } + + private ObjectId findFileId(RevCommit commit, IFile mainJava) + throws Exception { + TreeWalk tw = new TreeWalk(repo); + tw.reset(); + tw.setRecursive(true); + String path = Repository.stripWorkDir(repo.getWorkTree(), mainJava + .getLocation().toFile()); + tw.setFilter(PathFilter.create(path)); + int nth = tw.addTree(commit.getTree()); + tw.next(); + + return tw.getObjectId(nth); + } + + private IResourceVariant commonAssertionsForBaseTree( + IResourceVariantTree baseTree, IResource resource) + throws TeamException { + assertNotNull(baseTree); + assertTrue(baseTree instanceof GitBaseResourceVariantTree); + IResourceVariant resourceVariant = baseTree + .getResourceVariant(resource); + assertNotNull(resourceVariant); + assertTrue(resourceVariant instanceof GitResourceVariant); + return resourceVariant; + } + +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest2.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest2.java new file mode 100644 index 0000000000..4a12d331bb --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriberTest2.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org> + * + * 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.synchronize; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.project.RepositoryMapping; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; +import org.eclipse.egit.core.test.GitTestCase; +import org.eclipse.egit.core.test.TestRepository; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.variants.IResourceVariant; +import org.eclipse.team.core.variants.IResourceVariantTree; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class GitResourceVariantTreeSubscriberTest2 extends GitTestCase { + + private Repository repo; + + private IProject iProject; + + private TestRepository testRepo; + + @Before + public void setUp() throws Exception { + super.setUp(); + iProject = project.getProject(); + testRepo = new TestRepository(gitDir); + testRepo.connect(iProject); + repo = RepositoryMapping.getMapping(iProject).getRepository(); + } + + @After + public void clearGitResources() throws Exception { + testRepo.disconnect(iProject); + testRepo.dispose(); + repo = null; + super.tearDown(); + } + + /** + * Both source and destination branches has some different commits but they + * has also common ancestor. This situation is described more detailed in + * bug #317934 + * + * This test passes when it is run as a stand alone test case, but it fails + * when it is run as part of test suite. It throws NPE when it try's to + * checkout master branch. + * + * @throws Exception + */ + @Test + public void shouldReturnCommonAncestorAsBase() throws Exception { + // when + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + RevCommit commit = testRepo.appendContentAndCommit(iProject, file, + "class Main {}", "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); + // this should be our common ancestor + ObjectId fileId = findFileId(commit, mainJava); + + testRepo.createAndCheckoutBranch(Constants.HEAD, Constants.R_HEADS + + "test"); + testRepo.appendContentAndCommit(iProject, file, "// test 1", + "second commit"); + + testRepo.checkoutBranch(Constants.R_HEADS + Constants.MASTER); + testRepo.appendContentAndCommit(iProject, file, "// test 2", + "third commit"); + + // given + GitResourceVariantTreeSubscriber grvts = createGitResourceVariantTreeSubscriber( + Constants.HEAD, Constants.R_HEADS + "test"); + grvts.getBaseTree(); + IResourceVariantTree baseTree = grvts.getBaseTree(); + + // then + IResourceVariant actual = commonAssertionsForBaseTree(baseTree, + mainJava); + assertEquals(fileId.getName(), actual.getContentIdentifier()); + } + + private GitResourceVariantTreeSubscriber createGitResourceVariantTreeSubscriber( + String src, String dst) { + GitSynchronizeData gsd = new GitSynchronizeData(repo, src, dst, false); + GitSynchronizeDataSet gsds = new GitSynchronizeDataSet(gsd); + new GitResourceVariantTreeSubscriber(gsds); + return new GitResourceVariantTreeSubscriber(gsds); + } + + private ObjectId findFileId(RevCommit commit, IFile mainJava) + throws Exception { + TreeWalk tw = new TreeWalk(repo); + tw.reset(); + tw.setRecursive(true); + String path = Repository.stripWorkDir(repo.getWorkTree(), mainJava + .getLocation().toFile()); + tw.setFilter(PathFilter.create(path)); + int nth = tw.addTree(commit.getTree()); + tw.next(); + + return tw.getObjectId(nth); + } + + private IResourceVariant commonAssertionsForBaseTree( + IResourceVariantTree baseTree, IResource resource) + throws TeamException { + assertNotNull(baseTree); + assertTrue(baseTree instanceof GitBaseResourceVariantTree); + IResourceVariant resourceVariant = baseTree + .getResourceVariant(resource); + assertNotNull(resourceVariant); + assertTrue(resourceVariant instanceof GitResourceVariant); + return resourceVariant; + } + +} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java index 7d23d5f944..f18972b66c 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeTest.java @@ -15,67 +15,54 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.io.ByteArrayInputStream; +import java.io.File; import java.io.InputStream; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.egit.core.op.AddToIndexOperation; -import org.eclipse.egit.core.op.BranchOperation; import org.eclipse.egit.core.op.ConnectProviderOperation; -import org.eclipse.egit.core.op.DisconnectProviderOperation; import org.eclipse.egit.core.project.RepositoryMapping; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; import org.eclipse.egit.core.test.GitTestCase; import org.eclipse.egit.core.test.TestProject; +import org.eclipse.egit.core.test.TestRepository; import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IType; -import org.eclipse.jgit.api.Git; import org.eclipse.jgit.lib.Constants; -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.storage.file.FileRepository; import org.eclipse.team.core.variants.IResourceVariant; import org.eclipse.team.core.variants.ResourceVariantByteStore; -import org.eclipse.team.core.variants.SessionResourceVariantByteStore; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class GitResourceVariantTreeTest extends GitTestCase { private Repository repo; + private IProject iProject; + + private TestRepository testRepo; + private ResourceVariantByteStore store; @Before public void createGitRepository() throws Exception { - IProject iProject = project.project; - if (!gitDir.exists()) - new FileRepository(gitDir).create(); - - new ConnectProviderOperation(iProject, gitDir).execute(null); + iProject = project.project; + testRepo = new TestRepository(gitDir); + testRepo.connect(iProject); repo = RepositoryMapping.getMapping(iProject).getRepository(); - - store = new SessionResourceVariantByteStore(); } @After public void clearGitResources() throws Exception { - List<IProject> projects = new ArrayList<IProject>(); - projects.add(project.project); - new DisconnectProviderOperation(projects).execute(null); - - repo.close(); + testRepo.disconnect(iProject); + testRepo.dispose(); + repo = null; super.tearDown(); } @@ -104,6 +91,7 @@ public class GitResourceVariantTreeTest extends GitTestCase { * When we have two or more project associated with repository, roots() * method should return list of project. In this case we have two project * associated with particular repository, therefore '2' value is expected. + * * @throws Exception */ @Test @@ -130,72 +118,10 @@ public class GitResourceVariantTreeTest extends GitTestCase { } /** - * When we want to obtain list of members, members() method should return - * only members that are in repository. In this test we create Main.java - * file, stage it and commit it. Then we create Main2.java file with we don't - * add to repository. members() method should return one member because only - * one file is in repository. - * @throws Exception - */ - @Test - public void shouldReturnOneMember() throws Exception { - // when - createResourceAndCommit("org.egit.test", "Main.java", "class Main {}", - "Initial commit"); - // create second file that isn't tracked - IPackageFragment iPackage = project - .createPackage("org.egit.test.nested"); - project.createType(iPackage, "Main2.java", "class Main2 {}"); - GitSynchronizeData data = new GitSynchronizeData(repo, Constants.HEAD, - Constants.MASTER, false); - GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); - - // given - GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet, - store); - - // then - assertEquals(1, grvt.members(project.project).length); - IResource[] members = grvt.members(project.project); - assertEquals("src", members[0].getName()); - } - - /** - * members() method should return only members that are on same level (it - * cannot work recursively). In this test it should return one file and one - * folder member. - * @throws Exception - */ - @Ignore - @Test - public void shouldReturnTwoMembers() throws Exception { - // when - IPackageFragment iPackage = project.createPackage("org.egit.test"); - createResourceAndCommit(iPackage, "Main.java", "class Main {}", - "Initial commit"); - // create second file that isn't tracked - createResourceAndCommit("org.egit.test.nested", "Main2.java", - "class Main2 {}", "Second commit"); - - GitSynchronizeData data = new GitSynchronizeData(repo, Constants.HEAD, - Constants.MASTER, false); - GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); - - // given - GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet, - store); - - // then - assertEquals(2, grvt.members(iPackage.getResource()).length); - IResource[] members = grvt.members(iPackage.getResource()); - assertEquals("nested", members[0].getName()); - assertEquals("Main.java", members[1].getName()); - } - - /** * Checks that getResourceVariant will not throw NPE for null argument. This * method is called with null argument when local or remote resource does * not exist. + * * @throws Exception */ @Test @@ -206,8 +132,7 @@ public class GitResourceVariantTreeTest extends GitTestCase { GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); // given - GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet, - store); + GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet); // then assertNull(grvt.getResourceVariant(null)); @@ -216,6 +141,7 @@ public class GitResourceVariantTreeTest extends GitTestCase { /** * getResourceVariant() should return null when given resource doesn't exist * in repository. + * * @throws Exception */ @Test @@ -229,8 +155,7 @@ public class GitResourceVariantTreeTest extends GitTestCase { GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); // given - GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet, - store); + GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet); // then assertNull(grvt.getResourceVariant(mainJava.getResource())); @@ -240,35 +165,34 @@ public class GitResourceVariantTreeTest extends GitTestCase { * Check if getResourceVariant() does return the same resource that was * committed. Passes only when it is run as a single test, not as a part of * largest test suite + * * @throws Exception */ - @Ignore @Test public void shoulReturnSameResourceVariant() throws Exception { // when - IType mainJava = createResourceAndCommit("org.egit.test", "Main.java", - "class Main {}", "Initial commit"); + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + testRepo.appendContentAndCommit(iProject, file, "class Main {}", + "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); GitSynchronizeData data = new GitSynchronizeData(repo, Constants.HEAD, Constants.MASTER, false); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); // given - GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet, - store); + GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet); // then - IResourceVariant actual = grvt.getResourceVariant(mainJava - .getResource()); + IResourceVariant actual = grvt.getResourceVariant(mainJava); assertNotNull(actual); - assertEquals("Main.java", actual.getName()); + assertEquals(fileName, actual.getName()); InputStream actualIn = actual.getStorage(new NullProgressMonitor()) .getContents(); - byte[] actualByte = new byte[actualIn.available()]; - actualIn.read(actualByte); - InputStream expectedIn = ((IFile) mainJava.getResource()).getContents(); - byte[] expectedByte = new byte[expectedIn.available()]; - expectedIn.read(expectedByte); + byte[] actualByte = getBytesAndCloseStream(actualIn); + InputStream expectedIn = mainJava.getContents(); + byte[] expectedByte = getBytesAndCloseStream(expectedIn); assertArrayEquals(expectedByte, actualByte); } @@ -278,89 +202,52 @@ public class GitResourceVariantTreeTest extends GitTestCase { * getResourceVariant() should obtain Main.java file content from "master" * branch. Passes only when it is run as a single test, not as a part of * largest test suite + * * @throws Exception */ - @Ignore @Test public void shouldReturnDifferentResourceVariant() throws Exception { // when - IType mainJava = createResourceAndCommit("org.egit.test", "Main.java", - "class Main {}", "Initial commit"); - createBranch("test"); - // checkout branch - new BranchOperation(repo, "refs/heads/test").execute(null); - ((IFile) mainJava.getResource()).appendContents( - new ByteArrayInputStream("// test".getBytes()), 0, null); - addAndCommitResource(mainJava, "Second commit"); + String fileName = "Main.java"; + File file = testRepo.createFile(iProject, fileName); + testRepo.appendContentAndCommit(iProject, file, "class Main {}", + "initial commit"); + IFile mainJava = testRepo.getIFile(iProject, file); + + testRepo.createAndCheckoutBranch(Constants.R_HEADS + Constants.MASTER, Constants.R_HEADS + "test"); + testRepo.appendContentAndCommit(iProject, file, "// test", "first commit"); GitSynchronizeData data = new GitSynchronizeData(repo, Constants.HEAD, Constants.MASTER, false); GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); // given - GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet, - store); + GitResourceVariantTree grvt = new GitRemoteResourceVariantTree(dataSet); // then - IResourceVariant actual = grvt.getResourceVariant(mainJava - .getResource()); + IResourceVariant actual = grvt.getResourceVariant(mainJava); assertNotNull(actual); - assertEquals("Main.java", actual.getName()); + assertEquals(fileName, actual.getName()); InputStream actualIn = actual.getStorage(new NullProgressMonitor()) - .getContents(); - byte[] actualByte = new byte[actualIn.available()]; - actualIn.read(actualByte); - InputStream expectedIn = ((IFile) mainJava.getResource()).getContents(); - byte[] expectedByte = new byte[expectedIn.available()]; - expectedIn.read(expectedByte); + .getContents(); + byte[] actualByte = getBytesAndCloseStream(actualIn); + InputStream expectedIn = mainJava.getContents(); + byte[] expectedByte = getBytesAndCloseStream(expectedIn); // assert arrays not equals - if (Arrays.equals(expectedByte, actualByte)) { + if (Arrays.equals(expectedByte, actualByte)) fail(); - } else { + else assertTrue(true); - } } - private IType createResourceAndCommit(String packageName, String fileName, - String fileContent, String commitMsg) throws Exception { - IPackageFragment iPackage = project.createPackage(packageName); - return createResourceAndCommit(iPackage, fileName, fileContent, - commitMsg); - } - - private IType createResourceAndCommit(IPackageFragment iPackage, - String fileName, String fileContent, String commitMsg) - throws Exception { - IType mainJava = project.createType(iPackage, fileName, fileContent); - addAndCommitResource(mainJava, commitMsg); - - return mainJava; - } - - private void addAndCommitResource(IType mainJava, String commitMsg) - throws Exception { - List<IResource> resources = new ArrayList<IResource>(); - resources.add(mainJava.getResource()); - new AddToIndexOperation(resources).execute(null); // add resource to git - new Git(repo).commit().setMessage(commitMsg).call(); // make commit - } - - private void createBranch(String branchName) throws Exception { - RefUpdate updateRef; - updateRef = repo.updateRef(Constants.R_HEADS + branchName); - Ref startRef = repo.getRef(branchName); - ObjectId startAt = repo.resolve(Constants.HEAD); - String startBranch; - if (startRef != null) - startBranch = branchName; - else - startBranch = startAt.name(); - startBranch = repo.shortenRefName(startBranch); - updateRef.setNewObjectId(startAt); - updateRef - .setRefLogMessage("branch: Created from " + startBranch, false); //$NON-NLS-1$ - updateRef.update(); + private byte[] getBytesAndCloseStream(InputStream stream) throws Exception { + try { + byte[] actualByte = new byte[stream.available()]; + stream.read(actualByte); + return actualByte; + } finally { + stream.close(); + } } - } diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitSyncInfoTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitSyncInfoTest.java deleted file mode 100644 index 22f858857e..0000000000 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitSyncInfoTest.java +++ /dev/null @@ -1,734 +0,0 @@ -/******************************************************************************* - * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org> - * - * 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.synchronize; - -import static org.easymock.EasyMock.createMock; -import static org.easymock.EasyMock.expect; -import static org.easymock.EasyMock.replay; -import static org.easymock.EasyMock.verify; -import static org.eclipse.team.core.synchronize.SyncInfo.ADDITION; -import static org.eclipse.team.core.synchronize.SyncInfo.CHANGE; -import static org.eclipse.team.core.synchronize.SyncInfo.CONFLICTING; -import static org.eclipse.team.core.synchronize.SyncInfo.DELETION; -import static org.eclipse.team.core.synchronize.SyncInfo.INCOMING; -import static org.eclipse.team.core.synchronize.SyncInfo.IN_SYNC; -import static org.eclipse.team.core.synchronize.SyncInfo.OUTGOING; -import static org.eclipse.team.core.synchronize.SyncInfo.PSEUDO_CONFLICT; -import static org.junit.Assert.assertEquals; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.util.Arrays; - -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.IPath; -import org.eclipse.egit.core.op.ConnectProviderOperation; -import org.eclipse.egit.core.project.RepositoryMapping; -import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; -import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; -import org.eclipse.egit.core.test.GitTestCase; -import org.eclipse.jgit.api.CommitCommand; -import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.lib.GitIndex; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.GitIndex.Entry; -import org.eclipse.jgit.revwalk.RevCommit; -import org.eclipse.jgit.revwalk.RevCommitList; -import org.eclipse.jgit.storage.file.FileRepository; -import org.junit.Before; -import org.junit.Test; - -public class GitSyncInfoTest extends GitTestCase { - - private Repository repo; - - private GitResourceVariantComparator comparator; - - @Before - public void setUp() throws Exception { - super.setUp(); - IProject iProject = project.project; - if (!gitDir.exists()) - new FileRepository(gitDir).create(); - - new ConnectProviderOperation(iProject, gitDir).execute(null); - repo = RepositoryMapping.getMapping(iProject).getRepository(); - - GitSynchronizeData data = new GitSynchronizeData(repo, "", "", true); - GitSynchronizeDataSet dataSet = new GitSynchronizeDataSet(data); - comparator = new GitResourceVariantComparator(dataSet, null); - } - - /** - * File is in sync when local, base and remote objects refer to identical - * file (same git ObjectId). - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnResourceFileInSync() throws Exception { - // when - - // given - String fileName = "test-file"; - byte[] localBytes = new byte[8200]; - Arrays.fill(localBytes, (byte) 'a'); - - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(2); - expect(local.getName()).andReturn(fileName).anyTimes(); - expect(local.getProject()).andReturn(project.getProject()); - expect(local.getContents()).andReturn( - new ByteArrayInputStream(localBytes)); - replay(local); - - ObjectId objectId = stageAndCommit(fileName, localBytes); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, objectId, null); - - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, objectId, null); - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(IN_SYNC, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Folders are in sync when they have same name. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnResourceFolderInSync() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(3); - expect(local.getName()).andReturn("src").anyTimes(); - replay(local); - - GitFolderResourceVariant base = new GitFolderResourceVariant(local); - GitFolderResourceVariant remote = new GitFolderResourceVariant(local); - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(IN_SYNC, gsi.getKind()); - verify(local); - } - - /** - * Outgoing change should be returned when base RevCommitList has more - * commits than remote RevCommitList - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnOutgoingFileChange() throws Exception { - // when - - // given - String fileName = "test-file"; - byte[] localBytes = new byte[8200]; - Arrays.fill(localBytes, (byte) 'a'); - - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(2); - expect(local.getName()).andReturn(fileName).anyTimes(); - expect(local.getProject()).andReturn(project.getProject()); - expect(local.getContents()).andReturn( - new ByteArrayInputStream(localBytes)); - replay(local); - - stage(fileName, localBytes); - RevCommit firstCommit = commit(); - localBytes[8120] = 'b'; - ObjectId objectId = stage(fileName, localBytes); - RevCommit secondCommit = commit(); - RevCommitList<RevCommit> baseCommits = new RevCommitList<RevCommit>(); - baseCommits.add(firstCommit); - baseCommits.add(secondCommit); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, objectId, baseCommits); // baseComits has two entry - - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - RevCommitList<RevCommit> remoteCommits = new RevCommitList<RevCommit>(); - remoteCommits.add(firstCommit); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, - repo, - ObjectId.fromString("0123456789012345678901234567890123456789"), - remoteCommits); // remoteCommits has only one entry - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(OUTGOING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Should return incoming change when remote RevCommitList has more commit - * objects then base RevCommitList - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnIncomingFileChange() throws Exception { - // when - - // given - String fileName = "test-file"; - byte[] localBytes = new byte[8200]; - Arrays.fill(localBytes, (byte) 'a'); - - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(2); - expect(local.getName()).andReturn(fileName).anyTimes(); - expect(local.getProject()).andReturn(project.getProject()); - expect(local.getContents()).andReturn( - new ByteArrayInputStream(localBytes)); - replay(local); - - ObjectId objectId = stage(fileName, localBytes); - RevCommit firstCommit = commit(); - RevCommitList<RevCommit> baseCommits = new RevCommitList<RevCommit>(); - baseCommits.add(firstCommit); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, objectId, baseCommits); // baseCommits has one element - - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - - stage(fileName, localBytes); - RevCommit secondCommit = commit(); - RevCommitList<RevCommit> remoteCommits = new RevCommitList<RevCommit>(); - remoteCommits.add(secondCommit); - remoteCommits.add(firstCommit); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, - repo, - ObjectId.fromString("0123456789012345678901234567890123456789"), - remoteCommits); // remoteCommits has two elements - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(INCOMING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Outgoing deletion should be returned when resource exist in base and - * remote but does not exist locally. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnOutgoingDeletion() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IPath path = createMock(IPath.class); - replay(path); - - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - expect(baseResource.getFullPath()).andReturn(path); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - expect(remoteResource.getFullPath()).andReturn(path); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(OUTGOING | DELETION, gsi.getKind()); - verify(local, baseResource, remoteResource, path); - } - - /** - * Incoming deletion should be returned when file exists locally and in base - * but does not exist in remote resource variant. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnIncomingFileDeletion() throws Exception { - // when - - // given - String fileName = "test-file"; - byte[] localBytes = new byte[8200]; - Arrays.fill(localBytes, (byte) 'a'); - - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(2); - expect(local.getName()).andReturn(fileName).anyTimes(); - expect(local.getProject()).andReturn(project.getProject()); - expect(local.getContents()).andReturn( - new ByteArrayInputStream(localBytes)); - replay(local); - - stage(fileName, localBytes); - RevCommit firstCommit = commit(); - ObjectId objectId = stage(fileName, localBytes); - RevCommit secondCommit = commit(); - RevCommitList<RevCommit> baseCommits = new RevCommitList<RevCommit>(); - baseCommits.add(firstCommit); - baseCommits.add(secondCommit); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, objectId, baseCommits); - - GitSyncInfo gsi = new GitSyncInfo(local, base, null, comparator); - gsi.init(); - - // then - assertEquals(INCOMING | DELETION, gsi.getKind()); - verify(local, baseResource); - } - - /** - * Outgoing addition should be returned when resource exists locally but it - * can't be found in base and remote resource variant. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnOutgoingAddition() throws Exception { - // when - - // given - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true).times(2); - expect(baseResource.isDerived()).andReturn(false); - expect(baseResource.getName()).andReturn("Mian.java").anyTimes(); - replay(baseResource); - GitSyncInfo gsi = new GitSyncInfo(baseResource, null, null, comparator); - gsi.init(); - - // then - assertEquals(OUTGOING | ADDITION, gsi.getKind()); - verify(baseResource); - } - - /** - * Conflicting change should be returned when remote and base RevCommitList - * have same number of RevCommit object's but these lists have one ore more - * different RevCommit objects. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFileChange() throws Exception { - // when - - // given - String fileName = "test-file"; - byte[] localBytes = new byte[8200]; - Arrays.fill(localBytes, (byte) 'a'); - - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(2); - expect(local.getName()).andReturn(fileName).anyTimes(); - expect(local.getProject()).andReturn(project.getProject()); - expect(local.getContents()).andReturn( - new ByteArrayInputStream(localBytes)); - replay(local); - - ObjectId objectId = stage(fileName, localBytes); - RevCommit firstCommit = commit(); - RevCommitList<RevCommit> baseCommits = new RevCommitList<RevCommit>(); - baseCommits.add(firstCommit); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, objectId, baseCommits); - - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - - stage(fileName, localBytes); - RevCommit secondCommit = commit(); - RevCommitList<RevCommit> remoteCommits = new RevCommitList<RevCommit>(); - remoteCommits.add(secondCommit); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, - repo, - ObjectId.fromString("0123456789012345678901234567890123456789"), - remoteCommits); - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Conflicting change should be returned when file was created locally with - * same name as incoming folder in remote. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFileChange1() throws Exception { - // when - - // given - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(1); - expect(local.getName()).andReturn("test-file").anyTimes(); - replay(local); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, null, null); - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); - - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Conflicting change should be returned when local resource differ from - * base resource variant and remote variant does not exist. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFileChange2() throws Exception { - // when - - // given - String fileName = "test-file"; - byte[] localBytes = new byte[8200]; - Arrays.fill(localBytes, (byte) 'a'); - - IFile local = createMock(IFile.class); - expect(local.isDerived()).andReturn(false); - expect(local.exists()).andReturn(true).times(2); - expect(local.getName()).andReturn(fileName).anyTimes(); - expect(local.getProject()).andReturn(project.getProject()); - expect(local.getContents()).andReturn( - new ByteArrayInputStream(localBytes)); - replay(local); - - stage(fileName, localBytes); - RevCommit firstCommit = commit(); - byte[] remoteBytes = new byte[localBytes.length]; - System.arraycopy(localBytes, 0, remoteBytes, 0, localBytes.length); - remoteBytes[8100] = 'b'; - ObjectId objectId = stage(fileName, remoteBytes); - RevCommit secondCommit = commit(); - RevCommitList<RevCommit> baseCommits = new RevCommitList<RevCommit>(); - baseCommits.add(firstCommit); - baseCommits.add(secondCommit); - - IFile baseResource = createMock(IFile.class); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, objectId, baseCommits); - - GitSyncInfo gsi = new GitSyncInfo(local, base, null, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource); - } - - /** - * Conflicting change when folder exists locally and remotely but it does - * not exist in base resource variant. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFolderChange() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.exists()).andReturn(true); - expect(local.isDerived()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(false); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Conflicting change when folder exists in base but it does not exist in - * local and remote. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFolderChange1() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.exists()).andReturn(false); - expect(local.isDerived()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(false); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /* - * When local resource is file, base resource variant is folder and remote - * variant is a file, then getKind() should return conflicting change. - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFolderAndFileChange() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.exists()).andReturn(false); - expect(local.isDerived()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, ObjectId.zeroId(), null); - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * When remote is folder and base is a file getKind() should return - * conflicting change - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingFileAndFolderChange() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.exists()).andReturn(false); - expect(local.isDerived()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IResource baseResource = createMock(IResource.class); - expect(baseResource.exists()).andReturn(true); - replay(baseResource); - GitBlobResourceVariant base = new GitBlobResourceVariant(baseResource, - repo, ObjectId.zeroId(), null); - - IResource remoteResource = createMock(IResource.class); - expect(remoteResource.exists()).andReturn(true); - replay(remoteResource); - GitFolderResourceVariant remote = new GitFolderResourceVariant( - remoteResource); - GitSyncInfo gsi = new GitSyncInfo(local, base, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | CHANGE, gsi.getKind()); - verify(local, baseResource, remoteResource); - } - - /** - * Remote resource variant was not found, local resource does not exist but - * there is a base resource. In such situation we should return conflicting - * deletion. - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingDeletationPseudoConflict() - throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.exists()).andReturn(false); - expect(local.isDerived()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IResource baseResource = createMock(IResource.class); - replay(baseResource); - GitFolderResourceVariant base = new GitFolderResourceVariant( - baseResource); - - GitSyncInfo gsi = new GitSyncInfo(local, base, null, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | DELETION | PSEUDO_CONFLICT, gsi.getKind()); - verify(local, baseResource); - } - - /** - * Conflicting addition should be returned when resource exists in local and - * remote but cannot be found in base - * @throws Exception - */ - @Test - @SuppressWarnings("boxing") - public void shouldReturnConflictingAddition() throws Exception { - // when - - // given - IResource local = createMock(IResource.class); - expect(local.exists()).andReturn(true).times(3); - expect(local.isDerived()).andReturn(false); - expect(local.getName()).andReturn("Mian.java").anyTimes(); - replay(local); - - IResource remoteResource = createMock(IResource.class); - replay(remoteResource); - GitBlobResourceVariant remote = new GitBlobResourceVariant( - remoteResource, repo, ObjectId.zeroId(), null); - GitSyncInfo gsi = new GitSyncInfo(local, null, remote, comparator); - gsi.init(); - - // then - assertEquals(CONFLICTING | ADDITION, gsi.getKind()); - verify(local, remoteResource); - } - - private ObjectId stageAndCommit(String fileName, byte[] content) - throws Exception { - ObjectId objectId = stage(fileName, content); - commit(); - - return objectId; - } - - private ObjectId stage(String fileName, byte[] content) throws Exception { - // TODO reimplement using DirCache class - GitIndex index = repo.getIndex(); - File workdir = project.getProject().getFullPath().toFile(); - File file = new File(workdir, fileName); - Entry entry = index.add(workdir, file, content); - index.write(); - - return entry.getObjectId(); - } - - private RevCommit commit() throws Exception { - Git git = new Git(repo); - CommitCommand commit = git.commit(); - commit.setMessage("Initial commit"); - commit.setAuthor("EGit", "egi@eclipse.org"); - return commit.call(); - } - -} diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitTestResourceVariantTree.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitTestResourceVariantTree.java index 0cdd595f76..fbbd67bd84 100644 --- a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitTestResourceVariantTree.java +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/synchronize/GitTestResourceVariantTree.java @@ -8,12 +8,10 @@ *******************************************************************************/ package org.eclipse.egit.core.synchronize; -import java.io.IOException; - -import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Tree; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.team.core.TeamException; import org.eclipse.team.core.variants.ResourceVariantByteStore; /** @@ -25,18 +23,12 @@ class GitTestResourceVariantTree extends GitResourceVariantTree { GitTestResourceVariantTree(GitSynchronizeDataSet data, ResourceVariantByteStore store) { - super(data, store); - } - - @Override - ObjectId getRevObjId(IResource resource) throws IOException { - // not used in test case - return null; + super(store, data); } @Override - Tree getRevTree(IResource resource) throws IOException { - // TODO not used in test case + protected RevCommit getRevCommit(GitSynchronizeData gsd) + throws TeamException { return null; } 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 2ec001411e..0cd8f283b8 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 @@ -9,10 +9,13 @@ *******************************************************************************/ package org.eclipse.egit.core.test; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.URL; +import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; @@ -111,6 +114,24 @@ public class TestProject { return cu.getTypes()[0]; } + public IFile createFile(String name, byte[] content) throws Exception { + IFile file = project.getFile(name); + InputStream inputStream = new ByteArrayInputStream(content); + file.create(inputStream, true, null); + + return file; + } + + public IFolder createFolder(String name) throws Exception { + IFolder folder = project.getFolder(name); + folder.create(true, true, null); + + IFile keep = project.getFile(name + "/keep"); + keep.create(new ByteArrayInputStream(new byte[] {0}), true, null); + + return folder; + } + public void dispose() throws CoreException, IOException { waitForIndexer(); if (project.exists()) 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 index cd5a5a87f1..fb68ba22a2 100644 --- 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 @@ -11,12 +11,18 @@ package org.eclipse.egit.core.test; import static org.junit.Assert.assertNotNull; import java.io.File; +import java.io.FileWriter; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; +import java.util.regex.Pattern; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; +import org.eclipse.egit.core.op.BranchOperation; import org.eclipse.egit.core.op.ConnectProviderOperation; +import org.eclipse.egit.core.op.DisconnectProviderOperation; import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.ConcurrentRefUpdateException; import org.eclipse.jgit.api.Git; @@ -28,12 +34,12 @@ import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.errors.UnmergedPathException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.GitIndex; +import org.eclipse.jgit.lib.GitIndex.Entry; 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; import org.eclipse.jgit.storage.file.FileRepository; @@ -44,6 +50,7 @@ import org.eclipse.jgit.storage.file.FileRepository; public class TestRepository { Repository repository; + String workdirPrefix; /** @@ -61,12 +68,13 @@ public class TestRepository { workdirPrefix = repository.getWorkTree().getAbsolutePath(); } workdirPrefix = workdirPrefix.replace('\\', '/'); - if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$ - workdirPrefix += "/"; //$NON-NLS-1$ + if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$ + workdirPrefix += "/"; //$NON-NLS-1$ } /** * Creates a test repository from an existing Repository + * * @param repository * @throws IOException */ @@ -78,8 +86,8 @@ public class TestRepository { workdirPrefix = repository.getWorkTree().getAbsolutePath(); } workdirPrefix = workdirPrefix.replace('\\', '/'); - if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$ - workdirPrefix += "/"; //$NON-NLS-1$ + if (!workdirPrefix.endsWith("/")) //$NON-NLS-1$ + workdirPrefix += "/"; //$NON-NLS-1$ } /** @@ -113,6 +121,81 @@ public class TestRepository { } /** + * Create new file + * + * @param project + * instance of project inside with file will be created + * @param name + * name of file + * @return nearly created file + * @throws IOException + */ + public File createFile(IProject project, String name) throws IOException { + String path = project.getLocation().append(name).toOSString(); + int lastSeparator = path.lastIndexOf(File.separator); + new File(path.substring(0, lastSeparator)).mkdirs(); + + File file = new File(path); + file.createNewFile(); + + return file; + } + + /** + * Track, add to index and finally commit given file + * + * @param project + * @param file + * @param commitMessage + * @return commit object + * @throws Exception + */ + public RevCommit addAndCommit(IProject project, File file, String commitMessage) + throws Exception { + track(file); + addToIndex(project, file); + + return commit(commitMessage); + } + + /** + * Appends file content to given file, then track, add to index and finally + * commit it. + * + * @param project + * @param file + * @param content + * @param commitMessage + * @return commit object + * @throws Exception + */ + public RevCommit appendContentAndCommit(IProject project, File file, + byte[] content, String commitMessage) throws Exception { + return appendContentAndCommit(project, file, new String(content), + commitMessage); + } + + /** + * Appends file content to given file, then track, add to index and finally + * commit it. + * + * @param project + * @param file + * @param content + * @param commitMessage + * @return commit object + * @throws Exception + */ + public RevCommit appendContentAndCommit(IProject project, File file, + String content, String commitMessage) throws Exception { + appendFileContent(file, content); + track(file); + addToIndex(project, file); + + return commit(commitMessage); + } + + /** * Commits the current index * * @param message @@ -152,6 +235,19 @@ public class TestRepository { } /** + * Creates a new branch and immediately checkout it. + * + * @param refName + * starting point for the new branch + * @param newRefName + * @throws Exception + */ + public void createAndCheckoutBranch(String refName, String newRefName) throws Exception { + createBranch(refName, newRefName); + checkoutBranch(newRefName); + } + + /** * Creates a new branch * * @param refName @@ -178,13 +274,39 @@ public class TestRepository { } /** + * Checkouts branch + * + * @param refName + * full name of branch + * @throws CoreException + */ + public void checkoutBranch(String refName) throws CoreException { + new BranchOperation(repository, refName).execute(null); + } + + /** + * Adds the given file to the index + * + * @param project + * @param file + * @throws Exception + */ + public void addToIndex(IProject project, File file) throws Exception { + IFile iFile = getIFile(project, file); + addToIndex(iFile); + } + + + /** * 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())); + Entry entry = index.getEntry(getRepoRelativePath(file.getLocation() + .toOSString())); assertNotNull(entry); if (entry.isModified(repository.getWorkTree())) entry.update(new File(repository.getWorkTree(), entry.getName())); @@ -192,7 +314,62 @@ public class TestRepository { } /** + * Appends content to end of given file. + * + * @param file + * @param content + * @throws IOException + */ + public void appendFileContent(File file, byte[] content) throws IOException { + appendFileContent(file, new String(content), true); + } + + /** + * Appends content to end of given file. + * + * @param file + * @param content + * @throws IOException + */ + public void appendFileContent(File file, String content) throws IOException { + appendFileContent(file, new String(content), true); + } + + /** + * Appends content to given file. + * + * @param file + * @param content + * @param append + * if true, then bytes will be written to the end of the file + * rather than the beginning + * @throws IOException + */ + public void appendFileContent(File file, byte[] content, boolean append) + throws IOException { + appendFileContent(file, new String(content), append); + } + + /** + * Appends content to given file. + * + * @param file + * @param content + * @param append + * if true, then bytes will be written to the end of the file + * rather than the beginning + * @throws IOException + */ + public void appendFileContent(File file, String content, boolean append) + throws IOException { + FileWriter fw = new FileWriter(file, append); + fw.append(content); + fw.close(); + } + + /** * Checks if a file with the given path exists in the HEAD tree + * * @param path * @return true if the file exists * @throws IOException @@ -234,10 +411,22 @@ public class TestRepository { if (pLen > pfxLen) return path.substring(pfxLen); else if (path.length() == pfxLen - 1) - return ""; //$NON-NLS-1$ + return ""; //$NON-NLS-1$ return null; } + public IFile getIFile(IProject project, File file) throws CoreException { + String relativePath = getRepoRelativePath(file.getAbsolutePath()); + + String quotedProjectName = Pattern.quote(project.getName()); + relativePath = relativePath.replaceFirst(quotedProjectName, ""); + + IFile iFile = project.getFile(relativePath); + iFile.refreshLocal(0, null); + + return iFile; + } + public void dispose() { repository.close(); repository = null; @@ -245,6 +434,7 @@ public class TestRepository { /** * Connect a project to this repository + * * @param project * @throws CoreException */ @@ -253,4 +443,19 @@ public class TestRepository { this.getRepository().getDirectory()); op.execute(null); } + + /** + * Disconnects provider from project + * + * @param project + * @throws CoreException + */ + public void disconnect(IProject project) throws CoreException { + Collection<IProject> projects = Collections.singleton(project + .getProject()); + DisconnectProviderOperation disconnect = new DisconnectProviderOperation( + projects); + disconnect.execute(null); + } + } diff --git a/org.eclipse.egit.core/META-INF/MANIFEST.MF b/org.eclipse.egit.core/META-INF/MANIFEST.MF index fa338028e3..a42ebcba92 100644 --- a/org.eclipse.egit.core/META-INF/MANIFEST.MF +++ b/org.eclipse.egit.core/META-INF/MANIFEST.MF @@ -29,6 +29,7 @@ Import-Package: org.eclipse.jgit.api;version="[0.9.0,0.10.0)", org.eclipse.jgit.merge;version="[0.9.0,0.10.0)", org.eclipse.jgit.revwalk;version="[0.9.0,0.10.0)", org.eclipse.jgit.storage.file;version="[0.9.0,0.10.0)", + org.eclipse.jgit.revwalk.filter;version="[0.9.0,0.10.0)", org.eclipse.jgit.transport;version="[0.9.0,0.10.0)", org.eclipse.jgit.treewalk;version="[0.9.0,0.10.0)", org.eclipse.jgit.treewalk.filter;version="[0.9.0,0.10.0)", diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java index ae85fcca97..3a45085014 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java @@ -263,16 +263,19 @@ public class CoreText extends NLS { public static String TagOperation_objectIdNotFound; /** */ - public static String GitResourceVariantTree_couldNotFindBlob; + public static String GitResourceVariantTree_couldNotFindResourceVariant; /** */ - public static String GitResourceVariantTree_fetchingMembers; + public static String GitResourceVariantTree_couldNotFetchMembers; + + /** */ + public static String GitFolderResourceVariant_fetchingMembers; /** */ public static String GitResourceVariantTree_fetchingVariant; /** */ - public static String GitResourceVariantTree_unableToReadRepository; + public static String GitResourceVariantTree_couldNotFetchMembersOf; /** */ public static String GitBranchResourceVariantTreeSubscriber_gitRepository; @@ -287,4 +290,5 @@ public class CoreText extends NLS { initializeMessages("org.eclipse.egit.core.coretext", //$NON-NLS-1$ CoreText.class); } + } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties b/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties index 8101d4482a..a00fbaa470 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties @@ -108,12 +108,14 @@ TagOperation_performingTagging=Making tag {0} TagOperation_taggingFailure=Tag {0} creation failed (cause: {1}) TagOperation_objectIdNotFound=Could not find object Id associated with tag {0} (cause: {1}) -GitResourceVariantTree_couldNotFindBlob=Could not find blob member for {0} -GitResourceVariantTree_fetchingMembers=Fetching members of {0} +GitFolderResourceVariant_fetchingMembers=Fetching members of {0} + +GitResourceVariantTree_couldNotFindResourceVariant=Could not find variant for resource: {0} +GitResourceVariantTree_couldNotFetchMembers=Could not fetch members of {0} GitResourceVariantTree_fetchingVariant=Fetching variant for: {0} -GitResourceVariantTree_unableToReadRepository="Unable to read repository for {0} +GitResourceVariantTree_couldNotFetchMembersOf=Could not fetch members of {0} GitBranchResourceVariantTreeSubscriber_gitRepository=Git without local changes OperationAlreadyExecuted=Operation has already been executed and can not be executed again OperationNotYetExecuted=Operation has not yet been executed and can not return a result -RemoteRefUpdateCantBeReused=The RemoteRefUpdate instance can not be re-used
\ No newline at end of file +RemoteRefUpdateCantBeReused=The RemoteRefUpdate instance can not be re-used diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBaseResourceVariantTree.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBaseResourceVariantTree.java index 9ef41da54e..3439028e66 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBaseResourceVariantTree.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBaseResourceVariantTree.java @@ -13,26 +13,46 @@ package org.eclipse.egit.core.synchronize; import java.io.IOException; -import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Tree; -import org.eclipse.team.core.variants.ResourceVariantByteStore; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.revwalk.filter.RevFilter; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.variants.SessionResourceVariantByteStore; class GitBaseResourceVariantTree extends GitResourceVariantTree { - GitBaseResourceVariantTree(GitSynchronizeDataSet data, ResourceVariantByteStore store) { - super(data, store); + GitBaseResourceVariantTree(GitSynchronizeDataSet gsds) { + super(new SessionResourceVariantByteStore(), gsds); } @Override - Tree getRevTree(IResource resource) throws IOException { - return getSyncData().getData(resource.getProject()).mapSrcTree(); - } + protected RevCommit getRevCommit(GitSynchronizeData gsd) + throws TeamException { + RevCommit result; + Repository repo = gsd.getRepository(); + RevWalk rw = new RevWalk(repo); + rw.setRevFilter(RevFilter.MERGE_BASE); - @Override - ObjectId getRevObjId(IResource resource) throws IOException { - return getSyncData().getData(resource.getProject()).getSrcObjectId(); + try { + Ref srcRef = repo.getRef(gsd.getSrcRev()); + Ref dstRef = repo.getRef(gsd.getDstRev()); + + RevCommit srcRev = rw.parseCommit(srcRef.getObjectId()); + RevCommit dstRev = rw.parseCommit(dstRef.getObjectId()); + + rw.markStart(dstRev); + rw.markStart(srcRev); + + result = rw.next(); + } catch (IOException e) { + throw new TeamException(e.getMessage(), e); + } + + return result != null ? result : null; } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBlobResourceVariant.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBlobResourceVariant.java index efce2d9ad1..ff25e50fc3 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBlobResourceVariant.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitBlobResourceVariant.java @@ -7,14 +7,15 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Dariusz Luksza <dariusz@luksza.org> *******************************************************************************/ package org.eclipse.egit.core.synchronize; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import org.eclipse.core.resources.IEncodedStorage; -import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; @@ -25,11 +26,12 @@ import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.content.IContentDescription; import org.eclipse.core.runtime.content.IContentTypeManager; import org.eclipse.egit.core.Activator; -import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; -import org.eclipse.jgit.revwalk.RevCommitList; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; import org.eclipse.team.core.TeamException; /** @@ -37,28 +39,18 @@ import org.eclipse.team.core.TeamException; */ class GitBlobResourceVariant extends GitResourceVariant { - private ObjectId id; - - private Repository repository; - private IStorage storage; - private RevCommitList<RevCommit> commitList; - - GitBlobResourceVariant(IResource resource, Repository repository, - ObjectId id, RevCommitList<RevCommit> commitList) { - super(resource); - this.repository = repository; - this.id = id; - this.commitList = commitList; - } + private byte[] bytes; - ObjectId getId() { - return id; - } + GitBlobResourceVariant(Repository repo, RevCommit revCommit, String path) + throws IOException { + super(repo, revCommit, path); - RevCommitList<RevCommit> getCommitList() { - return commitList; + if (getObjectId() != null) { + ObjectLoader blob = repo.open(getObjectId()); + bytes = blob.getBytes(); + } } public boolean isContainer() { @@ -85,13 +77,7 @@ class GitBlobResourceVariant extends GitResourceVariant { } public InputStream getContents() throws CoreException { - try { - return repository.open(id, Constants.OBJ_BLOB) - .openStream(); - } catch (IOException err) { - throw new TeamException(new Status(IStatus.ERROR, - Activator.getPluginId(), err.getMessage(), err)); - } + return new ByteArrayInputStream(bytes); } public String getCharset() throws CoreException { @@ -110,11 +96,24 @@ class GitBlobResourceVariant extends GitResourceVariant { } }; } + return storage; } - public String getContentIdentifier() { - return id.name(); + public byte[] asBytes() { + return bytes; + } + + @Override + protected TreeWalk getTreeWalk(Repository repo, RevTree revTree, + String path) throws IOException { + TreeWalk tw = new TreeWalk(repo); + tw.reset(); + tw.addTree(revTree); + tw.setRecursive(true); + tw.setFilter(PathFilter.create(path)); + + return tw.next() ? tw : null; } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitFolderResourceVariant.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitFolderResourceVariant.java index 682db175fa..2d408e1eb1 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitFolderResourceVariant.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitFolderResourceVariant.java @@ -7,23 +7,36 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Dariusz Luksza <dariusz@luksza.org> *******************************************************************************/ package org.eclipse.egit.core.synchronize; -import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IResource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import org.eclipse.core.resources.IStorage; import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.egit.core.CoreText; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; +import org.eclipse.osgi.util.NLS; import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.variants.IResourceVariant; class GitFolderResourceVariant extends GitResourceVariant { - GitFolderResourceVariant(IResource resource) { - super(resource); - } + private TreeWalk tw; + + private IResourceVariant members[]; - IContainer getContainer() { - return (IContainer) getResource(); + GitFolderResourceVariant(Repository repo, RevCommit revCommit, String path) + throws IOException { + super(repo, revCommit, path); } public boolean isContainer() { @@ -34,26 +47,59 @@ class GitFolderResourceVariant extends GitResourceVariant { return null; } - public String getContentIdentifier() { - return getName(); + public byte[] asBytes() { + return getName().getBytes(); } - @Override - public int hashCode() { - return getResource().hashCode(); + public IResourceVariant[] getMembers(IProgressMonitor progress) + throws IOException { + if (members != null) + try { + return members; + } finally { + progress.done(); + } + + IProgressMonitor monitor = SubMonitor.convert(progress); + monitor.beginTask( + NLS.bind(CoreText.GitFolderResourceVariant_fetchingMembers, this), + tw.getTreeCount()); + + Repository repo = getRepository(); + List<IResourceVariant> result = new ArrayList<IResourceVariant>(); + + try { + while (tw.next()) { + String path = getPath() + "/" + new String(tw.getRawPath()); //$NON-NLS-1$ + if (tw.isSubtree()) + result.add(new GitFolderResourceVariant(repo, getRevCommit(), + path)); + else + result.add(new GitBlobResourceVariant(repo, getRevCommit(), + path)); + monitor.worked(1); + } + + members = result.toArray(new IResourceVariant[result.size()]); + return members; + } finally { + monitor.done(); + } } @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } else if (obj == null) { - return false; - } else if (getClass() != obj.getClass()) { - return false; - } - GitFolderResourceVariant other = (GitFolderResourceVariant) obj; - return getResource().equals(other.getResource()); + protected TreeWalk getTreeWalk(Repository repo, RevTree revTree, String path) + throws IOException { + tw = new TreeWalk(repo); + tw.reset(); + tw.addTree(revTree); + tw.setFilter(PathFilter.create(path)); + + while (tw.next() && !path.equals(tw.getPathString())) + if (tw.isSubtree()) + tw.enterSubtree(); + + return tw; } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitRemoteResourceVariantTree.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitRemoteResourceVariantTree.java index 4742a39bde..ae90e15697 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitRemoteResourceVariantTree.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitRemoteResourceVariantTree.java @@ -13,26 +13,40 @@ package org.eclipse.egit.core.synchronize; import java.io.IOException; -import org.eclipse.core.resources.IResource; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.Tree; -import org.eclipse.team.core.variants.ResourceVariantByteStore; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.revwalk.filter.RevFilter; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.variants.SessionResourceVariantByteStore; class GitRemoteResourceVariantTree extends GitResourceVariantTree { - GitRemoteResourceVariantTree(GitSynchronizeDataSet data, ResourceVariantByteStore store) { - super(data, store); + GitRemoteResourceVariantTree(GitSynchronizeDataSet data) { + super(new SessionResourceVariantByteStore(), data); } @Override - Tree getRevTree(IResource resource) throws IOException { - return getSyncData().getData(resource.getProject()).mapDstTree(); - } + protected RevCommit getRevCommit(GitSynchronizeData gsd) throws TeamException { + RevCommit result; + Repository repo = gsd.getRepository(); + RevWalk rw = new RevWalk(repo); + rw.setRevFilter(RevFilter.MERGE_BASE); - @Override - ObjectId getRevObjId(IResource resource) throws IOException { - return getSyncData().getData(resource.getProject()).getDstObjectId(); + try { + Ref dstRef = repo.getRef(gsd.getDstRev()); + if (dstRef == null) + result = null; + else + result = rw.parseCommit(dstRef.getObjectId()); + } catch (IOException e) { + throw new TeamException("", e); //$NON-NLS-1$ + } + + return result != null ? result : null; } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariant.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariant.java index 546da080b1..c3ec40b6a3 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariant.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariant.java @@ -7,30 +7,132 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Dariusz Luksza <dariusz@luksza.org> *******************************************************************************/ package org.eclipse.egit.core.synchronize; -import org.eclipse.core.resources.IResource; +import java.io.IOException; + +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.team.core.variants.IResourceVariant; abstract class GitResourceVariant implements IResourceVariant { - private IResource resource; + private final String path; + + private final Repository repo; + + private final ObjectId objectId; + + private final RevCommit revCommit; - GitResourceVariant(IResource resource) { - this.resource = resource; + private String name; + + private IPath fullPath; + + /** + * Construct Git representation of {@link IResourceVariant}. + * + * @param repo + * @param revCommit + * @param path + * should be repository relative + * @throws IOException + */ + GitResourceVariant(Repository repo, RevCommit revCommit, String path) + throws IOException { + this.repo = repo; + this.revCommit = revCommit; + TreeWalk tw = getTreeWalk(repo, revCommit.getTree(), path); + if (tw == null) { + objectId = null; + this.path = null; + } else { + objectId = tw.getObjectId(0); + this.path = new String(tw.getRawPath()); + } } - public IResource getResource() { - return resource; + public String getContentIdentifier() { + return objectId.getName(); } public String getName() { - return resource.getName(); + if (name == null && path != null) { + int lastSeparator = path.lastIndexOf('/'); + if (lastSeparator > -1) + name = path.substring(lastSeparator + 1); + else + name = path; + } + + return name; + } + + @Override + public int hashCode() { + return objectId.getName().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof GitResourceVariant) + return objectId.equals(((GitResourceVariant) obj).getObjectId()); + + return false; + } + + @Override + public String toString() { + return path + "(" + objectId.getName() + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } + + /** + * + * @param repo + * @param revTree + * base commit + * @param path + * to resource variant + * @return new tree walk positioned on given object or <code>null</code> + * when given path was not found in repository + * @throws IOException + * when something goes wrong during tree walk initialization + */ + protected abstract TreeWalk getTreeWalk(Repository repo, RevTree revTree, + String path) throws IOException; + + protected ObjectId getObjectId() { + return objectId; + } + + protected Repository getRepository() { + return repo; + } + + protected RevCommit getRevCommit() { + return revCommit; + } + + protected String getPath() { + return path; + } + + protected IPath getFullPath() { + if (fullPath == null) + fullPath = new Path(path); + + return fullPath; } - public byte[] asBytes() { - return getContentIdentifier().getBytes(); + public boolean exists() { + return objectId != null && !objectId.equals(ObjectId.zeroId()); } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparator.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparator.java index 5f5caed04d..ae8cf69eb1 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparator.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantComparator.java @@ -23,24 +23,18 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.Status; -import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; import org.eclipse.egit.core.Activator; -import org.eclipse.egit.core.synchronize.GitBlobResourceVariant; -import org.eclipse.egit.core.synchronize.GitFolderResourceVariant; -import org.eclipse.egit.core.synchronize.GitResourceVariant; +import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; import org.eclipse.team.core.TeamException; import org.eclipse.team.core.variants.IResourceVariant; import org.eclipse.team.core.variants.IResourceVariantComparator; -import org.eclipse.team.core.variants.ResourceVariantByteStore; class GitResourceVariantComparator implements IResourceVariantComparator { private final GitSynchronizeDataSet gsd; - private final ResourceVariantByteStore store; - public GitResourceVariantComparator(GitSynchronizeDataSet dataSet, ResourceVariantByteStore store) { + GitResourceVariantComparator(GitSynchronizeDataSet dataSet) { gsd = dataSet; - this.store = store; } public boolean compare(IResource local, IResourceVariant remote) { @@ -56,8 +50,8 @@ class GitResourceVariantComparator implements IResourceVariantComparator { InputStream stream = null; InputStream remoteStream = null; try { - remoteStream = remote.getStorage( - new NullProgressMonitor()).getContents(); + remoteStream = remote.getStorage(new NullProgressMonitor()) + .getContents(); stream = getLocal(local); byte[] remoteBytes = new byte[8096]; byte[] bytes = new byte[8096]; @@ -94,9 +88,8 @@ class GitResourceVariantComparator implements IResourceVariantComparator { return false; } - GitFolderResourceVariant gitVariant = (GitFolderResourceVariant) remote; - return local.getFullPath().equals( - gitVariant.getResource().getFullPath()); + GitResourceVariant gitVariant = (GitResourceVariant) remote; + return local.getFullPath().equals(gitVariant.getFullPath()); } return false; } @@ -104,26 +97,9 @@ class GitResourceVariantComparator implements IResourceVariantComparator { public boolean compare(IResourceVariant base, IResourceVariant remote) { GitResourceVariant gitBase = (GitResourceVariant) base; GitResourceVariant gitRemote = (GitResourceVariant) remote; - IResource resourceBase = gitBase.getResource(); - IResource resourceRemote = gitRemote.getResource(); - if (!resourceBase.exists() || !resourceRemote.exists()) { - return false; - } - - if (base.isContainer()) { - if (remote.isContainer()) { - return resourceBase.getFullPath().equals( - resourceRemote.getFullPath()); - } - return false; - } else if (remote.isContainer()) { - return false; - } - - GitBlobResourceVariant baseBlob = (GitBlobResourceVariant) base; - GitBlobResourceVariant remoteBlob = (GitBlobResourceVariant) remote; - return baseBlob.getId().equals(remoteBlob.getId()); + boolean exists = gitBase.exists() && gitRemote.exists(); + return exists && gitBase.getObjectId().equals(gitRemote.getObjectId()); } public boolean isThreeWay() { @@ -131,22 +107,22 @@ class GitResourceVariantComparator implements IResourceVariantComparator { } private InputStream getLocal(IResource resource) throws CoreException { - if (gsd.getData(resource.getProject()).shouldIncludeLocal()) { + if (gsd.getData(resource.getProject().getName()).shouldIncludeLocal()) return ((IFile) resource).getContents(); - } else { + else try { - byte[] bytes = store.getBytes(resource); - return new ByteArrayInputStream(bytes != null ? bytes : new byte[0]); + if (resource.getType() == IResource.FILE) + return ((IFile) resource).getContents(); + else + return new ByteArrayInputStream(null); } catch (TeamException e) { throw new CoreException(e.getStatus()); } - } - } private void logException(Exception e) { - IStatus error = new Status(IStatus.ERROR, Activator - .getPluginId(), e.getMessage(), e); + IStatus error = new Status(IStatus.ERROR, Activator.getPluginId(), + e.getMessage(), e); Activator.getDefault().getLog().log(error); } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTree.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTree.java index 9dc09c1a24..b20f24753b 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTree.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTree.java @@ -11,436 +11,149 @@ *******************************************************************************/ package org.eclipse.egit.core.synchronize; -import java.io.File; import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Set; -import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFolder; -import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.core.runtime.Path; -import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.egit.core.Activator; import org.eclipse.egit.core.CoreText; -import org.eclipse.egit.core.project.RepositoryMapping; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeData; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; -import org.eclipse.jgit.lib.AbstractIndexTreeVisitor; -import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.IndexTreeWalker; -import org.eclipse.jgit.lib.ObjectId; -import org.eclipse.jgit.lib.ObjectLoader; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.Tree; -import org.eclipse.jgit.lib.TreeEntry; -import org.eclipse.jgit.lib.GitIndex.Entry; import org.eclipse.jgit.revwalk.RevCommit; -import org.eclipse.jgit.revwalk.RevCommitList; -import org.eclipse.jgit.revwalk.RevSort; -import org.eclipse.jgit.revwalk.RevWalk; -import org.eclipse.jgit.treewalk.filter.AndTreeFilter; -import org.eclipse.jgit.treewalk.filter.PathFilterGroup; -import org.eclipse.jgit.treewalk.filter.TreeFilter; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.treewalk.filter.PathFilter; import org.eclipse.osgi.util.NLS; import org.eclipse.team.core.TeamException; -import org.eclipse.team.core.variants.AbstractResourceVariantTree; import org.eclipse.team.core.variants.IResourceVariant; import org.eclipse.team.core.variants.ResourceVariantByteStore; +import org.eclipse.team.core.variants.ResourceVariantTree; -abstract class GitResourceVariantTree extends AbstractResourceVariantTree { +abstract class GitResourceVariantTree extends ResourceVariantTree { - /** - * A map of a given resource's trail of commits. - */ - private Map<String, RevCommitList<RevCommit>> dates = new HashMap<String, RevCommitList<RevCommit>>(); + private final GitSynchronizeDataSet gsds; - /** - * A map of a given resource to its latest blob within the branch. - */ - private Map<String, ObjectId> updated = new HashMap<String, ObjectId>(); - - /** - * A map of repositories to their trees. - */ - private Map<Repository, Tree> trees = new HashMap<Repository, Tree>(); - - private GitSynchronizeDataSet gsdData; - - private final ResourceVariantByteStore store; - - GitResourceVariantTree(GitSynchronizeDataSet data, - ResourceVariantByteStore store) { - this.store = store; - this.gsdData = data; + GitResourceVariantTree(ResourceVariantByteStore store, + GitSynchronizeDataSet gsds) { + super(store); + this.gsds = gsds; } public IResource[] roots() { Set<IResource> roots = new HashSet<IResource>(); - for (GitSynchronizeData gsd : gsdData) { + for (GitSynchronizeData gsd : gsds) roots.addAll(gsd.getProjects()); - } - return roots.toArray(new IResource[roots.size()]); - } - - public IResource[] members(IResource resource) throws TeamException { - if (resource.exists() && resource instanceof IContainer) { - GitSynchronizeData gsd = getSyncData().getData( - resource.getProject()); - if (gsd.shouldIncludeLocal()) { - try { - return ((IContainer) resource).members(); - } catch (CoreException e) { - throw new TeamException(e.getStatus()); - } - } else { - return getMembersAndStore(resource, gsd); - } - } - return new IResource[0]; - } - - /** - * Returns whether this file is of interest to this resource variant tree. - * Due to the fact that a repository may have many, many files, we only want - * to retrieve and store information about files that the user is actually - * interested in. That is, if they only wish to synchronize on one project, - * then there is no reason for this tree to be storing information about - * other projects that are contained within the repository. - * - * @param file - * the file to check - * @return <code>true</code> if the blob information about this file is of - * interest to this tree, <code>false</code> otherwise - */ - private boolean contains(File file) { - for (GitSynchronizeData gsd : gsdData) { - if (gsd.contains(file)) { - return true; - } - } - - return false; - } - - /** - * Retrieves the name of the branch that this variant tree should be - * compared against for the given resource. - * - * @param resource - * the resource that is being compared for - * @return the name of the target comparison branch - * @throws IOException - */ - abstract Tree getRevTree(IResource resource) throws IOException; - abstract ObjectId getRevObjId(IResource resource) throws IOException; - - /** - * Initializes the repository information for the specified resource. - * - * @param resource - * the resource that needs to have its repository information - * initialized for - * @throws IOException - * if an error occurs while walking the branch - */ - private synchronized void initialize(IResource resource) throws IOException { - IProject project = resource.getProject(); - if (!gsdData.contains(project)) { - return; - } - - Repository db = gsdData.getData(project).getRepository(); - if (!trees.containsKey(db)) { - Tree tree = getRevTree(resource); - ObjectId objId = getRevObjId(resource); - - if (objId != null && tree != null) { - trees.put(db, tree); - // walk the tree to retrieve information - walk(db, objId, tree); - } - } - } - - private void walk(final Repository db, final ObjectId objId, Tree merge) - throws IOException { - IndexTreeWalker walker = new IndexTreeWalker(db.getIndex(), merge, db - .getWorkTree(), new AbstractIndexTreeVisitor() { - public void visitEntry(TreeEntry treeEntry, Entry indexEntry, - File file) throws IOException { - if (treeEntry != null && contains(file)) { - store(db, objId, treeEntry); - } - } - }); - walker.walk(); - } - - private void store(Repository db, ObjectId objId, TreeEntry treeEntry) - throws IOException { - String entry = treeEntry.getFullName(); - RevWalk walk = new RevWalk(db); - walk.sort(RevSort.COMMIT_TIME_DESC, true); - walk.sort(RevSort.BOUNDARY, true); - walk.markStart(walk.parseCommit(objId)); - walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup - .createFromStrings(Collections.singleton(entry)), - TreeFilter.ANY_DIFF)); - - RevCommitList<RevCommit> list = new RevCommitList<RevCommit>(); - list.source(walk); - - int lastSize = 0; - do { - lastSize = list.size(); - list.fillTo(Integer.MAX_VALUE); - } while (lastSize != list.size()); - - dates.put(entry, list); - updated.put(entry, treeEntry.getId()); + return roots.toArray(new IResource[roots.size()]); } - public IResourceVariant getResourceVariant(IResource resource) + public IResourceVariant getResourceVariant(final IResource resource) throws TeamException { - return fetchVariant(resource, 0, new NullProgressMonitor()); - } - - private IResourceVariant findFolderVariant(IResource resource, - Repository repository) { - File workDir = repository.getWorkTree(); - if (resource.getLocation() == null) + if (resource == null) return null; - File resourceLocation = resource.getLocation().toFile(); - String resLocationAbsolutePath = resourceLocation.getAbsolutePath(); - - for (Map.Entry<String, ObjectId> entry : updated.entrySet()) { - String entryName = entry.getKey(); - File file = new File(workDir, entryName); - - if (file.getAbsolutePath().startsWith(resLocationAbsolutePath)) - return new GitFolderResourceVariant(resource); - } - return null; - } + GitSynchronizeData gsd = gsds.getData(resource.getProject()); + if (gsd == null) + return null; - private IResourceVariant findFileVariant(IResource resource, - Repository repository) throws TeamException { - RepositoryMapping repoMapping = RepositoryMapping.getMapping(resource); - if (repoMapping == null) + Repository repo = gsd.getRepository(); + String path = getPath(resource, repo); + RevCommit revCommit = getRevCommit(gsd); + if (revCommit == null) return null; - String gitPath = repoMapping.getRepoRelativePath(resource); - ObjectId objectId = updated.get(gitPath); - if (objectId != null) { - File root = repository.getWorkTree(); - File file = new File(root, gitPath); + if (path.length() == 0) + return handleRepositoryRoot(resource, repo, revCommit); - if (resource.getLocation().toFile().equals(file)) { - try { - Tree merge = trees.get(repository); - TreeEntry tree = merge.findBlobMember(gitPath); - GitBlobResourceVariant variant = new GitBlobResourceVariant( - resource, repository, tree.getId(), dates - .get(gitPath)); - return variant; - } catch (IOException e) { - throw new TeamException(new Status(IStatus.ERROR, Activator - .getPluginId(), NLS.bind( - CoreText.GitResourceVariantTree_couldNotFindBlob, - gitPath), e)); - } - } - } - return null; - } + TreeWalk tw = initializeTreeWalk(repo, path); - public boolean hasResourceVariant(IResource resource) throws TeamException { - return getResourceVariant(resource) != null; - } + try { + tw.addTree(revCommit.getTree()); + if (resource.getType() == IResource.FILE) { + tw.setRecursive(true); + if (tw.next()) + return new GitBlobResourceVariant(repo, revCommit, path); + } else + return new GitFolderResourceVariant(repo, revCommit, path); + } catch (IOException e) { + throw new TeamException( + NLS.bind( + CoreText.GitResourceVariantTree_couldNotFindResourceVariant, + resource), e); + } - public void flushVariants(IResource resource, int depth) - throws TeamException { - if (!gsdData.getData(resource.getProject()).shouldIncludeLocal()) - store.flushBytes(resource, depth); + return null; } @Override protected IResourceVariant[] fetchMembers(IResourceVariant variant, IProgressMonitor progress) throws TeamException { - if (!variant.isContainer()) { + if (variant == null || !(variant instanceof GitFolderResourceVariant)) return new IResourceVariant[0]; - } - - IProgressMonitor monitor = SubMonitor.convert(progress); - Set<IResourceVariant> members = new HashSet<IResourceVariant>(); - try { - GitFolderResourceVariant folderVariant = (GitFolderResourceVariant) variant; - IContainer container = folderVariant.getContainer(); - File resourceLocation = container.getLocation().toFile(); - IProject project = container.getProject(); - - Repository repository = gsdData.getData(project).getRepository(); - - monitor.beginTask(NLS.bind( - CoreText.GitResourceVariantTree_fetchingMembers, container - .getLocation()), updated.size()); - File root = repository.getWorkTree(); - - for (Map.Entry<String, ObjectId> entry : updated.entrySet()) { - String entryName = entry.getKey(); - File file = new File(root, entryName); - - if (file.getAbsolutePath().startsWith( - resourceLocation.getAbsolutePath())) { - members.add(getMember(container, repository, entryName)); - } - - monitor.worked(1); - } - } finally { - monitor.done(); - } - return members.toArray(new IResourceVariant[members.size()]); - } - - private IResourceVariant getMember(IContainer container, - Repository repository, String entryName) throws TeamException { - String gitPath = RepositoryMapping.getMapping(container) - .getRepoRelativePath(container); - Tree merge = trees.get(repository); - try { - TreeEntry tree = merge.findBlobMember(entryName); - GitBlobResourceVariant blobVariant = new GitBlobResourceVariant( - container.getFile(new Path(entryName)), repository, tree - .getId(), dates.get(entryName)); - return blobVariant; - } catch (IOException e) { - throw new TeamException(new Status(IStatus.ERROR, Activator - .getPluginId(), NLS.bind( - CoreText.GitResourceVariantTree_couldNotFindBlob, gitPath), - e)); - } - } + GitFolderResourceVariant gitVariant = (GitFolderResourceVariant) variant; - private IResourceVariant fetchVariant(IResource resource, - IProgressMonitor monitor) throws TeamException { try { - monitor.beginTask(NLS.bind( - CoreText.GitResourceVariantTree_fetchingVariant, resource - .getLocation()), 5); - initialize(resource); - monitor.worked(4); + return gitVariant.getMembers(progress); } catch (IOException e) { - throw new TeamException(new Status(IStatus.ERROR, Activator - .getPluginId(), NLS.bind( - CoreText.GitResourceVariantTree_unableToReadRepository, - resource.getName()), e)); + throw new TeamException(NLS.bind( + CoreText.GitResourceVariantTree_couldNotFetchMembers, + gitVariant), e); } - - Repository repository = gsdData.getData(resource.getProject()) - .getRepository(); - - if (resource instanceof IProject) { - return new GitFolderResourceVariant(resource); - } else if (resource instanceof IFolder) { - return findFolderVariant(resource, repository); - } - - return findFileVariant(resource, repository); } @Override protected IResourceVariant fetchVariant(IResource resource, int depth, IProgressMonitor monitor) throws TeamException { + SubMonitor subMonitor = SubMonitor.convert(monitor); + subMonitor.beginTask(NLS.bind( + CoreText.GitResourceVariantTree_fetchingVariant, + resource.getName()), IProgressMonitor.UNKNOWN); + try { - if (resource != null) - return fetchVariant(resource, monitor); - else - return null; + return getResourceVariant(resource); } finally { - monitor.done(); + subMonitor.done(); } } - @Override - protected boolean setVariant(IResource local, IResourceVariant remote) - throws TeamException { - return true; - } - - protected GitSynchronizeDataSet getSyncData() { - return gsdData; - } + /** + * + * @param gsd + * @return instance of {@link RevTree} for given {@link GitSynchronizeData} + * or <code>null</code> if rev tree was not found + * @throws TeamException + */ + protected abstract RevCommit getRevCommit(GitSynchronizeData gsd) + throws TeamException; - private IResource[] getMembersAndStore(IResource resource, - GitSynchronizeData gsd) throws TeamException { - Repository repo = gsd.getRepository(); + private IResourceVariant handleRepositoryRoot(final IResource resource, + Repository repo, RevCommit revCommit) throws TeamException { try { - Tree tree = gsd.mapSrcTree(); - - if (tree == null) - return new IResource[0]; - - IResource[] members = ((IContainer) resource).members(); - Set<IResource> membersSet = getAllMembers(repo, tree, members); - - return membersSet.toArray(new IResource[membersSet.size()]); + return new GitFolderResourceVariant(repo, revCommit, resource + .getLocation().toString()); } catch (IOException e) { - throw new TeamException(e.getMessage(), e); - } catch (CoreException e) { - throw TeamException.asTeamException(e); + throw new TeamException( + NLS.bind( + CoreText.GitResourceVariantTree_couldNotFindResourceVariant, + resource), e); } } - private Set<IResource> getAllMembers(Repository repo, Tree tree, - IResource[] members) throws IOException, TeamException { - Set<IResource> membersSet = new HashSet<IResource>(); - - for (IResource member : members) { - String memberRelPath = getMemberRelPath(repo, member); + private TreeWalk initializeTreeWalk(Repository repo, String path) { + TreeWalk tw = new TreeWalk(repo); + tw.reset(); + tw.setFilter(PathFilter.create(path)); - // check if this file exists in repository - if (tree.existsBlob(memberRelPath)) { - // read file content and add it into store - TreeEntry entry = tree.findBlobMember(memberRelPath); - ObjectLoader objLoader = repo.open(entry.getId(), - Constants.OBJ_BLOB); - store.setBytes(member, objLoader.getCachedBytes()); - membersSet.add(member); - } else if (tree.existsTree(memberRelPath)) { - // add to members if folder exists in repository - membersSet.add(member); - } - } - return membersSet; + return tw; } - private String getMemberRelPath(Repository repo, IResource member) { - String repoWorkDir = repo.getWorkTree().toString(); - if (!"/".equals(File.separator)) { //$NON-NLS-1$ - // fix file separator issue on windows - repoWorkDir = repoWorkDir.replace(File.separatorChar, '/'); - } - - String memberRelPath = member.getLocation().toString(); - memberRelPath = memberRelPath.replace(repoWorkDir, ""); //$NON-NLS-1$ - if (memberRelPath.startsWith("/"))//$NON-NLS-1$ - memberRelPath = memberRelPath.substring(1); - - return memberRelPath; + private String getPath(final IResource resource, Repository repo) { + return Repository.stripWorkDir(repo.getWorkTree(), resource + .getLocation().toFile()); } } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriber.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriber.java index 780f45b186..db67f6f0fe 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriber.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitResourceVariantTreeSubscriber.java @@ -19,7 +19,6 @@ import org.eclipse.team.core.synchronize.SyncInfo; import org.eclipse.team.core.variants.IResourceVariant; import org.eclipse.team.core.variants.IResourceVariantComparator; import org.eclipse.team.core.variants.IResourceVariantTree; -import org.eclipse.team.core.variants.ResourceVariantByteStore; import org.eclipse.team.core.variants.ResourceVariantTreeSubscriber; /** @@ -28,8 +27,6 @@ import org.eclipse.team.core.variants.ResourceVariantTreeSubscriber; public class GitResourceVariantTreeSubscriber extends ResourceVariantTreeSubscriber { - private final ResourceVariantByteStore store; - /** * A resource variant tree of the remote branch(es). */ @@ -40,16 +37,13 @@ public class GitResourceVariantTreeSubscriber extends */ private IResourceVariantTree baseTree; - private GitSynchronizeDataSet gitSynchronizeDataSet; + private GitSynchronizeDataSet gsds; /** * @param data - * @param store */ - public GitResourceVariantTreeSubscriber(GitSynchronizeDataSet data, - ResourceVariantByteStore store) { - this.store = store; - this.gitSynchronizeDataSet = data; + public GitResourceVariantTreeSubscriber(GitSynchronizeDataSet data) { + this.gsds = data; } private IResource[] roots; @@ -65,7 +59,7 @@ public class GitResourceVariantTreeSubscriber extends return roots; } - roots = gitSynchronizeDataSet.getAllResources(); + roots = gsds.getAllProjects(); return roots; } @@ -73,8 +67,7 @@ public class GitResourceVariantTreeSubscriber extends * @param data */ public void reset(GitSynchronizeDataSet data) { - gitSynchronizeDataSet = data; - store.dispose(); + gsds = data; roots = null; baseTree = null; @@ -82,24 +75,19 @@ public class GitResourceVariantTreeSubscriber extends } @Override - public IResource[] members(IResource resource) throws TeamException { - return getBaseTree().members(resource); - } - - @Override public String getName() { return CoreText.GitBranchResourceVariantTreeSubscriber_gitRepository; } @Override public IResourceVariantComparator getResourceComparator() { - return new GitResourceVariantComparator(gitSynchronizeDataSet, store); + return new GitResourceVariantComparator(gsds); } @Override protected IResourceVariantTree getBaseTree() { if (baseTree == null) { - baseTree = new GitBaseResourceVariantTree(gitSynchronizeDataSet, store); + baseTree = new GitBaseResourceVariantTree(gsds); } return baseTree; } @@ -107,7 +95,7 @@ public class GitResourceVariantTreeSubscriber extends @Override protected IResourceVariantTree getRemoteTree() { if (remoteTree == null) { - remoteTree = new GitRemoteResourceVariantTree(gitSynchronizeDataSet, store); + remoteTree = new GitRemoteResourceVariantTree(gsds); } return remoteTree; } @@ -115,7 +103,7 @@ public class GitResourceVariantTreeSubscriber extends @Override protected SyncInfo getSyncInfo(IResource local, IResourceVariant base, IResourceVariant remote) throws TeamException { - GitSyncInfo info = new GitSyncInfo(local, base, remote, + SyncInfo info = new SyncInfo(local, base, remote, getResourceComparator()); info.init(); return info; diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitSyncInfo.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitSyncInfo.java deleted file mode 100644 index 2898cbc2a5..0000000000 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/GitSyncInfo.java +++ /dev/null @@ -1,185 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 IBM Corporation and others. - * 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 - * - * Contributors: - * IBM Corporation - initial API and implementation - * Dariusz Luksza <dariusz@luksza.org> - *******************************************************************************/ -package org.eclipse.egit.core.synchronize; - -import org.eclipse.core.resources.IResource; -import org.eclipse.jgit.revwalk.RevCommit; -import org.eclipse.jgit.revwalk.RevCommitList; -import org.eclipse.team.core.Team; -import org.eclipse.team.core.TeamException; -import org.eclipse.team.core.synchronize.SyncInfo; -import org.eclipse.team.core.variants.IResourceVariant; -import org.eclipse.team.core.variants.IResourceVariantComparator; - -class GitSyncInfo extends SyncInfo { - - GitSyncInfo(IResource local, IResourceVariant base, - IResourceVariant remote, IResourceVariantComparator comparator) { - super(local, base, remote, comparator); - } - - @Override - protected int calculateKind() throws TeamException { - IResource local = getLocal(); - IResourceVariantComparator comparator = getComparator(); - GitResourceVariant base = (GitResourceVariant) getBase(); - GitResourceVariant remote = (GitResourceVariant) getRemote(); - - int description = IN_SYNC; - - boolean localExists = local.exists(); - - if (Team.isIgnoredHint(local)) { - // assume that ignored and transient files are always synchronized - // in sync - } else if (base == null) { - description = hadnleNullBase(local, comparator, remote); - } else { - if (!localExists) { - description = handleLocalDoesntExist(comparator, base, remote); - } else { - if (remote == null) { - if (comparator.compare(local, base)) - description = INCOMING | DELETION; - else - description = CONFLICTING | CHANGE; - } else { - description = calculateDescrBasedOnGitData(local, base, - remote); - } - } - } - - return description; - } - - private int hadnleNullBase(IResource local, - IResourceVariantComparator comparator, GitResourceVariant remote) { - int description; - boolean localExists = local.exists(); - - if (remote == null) { - if (!localExists) { - description = IN_SYNC; - } else { - description = OUTGOING | ADDITION; - } - } else { - if (!localExists) { - description = INCOMING | ADDITION; - } else { - description = CONFLICTING | ADDITION; - if (comparator.compare(local, remote)) { - description |= PSEUDO_CONFLICT; - } - } - } - - return description; - } - - private int handleLocalDoesntExist(IResourceVariantComparator comparator, - GitResourceVariant base, GitResourceVariant remote) { - int description; - - if (remote == null) { - description = CONFLICTING | DELETION | PSEUDO_CONFLICT; - } else { - if (comparator.compare(base, remote)) - description = OUTGOING | DELETION; - else - description = CONFLICTING | CHANGE; - } - - return description; - } - - private int calculateDescrBasedOnGitData(IResource local, - GitResourceVariant base, GitResourceVariant remote) { - int description = IN_SYNC; - - if (base instanceof GitBlobResourceVariant) { - if (remote instanceof GitBlobResourceVariant) { - if (getComparator().compare(local, base)) { - GitBlobResourceVariant baseBlob = (GitBlobResourceVariant) base; - GitBlobResourceVariant remoteBlob = (GitBlobResourceVariant) remote; - - if (!baseBlob.getId().equals(remoteBlob.getId())) { - description = calculateDescBasedOnGitCommits(baseBlob, - remoteBlob); - } - } else { - description = OUTGOING | CHANGE; - } - } else { - // file in local, folder on remote branch - description = CONFLICTING | CHANGE; - } - } else if (base.isContainer()) { - if (remote.isContainer()) { - description = compareTwoContainers(base, remote); - } else { - // folder in local, file in remote branch - description = CONFLICTING | CHANGE; - } - } - - return description; - } - - private int calculateDescBasedOnGitCommits(GitBlobResourceVariant baseBlob, - GitBlobResourceVariant remoteBlob) { - - RevCommitList<RevCommit> baseList = baseBlob.getCommitList(); - RevCommitList<RevCommit> remoteList = remoteBlob.getCommitList(); - RevCommit recentRemoteCommit = remoteList.get(0); - RevCommit recentBaseCommit = baseList.get(0); - - // TODO can we implement it better ? - for (RevCommit baseCommit : baseList) { - if (recentRemoteCommit.name().equals(baseCommit.name())) { - return OUTGOING | CHANGE; - } - } - - // TODO can we implement it better ? - for (RevCommit remoteCommit : remoteList) { - if (recentBaseCommit.name().equals(remoteCommit.name())) { - return INCOMING | CHANGE; - } - } - - return CONFLICTING | CHANGE; - } - - private int compareTwoContainers(GitResourceVariant base, - GitResourceVariant remote) { - final int description; - boolean baseExists = base.getResource().exists(); - boolean remoteExists = remote.getResource().exists(); - - if (baseExists && remoteExists) { - // there are no changes - description = IN_SYNC; - } else if (baseExists && remoteExists) { - // folder doesn't exist locally but it exists in - // common accessor and remote - description = INCOMING | ADDITION; - } else { - // in all other cases - description = CONFLICTING | CHANGE; - } - - return description; - } - -} diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeData.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeData.java index c005383eac..e1296b391d 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeData.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeData.java @@ -9,21 +9,14 @@ package org.eclipse.egit.core.synchronize.dto; import java.io.File; -import java.io.IOException; import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.egit.core.project.RepositoryMapping; -import org.eclipse.jgit.lib.Commit; -import org.eclipse.jgit.lib.Constants; -import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; -import org.eclipse.jgit.lib.Tag; -import org.eclipse.jgit.lib.Tree; /** * Simple data transfer object containing all necessary information for @@ -94,38 +87,6 @@ public class GitSynchronizeData { } /** - * @return source Tree - * @throws IOException - */ - public Tree mapSrcTree() throws IOException { - return mapTree(srcRev); - } - - /** - * @return destination Tree - * @throws IOException - */ - public Tree mapDstTree() throws IOException { - return mapTree(dstRev); - } - - /** - * @return source {@link ObjectId} - * @throws IOException - */ - public ObjectId getSrcObjectId() throws IOException { - return getObjecId(srcRev); - } - - /** - * @return destination {@link ObjectId} - * @throws IOException - */ - public ObjectId getDstObjectId() throws IOException { - return getObjecId(dstRev); - } - - /** * @return list of project's that are connected with this repository */ public Set<IProject> getProjects() { @@ -133,15 +94,6 @@ public class GitSynchronizeData { } /** - * - * @param resource - * @return <true> if given {@link IResource} is contained by this repository - */ - public boolean contains(IResource resource) { - return resource.getFullPath().toString().startsWith(repoParentPath); - } - - /** * @param file * @return <true> if given {@link File} is contained by this repository */ @@ -157,30 +109,4 @@ public class GitSynchronizeData { return includeLocal; } - private Tree mapTree(String rev) throws IOException { - if (rev.startsWith(Constants.R_TAGS)) { - Tag tag = repo.mapTag(rev); - if (tag != null) { - Commit commit = repo.mapCommit(tag.getObjId()); - if (commit != null) - return commit.getTree(); - } - return null; - } else - return repo.mapTree(rev); - } - - private ObjectId getObjecId(String rev) throws IOException { - if (rev.startsWith(Constants.R_TAGS)) { - Tag mapTag = repo.mapTag(rev); - if (mapTag != null) - return mapTag.getObjId(); - } else { - Commit mapCommit = repo.mapCommit(rev); - if (mapCommit != null) - return mapCommit.getCommitId(); - } - return null; - } - } diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeDataSet.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeDataSet.java index 4d4db8aad6..20dac6ad9e 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeDataSet.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/synchronize/dto/GitSynchronizeDataSet.java @@ -15,7 +15,6 @@ import java.util.Map; import java.util.Set; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; /** * @@ -24,14 +23,14 @@ public class GitSynchronizeDataSet implements Iterable<GitSynchronizeData> { private final Set<GitSynchronizeData> gsd; - private final Map<IProject, GitSynchronizeData> projectMapping; + private final Map<String, GitSynchronizeData> projectMapping; /** * Constructs GitSynchronizeDataSet. */ public GitSynchronizeDataSet() { gsd = new HashSet<GitSynchronizeData>(); - projectMapping = new HashMap<IProject, GitSynchronizeData>(); + projectMapping = new HashMap<String, GitSynchronizeData>(); } /** @@ -50,7 +49,7 @@ public class GitSynchronizeDataSet implements Iterable<GitSynchronizeData> { public void add(GitSynchronizeData data) { gsd.add(data); for (IProject proj : data.getProjects()) { - projectMapping.put(proj, data); + projectMapping.put(proj.getName(), data); } } @@ -63,11 +62,19 @@ public class GitSynchronizeDataSet implements Iterable<GitSynchronizeData> { } /** + * @param projectName + * @return <code>null</code> if project does not have corresponding data + */ + public GitSynchronizeData getData(String projectName) { + return projectMapping.get(projectName); + } + + /** * @param project * @return <code>null</code> if project does not have corresponding data */ public GitSynchronizeData getData(IProject project) { - return projectMapping.get(project); + return projectMapping.get(project.getName()); } public Iterator<GitSynchronizeData> iterator() { @@ -77,12 +84,12 @@ public class GitSynchronizeDataSet implements Iterable<GitSynchronizeData> { /** * @return list of all resources */ - public IResource[] getAllResources() { - Set<IResource> resource = new HashSet<IResource>(); + public IProject[] getAllProjects() { + Set<IProject> resource = new HashSet<IProject>(); for (GitSynchronizeData data : gsd) { resource.addAll(data.getProjects()); } - return resource.toArray(new IResource[resource.size()]); + return resource.toArray(new IProject[resource.size()]); } @Override diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/GitSubscriberParticipant.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/GitSubscriberParticipant.java index 8f47f5bb52..fb3c4c202a 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/GitSubscriberParticipant.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/synchronize/GitSubscriberParticipant.java @@ -15,8 +15,6 @@ import org.eclipse.egit.core.synchronize.GitResourceVariantTreeSubscriber; import org.eclipse.egit.core.synchronize.dto.GitSynchronizeDataSet; import org.eclipse.egit.ui.UIText; import org.eclipse.team.core.subscribers.Subscriber; -import org.eclipse.team.core.variants.ResourceVariantByteStore; -import org.eclipse.team.core.variants.SessionResourceVariantByteStore; import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; import org.eclipse.team.ui.synchronize.SubscriberParticipant; import org.eclipse.team.ui.synchronize.SynchronizePageActionGroup; @@ -39,10 +37,10 @@ public class GitSubscriberParticipant extends SubscriberParticipant { * Construct GitBranchSubscriberParticipant. * * @param data + * set of base data that are required to perform synchronization */ public GitSubscriberParticipant(GitSynchronizeDataSet data) { - ResourceVariantByteStore store = new SessionResourceVariantByteStore(); - setSubscriber(new GitResourceVariantTreeSubscriber(data, store)); + setSubscriber(new GitResourceVariantTreeSubscriber(data)); setName(data.toString()); } @@ -80,11 +78,14 @@ public class GitSubscriberParticipant extends SubscriberParticipant { } /** - * @param data + * Synchronous version of + * {@link #refresh(org.eclipse.core.resources.IResource[], String, String, org.eclipse.ui.IWorkbenchSite)} * + * @param data + * new data that should be set to participant */ public void refresh(GitSynchronizeDataSet data) { - refresh(data.getAllResources(), + refresh(data.getAllProjects(), UIText.GitSynchronizeWizard_gitResourceSynchronization, null, null); } |
