Fix build and test failures
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / op / ConnectProviderOperation.java
blobd61a0b37e65afbc0e4ce3aa76debfb1c00901d01
1 /*******************************************************************************
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3 * Copyright (C) 2008, Google Inc.
4 * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the Eclipse Public License v1.0
8 * which accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *******************************************************************************/
11 package org.eclipse.egit.core.op;
13 import java.io.File;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.Map;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspaceRunnable;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.NullProgressMonitor;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28 import org.eclipse.egit.core.Activator;
29 import org.eclipse.egit.core.CoreText;
30 import org.eclipse.egit.core.GitProvider;
31 import org.eclipse.egit.core.project.GitProjectData;
32 import org.eclipse.egit.core.project.RepositoryFinder;
33 import org.eclipse.egit.core.project.RepositoryMapping;
34 import org.eclipse.jgit.lib.Constants;
35 import org.eclipse.osgi.util.NLS;
36 import org.eclipse.team.core.RepositoryProvider;
38 /**
39 * Connects Eclipse to an existing Git repository
41 public class ConnectProviderOperation implements IWorkspaceRunnable {
42 private final Map<IProject, File> projects = new HashMap<IProject, File>();
44 /**
45 * Create a new connection operation to execute within the workspace.
46 * <p>
47 * Uses <code>.git</code> as a default relative path to repository.
48 * @see #ConnectProviderOperation(IProject, File)
50 * @param proj
51 * the project to connect to the Git team provider.
53 public ConnectProviderOperation(final IProject proj) {
54 this(proj, proj.getLocation().append("/").append(Constants.DOT_GIT).toFile());
57 /**
58 * Create a new connection operation to execute within the workspace.
60 * @param proj
61 * the project to connect to the Git team provider.
62 * @param pathToRepo
63 * absolute path to the repository
65 public ConnectProviderOperation(final IProject proj, File pathToRepo) {
66 this.projects.put(proj, pathToRepo);
69 /**
70 * Create a new connection operation to execute within the workspace.
72 * @param projects
73 * the projects to connect to the Git team provider.
75 public ConnectProviderOperation(final Map<IProject, File> projects) {
76 this.projects.putAll(projects);
79 public void run(IProgressMonitor m) throws CoreException {
80 if (m == null) {
81 m = new NullProgressMonitor();
84 m.beginTask(CoreText.ConnectProviderOperation_connecting,
85 100 * projects.size());
86 try {
88 for (Iterator iterator = projects.keySet().iterator(); iterator.hasNext();) {
89 IProject project = (IProject) iterator.next();
90 m.setTaskName(NLS.bind(
91 CoreText.ConnectProviderOperation_ConnectingProject,
92 project.getName()));
93 Activator.trace("Locating repository for " + project); //$NON-NLS-1$
94 Collection<RepositoryMapping> repos = new RepositoryFinder(
95 project).find(new SubProgressMonitor(m, 40));
96 File suggestedRepo = projects.get(project);
97 RepositoryMapping actualMapping= findActualRepository(repos, suggestedRepo);
98 if (actualMapping != null) {
99 GitProjectData projectData = new GitProjectData(project);
100 try {
101 projectData.setRepositoryMappings(Arrays.asList(actualMapping));
102 projectData.store();
103 } catch (CoreException ce) {
104 GitProjectData.delete(project);
105 throw ce;
106 } catch (RuntimeException ce) {
107 GitProjectData.delete(project);
108 throw ce;
110 RepositoryProvider
111 .map(project, GitProvider.class.getName());
112 projectData = GitProjectData.get(project);
113 project.refreshLocal(IResource.DEPTH_INFINITE,
114 new SubProgressMonitor(m, 50));
115 m.worked(10);
116 } else {
117 Activator
118 .trace("Attempted to share project without repository ignored :" //$NON-NLS-1$
119 + project);
120 m.worked(60);
123 } finally {
124 m.done();
129 * @param repos
130 * available repositories
131 * @param suggestedRepo
132 * relative path to git repository
133 * @return a repository mapping which corresponds to a suggested repository
134 * location, <code>null</code> otherwise
136 private RepositoryMapping findActualRepository(
137 Collection<RepositoryMapping> repos, File suggestedRepo) {
138 for (RepositoryMapping rm : repos) {
139 if (rm.getGitDirAbsolutePath().equals(Path.fromOSString(suggestedRepo.getPath())))
140 return rm;
142 return null;