refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / GitProjectSetCapability.java
blob91193cd7bb19a974b46cb9e2763ded16cb619b19
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
12 * Contributors:
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;
19 import java.net.URI;
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;
41 /**
42 * Capability for exporting and importing projects shared with Git as part of a
43 * team project set.
45 public final class GitProjectSetCapability extends ProjectSetCapability {
47 private static final String VERSION = "1.0"; //$NON-NLS-1$
49 @Override
50 public String[] asReference(IProject[] projects,
51 ProjectSetSerializationContext context, IProgressMonitor monitor)
52 throws TeamException {
53 List<String> references = new ArrayList<>(projects.length);
54 for (int i = 0; i < projects.length; i++) {
55 String reference = asReference(projects[i]);
56 if(reference != null){
57 references.add(reference);
60 return references.toArray(new String[0]);
63 @Nullable
64 private String asReference(IProject project) throws TeamException {
65 RepositoryMapping mapping = RepositoryMapping.getMapping(project);
66 if (mapping == null) {
67 return null;
69 String branch;
70 try {
71 branch = mapping.getRepository().getBranch();
72 } catch (IOException e) {
73 throw new TeamException(NLS.bind(
74 CoreText.GitProjectSetCapability_ExportCouldNotGetBranch,
75 project.getName()));
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);
82 if (url == null)
83 throw new TeamException(NLS.bind(
84 CoreText.GitProjectSetCapability_ExportNoRemote,
85 project.getName()));
87 String projectPath = mapping.getRepoRelativePath(project);
88 if (projectPath == null) {
89 return null;
91 if (projectPath.equals("")) //$NON-NLS-1$
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();
100 sb.append(VERSION);
101 sb.append(ProjectReference.SEPARATOR);
102 sb.append(url);
103 sb.append(ProjectReference.SEPARATOR);
104 sb.append(branch);
105 sb.append(ProjectReference.SEPARATOR);
106 sb.append(projectPath);
108 return sb.toString();
111 @Override
112 public IProject[] addToWorkspace(final String[] referenceStrings,
113 final ProjectSetSerializationContext context,
114 final IProgressMonitor monitor) throws TeamException {
115 final ArrayList<IProject> importedProjects = new ArrayList<IProject>();
117 try{
118 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
119 @Override
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]);
131 return result;
134 @Nullable
135 @Override
136 public String asReference(URI uri, String projectName) {
137 try {
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.
144 return null;