<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation and others.
 * Copyright (C) 2007, <NAME> (<EMAIL>)
 * Copyright (C) 2008, <NAME> <<EMAIL>>
 * Copyright (C) 2009, <NAME> <<EMAIL>>
 * Copyright (C) 2010, <NAME> <<EMAIL>>
 * Copyright (C) 2010, <NAME> <<EMAIL>>
 * Copyright (C) 2011, <NAME> <<EMAIL>>
 *
 * 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.ui.internal.clone;

import java.io.File;

import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.osgi.util.NLS;

/**
 * Used for creating projects out of .project files
 */
public class ProjectRecord {
	private final File projectSystemFile;

	private String projectName;

	private IProjectDescription description;

	/**
	 * Create a record for a project based on the info in the file.
	 *
	 * @param file
	 */
	public ProjectRecord(File file) {
		projectSystemFile = file;
		IPath path = new Path(projectSystemFile.getPath());
		try {
			description = ResourcesPlugin.getWorkspace()
					.loadProjectDescription(path);
			projectName = description.getName();
		} catch (CoreException e) {
			description = null;
			projectName = path.lastSegment();
		}
	}

	/**
	 * Get the name of the project
	 *
	 * @return String
	 */
	public String getProjectName() {
		return projectName;
	}

	/**
	 * Gets the label to be used when rendering this project record in the UI.
	 *
	 * @return String the label
	 */
	public String getProjectLabel() {
		String path = projectSystemFile.getParent();

		return NLS.bind("{0} ({1})", projectName, path); //$NON-NLS-1$
	}

	/**
	 * @return the project description
	 */
	public IProjectDescription getProjectDescription() {
		return description;
	}

	/**
	 * @param description
	 */
	public void setProjectDescription(IProjectDescription description) {
		this.description = description;
	}

	/**
	 * @return the file used in the constructor
	 */
	public File getProjectSystemFile() {
		return projectSystemFile;
	}

	@Override
	public String toString() {
		return projectName;
	}
}<commit_msg>Fix "overlaps the location of another project" when importing<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2013 IBM Corporation and others.
 * Copyright (C) 2007, <NAME> (<EMAIL>)
 * Copyright (C) 2008, <NAME> <<EMAIL>>
 * Copyright (C) 2009, <NAME> <<EMAIL>>
 * Copyright (C) 2010, <NAME> <<EMAIL>>
 * Copyright (C) 2010, <NAME> <<EMAIL>>
 * Copyright (C) 2011, <NAME> <<EMAIL>>
 *
 * 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.ui.internal.clone;

import java.io.File;

import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.util.NLS;

/**
 * Used for creating projects out of .project files
 */
public class ProjectRecord {
	private final File projectSystemFile;

	private String projectName;

	private IProjectDescription description;

	/**
	 * Create a record for a project based on the info in the file.
	 *
	 * @param file
	 */
	public ProjectRecord(File file) {
		projectSystemFile = file;
		IPath path = new Path(projectSystemFile.getPath());
		try {
			// If the file is in the default location, use the directory
			// name as the project name. Otherwise we will get an error like
			// "foo overlaps the location of another project foo" when importing
			// in case the directory name and the name in .project do not match.
			if (isDefaultLocation(path)) {
				projectName = path.segment(path.segmentCount() - 2);
				description = ResourcesPlugin.getWorkspace()
						.newProjectDescription(projectName);
			} else {
				description = ResourcesPlugin.getWorkspace()
						.loadProjectDescription(path);
				projectName = description.getName();
			}
		} catch (CoreException e) {
			description = null;
			projectName = path.lastSegment();
		}
	}

	/**
	 * Get the name of the project
	 *
	 * @return String
	 */
	public String getProjectName() {
		return projectName;
	}

	/**
	 * Gets the label to be used when rendering this project record in the UI.
	 *
	 * @return String the label
	 */
	public String getProjectLabel() {
		String path = projectSystemFile.getParent();

		return NLS.bind("{0} ({1})", projectName, path); //$NON-NLS-1$
	}

	/**
	 * @return the project description
	 */
	public IProjectDescription getProjectDescription() {
		return description;
	}

	/**
	 * @param description
	 */
	public void setProjectDescription(IProjectDescription description) {
		this.description = description;
	}

	/**
	 * @return the file used in the constructor
	 */
	public File getProjectSystemFile() {
		return projectSystemFile;
	}

	@Override
	public String toString() {
		return projectName;
	}

	/**
	 * Returns whether the given project description file path is in the default
	 * location for a project
	 *
	 * @param path
	 *            The path to examine
	 * @return Whether the given path is the default location for a project
	 */
	private boolean isDefaultLocation(IPath path) {
		// The project description file must at least be within the project,
		// which is within the workspace location
		if (path.segmentCount() < 2)
			return false;
		return path.removeLastSegments(2).toFile()
				.equals(Platform.getLocation().toFile());
	}

}
{'fraction_non_alphanumeric': 0.156, 'fraction_numerical': 0.01296, 'mean_word_length': 4.0008, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1118913', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1692, 'n_words': 570}