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
;
14 import java
.util
.Arrays
;
15 import java
.util
.Collection
;
16 import java
.util
.HashMap
;
17 import java
.util
.Iterator
;
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
;
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
>();
45 * Create a new connection operation to execute within the workspace.
47 * Uses <code>.git</code> as a default relative path to repository.
48 * @see #ConnectProviderOperation(IProject, File)
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());
58 * Create a new connection operation to execute within the workspace.
61 * the project to connect to the Git team provider.
63 * absolute path to the repository
65 public ConnectProviderOperation(final IProject proj
, File pathToRepo
) {
66 this.projects
.put(proj
, pathToRepo
);
70 * Create a new connection operation to execute within the workspace.
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
{
81 m
= new NullProgressMonitor();
84 m
.beginTask(CoreText
.ConnectProviderOperation_connecting
,
85 100 * projects
.size());
88 for (Iterator iterator
= projects
.keySet().iterator(); iterator
.hasNext();) {
89 IProject project
= (IProject
) iterator
.next();
90 m
.setTaskName(NLS
.bind(
91 CoreText
.ConnectProviderOperation_ConnectingProject
,
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
);
101 projectData
.setRepositoryMappings(Arrays
.asList(actualMapping
));
103 } catch (CoreException ce
) {
104 GitProjectData
.delete(project
);
106 } catch (RuntimeException ce
) {
107 GitProjectData
.delete(project
);
111 .map(project
, GitProvider
.class.getName());
112 projectData
= GitProjectData
.get(project
);
113 project
.refreshLocal(IResource
.DEPTH_INFINITE
,
114 new SubProgressMonitor(m
, 50));
118 .trace("Attempted to share project without repository ignored :" //$NON-NLS-1$
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())))