Search in sources :

Example 6 with GitCompareFileRevisionEditorInput

use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.

the class CompareUtils method compareBetween.

/**
 * Compares two explicit files specified by leftGitPath and rightGitPath
 * between the two revisions leftRev and rightRev.
 *
 * @param repository
 *            The repository to load file revisions from.
 * @param leftGitPath
 *            The repository relative path to be used for the left revision.
 * @param rightGitPath
 *            The repository relative path to be used for the right
 *            revision.
 * @param leftRev
 *            Left revision of the comparison (usually the local or "new"
 *            revision). Won't be used if <code>includeLocal</code> is
 *            <code>true</code>.
 * @param rightRev
 *            Right revision of the comparison (usually the "old" revision).
 * @param page
 *            If not {@null} try to re-use a compare editor on this
 *            page if any is available. Otherwise open a new one.
 */
public static void compareBetween(final Repository repository, final String leftGitPath, final String rightGitPath, final String leftRev, final String rightRev, final IWorkbenchPage page) {
    Job job = new Job(UIText.CompareUtils_jobName) {

        @Override
        public IStatus run(IProgressMonitor monitor) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            final ITypedElement left;
            final ITypedElement right;
            try {
                left = getTypedElementFor(repository, leftGitPath, leftRev);
                right = getTypedElementFor(repository, rightGitPath, rightRev);
            } catch (IOException e) {
                return Activator.createErrorStatus(UIText.CompareWithRefAction_errorOnSynchronize, e);
            }
            final ITypedElement commonAncestor;
            if (left != null && right != null && !GitFileRevision.INDEX.equals(leftRev) && !GitFileRevision.INDEX.equals(rightRev)) {
                commonAncestor = getTypedElementForCommonAncestor(repository, rightGitPath, leftRev, rightRev);
            } else {
                commonAncestor = null;
            }
            final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(left, right, commonAncestor, null);
            in.getCompareConfiguration().setLeftLabel(leftRev);
            in.getCompareConfiguration().setRightLabel(rightRev);
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            openCompareEditorRunnable(page, in);
            return Status.OK_STATUS;
        }
    };
    job.setUser(true);
    job.schedule();
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) IOException(java.io.IOException) Job(org.eclipse.core.runtime.jobs.Job)

Example 7 with GitCompareFileRevisionEditorInput

use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.

the class CompareUtils method openInCompare.

/**
 * Compares two files between the given commits, taking possible renames
 * into account.
 *
 * @param commit1
 *            the "left" commit for the comparison editor
 * @param commit2
 *            the "right" commit for the comparison editor
 * @param commit1Path
 *            path to the file within commit1's tree
 * @param commit2Path
 *            path to the file within commit2's tree
 * @param repository
 *            the repository this commit was loaded out of
 * @param workBenchPage
 *            the page to open the compare editor in
 */
public static void openInCompare(RevCommit commit1, RevCommit commit2, String commit1Path, String commit2Path, Repository repository, IWorkbenchPage workBenchPage) {
    final ITypedElement base = CompareUtils.getFileRevisionTypedElement(commit1Path, commit1, repository);
    final ITypedElement next = CompareUtils.getFileRevisionTypedElement(commit2Path, commit2, repository);
    CompareEditorInput in = new GitCompareFileRevisionEditorInput(base, next, null);
    CompareUtils.openInCompare(workBenchPage, in);
}
Also used : CompareEditorInput(org.eclipse.compare.CompareEditorInput) GitCompareEditorInput(org.eclipse.egit.ui.internal.merge.GitCompareEditorInput) SaveableCompareEditorInput(org.eclipse.team.ui.synchronize.SaveableCompareEditorInput) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement)

Example 8 with GitCompareFileRevisionEditorInput

use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.

the class CompareUtils method compareHeadWithWorkingTree.

/**
 * Opens a compare editor. The working tree version of the given file is
 * compared with the version in the HEAD commit. Use this method if the
 * given file is outide the workspace.
 *
 * @param repository
 * @param path
 */
public static void compareHeadWithWorkingTree(Repository repository, String path) {
    ITypedElement base = getHeadTypedElement(repository, path);
    if (base == null)
        return;
    IFileRevision nextFile;
    nextFile = new WorkingTreeFileRevision(new File(repository.getWorkTree(), path));
    String encoding = ResourcesPlugin.getEncoding();
    ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
    GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
    CompareUI.openCompareDialog(input);
}
Also used : WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 9 with GitCompareFileRevisionEditorInput

use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.

the class GitOpenInCompareAction method handleGitObjectComparison.

private void handleGitObjectComparison(GitModelBlob obj, boolean reuseEditor) {
    ITypedElement left;
    ITypedElement right;
    if (obj instanceof GitModelWorkingFile) {
        IPath location = obj.getLocation();
        if (location == null) {
            return;
        }
        IFile file = ResourceUtil.getFileForLocation(location, false);
        if (file == null) {
            Repository repository = null;
            GitModelObject modelObject = obj;
            while (modelObject != null) {
                if (modelObject instanceof GitModelRepository) {
                    repository = ((GitModelRepository) modelObject).getRepository();
                    break;
                }
                modelObject = modelObject.getParent();
            }
            if (repository == null) {
                return;
            }
            left = new LocalNonWorkspaceTypedElement(repository, location);
        } else {
            left = SaveableCompareEditorInput.createFileElement(file);
        }
        right = getCachedFileElement(obj);
    } else if (obj instanceof GitModelCacheFile) {
        left = getCachedFileElement(obj);
        right = getHeadFileElement(obj);
        if (right == null)
            return;
    } else {
        oldAction.run();
        return;
    }
    GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(left, right, null);
    IWorkbenchPage page = getWorkbenchPage(conf.getSite());
    OpenInCompareAction.openCompareEditor(in, page, reuseEditor);
}
Also used : GitModelRepository(org.eclipse.egit.ui.internal.synchronize.model.GitModelRepository) Repository(org.eclipse.jgit.lib.Repository) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) GitModelWorkingFile(org.eclipse.egit.ui.internal.synchronize.model.GitModelWorkingFile) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) LocalNonWorkspaceTypedElement(org.eclipse.egit.ui.internal.synchronize.compare.LocalNonWorkspaceTypedElement) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) GitModelRepository(org.eclipse.egit.ui.internal.synchronize.model.GitModelRepository) GitModelCacheFile(org.eclipse.egit.ui.internal.synchronize.model.GitModelCacheFile) GitModelObject(org.eclipse.egit.ui.internal.synchronize.model.GitModelObject)

Example 10 with GitCompareFileRevisionEditorInput

use of org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput in project egit by eclipse.

the class DiffViewer method showTwoWayFileDiff.

/**
 * Shows a two-way diff between the old and new versions of a
 * {@link FileDiff} in a compare editor.
 *
 * @param repository
 *            the {@link FileDiff} belongs to
 * @param d
 *            the {@link FileDiff} to show
 */
public static void showTwoWayFileDiff(Repository repository, FileDiff d) {
    String np = d.getNewPath();
    String op = d.getOldPath();
    RevCommit c = d.getCommit();
    ObjectId[] blobs = d.getBlobs();
    // extract commits
    final RevCommit oldCommit;
    final ObjectId oldObjectId;
    if (!d.getChange().equals(ChangeType.ADD)) {
        oldCommit = c.getParent(0);
        oldObjectId = blobs[0];
    } else {
        // Initial import
        oldCommit = null;
        oldObjectId = null;
    }
    final RevCommit newCommit;
    final ObjectId newObjectId;
    if (d.getChange().equals(ChangeType.DELETE)) {
        newCommit = null;
        newObjectId = null;
    } else {
        newCommit = c;
        newObjectId = blobs[blobs.length - 1];
    }
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    IWorkbenchPage page = window.getActivePage();
    if (oldCommit != null && newCommit != null && repository != null) {
        IFile file = np != null ? ResourceUtil.getFileForLocation(repository, np, false) : null;
        try {
            if (file != null) {
                CompareUtils.compare(file, repository, np, op, newCommit.getName(), oldCommit.getName(), false, page);
            } else {
                IPath location = new Path(repository.getWorkTree().getAbsolutePath()).append(np);
                CompareUtils.compare(location, repository, newCommit.getName(), oldCommit.getName(), false, page);
            }
        } catch (IOException e) {
            Activator.handleError(UIText.GitHistoryPage_openFailed, e, true);
        }
        return;
    }
    // still happens on initial commits
    final ITypedElement oldSide = createTypedElement(repository, op, oldCommit, oldObjectId);
    final ITypedElement newSide = createTypedElement(repository, np, newCommit, newObjectId);
    CompareUtils.openInCompare(page, new GitCompareFileRevisionEditorInput(newSide, oldSide, null));
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) ObjectId(org.eclipse.jgit.lib.ObjectId) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ITypedElement(org.eclipse.compare.ITypedElement) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ITypedElement (org.eclipse.compare.ITypedElement)10 GitCompareFileRevisionEditorInput (org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput)10 IFile (org.eclipse.core.resources.IFile)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)4 File (java.io.File)3 IOException (java.io.IOException)3 RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 Repository (org.eclipse.jgit.lib.Repository)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 IFileRevision (org.eclipse.team.core.history.IFileRevision)3 ExecutionException (org.eclipse.core.commands.ExecutionException)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2 FileRevisionTypedElement (org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)2 ArrayList (java.util.ArrayList)1 CompareEditorInput (org.eclipse.compare.CompareEditorInput)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1