From 6ed6b9104cbce86668d6819601694f8a29a16119 Mon Sep 17 00:00:00 2001 From: Robin Stocker Date: Tue, 17 Jan 2012 00:42:57 +0100 Subject: [PATCH] Refactor import code of GitProjectSetCapability Extracted a class to simplify things a bit. Bug: 366207 Change-Id: I52314c2487209bf477337c205ef3785adb2203d9 Signed-off-by: Robin Stocker Signed-off-by: Matthias Sohn --- .../eclipse/egit/core/GitProjectSetCapability.java | 138 +------------ .../core/internal/ProjectReferenceImporter.java | 219 +++++++++++++++++++++ 2 files changed, 224 insertions(+), 133 deletions(-) create mode 100644 org.eclipse.egit.core/src/org/eclipse/egit/core/internal/ProjectReferenceImporter.java 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 702633517..836081a66 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>> repositories = - new LinkedHashMap>>(); - for (final String reference : referenceStrings) { - try { - final ProjectReference projectReference = new ProjectReference( - reference); - Map> repositoryBranches = repositories - .get(projectReference.getRepository()); - if (repositoryBranches == null) { - repositoryBranches = new HashMap>(); - repositories.put(projectReference.getRepository(), - repositoryBranches); - } - Set projectReferences = repositoryBranches.get(projectReference.getBranch()); - if (projectReferences == null) { - projectReferences = new LinkedHashSet(); - 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>> entry : repositories.entrySet()) { - final URIish gitUrl = entry.getKey(); - final Map> branches = entry.getValue(); - - for (final Map.Entry> branchEntry : branches.entrySet()) { - final String branch = branchEntry.getKey(); - final Set projects = branchEntry.getValue(); - - try { - final IPath workDir = getWorkingDir(gitUrl, branch, - branches.keySet()); - if (workDir.toFile().exists()) { - final Collection projectNames = new LinkedList(); - 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 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 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 000000000..a55778a6a --- /dev/null +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/internal/ProjectReferenceImporter.java @@ -0,0 +1,219 @@ +/******************************************************************************* + * Copyright (C) 2011, Robin Stocker + * + * 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 run(IProgressMonitor monitor) throws TeamException { + + final Map>> repositories = parseReferenceStrings(); + + final List importedProjects = new ArrayList(); + + for (final Map.Entry>> entry : repositories + .entrySet()) { + final URIish gitUrl = entry.getKey(); + final Map> branches = entry + .getValue(); + + for (final Map.Entry> branchEntry : branches + .entrySet()) { + final String branch = branchEntry.getKey(); + final Set projects = branchEntry.getValue(); + + final IPath workDir = getWorkingDir(gitUrl, branch, + branches.keySet()); + if (workDir.toFile().exists()) { + final Collection projectNames = new LinkedList(); + 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 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>> parseReferenceStrings() + throws TeamException { + final Map>> repositories = new LinkedHashMap>>(); + + for (final String reference : referenceStrings) { + try { + final ProjectReference projectReference = new ProjectReference( + reference); + Map> repositoryBranches = repositories + .get(projectReference.getRepository()); + if (repositoryBranches == null) { + repositoryBranches = new HashMap>(); + repositories.put(projectReference.getRepository(), + repositoryBranches); + } + Set projectReferences = repositoryBranches + .get(projectReference.getBranch()); + if (projectReferences == null) { + projectReferences = new LinkedHashSet(); + 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 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 importProjects(final Set projects, + final IPath workDir, final File repositoryPath, + final IProgressMonitor monitor) throws TeamException { + try { + + List importedProjects = new ArrayList(); + + // 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); + } +} -- 2.11.4.GIT