/*******************************************************************************
 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
 *
 * 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.actions;

import java.io.IOException;

import org.eclipse.compare.CompareUI;
import org.eclipse.compare.ITypedElement;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.egit.core.internal.storage.GitFileRevision;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.FileRevisionTypedElement;
import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;
import org.eclipse.egit.ui.internal.LocalFileRevision;
import org.eclipse.egit.ui.internal.dialogs.CompareTargetSelectionDialog;
import org.eclipse.egit.ui.internal.dialogs.CompareTreeView;
import org.eclipse.jface.window.Window;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * The "compare with ref" action. This action opens a diff editor comparing the
 * file as found in the working directory and the version in the selected ref.
 */
public class CompareWithRefActionHandler extends RepositoryActionHandler {
	public Object execute(ExecutionEvent event) throws ExecutionException {
		final Repository repo = getRepository(true, event);
		// assert all resources map to the same repository
		if (repo == null)
			return null;
		final IResource[] resources = getSelectedResources(event);

		CompareTargetSelectionDialog dlg = new CompareTargetSelectionDialog(
				getShell(event), repo, resources.length == 1 ? resources[0]
						.getFullPath().toString() : null);
		if (dlg.open() == Window.OK) {

			if (resources.length == 1 && resources[0] instanceof IFile) {
				final IFile baseFile = (IFile) resources[0];

				final ITypedElement base = new FileRevisionTypedElement(
						new LocalFileRevision(baseFile));

				final ITypedElement next;
				try {
					RepositoryMapping mapping = RepositoryMapping
							.getMapping(resources[0]);
					next = getElementForRef(mapping.getRepository(), mapping
							.getRepoRelativePath(baseFile), dlg.getRefName());
				} catch (IOException e) {
					Activator.handleError(
							UIText.CompareWithIndexAction_errorOnAddToIndex, e,
							true);
					return null;
				}

				final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
						base, next, null);
				in.getCompareConfiguration().setRightLabel(dlg.getRefName());
				CompareUI.openCompareEditor(in);
			} else {
				CompareTreeView view;
				try {
					view = (CompareTreeView) PlatformUI.getWorkbench()
							.getActiveWorkbenchWindow().getActivePage()
							.showView(CompareTreeView.ID);
					view.setInput(resources, dlg.getRefName());
				} catch (PartInitException e) {
					Activator.handleError(e.getMessage(), e, true);
				}
			}
		}
		return null;
	}

	private ITypedElement getElementForRef(final Repository repository,
			final String gitPath, final String refName) throws IOException {
		ObjectId commitId = repository.resolve(refName + "^{commit}"); //$NON-NLS-1$
		RevWalk rw = new RevWalk(repository);
		RevCommit commit = rw.parseCommit(commitId);
		rw.release();

		IFileRevision nextFile = GitFileRevision.inCommit(repository, commit,
				gitPath, null);

		FileRevisionTypedElement element = new FileRevisionTypedElement(
				nextFile);
		return element;
	}

	@Override
	public boolean isEnabled() {
		return getRepository() != null;
	}
}

@@ -819,71 +819,8 @@
 ce;%0A
-import org.eclipse.egit.core.internal.storage.GitFileRevision;%0A
 impo
@@ -944,16 +944,66 @@
 UIText;%0A
+import org.eclipse.egit.ui.internal.CompareUtils;%0A
 import o
@@ -1523,60 +1523,8 @@
 lk;%0A
-import org.eclipse.team.core.history.IFileRevision;%0A
 impo
@@ -3850,197 +3850,84 @@
 %0A%0A%09%09
-IFileRevision nextFile = GitFileRevision.inCommit(repository, commit,%0A%09%09%09%09gitPath, null);%0A%0A%09%09FileRevisionTypedElement element = new FileRevisionTypedElement(%0A%09%09%09%09nextFile);%0A%09%09return element
+return CompareUtils.getFileRevisionTypedElement(gitPath, commit, repository)
 ;%0A%09%7D

Fix encoding of ref element