1 /*******************************************************************************
2 * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
3 * Copyright (C) 2011, Robin Stocker <robin@nibor.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License 2.0
7 * which accompanies this distribution, and is available at
8 * https://www.eclipse.org/legal/epl-2.0/
10 * SPDX-License-Identifier: EPL-2.0
13 * Manuel Doninger <manuel.doninger@googlemail.com>
14 * Tomasz Zarna <Tomasz.Zarna@pl.ibm.com>
15 *******************************************************************************/
16 package org
.eclipse
.egit
.core
;
18 import java
.io
.IOException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import org
.eclipse
.core
.resources
.IProject
;
24 import org
.eclipse
.core
.resources
.IWorkspace
;
25 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
26 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
27 import org
.eclipse
.core
.runtime
.CoreException
;
28 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
29 import org
.eclipse
.egit
.core
.internal
.CoreText
;
30 import org
.eclipse
.egit
.core
.internal
.GitURI
;
31 import org
.eclipse
.egit
.core
.internal
.ProjectReferenceImporter
;
32 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
33 import org
.eclipse
.jgit
.annotations
.Nullable
;
34 import org
.eclipse
.jgit
.lib
.ConfigConstants
;
35 import org
.eclipse
.jgit
.lib
.StoredConfig
;
36 import org
.eclipse
.osgi
.util
.NLS
;
37 import org
.eclipse
.team
.core
.ProjectSetCapability
;
38 import org
.eclipse
.team
.core
.ProjectSetSerializationContext
;
39 import org
.eclipse
.team
.core
.TeamException
;
42 * Capability for exporting and importing projects shared with Git as part of a
45 public final class GitProjectSetCapability
extends ProjectSetCapability
{
47 private static final String VERSION
= "1.0"; //$NON-NLS-1$
50 public String
[] asReference(IProject
[] projects
,
51 ProjectSetSerializationContext context
, IProgressMonitor monitor
)
52 throws TeamException
{
53 List
<String
> references
= new ArrayList
<>(projects
.length
);
54 for (IProject project
: projects
) {
55 String reference
= asReference(project
);
56 if(reference
!= null){
57 references
.add(reference
);
60 return references
.toArray(new String
[0]);
64 private String
asReference(IProject project
) throws TeamException
{
65 RepositoryMapping mapping
= RepositoryMapping
.getMapping(project
);
66 if (mapping
== null) {
71 branch
= mapping
.getRepository().getBranch();
72 } catch (IOException e
) {
73 throw new TeamException(NLS
.bind(
74 CoreText
.GitProjectSetCapability_ExportCouldNotGetBranch
,
77 StoredConfig config
= mapping
.getRepository().getConfig();
78 String remote
= config
.getString(ConfigConstants
.CONFIG_BRANCH_SECTION
,
79 branch
, ConfigConstants
.CONFIG_KEY_REMOTE
);
80 String url
= config
.getString(ConfigConstants
.CONFIG_REMOTE_SECTION
,
81 remote
, ConfigConstants
.CONFIG_KEY_URL
);
83 throw new TeamException(NLS
.bind(
84 CoreText
.GitProjectSetCapability_ExportNoRemote
,
87 String projectPath
= mapping
.getRepoRelativePath(project
);
88 if (projectPath
== null) {
91 if (projectPath
.isEmpty())
92 projectPath
= "."; //$NON-NLS-1$
94 return asReference(url
, branch
, projectPath
);
97 private String
asReference(String url
, String branch
, String projectPath
) {
98 StringBuilder sb
= new StringBuilder();
101 sb
.append(ProjectReference
.SEPARATOR
);
103 sb
.append(ProjectReference
.SEPARATOR
);
105 sb
.append(ProjectReference
.SEPARATOR
);
106 sb
.append(projectPath
);
108 return sb
.toString();
112 public IProject
[] addToWorkspace(final String
[] referenceStrings
,
113 final ProjectSetSerializationContext context
,
114 final IProgressMonitor monitor
) throws TeamException
{
115 final ArrayList
<IProject
> importedProjects
= new ArrayList
<>();
118 ResourcesPlugin
.getWorkspace().run(new IWorkspaceRunnable() {
120 public void run(IProgressMonitor wsOpMonitor
) throws CoreException
{
121 ProjectReferenceImporter importer
= new ProjectReferenceImporter(referenceStrings
);
122 List
<IProject
> p
= importer
.run(wsOpMonitor
);
123 importedProjects
.addAll(p
);
125 }, ResourcesPlugin
.getWorkspace().getRoot(), IWorkspace
.AVOID_UPDATE
, monitor
);
126 } catch (CoreException e
) {
127 throw TeamException
.asTeamException(e
);
129 final IProject
[] result
= importedProjects
130 .toArray(new IProject
[0]);
136 public String
asReference(URI uri
, String projectName
) {
138 GitURI gitURI
= new GitURI(uri
);
139 return asReference(gitURI
.getRepository().toString(),
140 gitURI
.getTag(), gitURI
.getPath().toString());
141 } catch (IllegalArgumentException e
) {
142 Activator
.logError(e
.getMessage(), e
);
143 // we must not fail but return null on invalid or unknown URI's.