2 * Copyright (C) 2006 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.egit
.core
.op
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Collection
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
26 import org
.eclipse
.core
.runtime
.CoreException
;
27 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
28 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
29 import org
.eclipse
.core
.runtime
.SubProgressMonitor
;
30 import org
.eclipse
.team
.core
.RepositoryProvider
;
31 import org
.spearce
.egit
.core
.Activator
;
32 import org
.spearce
.egit
.core
.CoreText
;
33 import org
.spearce
.egit
.core
.GitProvider
;
34 import org
.spearce
.egit
.core
.project
.GitProjectData
;
35 import org
.spearce
.egit
.core
.project
.RepositoryFinder
;
36 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
37 import org
.spearce
.jgit
.lib
.Repository
;
40 * Connects Eclipse to an existing Git repository, or creates a new one.
42 public class ConnectProviderOperation
implements IWorkspaceRunnable
{
43 private final IProject project
;
45 private final File newGitDir
;
48 * Create a new connection operation to execute within the workspace.
51 * the project to connect to the Git team provider.
53 * git repository to create if the user requested a new
54 * repository be constructed for this project; null to scan for
55 * an existing repository and connect to that.
57 public ConnectProviderOperation(final IProject proj
, final File newdir
) {
62 public void run(IProgressMonitor m
) throws CoreException
{
64 m
= new NullProgressMonitor();
67 m
.beginTask(CoreText
.ConnectProviderOperation_connecting
, 100);
69 final Collection repos
= new ArrayList();
71 if (newGitDir
!= null) {
75 m
.subTask(CoreText
.ConnectProviderOperation_creating
);
76 Activator
.trace("Creating repository " + newGitDir
);
78 db
= new Repository(newGitDir
);
80 repos
.add(new RepositoryMapping(project
, db
.getDirectory(),
84 // If we don't refresh the project directory right
85 // now we won't later know that a .git directory
86 // exists within it and we won't mark the .git
87 // directory as a team-private member. Failure
88 // to do so might allow someone to delete
89 // the .git directory without us stopping them.
91 project
.refreshLocal(IResource
.DEPTH_ONE
,
92 new SubProgressMonitor(m
, 10));
95 } catch (Throwable err
) {
96 throw Activator
.error(
97 CoreText
.ConnectProviderOperation_creating
, err
);
100 Activator
.trace("Finding existing repositories.");
101 repos
.addAll(new RepositoryFinder(project
)
102 .find(new SubProgressMonitor(m
, 20)));
105 m
.subTask(CoreText
.ConnectProviderOperation_recordingMapping
);
106 GitProjectData projectData
= new GitProjectData(project
);
107 projectData
.setRepositoryMappings(repos
);
111 RepositoryProvider
.map(project
, GitProvider
.class.getName());
112 } catch (CoreException ce
) {
113 GitProjectData
.delete(project
);
115 } catch (RuntimeException ce
) {
116 GitProjectData
.delete(project
);
120 projectData
= GitProjectData
.get(project
);
121 project
.refreshLocal(IResource
.DEPTH_INFINITE
,
122 new SubProgressMonitor(m
, 50));
124 m
.subTask(CoreText
.ConnectProviderOperation_updatingCache
);