<commit_before>/*******************************************************************************
 * Copyright (C) 2007, <NAME> <<EMAIL>>
 * Copyright (C) 2007, <NAME> <<EMAIL>>
 * Copyright (C) 2010, <NAME> <<EMAIL>>
 * Copyright (C) 2011, IBM Corporation
 * Copyright (C) 2012, <NAME> <<EMAIL>>
 * Copyright (C) 2012, <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.factories;

import java.io.File;
import java.net.URI;
import java.util.Collections;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.AdapterUtils;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.internal.history.GitHistoryPage;
import org.eclipse.egit.ui.internal.history.GitHistoryPageSource;
import org.eclipse.egit.ui.internal.repository.RepositoriesViewLabelProvider;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
import org.eclipse.egit.ui.internal.selection.SelectionUtils;
import org.eclipse.egit.ui.internal.synchronize.mapping.GitModelWorkbenchAdapter;
import org.eclipse.egit.ui.internal.synchronize.mapping.GitObjectMapping;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelBlob;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelObject;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelTree;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.team.ui.history.IHistoryPage;
import org.eclipse.team.ui.history.IHistoryPageSource;
import org.eclipse.team.ui.history.IHistoryView;
import org.eclipse.team.ui.mapping.ISynchronizationCompareAdapter;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.eclipse.ui.part.IShowInSource;

/**
 * This class is an intelligent "cast" operation for getting
 * an instance of a suitable object from another for a specific
 * purpose.
 */
public class GitAdapterFactory implements IAdapterFactory {

	private GitModelWorkbenchAdapter gitModelWorkbenchAdapter;

	private static final IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
			.getRoot();

	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (adapterType.isAssignableFrom(IHistoryPageSource.class)) {
			return GitHistoryPageSource.INSTANCE;
		}

		if (IWorkbenchAdapter.class == adapterType) {
			if (adaptableObject instanceof RepositoryNode) {
				return getRepsitoryNodeWorkbenchAdapter((RepositoryNode)adaptableObject);
			}

			if (gitModelWorkbenchAdapter == null) {
				gitModelWorkbenchAdapter = new GitModelWorkbenchAdapter();
			}
			return gitModelWorkbenchAdapter;
		}

		if (adaptableObject instanceof IHistoryView
				&& IShowInSource.class == adapterType) {
			IHistoryView historyView = (IHistoryView) adaptableObject;
			IHistoryPage historyPage = historyView.getHistoryPage();
			if (historyPage instanceof GitHistoryPage) {
				return historyPage;
			}
		}

		if (adaptableObject instanceof IURIEditorInput
				&& adapterType == Repository.class) {
			return getRepository((IURIEditorInput) adaptableObject);
		}

		if (adaptableObject instanceof IURIEditorInput
				&& adapterType == File.class) {
			return getFile((IURIEditorInput) adaptableObject);
		}

		if (adaptableObject instanceof GitModelObject
				&& adapterType == ResourceMapping.class) {
			return GitObjectMapping.create((GitModelObject) adaptableObject);
		}

		if (adaptableObject instanceof GitModelObject
				&& adapterType == IResource.class) {
			GitModelObject obj = (GitModelObject) adaptableObject;

			if (obj instanceof GitModelBlob) {
				IResource res = ResourceUtil
						.getFileForLocation(obj.getLocation(), false);
				if (res == null) {
					// Deleted resource?
					res = getWorkspaceResourceFromGitPath(obj.getLocation());
				}

				return res;
			}

			if (obj instanceof GitModelTree) {
				IResource res = root.getContainerForLocation(obj.getLocation());
				if (res == null) {
					res = root.getFolder(obj.getLocation());
				}

				return res;
			}
		}

		if (adapterType == Repository.class) {
			ResourceMapping m = AdapterUtils.adapt(adaptableObject,
					ResourceMapping.class);
			if (m != null) {
				return SelectionUtils.getRepository(new StructuredSelection(m));
			}
		}

		return null;
	}

	@Nullable
	private IResource getWorkspaceResourceFromGitPath(IPath gitPath) {
		Repository repository = Activator.getDefault().getRepositoryCache()
				.getRepository(gitPath);
		if (repository == null || repository.isBare()) {
			return null;
		}
		try {
			IPath repoRelativePath = gitPath.makeRelativeTo(
					new Path(repository.getWorkTree().getAbsolutePath()));
			IProject[] projects = ProjectUtil.getProjectsContaining(repository,
					Collections.singleton(repoRelativePath.toString()));
			if (projects.length > 0) {
				IPath projectRelativePath = gitPath
						.makeRelativeTo(projects[0].getLocation());
				return projects[0].getFile(projectRelativePath);
			}
		} catch (CoreException e) {<commit_msg>Handle edge case in GitAdapterFactory<commit_after>/*******************************************************************************
 * Copyright (C) 2007, <NAME> <<EMAIL>>
 * Copyright (C) 2007, <NAME> <<EMAIL>>
 * Copyright (C) 2010, <NAME> <<EMAIL>>
 * Copyright (C) 2011, IBM Corporation
 * Copyright (C) 2012, <NAME> <<EMAIL>>
 * Copyright (C) 2012, <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.factories;

import java.io.File;
import java.net.URI;
import java.util.Collections;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.AdapterUtils;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.internal.history.GitHistoryPage;
import org.eclipse.egit.ui.internal.history.GitHistoryPageSource;
import org.eclipse.egit.ui.internal.repository.RepositoriesViewLabelProvider;
import org.eclipse.egit.ui.internal.repository.tree.RepositoryNode;
import org.eclipse.egit.ui.internal.selection.SelectionUtils;
import org.eclipse.egit.ui.internal.synchronize.mapping.GitModelWorkbenchAdapter;
import org.eclipse.egit.ui.internal.synchronize.mapping.GitObjectMapping;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelBlob;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelObject;
import org.eclipse.egit.ui.internal.synchronize.model.GitModelTree;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.team.ui.history.IHistoryPage;
import org.eclipse.team.ui.history.IHistoryPageSource;
import org.eclipse.team.ui.history.IHistoryView;
import org.eclipse.team.ui.mapping.ISynchronizationCompareAdapter;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.model.IWorkbenchAdapter;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.eclipse.ui.part.IShowInSource;

/**
 * This class is an intelligent "cast" operation for getting
 * an instance of a suitable object from another for a specific
 * purpose.
 */
public class GitAdapterFactory implements IAdapterFactory {

	private GitModelWorkbenchAdapter gitModelWorkbenchAdapter;

	private static final IWorkspaceRoot root = ResourcesPlugin.getWorkspace()
			.getRoot();

	@Override
	public Object getAdapter(Object adaptableObject, Class adapterType) {
		if (adapterType.isAssignableFrom(IHistoryPageSource.class)) {
			return GitHistoryPageSource.INSTANCE;
		}

		if (IWorkbenchAdapter.class == adapterType) {
			if (adaptableObject instanceof RepositoryNode) {
				return getRepsitoryNodeWorkbenchAdapter((RepositoryNode)adaptableObject);
			}

			if (gitModelWorkbenchAdapter == null) {
				gitModelWorkbenchAdapter = new GitModelWorkbenchAdapter();
			}
			return gitModelWorkbenchAdapter;
		}

		if (adaptableObject instanceof IHistoryView
				&& IShowInSource.class == adapterType) {
			IHistoryView historyView = (IHistoryView) adaptableObject;
			IHistoryPage historyPage = historyView.getHistoryPage();
			if (historyPage instanceof GitHistoryPage) {
				return historyPage;
			}
		}

		if (adaptableObject instanceof IURIEditorInput
				&& adapterType == Repository.class) {
			return getRepository((IURIEditorInput) adaptableObject);
		}

		if (adaptableObject instanceof IURIEditorInput
				&& adapterType == File.class) {
			return getFile((IURIEditorInput) adaptableObject);
		}

		if (adaptableObject instanceof GitModelObject
				&& adapterType == ResourceMapping.class) {
			return GitObjectMapping.create((GitModelObject) adaptableObject);
		}

		if (adaptableObject instanceof GitModelObject
				&& adapterType == IResource.class) {
			GitModelObject obj = (GitModelObject) adaptableObject;

			if (obj instanceof GitModelBlob) {
				IResource res = ResourceUtil
						.getFileForLocation(obj.getLocation(), false);
				if (res == null) {
					// Deleted resource?
					res = getWorkspaceResourceFromGitPath(obj.getLocation());
				}

				return res;
			}

			if (obj instanceof GitModelTree) {
				IResource res = root.getContainerForLocation(obj.getLocation());
				if (res == null) {
					res = root.getFolder(obj.getLocation());
				}

				return res;
			}
		}

		if (adapterType == Repository.class) {
			ResourceMapping m = AdapterUtils.adapt(adaptableObject,
					ResourceMapping.class);
			if (m != null) {
				return SelectionUtils.getRepository(new StructuredSelection(m));
			}
		}

		return null;
	}

	@Nullable
	private IResource getWorkspaceResourceFromGitPath(IPath gitPath) {
		Repository repository = Activator.getDefault().getRepositoryCache()
				.getRepository(gitPath);
		if (repository == null || repository.isBare()) {
			return null;
		}
		try {
			IPath repoRelativePath = gitPath.makeRelativeTo(
					new Path(repository.getWorkTree().getAbsolutePath()));
			IProject[] projects = ProjectUtil.getProjectsContaining(repository,
					Collections.singleton(repoRelativePath.toString()));
			if (projects.length > 0) {
				IPath projectRelativePath = gitPath
						.makeRelativeTo(projects[0].getLocation());
				if (projectRelativePath.isEmpty()) {
					return projects[0];
				} else {
					return projects[0].getFile(projectRelativePath);
				}
			}
		} catch (CoreException e) {
{'fraction_non_alphanumeric': 0.12846265997084075, 'fraction_numerical': 0.0051028673254495385, 'mean_word_length': 5.127543424317618, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, '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': '2897083', 'n_tokens_mistral': 3997, 'n_tokens_neox': 3510, 'n_words': 711}