Search in sources :

Example 1 with GitCompareFileRevisionEditorInput

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

the class CompareUtils method compareHeadWithWorkspace.

/**
 * Opens a compare editor. The workspace version of the given file is
 * compared with the version in the HEAD commit.
 *
 * @param repository
 * @param file
 */
public static void compareHeadWithWorkspace(Repository repository, @NonNull IFile file) {
    RepositoryMapping mapping = RepositoryMapping.getMapping(file);
    if (mapping == null) {
        Activator.error(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, file.getLocation(), repository), null);
        return;
    }
    String path = mapping.getRepoRelativePath(file);
    ITypedElement base = getHeadTypedElement(repository, path);
    if (base == null)
        return;
    IFileRevision nextFile = new WorkspaceFileRevision(file);
    String encoding = null;
    try {
        encoding = file.getCharset();
    } catch (CoreException e) {
        Activator.handleError(UIText.CompareUtils_errorGettingEncoding, e, true);
    }
    ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
    GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
    CompareUI.openCompareDialog(input);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) WorkspaceFileRevision(org.eclipse.egit.core.internal.storage.WorkspaceFileRevision) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)

Example 2 with GitCompareFileRevisionEditorInput

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

the class CompareUtils method prepareCompareInput.

/*
	 * Creates a compare input that can be used to compare a given local file
	 * with another reference. The given "base" element should always reflect a
	 * local file, either in the workspace (IFile) or on the file system
	 * (java.io.File) since we'll use "HEAD" to find a common ancestor of this
	 * base and the reference we compare it with.
	 */
private static CompareEditorInput prepareCompareInput(Repository repository, String gitPath, ITypedElement base, String refName) throws IOException {
    final ITypedElement destCommit;
    ITypedElement commonAncestor = null;
    if (GitFileRevision.INDEX.equals(refName))
        destCommit = getIndexTypedElement(repository, gitPath);
    else if (Constants.HEAD.equals(refName))
        destCommit = getHeadTypedElement(repository, gitPath);
    else {
        final ObjectId destCommitId = repository.resolve(refName);
        try (RevWalk rw = new RevWalk(repository)) {
            RevCommit commit = rw.parseCommit(destCommitId);
            destCommit = getFileRevisionTypedElement(gitPath, commit, repository);
            if (base != null && commit != null) {
                final ObjectId headCommitId = repository.resolve(Constants.HEAD);
                commonAncestor = getFileRevisionTypedElementForCommonAncestor(gitPath, headCommitId, destCommitId, repository);
            }
        }
    }
    final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(base, destCommit, commonAncestor, null);
    in.getCompareConfiguration().setRightLabel(refName);
    return in;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with GitCompareFileRevisionEditorInput

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

the class CompareWithWorkingTreeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.isEmpty())
        return null;
    // Even if there's more than one element, only consider the first
    RevCommit commit = (RevCommit) selection.getFirstElement();
    Object input = getPage(event).getInputInternal().getSingleFile();
    IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
    if (input instanceof IFile) {
        IFile file = (IFile) input;
        final RepositoryMapping mapping = RepositoryMapping.getMapping(file);
        if (mapping != null) {
            final String gitPath = mapping.getRepoRelativePath(file);
            final String commitPath = getRenamedPath(gitPath, commit);
            ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, mapping.getRepository());
            final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(SaveableCompareEditorInput.createFileElement(file), right, null);
            CompareUtils.openInCompare(workBenchPage, in);
        }
    } else if (input instanceof File) {
        File file = (File) input;
        // TODO can we create a ITypedElement from the local file?
        Repository repo = getRepository(event);
        RevCommit leftCommit;
        try (RevWalk rw = new RevWalk(repo)) {
            leftCommit = rw.parseCommit(repo.resolve(Constants.HEAD));
        } catch (Exception e) {
            throw new ExecutionException(e.getMessage(), e);
        }
        final String leftCommitPath = getRepoRelativePath(repo, file);
        final String rightCommitPath = getRenamedPath(leftCommitPath, commit);
        CompareUtils.openInCompare(leftCommit, commit, leftCommitPath, rightCommitPath, repo, workBenchPage);
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ExecutionException(org.eclipse.core.commands.ExecutionException) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ExecutionException(org.eclipse.core.commands.ExecutionException) File(java.io.File) IFile(org.eclipse.core.resources.IFile) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 4 with GitCompareFileRevisionEditorInput

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

the class ShowVersionsHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    boolean compareMode = Boolean.TRUE.toString().equals(event.getParameter(COMPARE_MODE_PARAM));
    IStructuredSelection selection = getSelection(getPage());
    if (selection.size() < 1)
        return null;
    Object input = getPage().getInputInternal().getSingleFile();
    if (input == null)
        return null;
    IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
    boolean errorOccurred = false;
    List<ObjectId> ids = new ArrayList<>();
    String gitPath = null;
    if (input instanceof IFile) {
        IFile resource = (IFile) input;
        final RepositoryMapping map = RepositoryMapping.getMapping(resource);
        if (map != null) {
            gitPath = map.getRepoRelativePath(resource);
            Iterator<?> it = selection.iterator();
            while (it.hasNext()) {
                RevCommit commit = (RevCommit) it.next();
                String commitPath = getRenamedPath(gitPath, commit);
                IFileRevision rev = null;
                try {
                    rev = CompareUtils.getFileRevision(commitPath, commit, map.getRepository(), null);
                } catch (IOException e) {
                    Activator.logError(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, gitPath, commit.getId()), e);
                    errorOccurred = true;
                }
                if (rev != null) {
                    if (compareMode) {
                        ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, map.getRepository());
                        final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(SaveableCompareEditorInput.createFileElement(resource), right, null);
                        try {
                            CompareUtils.openInCompare(workBenchPage, in);
                        } catch (Exception e) {
                            errorOccurred = true;
                        }
                    } else
                        try {
                            EgitUiEditorUtils.openEditor(getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
                        } catch (CoreException e) {
                            Activator.logError(UIText.GitHistoryPage_openFailed, e);
                            errorOccurred = true;
                        }
                } else {
                    ids.add(commit.getId());
                }
            }
        }
    }
    if (input instanceof File) {
        File fileInput = (File) input;
        Repository repo = getRepository(event);
        gitPath = getRepoRelativePath(repo, fileInput);
        Iterator<?> it = selection.iterator();
        while (it.hasNext()) {
            RevCommit commit = (RevCommit) it.next();
            String commitPath = getRenamedPath(gitPath, commit);
            IFileRevision rev = null;
            try {
                rev = CompareUtils.getFileRevision(commitPath, commit, repo, null);
            } catch (IOException e) {
                Activator.logError(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, commitPath, commit.getId()), e);
                errorOccurred = true;
            }
            if (rev != null) {
                if (compareMode)
                    try (RevWalk rw = new RevWalk(repo)) {
                        ITypedElement left = CompareUtils.getFileRevisionTypedElement(gitPath, rw.parseCommit(repo.resolve(Constants.HEAD)), repo);
                        ITypedElement right = CompareUtils.getFileRevisionTypedElement(commitPath, commit, repo);
                        final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(left, right, null);
                        CompareUtils.openInCompare(workBenchPage, in);
                    } catch (IOException e) {
                        errorOccurred = true;
                    }
                else
                    try {
                        EgitUiEditorUtils.openEditor(getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
                    } catch (CoreException e) {
                        Activator.logError(UIText.GitHistoryPage_openFailed, e);
                        errorOccurred = true;
                    }
            } else
                ids.add(commit.getId());
        }
    }
    if (errorOccurred)
        Activator.showError(UIText.GitHistoryPage_openFailed, null);
    if (ids.size() > 0) {
        // $NON-NLS-1$
        StringBuilder idList = new StringBuilder("");
        for (ObjectId objectId : ids) idList.append(objectId.getName()).append(' ');
        MessageDialog.openError(getPart(event).getSite().getShell(), UIText.GitHistoryPage_fileNotFound, NLS.bind(UIText.GitHistoryPage_notContainedInCommits, gitPath, idList.toString()));
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) Repository(org.eclipse.jgit.lib.Repository) CoreException(org.eclipse.core.runtime.CoreException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IFile(org.eclipse.core.resources.IFile) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with GitCompareFileRevisionEditorInput

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

the class CompareTreeView method reactOnOpen.

private void reactOnOpen(OpenEvent event) {
    Object selected = ((IStructuredSelection) event.getSelection()).getFirstElement();
    ITypedElement left;
    ITypedElement right;
    if (selected instanceof ContainerNode) {
        // open/close folder
        TreeViewer tv = (TreeViewer) event.getViewer();
        tv.setExpandedState(selected, !tv.getExpandedState(selected));
    } else if (selected instanceof FileNode) {
        FileNode fileNode = (FileNode) selected;
        boolean compareMode = Activator.getDefault().getPreferenceStore().getBoolean(UIPreferences.TREE_COMPARE_COMPARE_MODE);
        if (compareMode) {
            left = getTypedElement(fileNode, fileNode.leftRevision, getBaseVersionText());
            right = getTypedElement(fileNode, fileNode.rightRevision, getCompareVersionText());
            GitCompareFileRevisionEditorInput compareInput = new GitCompareFileRevisionEditorInput(left, right, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage());
            CompareUtils.openInCompare(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), compareInput);
        } else {
            IFile file = fileNode.getFile();
            if (file != null) {
                DiffViewer.openFileInEditor(file.getLocation().toFile(), -1);
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) TreeViewer(org.eclipse.jface.viewers.TreeViewer) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

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