Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 33ccb21a6a5cfc2b39528d0f3ab6eafa2a6fae21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 * Copyright (C) 2008, Google Inc.
 *
 * 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.op;

import java.util.Collection;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.CoreText;
import org.eclipse.egit.core.GitProvider;
import org.eclipse.egit.core.project.GitProjectData;
import org.eclipse.egit.core.project.RepositoryFinder;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.RepositoryProvider;

/**
 * Connects Eclipse to an existing Git repository
 */
public class ConnectProviderOperation implements IWorkspaceRunnable {
	private final IProject[] projects;

	/**
	 * Create a new connection operation to execute within the workspace.
	 *
	 * @param proj
	 *            the project to connect to the Git team provider.
	 */
	public ConnectProviderOperation(final IProject proj) {
		this(new IProject[] { proj });
	}

	/**
	 * Create a new connection operation to execute within the workspace.
	 *
	 * @param projects
	 *            the projects to connect to the Git team provider.
	 */
	public ConnectProviderOperation(final IProject[] projects) {
		this.projects = projects;
	}

	public void run(IProgressMonitor m) throws CoreException {
		if (m == null) {
			m = new NullProgressMonitor();
		}

		m.beginTask(CoreText.ConnectProviderOperation_connecting,
				100 * projects.length);
		try {

			for (IProject project : projects) {
				m.setTaskName(NLS.bind(
						CoreText.ConnectProviderOperation_ConnectingProject,
						project.getName()));
				Activator.trace("Locating repository for " + project); //$NON-NLS-1$
				Collection<RepositoryMapping> repos = new RepositoryFinder(
						project).find(new SubProgressMonitor(m, 40));
				if (repos.size() == 1) {
					GitProjectData projectData = new GitProjectData(project);
					try {
						projectData.setRepositoryMappings(repos);
						projectData.store();
					} catch (CoreException ce) {
						GitProjectData.delete(project);
						throw ce;
					} catch (RuntimeException ce) {
						GitProjectData.delete(project);
						throw ce;
					}
					RepositoryProvider
							.map(project, GitProvider.class.getName());
					projectData = GitProjectData.get(project);
					project.refreshLocal(IResource.DEPTH_INFINITE,
							new SubProgressMonitor(m, 50));
					m.worked(10);
				} else {
					Activator
							.trace("Attempted to share project without repository ignored :" //$NON-NLS-1$
									+ project);
					m.worked(60);
				}
			}
		} finally {
			m.done();
		}
	}
}

Back to the top