Git Repositories View: Simple fetch and push
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / actions / CompareWithIndexAction.java
blobaf40616127602c765836f7554527fb3a36243a0b
1 /*******************************************************************************
2 * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org.eclipse.egit.ui.internal.actions;
12 import java.io.File;
13 import java.io.IOException;
14 import java.lang.reflect.InvocationTargetException;
16 import org.eclipse.compare.CompareUI;
17 import org.eclipse.compare.IContentChangeListener;
18 import org.eclipse.compare.IContentChangeNotifier;
19 import org.eclipse.compare.ITypedElement;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.egit.core.internal.storage.GitFileRevision;
23 import org.eclipse.egit.core.project.RepositoryMapping;
24 import org.eclipse.egit.ui.UIText;
25 import org.eclipse.egit.ui.internal.EditableRevision;
26 import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jgit.lib.GitIndex;
29 import org.eclipse.jgit.lib.Repository;
30 import org.eclipse.osgi.util.NLS;
31 import org.eclipse.team.core.TeamException;
32 import org.eclipse.team.core.history.IFileRevision;
33 import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
35 /**
36 * The "compare with index" action. This action opens a diff editor comparing
37 * the file as found in the working directory and the version found in the index
38 * of the repository.
40 @SuppressWarnings("restriction")
41 public class CompareWithIndexAction extends RepositoryAction {
43 @Override
44 public void execute(IAction action) throws InvocationTargetException {
45 final IResource resource = getSelectedResources()[0];
47 final IFile baseFile = (IFile) resource;
48 final ITypedElement base = SaveableCompareEditorInput
49 .createFileElement(baseFile);
51 final ITypedElement next = getHeadTypedElement(baseFile);
53 final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
54 base, next, null);
55 CompareUI.openCompareEditor(in);
58 private ITypedElement getHeadTypedElement(final IFile baseFile)
59 throws InvocationTargetException {
60 final RepositoryMapping mapping = RepositoryMapping.getMapping(baseFile
61 .getProject());
62 final Repository repository = mapping.getRepository();
63 String gitPath = mapping.getRepoRelativePath(baseFile);
65 try {
66 GitIndex index = repository.getIndex();
67 if (index.getEntry(gitPath) == null) {
68 // the file cannot be found in the index
69 return new GitCompareFileRevisionEditorInput.EmptyTypedElement(
70 NLS.bind(UIText.CompareWithIndexAction_FileNotInIndex,
71 baseFile.getName()));
73 } catch (IOException e) {
74 // this exception is handled by TeamAction.run
75 throw new InvocationTargetException(e);
78 IFileRevision nextFile = GitFileRevision.inIndex(repository, gitPath);
79 final EditableRevision next = new EditableRevision(nextFile);
81 IContentChangeListener listener = new IContentChangeListener() {
82 public void contentChanged(IContentChangeNotifier source) {
83 final byte[] newContent = next.getModifiedContent();
84 try {
85 final GitIndex index = repository.getIndex();
86 final File file = new File(baseFile.getLocation().toString());
87 index.add(mapping.getWorkDir(), file, newContent);
88 index.write();
89 } catch (IOException e) {
90 handle(
91 new TeamException(
92 UIText.CompareWithIndexAction_errorOnAddToIndex,
93 e),
94 UIText.CompareWithIndexAction_errorOnAddToIndex,
95 UIText.CompareWithIndexAction_errorOnAddToIndex);
96 return;
101 next.addContentChangeListener(listener);
102 return next;
105 @Override
106 public boolean isEnabled() {
107 final IResource[] selectedResources = getSelectedResources();
108 if (selectedResources.length != 1)
109 return false;
111 final IResource resource = selectedResources[0];
112 if (!(resource instanceof IFile)) {
113 return false;
115 final RepositoryMapping mapping = RepositoryMapping.getMapping(resource.getProject());
116 return mapping != null;