Search in sources :

Example 26 with ITypedElement

use of org.eclipse.compare.ITypedElement in project egit by eclipse.

the class CompareUtils method compareWorkspaceWithRef.

/**
 * Opens a compare editor comparing the working directory version of the
 * given file or link with the version of that file corresponding to
 * {@code refName}.
 *
 * @param repository
 *            The repository to load file revisions from.
 * @param file
 *            Resource to compare revisions for. Must be either
 *            {@link IFile} or a symbolic link to directory ({@link IFolder}).
 * @param refName
 *            Reference to compare with the workspace version of
 *            {@code file}. Can be either a commit ID, a reference or a
 *            branch name.
 * @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 compareWorkspaceWithRef(@NonNull final Repository repository, final IResource file, final String refName, final IWorkbenchPage page) {
    if (file == null) {
        return;
    }
    final IPath location = file.getLocation();
    if (location == null) {
        return;
    }
    Job job = new Job(UIText.CompareUtils_jobName) {

        @Override
        public IStatus run(IProgressMonitor monitor) {
            if (monitor.isCanceled()) {
                return Status.CANCEL_STATUS;
            }
            final RepositoryMapping mapping = RepositoryMapping.getMapping(file);
            if (mapping == null) {
                return Activator.createErrorStatus(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, location, repository));
            }
            final ITypedElement base;
            if (Files.isSymbolicLink(location.toFile().toPath())) {
                base = new LocalNonWorkspaceTypedElement(repository, location);
            } else if (file instanceof IFile) {
                base = SaveableCompareEditorInput.createFileElement((IFile) file);
            } else {
                return Activator.createErrorStatus(NLS.bind(UIText.CompareUtils_wrongResourceArgument, location, file));
            }
            final String gitPath = mapping.getRepoRelativePath(file);
            CompareEditorInput in;
            try {
                in = prepareCompareInput(repository, gitPath, base, refName);
            } catch (IOException e) {
                return Activator.createErrorStatus(UIText.CompareWithRefAction_errorOnSynchronize, e);
            }
            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) CompareEditorInput(org.eclipse.compare.CompareEditorInput) GitCompareEditorInput(org.eclipse.egit.ui.internal.merge.GitCompareEditorInput) SaveableCompareEditorInput(org.eclipse.team.ui.synchronize.SaveableCompareEditorInput) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) ITypedElement(org.eclipse.compare.ITypedElement) LocalNonWorkspaceTypedElement(org.eclipse.egit.ui.internal.synchronize.compare.LocalNonWorkspaceTypedElement) IOException(java.io.IOException) Job(org.eclipse.core.runtime.jobs.Job)

Example 27 with ITypedElement

use of org.eclipse.compare.ITypedElement 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 28 with ITypedElement

use of org.eclipse.compare.ITypedElement 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 29 with ITypedElement

use of org.eclipse.compare.ITypedElement in project egit by eclipse.

the class CompareUtils method getFileRevisionTypedElementForCommonAncestor.

/**
 * Creates a {@link ITypedElement} for the commit which is the common
 * ancestor of the provided commits. Returns null if no such commit exists
 * or if {@code gitPath} is not contained in the common ancestor or if the
 * common ancestor is equal to one of the given commits
 *
 * @param gitPath
 *            path within the ancestor commit's tree of the file.
 * @param commit1
 * @param commit2
 * @param db
 *            the repository this commit was loaded out of.
 * @return an instance of {@link ITypedElement} which can be used in
 *         {@link CompareEditorInput}
 */
public static ITypedElement getFileRevisionTypedElementForCommonAncestor(final String gitPath, ObjectId commit1, ObjectId commit2, Repository db) {
    ITypedElement ancestor = null;
    RevCommit commonAncestor = null;
    try {
        commonAncestor = RevUtils.getCommonAncestor(db, commit1, commit2);
    } catch (IOException e) {
        Activator.logError(NLS.bind(UIText.CompareUtils_errorCommonAncestor, commit1.getName(), commit2.getName()), e);
    }
    if (commonAncestor != null) {
        if (commit1.equals(commonAncestor.getId()) || commit2.equals(commonAncestor.getId())) {
            // of given commits, see bug 512395
            return null;
        }
        ITypedElement ancestorCandidate = CompareUtils.getFileRevisionTypedElement(gitPath, commonAncestor, db);
        if (!(ancestorCandidate instanceof EmptyTypedElement))
            ancestor = ancestorCandidate;
    }
    return ancestor;
}
Also used : ITypedElement(org.eclipse.compare.ITypedElement) EmptyTypedElement(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput.EmptyTypedElement) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 30 with ITypedElement

use of org.eclipse.compare.ITypedElement in project egit by eclipse.

the class GitModelSynchronizeParticipant method asCompareInput.

@Override
public ICompareInput asCompareInput(Object object) {
    final ICompareInput input = super.asCompareInput(object);
    final ISynchronizationContext ctx = getContext();
    if (input instanceof ResourceDiffCompareInput && ctx instanceof SubscriberMergeContext) {
        // Team only considers local resources as "left"
        // We'll use the cached data instead as left could be remote
        final IResource resource = ((ResourceNode) input.getLeft()).getResource();
        final Subscriber subscriber = ((SubscriberMergeContext) ctx).getSubscriber();
        if (resource instanceof IFile && subscriber instanceof GitResourceVariantTreeSubscriber) {
            try {
                final IFileRevision revision = ((GitResourceVariantTreeSubscriber) subscriber).getSourceFileRevision((IFile) resource);
                if (revision == null) {
                    final ITypedElement newSource = new GitCompareFileRevisionEditorInput.EmptyTypedElement(resource.getName());
                    ((ResourceDiffCompareInput) input).setLeft(newSource);
                } else if (!(revision instanceof WorkspaceFileRevision)) {
                    final ITypedElement newSource = new FileRevisionTypedElement(revision, getLocalEncoding(resource));
                    ((ResourceDiffCompareInput) input).setLeft(newSource);
                }
            } catch (TeamException e) {
                // Keep the input from super as-is
                String error = NLS.bind(UIText.GitModelSynchronizeParticipant_noCachedSourceVariant, resource.getName());
                Activator.logError(error, e);
            }
        }
    }
    return input;
}
Also used : IFile(org.eclipse.core.resources.IFile) ISynchronizationContext(org.eclipse.team.core.mapping.ISynchronizationContext) IFileRevision(org.eclipse.team.core.history.IFileRevision) ITypedElement(org.eclipse.compare.ITypedElement) WorkspaceFileRevision(org.eclipse.egit.core.internal.storage.WorkspaceFileRevision) ICompareInput(org.eclipse.compare.structuremergeviewer.ICompareInput) ResourceDiffCompareInput(org.eclipse.team.internal.ui.mapping.ResourceDiffCompareInput) ResourceNode(org.eclipse.compare.ResourceNode) TeamException(org.eclipse.team.core.TeamException) Subscriber(org.eclipse.team.core.subscribers.Subscriber) GitResourceVariantTreeSubscriber(org.eclipse.egit.core.synchronize.GitResourceVariantTreeSubscriber) SubscriberMergeContext(org.eclipse.team.core.subscribers.SubscriberMergeContext) GitSubscriberMergeContext(org.eclipse.egit.core.synchronize.GitSubscriberMergeContext) GitResourceVariantTreeSubscriber(org.eclipse.egit.core.synchronize.GitResourceVariantTreeSubscriber) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) IResource(org.eclipse.core.resources.IResource)

Aggregations

ITypedElement (org.eclipse.compare.ITypedElement)49 IFile (org.eclipse.core.resources.IFile)18 DiffNode (org.eclipse.compare.structuremergeviewer.DiffNode)15 CoreException (org.eclipse.core.runtime.CoreException)12 IOException (java.io.IOException)11 GitCompareFileRevisionEditorInput (org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput)10 File (java.io.File)6 CompareEditorInput (org.eclipse.compare.CompareEditorInput)6 ICompareInput (org.eclipse.compare.structuremergeviewer.ICompareInput)6 IDiffElement (org.eclipse.compare.structuremergeviewer.IDiffElement)6 IResource (org.eclipse.core.resources.IResource)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IFileRevision (org.eclipse.team.core.history.IFileRevision)6 IPath (org.eclipse.core.runtime.IPath)5 FileRevisionTypedElement (org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)5 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)5 ArrayList (java.util.ArrayList)4 CompareConfiguration (org.eclipse.compare.CompareConfiguration)4 IResourceProvider (org.eclipse.compare.IResourceProvider)4