public Object execute(ExecutionEvent event) throws ExecutionException { final Repository repository = getRepository(true, event); // assert all resources map to the same repository if (repository == null) return null; final IResource[] resources = getSelectedResources(event); if (resources.length == 1 && resources[0] instanceof IFile) { final IFile baseFile = (IFile) resources[0]; final String gitPath = RepositoryMapping.getMapping(baseFile.getProject()).getRepoRelativePath(baseFile); final ITypedElement base = SaveableCompareEditorInput.createFileElement(baseFile); ITypedElement next; try { Ref head = repository.getRef(Constants.HEAD); RevWalk rw = new RevWalk(repository); RevCommit commit = rw.parseCommit(head.getObjectId()); next = CompareUtils.getFileRevisionTypedElement(gitPath, commit, repository); } catch (IOException e) { Activator.handleError(e.getMessage(), e, true); return null; } final GitCompareFileRevisionEditorInput in = new GitCompareFileRevisionEditorInput(base, next, null); CompareUI.openCompareEditor(in); return null; } else { CompareTreeView view; try { view = (CompareTreeView) PlatformUI.getWorkbench() .getActiveWorkbenchWindow() .getActivePage() .showView(CompareTreeView.ID); try { view.setInput(resources, repository.resolve(Constants.HEAD).name()); } catch (IOException e) { Activator.handleError(e.getMessage(), e, true); return null; } } catch (PartInitException e) { Activator.handleError(e.getMessage(), e, true); return null; } return null; } }
public Object execute(ExecutionEvent event) throws ExecutionException { boolean compareMode = Boolean.TRUE.toString().equals(event.getParameter(HistoryViewCommands.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<ObjectId>(); String gitPath = null; if (input instanceof IFile) { IFile resource = (IFile) input; final RepositoryMapping map = RepositoryMapping.getMapping(resource); 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 { ITypedElement left = CompareUtils.getFileRevisionTypedElement( gitPath, new RevWalk(repo).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) { StringBuilder idList = new StringBuilder(""); // $NON-NLS-1$ 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; }