2 * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.eclipse
.egit
.ui
.internal
.actions
;
41 import java
.io
.IOException
;
43 import org
.eclipse
.compare
.CompareUI
;
44 import org
.eclipse
.compare
.IContentChangeListener
;
45 import org
.eclipse
.compare
.IContentChangeNotifier
;
46 import org
.eclipse
.compare
.ITypedElement
;
47 import org
.eclipse
.core
.resources
.IFile
;
48 import org
.eclipse
.core
.resources
.IResource
;
49 import org
.eclipse
.egit
.core
.internal
.storage
.GitFileRevision
;
50 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
51 import org
.eclipse
.egit
.ui
.internal
.EditableRevision
;
52 import org
.eclipse
.egit
.ui
.internal
.GitCompareFileRevisionEditorInput
;
53 import org
.eclipse
.jface
.action
.IAction
;
54 import org
.eclipse
.team
.core
.history
.IFileRevision
;
55 import org
.eclipse
.team
.internal
.ui
.Utils
;
56 import org
.eclipse
.team
.ui
.synchronize
.SaveableCompareEditorInput
;
57 import org
.spearce
.jgit
.lib
.GitIndex
;
58 import org
.spearce
.jgit
.lib
.Repository
;
61 * The "compare with index" action. This action opens a diff editor comparing
62 * the file as found in the working directory and the version found in the index
65 @SuppressWarnings("restriction")
66 public class CompareWithIndexAction
extends RepositoryAction
{
69 public void execute(IAction action
) {
70 final IResource resource
= getSelectedResources()[0];
71 final RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
.getProject());
72 final Repository repository
= mapping
.getRepository();
73 final String gitPath
= mapping
.getRepoRelativePath(resource
);
75 final IFileRevision nextFile
= GitFileRevision
.inIndex(repository
, gitPath
);
77 final IFile baseFile
= (IFile
) resource
;
78 final ITypedElement base
= SaveableCompareEditorInput
.createFileElement(baseFile
);
80 final EditableRevision next
= new EditableRevision(nextFile
);
82 IContentChangeListener listener
= new IContentChangeListener() {
83 public void contentChanged(IContentChangeNotifier source
) {
84 final byte[] newContent
= next
.getModifiedContent();
86 final GitIndex index
= repository
.getIndex();
87 final File file
= new File(baseFile
.getLocation().toString());
88 index
.add(mapping
.getWorkDir(), file
, newContent
);
90 } catch (IOException e
) {
91 Utils
.handleError(getTargetPart().getSite().getShell(), e
,
92 "Error during adding to index",
93 "Error during adding to index");
99 next
.addContentChangeListener(listener
);
101 final GitCompareFileRevisionEditorInput in
= new GitCompareFileRevisionEditorInput(
103 CompareUI
.openCompareEditor(in
);
107 public boolean isEnabled() {
108 final IResource
[] selectedResources
= getSelectedResources();
109 if (selectedResources
.length
!= 1)
112 final IResource resource
= selectedResources
[0];
113 if (!(resource
instanceof IFile
)) {
116 final RepositoryMapping mapping
= RepositoryMapping
.getMapping(resource
.getProject());
117 return mapping
!= null;