Author: adietish
Date: 2012-01-24 09:43:43 -0500 (Tue, 24 Jan 2012)
New Revision: 38098
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/EGitUtils.java
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java
Log:
[JBIDE-10624] implemented push force and test
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/EGitUtils.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/EGitUtils.java 2012-01-24
14:41:46 UTC (rev 38097)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/EGitUtils.java 2012-01-24
14:43:43 UTC (rev 38098)
@@ -61,6 +61,7 @@
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
+import org.eclipse.jgit.transport.RemoteRefUpdate;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;
@@ -417,47 +418,43 @@
private static PushOperation createPushOperation(RemoteConfig remoteConfig, Repository
repository, boolean force)
throws CoreException {
- PushOperationSpecification spec = new PushOperationSpecification();
- List<URIish> pushToUris = getPushURIs(remoteConfig);
- List<RefSpec> pushRefSpecs = getPushRefSpecs(remoteConfig);
- addURIRefToPushSpecification(pushToUris, pushRefSpecs, repository, spec);
-
- // return new PushOperation(repository, spec, false, PUSH_TIMEOUT);
- // TODO: fix pushoperation to really use the spec (currently seems like
- // it does not work so we push everything to the remote)
-
- // TODO ensure the 'force' is respected
- return new PushOperation(repository, remoteConfig.getName(), false, PUSH_TIMEOUT);
+ Collection<URIish> pushURIs = getPushURIs(remoteConfig);
+ Collection<RefSpec> pushRefSpecs = createForceRefSpecs(force,
getPushRefSpecs(remoteConfig));
+ PushOperationSpecification pushSpec = createPushSpec(pushURIs, pushRefSpecs,
repository);
+ return new PushOperation(repository, pushSpec, false, PUSH_TIMEOUT);
}
/**
- * Adds the given push uris to the given push operation specification.
+ * Creates a push operation specification for the given push uris to the
+ * given push operation specification.
*
- * @param urisToPush
- * the uris to push
+ * @param pushURIs
+ * the push uri's
* @param pushRefSpecs
* the push ref specs
* @param repository
* the repository
- * @param spec
- * the spec
+ * @return the push operation specification
* @throws CoreException
* the core exception
*/
- private static void addURIRefToPushSpecification(List<URIish> urisToPush,
List<RefSpec> pushRefSpecs,
- Repository repository, PushOperationSpecification spec) throws CoreException {
- for (URIish uri : urisToPush) {
- try {
- spec.addURIRefUpdates(uri,
- Transport.open(repository, uri).findRemoteRefUpdatesFor(pushRefSpecs));
- } catch (NotSupportedException e) {
- throw new CoreException(createStatus(e, "Could not connect repository
\"{0}\" to a remote",
- repository.toString()));
- } catch (IOException e) {
- throw new CoreException(createStatus(e,
- "Could not convert remote specifications for repository \"{0}\" to a
remote",
- repository.toString()));
+ private static PushOperationSpecification createPushSpec(Collection<URIish>
pushURIs, Collection<RefSpec> pushRefSpecs,
+ Repository repository) throws CoreException {
+ try {
+ PushOperationSpecification pushSpec = new PushOperationSpecification();
+ for (URIish uri : pushURIs) {
+ Collection<RemoteRefUpdate> remoteRefUpdates =
+ Transport.open(repository, uri).findRemoteRefUpdatesFor(pushRefSpecs);
+ pushSpec.addURIRefUpdates(uri, remoteRefUpdates);
}
+ return pushSpec;
+ } catch (NotSupportedException e) {
+ throw new CoreException(createStatus(e, "Could not connect repository
\"{0}\" to a remote",
+ repository.toString()));
+ } catch (IOException e) {
+ throw new CoreException(createStatus(e,
+ "Could not convert remote specifications for repository \"{0}\" to a
remote",
+ repository.toString()));
}
}
@@ -468,21 +465,26 @@
* the remote config
* @return the push ur is
*/
- private static List<URIish> getPushURIs(RemoteConfig remoteConfig) {
- List<URIish> urisToPush = new ArrayList<URIish>();
- for (URIish uri : remoteConfig.getPushURIs())
- urisToPush.add(uri);
- if (urisToPush.isEmpty() && !remoteConfig.getURIs().isEmpty())
- urisToPush.add(remoteConfig.getURIs().get(0));
- return urisToPush;
+ private static Collection<URIish> getPushURIs(RemoteConfig remoteConfig) {
+ List<URIish> pushURIs = new ArrayList<URIish>();
+ for (URIish uri : remoteConfig.getPushURIs()) {
+ pushURIs.add(uri);
+ }
+ if (pushURIs.isEmpty()
+ && !remoteConfig.getURIs().isEmpty()) {
+ pushURIs.add(remoteConfig.getURIs().get(0));
+ }
+ return pushURIs;
}
/**
- * Gets the push RefSpecs from the given remote configuration.
+ * Gets the push RefSpecs from the given remote configuration. If none is
+ * defined, a default refspec is returned with
+ * {@link #DEFAULT_REFSPEC_SOURCE} and {@link #DEFAULT_REFSPEC_DESTINATION}.
*
* @param config
- * the config
- * @return the push ref specs
+ * the remote config to get the push specs from
+ * @return the push specs to use for the given remote configuration.
*/
private static List<RefSpec> getPushRefSpecs(RemoteConfig config) {
List<RefSpec> pushRefSpecs = new ArrayList<RefSpec>();
@@ -497,6 +499,14 @@
return pushRefSpecs;
}
+ private static Collection<RefSpec> createForceRefSpecs(boolean forceUpdate,
Collection<RefSpec> refSpecs) {
+ List<RefSpec> newRefSpecs = new ArrayList<RefSpec>();
+ for (RefSpec refSpec : refSpecs) {
+ newRefSpecs.add(refSpec.setForceUpdate(forceUpdate));
+ }
+ return newRefSpecs;
+ }
+
/**
* Gets the repository that is configured to the given project.
*
@@ -719,10 +729,11 @@
* Returns <code>true</code> if the given repository has uncommitted
* changes.
*
- * @param repository the repository to check for uncommitted changes
+ * @param repository
+ * the repository to check for uncommitted changes
* @return
- * @throws IOException
- * @throws NoWorkTreeException
+ * @throws IOException
+ * @throws NoWorkTreeException
*/
public static boolean isDirty(Repository repository) throws NoWorkTreeException,
IOException {
boolean hasChanges = false;
Modified:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java 2012-01-24
14:41:46 UTC (rev 38097)
+++
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitUtilsTest.java 2012-01-24
14:43:43 UTC (rev 38098)
@@ -13,6 +13,7 @@
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.core.op.PushOperationResult;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
@@ -22,6 +23,7 @@
import org.jboss.tools.openshift.egit.internal.test.util.TestUtils;
import org.junit.After;
import org.junit.Before;
+import org.junit.Ignore;
import org.junit.Test;
public class EGitUtilsTest {
@@ -56,7 +58,7 @@
testRepository2.connect(testProject2.getProject());
this.testRepositoryClone = cloneRepository(testRepository);
- testRepositoryClone.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
+// testRepositoryClone.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
}
private TestRepository cloneRepository(TestRepository repository) throws
URISyntaxException,
@@ -117,13 +119,13 @@
public void fileAddedToCloneIsInRemoteAfterPush() throws Exception {
String fileName = "c.txt";
String fileContent = "adietish(a)redhat.com";
-
File file = testRepositoryClone.createFile(fileName, fileContent);
testRepositoryClone.addAndCommit(file, "adding a file");
+ testRepositoryClone.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
EGitUtils.push(REPO2_REMOTE_NAME, testRepositoryClone.getRepository(), null);
- // does origin contain file added to clone?
+ // repo2 must contain file added to clone
testUtils.assertRepositoryContainsFilesWithContent(
testRepository2.getRepository(),
fileName,
@@ -132,27 +134,54 @@
@Test
public void forcedPushRemovesFileInRemote() throws Exception {
- String fileName = "c.txt";
+ String fileName = "a.txt";
String fileContent = "adietish(a)redhat.com";
+ File file = testRepository.createFile(fileName, fileContent);
+ testRepository.addAndCommit(file, "adding a file");
- IFile fileInRepo2 = testUtils.addFileToProject(
- testProject2.getProject(),
+ File file2 = testRepository2.createFile("b.txt", "bingobongo");
+ testRepository2.addAndCommit(file2, "adding a file");
+
+ testRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
+ EGitUtils.pushForce(REPO2_REMOTE_NAME, testRepository.getRepository(), null);
+
+ // repo2 mustn't contain "b.txt"
+ testUtils.assertRepositoryMisses(
+ testRepository2.getRepository(),
+ file2.getName());
+ // repo2 must contain "a.txt"
+ testUtils.assertRepositoryContainsFilesWithContent(
+ testRepository2.getRepository(),
fileName,
fileContent);
- testRepository2.add(fileInRepo2);
+ }
+
- File fileInClone = testRepositoryClone.createFile(fileName, fileContent);
- testRepositoryClone.addAndCommit(fileInClone, "adding a file");
+ @Ignore
+ @Test
+ public void pushFailsOnNonFastForward() throws Exception {
+ String fileName = "a.txt";
+ String fileContent = "adietish(a)redhat.com";
+ File file = testRepository.createFile(fileName, fileContent);
+ testRepository.addAndCommit(file, "adding a file");
- EGitUtils.push(REPO2_REMOTE_NAME, testRepositoryClone.getRepository(), null);
+ File file2 = testRepository2.createFile("b.txt", "bingobongo");
+ testRepository2.addAndCommit(file2, "adding a file");
+
+ testRepository.addRemoteTo(REPO2_REMOTE_NAME, testRepository2.getRepository());
+ PushOperationResult result = EGitUtils.push(REPO2_REMOTE_NAME,
testRepository.getRepository(), null);
- // does origin contain file added to clone?
+ // repo2 mustn't contain "b.txt"
+ testUtils.assertRepositoryMisses(
+ testRepository2.getRepository(),
+ file2.getName());
+ // repo2 must contain "a.txt"
testUtils.assertRepositoryContainsFilesWithContent(
- testRepositoryClone.getRepository(),
+ testRepository2.getRepository(),
fileName,
fileContent);
}
-
+
@Test
public void canGetRepoForProject() throws Exception {
Repository repository = EGitUtils.getRepository(testProject.getProject());
Modified:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java 2012-01-24
14:41:46 UTC (rev 38097)
+++
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/util/TestUtils.java 2012-01-24
14:43:43 UTC (rev 38098)
@@ -17,9 +17,12 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IFile;
@@ -46,7 +49,7 @@
/**
* Create a "temporary" directory
- *
+ *
* @param name
* the name of the directory
* @return a directory as child of a "temporary" folder in the user home
@@ -68,7 +71,7 @@
/**
* Cleanup: delete the "temporary" folder and all children
- *
+ *
* @throws IOException
*/
public void deleteTempDirs() throws IOException {
@@ -80,7 +83,7 @@
/**
* Read the stream into a String
- *
+ *
* @param inputStream
* @return the contents of the stream
* @throws IOException
@@ -100,7 +103,7 @@
/**
* Add a file to an existing project
- *
+ *
* @param project
* the project
* @param path
@@ -132,7 +135,7 @@
/**
* Change the content of a file
- *
+ *
* @param project
* @param file
* @param newContent
@@ -148,7 +151,7 @@
/**
* Create a project in the local file system
- *
+ *
* @param parentFile
* the parent
* @param projectName
@@ -177,43 +180,113 @@
/**
* verifies that repository contains exactly the given files.
+ *
* @param repository
* @param paths
* @throws Exception
*/
- public void assertRepositoryContainsFiles(Repository repository,
- String[] paths) throws Exception {
- Set<String> expectedfiles = new HashSet<String>();
- for (String path : paths)
- expectedfiles.add(path);
+ public void assertRepositoryExactlyContains(Repository repository, String... paths)
throws Exception {
+ RepoDiff repoDiff = createRepoDiff(repository, paths);
+ if (repoDiff.hasUnexpected()) {
+ fail(repoDiff.getUnexpectedFiles());
+ }
+ if (repoDiff.hasMissing()) {
+ fail(repoDiff.getUnexpectedFiles());
+ }
+ }
+
+ public void assertRepositoryMisses(Repository repository, String... paths) throws
Exception {
+ RepoDiff repoDiff = createRepoDiff(repository, paths);
+ for (String missingPath : paths) {
+ assertTrue(repoDiff.getMissing().contains(missingPath));
+ }
+ }
+
+ /**
+ * verifies that repository contains exactly the given files.
+ *
+ * @param repository
+ * @param paths
+ * @throws Exception
+ */
+ private RepoDiff createRepoDiff(Repository repository, String... expectedPaths) throws
Exception {
+ RepoDiff repoDiff = new RepoDiff();
+ Set<String> expectedFiles = new
HashSet<String>(Arrays.asList(expectedPaths));
+
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(repository.resolve("HEAD^{tree}"));
treeWalk.setRecursive(true);
+
while (treeWalk.next()) {
String path = treeWalk.getPathString();
- if (!expectedfiles.contains(path))
- fail("Repository contains unexpected expected file " + path);
- expectedfiles.remove(path);
+ if (!expectedFiles.contains(path)) {
+ repoDiff.addUnexpected(path);
+ }
+ expectedFiles.remove(path);
}
- if (expectedfiles.size() > 0) {
- StringBuilder message = new StringBuilder(
- "Repository does not contain expected files: ");
- for (String path : expectedfiles) {
- message.append(path);
- message.append(" ");
+ repoDiff.addAllMissing(expectedFiles);
+ return repoDiff;
+ }
+
+ public class RepoDiff {
+
+ private List<String> unexpected = new ArrayList<String>();
+ private List<String> missing = new ArrayList<String>();
+
+ public void addMissing(String path) {
+ missing.add(path);
+ }
+
+ public void addAllMissing(Collection<String> paths) {
+ missing.addAll(paths);
+ }
+
+ public List<String> getMissing() {
+ return missing;
+ }
+
+ public boolean hasMissing() {
+ return !missing.isEmpty();
+ }
+
+ public String getMissingFiles() {
+ StringBuilder builder = new StringBuilder("Repository is nissing files: ");
+ for (String missingPath : getMissing()) {
+ builder.append(missingPath).append(',');
}
- fail(message.toString());
+ return builder.toString();
}
+
+ public void addUnexpected(String path) {
+ unexpected.add(path);
+ }
+
+ public List<String> getUnexpected() {
+ return unexpected;
+ }
+
+ public boolean hasUnexpected() {
+ return !unexpected.isEmpty();
+ }
+
+ public String getUnexpectedFiles() {
+ StringBuilder builder = new StringBuilder("Repository contains unexpected
expected files: ");
+ for (String unexpectedPath : getUnexpected()) {
+ builder.append(unexpectedPath).append(',');
+ }
+ return builder.toString();
+ }
}
/**
* verifies that repository contains exactly the given files with the given
* content. Usage example:<br>
- *
+ *
* <code>
* assertRepositoryContainsFiles(repository, "foo/a.txt", "content of
A",
* "foo/b.txt", "content of
B")
* </code>
+ *
* @param repository
* @param args
* @throws Exception
@@ -255,11 +328,11 @@
throw new IllegalArgumentException("needs to be pairs");
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < args.length; i += 2) {
- map.put(args[i], args[i+1]);
+ map.put(args[i], args[i + 1]);
}
return map;
}
-
+
public String getPathInRepository(IResource resource) {
RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
if (mapping == null) {