Skip to content

Commit

Permalink
Allow duplicate folder names in SmartImport wizard #1294
Browse files Browse the repository at this point in the history
Only block if new project's description's name is set and already taken.
Otherwise the smart import will come up with a new project name
prepending the parent folder name followed by "_" (recursively).

Fixes #1294
  • Loading branch information
kwin authored and mickaelistria committed Dec 10, 2023
1 parent 7d88c88 commit 4760c84
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
Expand Down Expand Up @@ -570,20 +571,27 @@ private IProject projectAlreadyExistsInWorkspace(File directory) {
return null;
}

private IProject createOrImportProject(File directory, IProgressMonitor progressMonitor) throws Exception {
IProjectDescription desc = null;
static Optional<IProjectDescription> getOptionalProjectDescription(File directory) throws CoreException {
File expectedProjectDescriptionFile = new File(directory, IProjectDescription.DESCRIPTION_FILE_NAME);
if (expectedProjectDescriptionFile.exists()) {
desc = ResourcesPlugin.getWorkspace().loadProjectDescription(IPath.fromOSString(expectedProjectDescriptionFile.getAbsolutePath()));
String expectedName = desc.getName();
IProject projectWithSameName = this.workspaceRoot.getProject(expectedName);
return Optional.ofNullable(ResourcesPlugin.getWorkspace()
.loadProjectDescription(IPath.fromOSString(expectedProjectDescriptionFile.getAbsolutePath())));
}
return Optional.empty();
}

private IProject createOrImportProject(File directory, IProgressMonitor progressMonitor) throws Exception {
IProjectDescription desc = getOptionalProjectDescription(directory).orElse(null);
if (desc != null) {
IProject projectWithSameName = this.workspaceRoot.getProject(desc.getName());
if (projectWithSameName.exists()) {
if (directory.equals(SmartImportWizard.toFile(projectWithSameName))) {
// project seems already there
return projectWithSameName;
}
throw new CouldNotImportProjectException(directory,
NLS.bind(DataTransferMessages.SmartImportProposals_anotherProjectWithSameNameExists_description, expectedName));
NLS.bind(DataTransferMessages.SmartImportProposals_anotherProjectWithSameNameExists_description,
desc.getName()));
}
} else {
String projectName = generateNewProjectName(directory, this.workspaceRoot);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import java.util.function.Supplier;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -712,8 +714,14 @@ protected boolean isExistingProject(File element) {
}

protected boolean isExistingProjectName(File element) {
String name = element.getName();
return !name.isEmpty() && ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists();
try {
String name = SmartImportJob.getOptionalProjectDescription(element).map(IProjectDescription::getName)
.orElse(""); //$NON-NLS-1$
return !name.isEmpty() && ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists();
} catch (CoreException e) {
StatusManager.getManager().handle(e.getStatus(), StatusManager.LOG | StatusManager.SHOW);
return false;
}
}

protected void validatePage() {
Expand Down

0 comments on commit 4760c84

Please sign in to comment.