aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobin Stocker2012-01-16 18:42:57 (EST)
committerMatthias Sohn2012-01-16 18:42:57 (EST)
commit6ed6b9104cbce86668d6819601694f8a29a16119 (patch)
tree6aa632d50dcff6fdf87bf3f79acb3156f2d67803
parent7f75ec73cafcc6d4672e2ba49ec85846ab01200d (diff)
downloadegit-6ed6b9104cbce86668d6819601694f8a29a16119.zip
egit-6ed6b9104cbce86668d6819601694f8a29a16119.tar.gz
egit-6ed6b9104cbce86668d6819601694f8a29a16119.tar.bz2
Refactor import code of GitProjectSetCapabilityrefs/changes/79/4779/4
Extracted a class to simplify things a bit. Bug: 366207 Change-Id: I52314c2487209bf477337c205ef3785adb2203d9 Signed-off-by: Robin Stocker <robin@nibor.org> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java138
-rw-r--r--org.eclipse.egit.core/src/org/eclipse/egit/core/internal/ProjectReferenceImporter.java219
2 files changed, 224 insertions, 133 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java
index 7026335..836081a 100644
--- a/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java
@@ -13,37 +13,22 @@
*******************************************************************************/
package org.eclipse.egit.core;
-import java.io.File;
import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
import java.net.URI;
-import java.net.URISyntaxException;
import java.util.ArrayList;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Set;
+import java.util.List;
import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IWorkspace;
-import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.core.internal.GitURI;
-import org.eclipse.egit.core.op.CloneOperation;
-import org.eclipse.egit.core.op.ConnectProviderOperation;
+import org.eclipse.egit.core.internal.ProjectReferenceImporter;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.jgit.lib.ConfigConstants;
-import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.StoredConfig;
-import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.ProjectSetCapability;
import org.eclipse.team.core.ProjectSetSerializationContext;
@@ -116,95 +101,10 @@ public final class GitProjectSetCapability extends ProjectSetCapability {
try{
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
-
public void run(IProgressMonitor wsOpMonitor) throws CoreException {
- final Map<URIish, Map<String, Set<ProjectReference>>> repositories =
- new LinkedHashMap<URIish, Map<String, Set<ProjectReference>>>();
- for (final String reference : referenceStrings) {
- try {
- final ProjectReference projectReference = new ProjectReference(
- reference);
- Map<String, Set<ProjectReference>> repositoryBranches = repositories
- .get(projectReference.getRepository());
- if (repositoryBranches == null) {
- repositoryBranches = new HashMap<String, Set<ProjectReference>>();
- repositories.put(projectReference.getRepository(),
- repositoryBranches);
- }
- Set<ProjectReference> projectReferences = repositoryBranches.get(projectReference.getBranch());
- if (projectReferences == null) {
- projectReferences = new LinkedHashSet<ProjectReference>();
- repositoryBranches.put(projectReference.getBranch(), projectReferences);
- }
-
- projectReferences.add(projectReference);
- } catch (final IllegalArgumentException e) {
- throw new TeamException(reference, e);
- } catch (final URISyntaxException e) {
- throw new TeamException(reference, e);
- }
- }
- for (final Map.Entry<URIish, Map<String, Set<ProjectReference>>> entry : repositories.entrySet()) {
- final URIish gitUrl = entry.getKey();
- final Map<String, Set<ProjectReference>> branches = entry.getValue();
-
- for (final Map.Entry<String, Set<ProjectReference>> branchEntry : branches.entrySet()) {
- final String branch = branchEntry.getKey();
- final Set<ProjectReference> projects = branchEntry.getValue();
-
- try {
- final IPath workDir = getWorkingDir(gitUrl, branch,
- branches.keySet());
- if (workDir.toFile().exists()) {
- final Collection<String> projectNames = new LinkedList<String>();
- for (final ProjectReference projectReference : projects)
- projectNames.add(projectReference.getProjectDir());
- throw new TeamException(NLS.bind(
- CoreText.GitProjectSetCapability_CloneToExistingDirectory,
- new Object[] { workDir, projectNames, gitUrl }));
- }
-
- int timeout = 60;
- String refName = Constants.R_HEADS + branch;
- final CloneOperation cloneOperation = new CloneOperation(
- gitUrl, true, null, workDir.toFile(), refName,
- Constants.DEFAULT_REMOTE_NAME, timeout);
- cloneOperation.run(wsOpMonitor);
-
- final File repositoryPath = workDir.append(Constants.DOT_GIT_EXT).toFile();
-
- Activator.getDefault().getRepositoryUtil().addConfiguredRepository(repositoryPath);
-
- // import projects from the current repository to workspace
- final IWorkspace workspace = ResourcesPlugin.getWorkspace();
- final IWorkspaceRoot root = workspace.getRoot();
- for (final ProjectReference projectToImport : projects) {
- final IPath projectDir = workDir
- .append(projectToImport.getProjectDir());
- final IProjectDescription projectDescription = workspace
- .loadProjectDescription(projectDir
- .append(IProjectDescription.DESCRIPTION_FILE_NAME));
- final IProject project = root
- .getProject(projectDescription.getName());
- project.create(projectDescription, wsOpMonitor);
- importedProjects.add(project);
-
- project.open(wsOpMonitor);
- final ConnectProviderOperation connectProviderOperation = new ConnectProviderOperation(
- project, repositoryPath);
- connectProviderOperation.execute(wsOpMonitor);
- }
- } catch (final InvocationTargetException e) {
- throwTeamException(e);
- } catch (final CoreException e) {
- throw TeamException.asTeamException(e);
- } catch (final InterruptedException e) {
- // was canceled by user
- importedProjects.clear();
- }
- }
- }
-
+ ProjectReferenceImporter importer = new ProjectReferenceImporter(referenceStrings);
+ List<IProject> p = importer.run(wsOpMonitor);
+ importedProjects.addAll(p);
}
}, ResourcesPlugin.getWorkspace().getRoot(), IWorkspace.AVOID_UPDATE, monitor);
} catch (CoreException e) {
@@ -220,32 +120,4 @@ public final class GitProjectSetCapability extends ProjectSetCapability {
GitURI gitURI = new GitURI(uri);
return asReference(gitURI.getRepository().toString(), gitURI.getTag(), gitURI.getPath().toString());
}
-
- private TeamException throwTeamException(Throwable th) throws TeamException{
- Throwable current = th;
- while(current.getCause()!=null){
- current = current.getCause();
- }
- throw new TeamException(current.getMessage(), current);
- }
-
- /**
- * @param gitUrl
- * @param branch the branch to check out
- * @param allBranches all branches which should be checked out for this gitUrl
- * @return the directory where the project should be checked out
- */
- private static IPath getWorkingDir(URIish gitUrl, String branch, Set<String> allBranches) {
- final IPath workspaceLocation = ResourcesPlugin.getWorkspace()
- .getRoot().getRawLocation();
- final String humanishName = gitUrl.getHumanishName();
- String extendedName;
- if (allBranches.size() == 1 || branch.equals(Constants.MASTER))
- extendedName = humanishName;
- else
- extendedName = humanishName + "_" + branch; //$NON-NLS-1$
- final IPath workDir = workspaceLocation.append(extendedName);
- return workDir;
- }
-
} \ No newline at end of file
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/ProjectReferenceImporter.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/ProjectReferenceImporter.java
new file mode 100644
index 0000000..a55778a
--- /dev/null
+++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/ProjectReferenceImporter.java
@@ -0,0 +1,219 @@
+/*******************************************************************************
+ * Copyright (C) 2011, Robin Stocker <robin@nibor.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.internal;
+
+import java.io.File;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.egit.core.Activator;
+import org.eclipse.egit.core.CoreText;
+import org.eclipse.egit.core.ProjectReference;
+import org.eclipse.egit.core.op.CloneOperation;
+import org.eclipse.egit.core.op.ConnectProviderOperation;
+import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.transport.URIish;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.team.core.TeamException;
+
+/**
+ * Processes project references, clones and imports them.
+ */
+public class ProjectReferenceImporter {
+
+ private final String[] referenceStrings;
+
+ /**
+ * @param referenceStrings the reference strings to import
+ */
+ public ProjectReferenceImporter(String[] referenceStrings) {
+ this.referenceStrings = referenceStrings;
+ }
+
+ /**
+ * Imports the projects as described in the reference strings.
+ *
+ * @param monitor progress monitor
+ * @return the imported projects
+ * @throws TeamException
+ */
+ public List<IProject> run(IProgressMonitor monitor) throws TeamException {
+
+ final Map<URIish, Map<String, Set<ProjectReference>>> repositories = parseReferenceStrings();
+
+ final List<IProject> importedProjects = new ArrayList<IProject>();
+
+ for (final Map.Entry<URIish, Map<String, Set<ProjectReference>>> entry : repositories
+ .entrySet()) {
+ final URIish gitUrl = entry.getKey();
+ final Map<String, Set<ProjectReference>> branches = entry
+ .getValue();
+
+ for (final Map.Entry<String, Set<ProjectReference>> branchEntry : branches
+ .entrySet()) {
+ final String branch = branchEntry.getKey();
+ final Set<ProjectReference> projects = branchEntry.getValue();
+
+ final IPath workDir = getWorkingDir(gitUrl, branch,
+ branches.keySet());
+ if (workDir.toFile().exists()) {
+ final Collection<String> projectNames = new LinkedList<String>();
+ for (final ProjectReference projectReference : projects)
+ projectNames.add(projectReference.getProjectDir());
+ throw new TeamException(
+ NLS.bind(
+ CoreText.GitProjectSetCapability_CloneToExistingDirectory,
+ new Object[] { workDir, projectNames,
+ gitUrl }));
+ }
+
+ try {
+ int timeout = 60;
+ String refName = Constants.R_HEADS + branch;
+ final CloneOperation cloneOperation = new CloneOperation(
+ gitUrl, true, null, workDir.toFile(), refName,
+ Constants.DEFAULT_REMOTE_NAME, timeout);
+ cloneOperation.run(monitor);
+
+ final File repositoryPath = workDir.append(
+ Constants.DOT_GIT_EXT).toFile();
+
+ Activator.getDefault().getRepositoryUtil()
+ .addConfiguredRepository(repositoryPath);
+
+ List<IProject> p = importProjects(projects, workDir,
+ repositoryPath, monitor);
+ importedProjects.addAll(p);
+ } catch (final InvocationTargetException e) {
+ throw getTeamException(e);
+
+ } catch (final InterruptedException e) {
+ // was canceled by user
+ return Collections.emptyList();
+ }
+ }
+ }
+ return importedProjects;
+ }
+
+ private Map<URIish, Map<String, Set<ProjectReference>>> parseReferenceStrings()
+ throws TeamException {
+ final Map<URIish, Map<String, Set<ProjectReference>>> repositories = new LinkedHashMap<URIish, Map<String, Set<ProjectReference>>>();
+
+ for (final String reference : referenceStrings) {
+ try {
+ final ProjectReference projectReference = new ProjectReference(
+ reference);
+ Map<String, Set<ProjectReference>> repositoryBranches = repositories
+ .get(projectReference.getRepository());
+ if (repositoryBranches == null) {
+ repositoryBranches = new HashMap<String, Set<ProjectReference>>();
+ repositories.put(projectReference.getRepository(),
+ repositoryBranches);
+ }
+ Set<ProjectReference> projectReferences = repositoryBranches
+ .get(projectReference.getBranch());
+ if (projectReferences == null) {
+ projectReferences = new LinkedHashSet<ProjectReference>();
+ repositoryBranches.put(projectReference.getBranch(),
+ projectReferences);
+ }
+
+ projectReferences.add(projectReference);
+ } catch (final IllegalArgumentException e) {
+ throw new TeamException(reference, e);
+ } catch (final URISyntaxException e) {
+ throw new TeamException(reference, e);
+ }
+ }
+
+ return repositories;
+ }
+
+ /**
+ * @param gitUrl
+ * @param branch
+ * the branch to check out
+ * @param allBranches
+ * all branches which should be checked out for this gitUrl
+ * @return the directory where the project should be checked out
+ */
+ private static IPath getWorkingDir(URIish gitUrl, String branch,
+ Set<String> allBranches) {
+ final IPath workspaceLocation = ResourcesPlugin.getWorkspace()
+ .getRoot().getRawLocation();
+ final String humanishName = gitUrl.getHumanishName();
+ String extendedName;
+ if (allBranches.size() == 1 || branch.equals(Constants.MASTER))
+ extendedName = humanishName;
+ else
+ extendedName = humanishName + "_" + branch; //$NON-NLS-1$
+ final IPath workDir = workspaceLocation.append(extendedName);
+ return workDir;
+ }
+
+ private List<IProject> importProjects(final Set<ProjectReference> projects,
+ final IPath workDir, final File repositoryPath,
+ final IProgressMonitor monitor) throws TeamException {
+ try {
+
+ List<IProject> importedProjects = new ArrayList<IProject>();
+
+ // import projects from the current repository to workspace
+ final IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ final IWorkspaceRoot root = workspace.getRoot();
+ for (final ProjectReference projectToImport : projects) {
+ final IPath projectDir = workDir.append(projectToImport
+ .getProjectDir());
+ final IProjectDescription projectDescription = workspace
+ .loadProjectDescription(projectDir
+ .append(IProjectDescription.DESCRIPTION_FILE_NAME));
+ final IProject project = root.getProject(projectDescription
+ .getName());
+ project.create(projectDescription, monitor);
+ importedProjects.add(project);
+
+ project.open(monitor);
+ final ConnectProviderOperation connectProviderOperation = new ConnectProviderOperation(
+ project, repositoryPath);
+ connectProviderOperation.execute(monitor);
+ }
+
+ return importedProjects;
+
+ } catch (final CoreException e) {
+ throw TeamException.asTeamException(e);
+ }
+ }
+
+ private TeamException getTeamException(final Throwable throwable) {
+ Throwable current = throwable;
+ while (current.getCause() != null)
+ current = current.getCause();
+ return new TeamException(current.getMessage(), current);
+ }
+}