Always use a single WindowCache for the entire JVM
[egit/zawir.git] / org.spearce.egit.core / src / org / spearce / egit / core / op / ConnectProviderOperation.java
blob612ab8a76034a7ed95556698aa46fea9615d102d
1 /*
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;
19 import java.io.File;
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;
39 /**
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;
47 /**
48 * Create a new connection operation to execute within the workspace.
50 * @param proj
51 * the project to connect to the Git team provider.
52 * @param newdir
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) {
58 project = proj;
59 newGitDir = newdir;
62 public void run(IProgressMonitor m) throws CoreException {
63 if (m == null) {
64 m = new NullProgressMonitor();
67 m.beginTask(CoreText.ConnectProviderOperation_connecting, 100);
68 try {
69 final Collection repos = new ArrayList();
71 if (newGitDir != null) {
72 try {
73 final Repository db;
75 m.subTask(CoreText.ConnectProviderOperation_creating);
76 Activator.trace("Creating repository " + newGitDir);
78 db = new Repository(newGitDir);
79 db.create();
80 repos.add(new RepositoryMapping(project, db.getDirectory(),
81 null));
82 db.close();
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));
94 m.worked(10);
95 } catch (Throwable err) {
96 throw Activator.error(
97 CoreText.ConnectProviderOperation_creating, err);
99 } else {
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);
108 projectData.store();
110 try {
111 RepositoryProvider.map(project, GitProvider.class.getName());
112 } catch (CoreException ce) {
113 GitProjectData.delete(project);
114 throw ce;
115 } catch (RuntimeException ce) {
116 GitProjectData.delete(project);
117 throw ce;
120 projectData = GitProjectData.get(project);
121 project.refreshLocal(IResource.DEPTH_INFINITE,
122 new SubProgressMonitor(m, 50));
124 m.subTask(CoreText.ConnectProviderOperation_updatingCache);
125 } finally {
126 m.done();