use of org.eclipse.egit.ui.internal.history.FileDiff in project egit by eclipse.
the class RepositoryCommitTest method testDiffs.
@Test
public void testDiffs() throws Exception {
RepositoryCommit repoCommit = new RepositoryCommit(repository, commit);
FileDiff[] diffs = repoCommit.getDiffs();
assertNotNull(diffs);
assertTrue(diffs.length > 0);
for (FileDiff diff : diffs) assertNotNull(diff);
}
use of org.eclipse.egit.ui.internal.history.FileDiff in project egit by eclipse.
the class DiffEditorOutlinePage method createContextMenu.
private void createContextMenu(TreeViewer viewer) {
MenuManager contextMenu = new MenuManager();
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(menuManager -> {
setFocus();
Collection<FileDiffRegion> selected = getSelectedFileDiffs();
if (selected.isEmpty()) {
return;
}
Collection<FileDiffRegion> haveNew = selected.stream().filter(diff -> !diff.getDiff().getChange().equals(DiffEntry.ChangeType.DELETE)).collect(Collectors.toList());
Collection<FileDiffRegion> haveOld = selected.stream().filter(diff -> !diff.getDiff().getChange().equals(DiffEntry.ChangeType.ADD)).collect(Collectors.toList());
Collection<FileDiffRegion> existing = haveNew.stream().filter(diff -> new Path(diff.getRepository().getWorkTree().getAbsolutePath()).append(diff.getDiff().getNewPath()).toFile().exists()).collect(Collectors.toList());
if (!existing.isEmpty()) {
menuManager.add(new Action(UIText.CommitFileDiffViewer_OpenWorkingTreeVersionInEditorMenuLabel) {
@Override
public void run() {
for (FileDiffRegion fileDiff : existing) {
File file = new Path(fileDiff.getRepository().getWorkTree().getAbsolutePath()).append(fileDiff.getDiff().getNewPath()).toFile();
DiffViewer.openFileInEditor(file, -1);
}
}
});
}
if (!haveNew.isEmpty()) {
menuManager.add(new Action(UIText.CommitFileDiffViewer_OpenInEditorMenuLabel) {
@Override
public void run() {
for (FileDiffRegion fileDiff : haveNew) {
DiffViewer.openInEditor(fileDiff.getRepository(), fileDiff.getDiff(), DiffEntry.Side.NEW, -1);
}
}
});
}
if (!haveOld.isEmpty()) {
menuManager.add(new Action(UIText.CommitFileDiffViewer_OpenPreviousInEditorMenuLabel) {
@Override
public void run() {
for (FileDiffRegion fileDiff : haveOld) {
DiffViewer.openInEditor(fileDiff.getRepository(), fileDiff.getDiff(), DiffEntry.Side.OLD, -1);
}
}
});
}
if (selected.size() == 1) {
menuManager.add(new Separator());
menuManager.add(new Action(UIText.CommitFileDiffViewer_CompareMenuLabel) {
@Override
public void run() {
FileDiffRegion fileDiff = selected.iterator().next();
DiffViewer.showTwoWayFileDiff(fileDiff.getRepository(), fileDiff.getDiff());
}
});
}
});
Menu menu = contextMenu.createContextMenu(viewer.getTree());
viewer.getTree().setMenu(menu);
}
use of org.eclipse.egit.ui.internal.history.FileDiff in project egit by eclipse.
the class DiffEditorPage method getDiffs.
/**
* Gets the full unified diff of a {@link RepositoryCommit}.
*
* @param commit
* to get the diff
* @return the diff as a sorted (by file path) array of {@link FileDiff}s
*/
protected FileDiff[] getDiffs(RepositoryCommit commit) {
List<FileDiff> diffResult = new ArrayList<>();
diffResult.addAll(asList(commit.getDiffs()));
if (commit.getRevCommit().getParentCount() > 2) {
RevCommit untrackedCommit = commit.getRevCommit().getParent(StashEditorPage.PARENT_COMMIT_UNTRACKED);
diffResult.addAll(asList(new RepositoryCommit(commit.getRepository(), untrackedCommit).getDiffs()));
}
FileDiff[] result = diffResult.toArray(new FileDiff[diffResult.size()]);
Arrays.sort(result, FileDiff.PATH_COMPARATOR);
return result;
}
use of org.eclipse.egit.ui.internal.history.FileDiff in project egit by eclipse.
the class DiffEditorPage method formatDiff.
/**
* Asynchronously gets the diff of the commit set on our
* {@link CommitEditorInput}, formats it into a {@link DiffDocument}, and
* then re-sets this editors's input to a {@link DiffEditorInput} which will
* cause this document to be shown.
*/
private void formatDiff() {
RepositoryCommit commit = AdapterUtils.adapt(getEditor(), RepositoryCommit.class);
if (commit == null) {
return;
}
if (!commit.isStash() && commit.getRevCommit().getParentCount() > 1) {
setInput(new DiffEditorInput(commit, null));
return;
}
Job job = new Job(UIText.DiffEditorPage_TaskGeneratingDiff) {
@Override
protected IStatus run(IProgressMonitor monitor) {
FileDiff[] diffs = getDiffs(commit);
DiffDocument document = new DiffDocument();
try (DiffRegionFormatter formatter = new DiffRegionFormatter(document)) {
SubMonitor progress = SubMonitor.convert(monitor, diffs.length);
Repository repository = commit.getRepository();
for (FileDiff diff : diffs) {
if (progress.isCanceled()) {
break;
}
progress.subTask(diff.getPath());
try {
formatter.write(repository, diff);
} catch (IOException ignore) {
// Ignored
}
progress.worked(1);
}
document.connect(formatter);
}
new UIJob(UIText.DiffEditorPage_TaskUpdatingViewer) {
@Override
public IStatus runInUIThread(IProgressMonitor uiMonitor) {
if (UIUtils.isUsable(getPartControl())) {
setInput(new DiffEditorInput(commit, document));
}
return Status.OK_STATUS;
}
}.schedule();
return Status.OK_STATUS;
}
};
job.schedule();
}
use of org.eclipse.egit.ui.internal.history.FileDiff in project egit by eclipse.
the class RepositoryCommit method getDiffs.
/**
* Gets the changes between this commit and specific parent commits
*
* @param parents
* parents to which the current commit is compared
*
* @return non-null but possibly empty array of {@link FileDiff} instances.
*/
public FileDiff[] getDiffs(RevCommit... parents) {
FileDiff[] diffsResult = null;
try (RevWalk revWalk = new RevWalk(repository);
TreeWalk treewalk = new TreeWalk(revWalk.getObjectReader())) {
treewalk.setRecursive(true);
treewalk.setFilter(TreeFilter.ANY_DIFF);
loadParents();
diffsResult = FileDiff.compute(repository, treewalk, commit, parents, TreeFilter.ALL);
} catch (IOException e) {
diffsResult = new FileDiff[0];
}
return diffsResult;
}
Aggregations