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
.UIText
;
24 import org
.eclipse
.egit
.ui
.internal
.EditableRevision
;
25 import org
.eclipse
.egit
.ui
.internal
.GitCompareFileRevisionEditorInput
;
26 import org
.eclipse
.jface
.action
.IAction
;
27 import org
.eclipse
.team
.core
.history
.IFileRevision
;
28 import org
.eclipse
.team
.internal
.ui
.Utils
;
29 import org
.eclipse
.team
.ui
.synchronize
.SaveableCompareEditorInput
;
30 import org
.eclipse
.jgit
.lib
.GitIndex
;
31 import org
.eclipse
.jgit
.lib
.Repository
;
34 * The "compare with index" action. This action opens a diff editor comparing
35 * the file as found in the working directory and the version found in the index
38 @SuppressWarnings("restriction")
39 public class CompareWithIndexAction
extends RepositoryAction
{
42 public void execute(IAction action
) {
43 final IResource resource
= getSelectedResources()[0];
44 final RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
.getProject());
45 final Repository repository
= mapping
.getRepository();
46 final String gitPath
= mapping
.getRepoRelativePath(resource
);
48 final IFileRevision nextFile
= GitFileRevision
.inIndex(repository
, gitPath
);
50 final IFile baseFile
= (IFile
) resource
;
51 final ITypedElement base
= SaveableCompareEditorInput
.createFileElement(baseFile
);
53 final EditableRevision next
= new EditableRevision(nextFile
);
55 IContentChangeListener listener
= new IContentChangeListener() {
56 public void contentChanged(IContentChangeNotifier source
) {
57 final byte[] newContent
= next
.getModifiedContent();
59 final GitIndex index
= repository
.getIndex();
60 final File file
= new File(baseFile
.getLocation().toString());
61 index
.add(mapping
.getWorkDir(), file
, newContent
);
63 } catch (IOException e
) {
64 Utils
.handleError(getTargetPart().getSite().getShell(), e
,
65 UIText
.CompareWithIndexAction_errorOnAddToIndex
,
66 UIText
.CompareWithIndexAction_errorOnAddToIndex
);
72 next
.addContentChangeListener(listener
);
74 final GitCompareFileRevisionEditorInput in
= new GitCompareFileRevisionEditorInput(
76 CompareUI
.openCompareEditor(in
);
80 public boolean isEnabled() {
81 final IResource
[] selectedResources
= getSelectedResources();
82 if (selectedResources
.length
!= 1)
85 final IResource resource
= selectedResources
[0];
86 if (!(resource
instanceof IFile
)) {
89 final RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
.getProject());
90 return mapping
!= null;