blob: 8956463da0cc6905e4d1bdb7f225c48ae9fafff7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
/*******************************************************************************
* Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.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.File;
import java.io.IOException;
import org.eclipse.compare.CompareUI;
import org.eclipse.compare.IContentChangeListener;
import org.eclipse.compare.IContentChangeNotifier;
import org.eclipse.compare.ITypedElement;
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.internal.EditableRevision;
import org.eclipse.egit.ui.internal.GitCompareFileRevisionEditorInput;
import org.eclipse.jface.action.IAction;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.ui.synchronize.SaveableCompareEditorInput;
import org.eclipse.jgit.lib.GitIndex;
import org.eclipse.jgit.lib.Repository;
/**
* The "compare with index" action. This action opens a diff editor comparing
* the file as found in the working directory and the version found in the index
* of the repository.
*/
@SuppressWarnings("restriction")
public class CompareWithIndexAction extends RepositoryAction {
@Override
public void execute(IAction action) {
final IResource resource = getSelectedResources()[0];
final RepositoryMapping mapping = RepositoryMapping.getMapping(resource.getProject());
final Repository repository = mapping.getRepository();
final String gitPath = mapping.getRepoRelativePath(resource);
final IFileRevision nextFile = GitFileRevision.inIndex(repository, gitPath);
final IFile baseFile = (IFile) resource;
final ITypedElement base = SaveableCompareEditorInput.createFileElement(baseFile);
final EditableRevision next = new EditableRevision(nextFile);
IContentChangeListener listener = new IContentChangeListener() {
public void contentChanged(IContentChangeNotifier source) {
final byte[] newContent = next.getModifiedContent();
try {
final GitIndex index = repository.getIndex();
final File file = new File(baseFile.getLocation().toString());
index.add(mapping.getWorkDir(), file, newContent);
index.write();
} catch (IOException e) {
Utils.handleError(getTargetPart().getSite().getShell(), e,
"Error during adding to index",
"Error during adding to index");
return;
}
}
};
next.addContentChangeListener(listener);
final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(
base, next, null);
CompareUI.openCompareEditor(in);
}
@Override
public boolean isEnabled() {
final IResource[] selectedResources = getSelectedResources();
if (selectedResources.length != 1)
return false;
final IResource resource = selectedResources[0];
if (!(resource instanceof IFile)) {
return false;
}
final RepositoryMapping mapping = RepositoryMapping.getMapping(resource.getProject());
return mapping != null;
}
}
|