Author: adietish
Date: 2012-01-16 09:23:58 -0500 (Mon, 16 Jan 2012)
New Revision: 37862
Added:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestSuite.java
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java
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/GitIgnore.java
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/ConfigureGitSharedProject.java
Log:
[JBIDE-10391] implemented GitIgnore to be able to merge new entries with existing ones
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-16
13:48:45 UTC (rev 37861)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/EGitUtils.java 2012-01-16
14:23:58 UTC (rev 37862)
@@ -73,14 +73,12 @@
*/
public class EGitUtils {
- private static final RefSpec DEFAULT_PUSH_REF_SPEC =
- new RefSpec("refs/heads/*:refs/remotes/origin/*"); //$NON-NLS-1$
-
+ // private static final RefSpec DEFAULT_PUSH_REF_SPEC = new
RefSpec("refs/heads/*:refs/remotes/origin/*"); //$NON-NLS-1$
+ private static final String DEFAULT_REFSPEC_SOURCE = Constants.HEAD; // HEAD
+ private static final String DEFAULT_REFSPEC_DESTINATION = Constants.R_HEADS +
Constants.MASTER; // refs/heads/master
private static final int PUSH_TIMEOUT = 10 * 1024;
+ private static final String EGIT_TEAM_PROVIDER_ID =
"org.eclipse.egit.core·GitProvider";
- private static final String EGIT_TEAM_PROVIDER_ID =
- "org.eclipse.egit.core·GitProvider";
-
private EGitUtils() {
// inhibit instantiation
}
@@ -296,7 +294,8 @@
*
* @see #connect(IProject, Repository)
*/
- private static RevCommit commit(IProject project, String commitMessage, IProgressMonitor
monitor) throws CoreException {
+ private static RevCommit commit(IProject project, String commitMessage, IProgressMonitor
monitor)
+ throws CoreException {
Repository repository = getRepository(project);
Assert.isLegal(repository != null, "Cannot commit project to repository. ");
return commit(project, commitMessage, repository, monitor);
@@ -306,7 +305,8 @@
return commit(project, "Commit from JBoss Tools", monitor);
}
- private static RevCommit commit(IProject project, String commitMessage, Repository
repository, IProgressMonitor monitor)
+ private static RevCommit commit(IProject project, String commitMessage, Repository
repository,
+ IProgressMonitor monitor)
throws CoreException {
Assert.isLegal(project != null, "Could not commit project. No project
provided");
Assert.isLegal(
@@ -365,14 +365,13 @@
return push(repository, remoteConfig, false, monitor);
}
-
public static PushOperationResult pushForce(String remote, Repository repository,
IProgressMonitor monitor)
throws CoreException {
RemoteConfig remoteConfig = getRemoteConfig(remote, repository);
return push(repository, remoteConfig, true, monitor);
}
- private static PushOperationResult push(Repository repository, RemoteConfig
remoteConfig,
+ private static PushOperationResult push(Repository repository, RemoteConfig
remoteConfig,
boolean force, IProgressMonitor monitor) throws CoreException {
try {
if (remoteConfig == null) {
@@ -381,30 +380,21 @@
}
PushOperation op = createPushOperation(remoteConfig, repository, force);
op.run(monitor);
- PushOperationResult result = op.getOperationResult();
- return result;
+ return op.getOperationResult();
} catch (CoreException e) {
throw e;
} catch (Exception e) {
throw new CoreException(createStatus(e, "Could not push repo {0}",
repository.toString()));
}
}
-//
- // only available in EGit 1.1
- //
- // private static PushOperation createPushOperation(String remoteName,
- // Repository repository) { return new PushOperation(repository, remoteName,
- // false, PUSH_TIMEOUT); }
- //
- private static PushOperation createPushOperation(RemoteConfig remoteConfig, Repository
repository)
- throws CoreException {
- return createPushOperation(remoteConfig, repository, false);
+ private static PushOperation createPushOperation(String remoteName, Repository
repository) {
+ return new PushOperation(repository, remoteName, false, PUSH_TIMEOUT);
}
-
+
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);
@@ -413,7 +403,7 @@
// 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);
}
@@ -474,10 +464,13 @@
*/
private static List<RefSpec> getPushRefSpecs(RemoteConfig config) {
List<RefSpec> pushRefSpecs = new ArrayList<RefSpec>();
- pushRefSpecs.addAll(config.getPushRefSpecs());
- if (pushRefSpecs.isEmpty()) {
- // default push to all branches
- pushRefSpecs.add(DEFAULT_PUSH_REF_SPEC);
+ List<RefSpec> remoteConfigPushRefSpecs = config.getPushRefSpecs();
+ if (!remoteConfigPushRefSpecs.isEmpty()) {
+ pushRefSpecs.addAll(remoteConfigPushRefSpecs);
+ } else {
+ // default is to push current HEAD to remote MASTER
+ pushRefSpecs.add(new RefSpec()
+ .setSource(DEFAULT_REFSPEC_SOURCE).setDestination(DEFAULT_REFSPEC_DESTINATION));
}
return pushRefSpecs;
}
@@ -704,7 +697,8 @@
return -1;
}
- private static Set<String> getCommitableChanges(IProject project, IServer server,
IProgressMonitor monitor) throws IOException {
+ private static Set<String> getCommitableChanges(IProject project, IServer server,
IProgressMonitor monitor)
+ throws IOException {
Repository repo = getRepository(project);
EclipseGitProgressTransformer jgitMonitor = new
EclipseGitProgressTransformer(monitor);
@@ -713,52 +707,57 @@
indexDiff.diff(jgitMonitor, 0, 0, NLS.bind(
UIText.CommitActionHandler_repository, repo.getDirectory().getPath()));
Set<String> set = new HashSet<String>();
- if( commitAddedResources(server))
+ if (commitAddedResources(server))
set.addAll(indexDiff.getAdded());
- if( commitChangedResources(server))
+ if (commitChangedResources(server))
set.addAll(indexDiff.getChanged());
- if( commitConflictingResources(server))
+ if (commitConflictingResources(server))
set.addAll(indexDiff.getConflicting());
- if( commitMissingResources(server))
+ if (commitMissingResources(server))
set.addAll(indexDiff.getMissing());
- if( commitModifiedResources(server))
+ if (commitModifiedResources(server))
set.addAll(indexDiff.getModified());
- if( commitRemovedResources(server))
+ if (commitRemovedResources(server))
set.addAll(indexDiff.getRemoved());
- if( commitUntrackedResources(server))
+ if (commitUntrackedResources(server))
set.addAll(indexDiff.getUntracked());
return set;
}
/*
- * Current behaviour is to commit only:
- * added, changed, modified, removed
- *
- * These can be customized as properties on the server one day, if we wish,
- * such that each server can have custom settings, or, they can be global settings
+ * Current behaviour is to commit only: added, changed, modified, removed
+ *
+ * These can be customized as properties on the server one day, if we wish,
+ * such that each server can have custom settings, or, they can be global
+ * settings
*/
public static boolean commitAddedResources(IServer server) {
return true;
}
+
public static boolean commitChangedResources(IServer server) {
return true;
}
+
public static boolean commitConflictingResources(IServer server) {
return false;
}
+
public static boolean commitMissingResources(IServer server) {
return false;
}
+
public static boolean commitModifiedResources(IServer server) {
return true;
}
+
public static boolean commitRemovedResources(IServer server) {
return true;
}
+
public static boolean commitUntrackedResources(IServer server) {
return false;
}
-
}
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java 2012-01-16
13:48:45 UTC (rev 37861)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.egit.core/src/org/jboss/tools/openshift/egit/core/GitIgnore.java 2012-01-16
14:23:58 UTC (rev 37862)
@@ -10,30 +10,70 @@
******************************************************************************/
package org.jboss.tools.openshift.egit.core;
-import java.io.BufferedOutputStream;
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
import java.io.File;
-import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
+import java.io.Reader;
+import java.util.HashSet;
+import java.util.Set;
+import org.eclipse.core.resources.IProject;
import org.eclipse.jgit.lib.Constants;
+/**
+ * @author André Dietisheim
+ */
public class GitIgnore {
- private List<String> entries;
- private File baseDir;
+ public static final String NL = System.getProperty("line.separator");
+
+ private Set<String> entries;
+ private File file;
- public GitIgnore(File baseDir) {
- this.baseDir = baseDir;
- this.entries = new ArrayList<String>();
+ public GitIgnore(IProject project) throws IOException {
+ this(new File(project.getLocation().toFile(), Constants.GITIGNORE_FILENAME));
}
+ public GitIgnore(File gitIgnoreFile) throws IOException {
+ this.file = gitIgnoreFile;
+ initEntries(gitIgnoreFile);
+ }
+
+ private void initEntries(File gitIgnore) throws IOException {
+ this.entries = new HashSet<String>();
+ if (gitIgnore == null
+ || !gitIgnore.canRead()) {
+ return;
+ }
+ BufferedReader reader = null;
+ try {
+ reader = new BufferedReader(new FileReader(gitIgnore));
+ for (String line = null; (line = reader.readLine()) != null;) {
+ if (line != null) {
+ entries.add(line);
+ }
+ }
+ } finally {
+ safeClose(reader);
+ }
+ }
+
public GitIgnore add(String entry) {
this.entries.add(entry);
return this;
}
+ public boolean contains(String entry) {
+ return entries.contains(entry);
+ }
+
+ public int size() {
+ return entries.size();
+ }
+
/**
* Writes the entries in this instance to the .gitignore file. Overwrites
* and existing file if the given overwrite is set to <code>true</code>,
@@ -45,25 +85,31 @@
* @throws IOException
*/
public void write(boolean overwrite) throws IOException {
- File file = getFile();
- BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file,
!overwrite));
+ BufferedWriter writer = new BufferedWriter(new FileWriter(file, !overwrite));
try {
for (String entry : entries) {
- out.write(entry.getBytes());
- out.write('\n');
+ writer.write(entry);
+ writer.write(NL);
}
- out.flush();
+ writer.flush();
} finally {
- out.close();
+ writer.close();
}
}
public boolean exists() {
- return getFile().exists();
+ return file != null
+ && file.exists();
}
-
- private File getFile() {
- File file = new File(baseDir, Constants.GITIGNORE_FILENAME);
- return file;
+
+ private void safeClose(Reader reader) {
+ if (reader == null) {
+ return;
+ }
+ try {
+ reader.close();
+ } catch (IOException e) {
+ // swallow
+ }
}
}
Modified:
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/ConfigureGitSharedProject.java
===================================================================
---
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/ConfigureGitSharedProject.java 2012-01-16
13:48:45 UTC (rev 37861)
+++
trunk/openshift/plugins/org.jboss.tools.openshift.express.ui/src/org/jboss/tools/openshift/express/internal/ui/wizard/appimport/ConfigureGitSharedProject.java 2012-01-16
14:23:58 UTC (rev 37862)
@@ -13,7 +13,6 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
-import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
@@ -22,8 +21,6 @@
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.jgit.lib.Repository;
-import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;
import org.jboss.ide.eclipse.as.core.util.FileUtil;
import org.jboss.tools.openshift.egit.core.EGitUtils;
@@ -113,9 +110,11 @@
Assert.isLegal(project != null);
File projectFolder = project.getLocation().toFile();
monitor.subTask(NLS.bind("Copying openshift configuration to project {0}...",
project.getName()));
+
FileUtils.copy(new File(sourceFolder, ".openshift"), projectFolder, false);
FileUtils.copy(new File(sourceFolder, "deployments"), projectFolder, false);
- createGitIgnore(projectFolder);
+
+ createGitIgnore(project);
}
/**
@@ -125,8 +124,8 @@
* @param projectFolder
* @throws IOException
*/
- private void createGitIgnore(File projectFolder) throws IOException {
- GitIgnore gitIgnore = new GitIgnore(projectFolder);
+ private void createGitIgnore(IProject project) throws IOException {
+ GitIgnore gitIgnore = new GitIgnore(project);
// TODO: merge existing .gitignore
// (
https://issues.jboss.org/browse/JBIDE-10391)
if (gitIgnore.exists()) {
Added:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestSuite.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestSuite.java
(rev 0)
+++
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestSuite.java 2012-01-16
14:23:58 UTC (rev 37862)
@@ -0,0 +1,26 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.openshift.egit.internal.test;
+
+import org.junit.runner.RunWith;
+import org.junit.runners.Suite;
+import org.junit.runners.Suite.SuiteClasses;
+
+(a)RunWith(Suite.class)
+@SuiteClasses({
+ EGitUtilsTest.class
+ , GitIgnoreTest.class
+})
+/**
+ * @author Andre Dietisheim
+ */
+public class EGitTestSuite {
+}
Property changes on:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/EGitTestSuite.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java
===================================================================
---
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java
(rev 0)
+++
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java 2012-01-16
14:23:58 UTC (rev 37862)
@@ -0,0 +1,110 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is 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:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.openshift.egit.internal.test;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.eclipse.jgit.lib.Constants;
+import org.jboss.tools.openshift.egit.core.GitIgnore;
+import org.junit.Test;
+
+/**
+ * @author André Dietisheim
+ */
+public class GitIgnoreTest {
+
+ public static final String NL = System.getProperty("line.separator");
+
+ private File gitIgnoreFile;
+
+ public void setUp() throws IOException {
+ this.gitIgnoreFile = createGitFile("");
+ }
+
+ public void tearDown() {
+ gitIgnoreFile.delete();
+ }
+
+ @Test
+ public void canAddEntries() throws IOException {
+ GitIgnore gitIgnore = new GitIgnore(gitIgnoreFile);
+ assertTrue(gitIgnore.size() == 0);
+ String entry = "dummy";
+ gitIgnore.add(entry);
+ assertTrue(gitIgnore.size() == 1);
+ assertTrue(gitIgnore.contains(entry));
+ }
+
+ @Test
+ public void canParseExistingEntries() throws IOException {
+ File gitIgnoreFile = null;
+ try {
+ gitIgnoreFile = createGitFile("redhat", "jboss",
"tools");
+ GitIgnore gitIgnore = new GitIgnore(gitIgnoreFile);
+ assertTrue(gitIgnore.size() == 3);
+ assertTrue(gitIgnore.contains("redhat"));
+ assertTrue(gitIgnore.contains("jboss"));
+ assertTrue(gitIgnore.contains("tools"));
+ } finally {
+ if (gitIgnoreFile != null) {
+ gitIgnoreFile.delete();
+ }
+ }
+ }
+
+ @Test
+ public void entryIsNotAddedTwice() throws IOException {
+ File gitIgnoreFile = null;
+ try {
+ gitIgnoreFile = createGitFile("redhat");
+ GitIgnore gitIgnore = new GitIgnore(gitIgnoreFile);
+ assertTrue(gitIgnore.size() == 1);
+ assertTrue(gitIgnore.contains("redhat"));
+ gitIgnore.add("redhat");
+ assertTrue(gitIgnore.size() == 1);
+ } finally {
+ gitIgnoreFile.delete();
+ }
+ }
+
+ private File createTmpFolder() throws IOException {
+ String tmpFolder = System.getProperty("java.io.tmpdir");
+ String currentTime = String.valueOf(System.currentTimeMillis());
+ File randomTmpFolder = new File(tmpFolder, currentTime);
+ randomTmpFolder.mkdir();
+ return randomTmpFolder;
+
+ }
+
+ private File createGitFile(String... gitIgnoreEntries) throws IOException {
+ File gitFile = new File(createTmpFolder(), Constants.GITIGNORE_FILENAME);
+ BufferedWriter writer = null;
+ try {
+ writer = new BufferedWriter(new FileWriter(gitFile));
+ for (String entry : gitIgnoreEntries) {
+ writer.write(entry);
+ writer.write(NL);
+ }
+ writer.flush();
+ } finally {
+ if (writer != null) {
+ writer.close();
+ }
+ }
+
+ return gitFile;
+ }
+}
Property changes on:
trunk/openshift/tests/org.jboss.tools.openshift.egit.test/src/org/jboss/tools/openshift/egit/internal/test/GitIgnoreTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain