1 /*******************************************************************************
2 * Copyright (c) 2010, 2013, 2015 SAP AG and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * Mathias Kinzler (SAP AG) - initial implementation
10 * Thomas Wolf <thomas.wolf@paranor.ch> - Bugs 477281, 478877
11 *******************************************************************************/
12 package org
.eclipse
.egit
.ui
.internal
.repository
.tree
.command
;
15 import java
.io
.IOException
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.eclipse
.core
.commands
.ExecutionEvent
;
21 import org
.eclipse
.core
.commands
.ExecutionException
;
22 import org
.eclipse
.core
.resources
.IProject
;
23 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IPath
;
26 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
27 import org
.eclipse
.core
.runtime
.Path
;
28 import org
.eclipse
.core
.runtime
.preferences
.DefaultScope
;
29 import org
.eclipse
.core
.runtime
.preferences
.IEclipsePreferences
;
30 import org
.eclipse
.core
.runtime
.preferences
.InstanceScope
;
31 import org
.eclipse
.egit
.core
.Activator
;
32 import org
.eclipse
.egit
.core
.GitCorePreferences
;
33 import org
.eclipse
.egit
.core
.JobFamilies
;
34 import org
.eclipse
.egit
.core
.internal
.CoreText
;
35 import org
.eclipse
.egit
.core
.internal
.gerrit
.GerritUtil
;
36 import org
.eclipse
.egit
.core
.internal
.job
.JobUtil
;
37 import org
.eclipse
.egit
.core
.op
.ConnectProviderOperation
;
38 import org
.eclipse
.egit
.core
.project
.RepositoryFinder
;
39 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
40 import org
.eclipse
.egit
.ui
.internal
.UIText
;
41 import org
.eclipse
.egit
.ui
.internal
.repository
.RepositorySearchWizard
;
42 import org
.eclipse
.egit
.ui
.internal
.repository
.tree
.RepositoryTreeNode
;
43 import org
.eclipse
.jface
.dialogs
.IDialogConstants
;
44 import org
.eclipse
.jface
.window
.Window
;
45 import org
.eclipse
.jface
.wizard
.WizardDialog
;
46 import org
.eclipse
.jgit
.lib
.Repository
;
47 import org
.eclipse
.jgit
.util
.FileUtils
;
48 import org
.eclipse
.swt
.widgets
.Button
;
49 import org
.eclipse
.swt
.widgets
.Composite
;
50 import org
.eclipse
.team
.core
.RepositoryProvider
;
55 public class AddCommand
extends
56 RepositoriesViewCommandHandler
<RepositoryTreeNode
> {
58 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
59 RepositorySearchWizard wizard
= new RepositorySearchWizard(
60 util
.getConfiguredRepositories(), true);
61 WizardDialog dialog
= new WizardDialog(getShell(event
), wizard
) {
63 protected Button
createButton(Composite parent
, int id
,
64 String label
, boolean defaultButton
) {
65 if (id
== IDialogConstants
.FINISH_ID
) {
66 return super.createButton(parent
, id
,
67 UIText
.AddCommand_AddButtonLabel
, defaultButton
);
69 return super.createButton(parent
, id
, label
, defaultButton
);
72 if (dialog
.open() == Window
.OK
) {
73 for (String dir
: wizard
.getDirectories()) {
74 File repositoryDir
= FileUtils
.canonicalize(new File(dir
));
75 addRepository(repositoryDir
);
81 private void addRepository(File repositoryDir
) {
82 GerritUtil
.tryToAutoConfigureForGerrit(repositoryDir
);
83 util
.addConfiguredRepository(repositoryDir
);
85 autoShareProjects(repositoryDir
);
89 private void autoShareProjects(File repositoryDir
) {
90 // Don't even try to auto-share for bare repositories.
93 Repository repo
= Activator
.getDefault().getRepositoryCache()
94 .lookupRepository(repositoryDir
);
98 workingDirPath
= new Path(repo
.getWorkTree().getAbsolutePath());
99 } catch (IOException e
) {
100 org
.eclipse
.egit
.ui
.Activator
.logError(e
.getLocalizedMessage(), e
);
103 Map
<IProject
, File
> connections
= new HashMap
<>();
104 IProject
[] projects
= ResourcesPlugin
.getWorkspace().getRoot()
106 for (IProject project
: projects
) {
107 // Skip closed projects
108 if (!project
.isAccessible()) {
111 RepositoryProvider provider
= RepositoryProvider
112 .getProvider(project
);
113 if (provider
!= null) {
116 IPath location
= project
.getLocation();
117 if (location
== null) {
120 // In case the project is not inside the working directory, don't
121 // even search for a mapping.
122 if (!workingDirPath
.isPrefixOf(location
)) {
125 RepositoryFinder f
= new RepositoryFinder(project
);
126 f
.setFindInChildren(false);
128 List
<RepositoryMapping
> mappings
= f
129 .find(new NullProgressMonitor());
130 if (!mappings
.isEmpty()) {
131 // Connect to the first one; it's the innermost.
132 IPath gitDir
= mappings
.get(0).getGitDirAbsolutePath();
133 if (gitDir
!= null) {
134 connections
.put(project
, gitDir
.toFile());
137 } catch (CoreException e
) {
138 // Ignore this project in that case
142 if (!connections
.isEmpty()) {
143 ConnectProviderOperation operation
= new ConnectProviderOperation(
145 operation
.setRefreshResources(false);
146 JobUtil
.scheduleUserJob(operation
,
147 CoreText
.Activator_AutoShareJobName
, JobFamilies
.AUTO_SHARE
);
151 private boolean doAutoShare() {
152 IEclipsePreferences d
= DefaultScope
.INSTANCE
.getNode(Activator
154 IEclipsePreferences p
= InstanceScope
.INSTANCE
.getNode(Activator
156 return p
.getBoolean(GitCorePreferences
.core_autoShareProjects
,
157 d
.getBoolean(GitCorePreferences
.core_autoShareProjects
, true));