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
;
13 import java
.io
.IOException
;
15 import org
.eclipse
.compare
.CompareUI
;
16 import org
.eclipse
.compare
.IContentChangeListener
;
17 import org
.eclipse
.compare
.IContentChangeNotifier
;
18 import org
.eclipse
.compare
.ITypedElement
;
19 import org
.eclipse
.core
.resources
.IFile
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.egit
.core
.internal
.storage
.GitFileRevision
;
22 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
23 import org
.eclipse
.egit
.ui
.internal
.EditableRevision
;
24 import org
.eclipse
.egit
.ui
.internal
.GitCompareFileRevisionEditorInput
;
25 import org
.eclipse
.jface
.action
.IAction
;
26 import org
.eclipse
.team
.core
.history
.IFileRevision
;
27 import org
.eclipse
.team
.internal
.ui
.Utils
;
28 import org
.eclipse
.team
.ui
.synchronize
.SaveableCompareEditorInput
;
29 import org
.spearce
.jgit
.lib
.GitIndex
;
30 import org
.spearce
.jgit
.lib
.Repository
;
33 * The "compare with index" action. This action opens a diff editor comparing
34 * the file as found in the working directory and the version found in the index
37 @SuppressWarnings("restriction")
38 public class CompareWithIndexAction
extends RepositoryAction
{
41 public void execute(IAction action
) {
42 final IResource resource
= getSelectedResources()[0];
43 final RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
.getProject());
44 final Repository repository
= mapping
.getRepository();
45 final String gitPath
= mapping
.getRepoRelativePath(resource
);
47 final IFileRevision nextFile
= GitFileRevision
.inIndex(repository
, gitPath
);
49 final IFile baseFile
= (IFile
) resource
;
50 final ITypedElement base
= SaveableCompareEditorInput
.createFileElement(baseFile
);
52 final EditableRevision next
= new EditableRevision(nextFile
);
54 IContentChangeListener listener
= new IContentChangeListener() {
55 public void contentChanged(IContentChangeNotifier source
) {
56 final byte[] newContent
= next
.getModifiedContent();
58 final GitIndex index
= repository
.getIndex();
59 final File file
= new File(baseFile
.getLocation().toString());
60 index
.add(mapping
.getWorkDir(), file
, newContent
);
62 } catch (IOException e
) {
63 Utils
.handleError(getTargetPart().getSite().getShell(), e
,
64 "Error during adding to index",
65 "Error during adding to index");
71 next
.addContentChangeListener(listener
);
73 final GitCompareFileRevisionEditorInput in
= new GitCompareFileRevisionEditorInput(
75 CompareUI
.openCompareEditor(in
);
79 public boolean isEnabled() {
80 final IResource
[] selectedResources
= getSelectedResources();
81 if (selectedResources
.length
!= 1)
84 final IResource resource
= selectedResources
[0];
85 if (!(resource
instanceof IFile
)) {
88 final RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
.getProject());
89 return mapping
!= null;