Search in sources :

Example 6 with FileDiff

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);
}
Also used : FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) RepositoryCommit(org.eclipse.egit.ui.internal.commit.RepositoryCommit) Test(org.junit.Test)

Example 7 with FileDiff

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);
}
Also used : FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) ResourceManager(org.eclipse.jface.resource.ResourceManager) Image(org.eclipse.swt.graphics.Image) HashMap(java.util.HashMap) JFaceResources(org.eclipse.jface.resource.JFaceResources) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) IDocument(org.eclipse.jface.text.IDocument) Composite(org.eclipse.swt.widgets.Composite) Map(java.util.Map) ITreeContentProvider(org.eclipse.jface.viewers.ITreeContentProvider) Separator(org.eclipse.jface.action.Separator) Viewer(org.eclipse.jface.viewers.Viewer) PlatformUI(org.eclipse.ui.PlatformUI) Collection(java.util.Collection) MenuManager(org.eclipse.jface.action.MenuManager) Action(org.eclipse.jface.action.Action) Collectors(java.util.stream.Collectors) File(java.io.File) ISharedImages(org.eclipse.ui.ISharedImages) List(java.util.List) UIText(org.eclipse.egit.ui.internal.UIText) Path(org.eclipse.core.runtime.Path) IOpenListener(org.eclipse.jface.viewers.IOpenListener) ISelection(org.eclipse.jface.viewers.ISelection) TreeViewer(org.eclipse.jface.viewers.TreeViewer) LocalResourceManager(org.eclipse.jface.resource.LocalResourceManager) DiffEntry(org.eclipse.jgit.diff.DiffEntry) Menu(org.eclipse.swt.widgets.Menu) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SafeRunnable(org.eclipse.jface.util.SafeRunnable) LabelProvider(org.eclipse.jface.viewers.LabelProvider) OpenEvent(org.eclipse.jface.viewers.OpenEvent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Path(org.eclipse.core.runtime.Path) Action(org.eclipse.jface.action.Action) FileDiffRegion(org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion) MenuManager(org.eclipse.jface.action.MenuManager) Menu(org.eclipse.swt.widgets.Menu) File(java.io.File) Separator(org.eclipse.jface.action.Separator)

Example 8 with FileDiff

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;
}
Also used : ArrayList(java.util.ArrayList) FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with FileDiff

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();
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Repository(org.eclipse.jgit.lib.Repository) UIJob(org.eclipse.ui.progress.UIJob) FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) Job(org.eclipse.core.runtime.jobs.Job) UIJob(org.eclipse.ui.progress.UIJob)

Example 10 with FileDiff

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;
}
Also used : FileDiff(org.eclipse.egit.ui.internal.history.FileDiff) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Aggregations

FileDiff (org.eclipse.egit.ui.internal.history.FileDiff)12 ArrayList (java.util.ArrayList)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 IOException (java.io.IOException)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 Job (org.eclipse.core.runtime.jobs.Job)3 File (java.io.File)2 DiffEntry (org.eclipse.jgit.diff.DiffEntry)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Collectors (java.util.stream.Collectors)1 IStatus (org.eclipse.core.runtime.IStatus)1 Path (org.eclipse.core.runtime.Path)1