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; } }
@Override 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); IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage(); try { CompareUtils.compare( resources, repository, Constants.HEAD, Constants.HEAD, true, workBenchPage); } catch (IOException e) { Activator.handleError(UIText.CompareWithRefAction_errorOnSynchronize, e, true); } 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; }
private void compare(CommitItem commitItem) { IFile file = findFile(commitItem.path); if (file == null || RepositoryProvider.getProvider(file.getProject()) == null) CompareUtils.compareHeadWithWorkingTree(repository, commitItem.path); else CompareUtils.compareHeadWithWorkspace(repository, file); }
private void reactOnOpen(OpenEvent event) { Object selected = ((IStructuredSelection) event.getSelection()).getFirstElement(); ITypedElement left; ITypedElement right; if (selected instanceof IContainer) { // open/close folder TreeViewer tv = (TreeViewer) event.getViewer(); tv.setExpandedState(selected, !tv.getExpandedState(selected)); return; } else if (selected instanceof IFile) { final IFile res = (IFile) selected; left = new EditableRevision(new LocalFileRevision(res)) { @Override public void setContent(final byte[] newContent) { try { PlatformUI.getWorkbench() .getProgressService() .run( false, false, new IRunnableWithProgress() { public void run(IProgressMonitor myMonitor) throws InvocationTargetException, InterruptedException { try { res.setContents( new ByteArrayInputStream(newContent), false, true, myMonitor); } catch (CoreException e) { throw new InvocationTargetException(e); } } }); } catch (InvocationTargetException e) { Activator.handleError( e.getTargetException().getMessage(), e.getTargetException(), true); } catch (InterruptedException e) { // ignore here } } }; GitFileRevision rightRevision = compareVersionMap.get(new Path(repositoryMapping.getRepoRelativePath(res))); if (rightRevision == null) { right = new GitCompareFileRevisionEditorInput.EmptyTypedElement( NLS.bind( UIText.CompareTreeView_ItemNotFoundInVersionMessage, res.getName(), getCompareVersion())); } else { String encoding = CompareCoreUtils.getResourceEncoding(res); right = new FileRevisionTypedElement(rightRevision, encoding); } GitCompareFileRevisionEditorInput compareInput = new GitCompareFileRevisionEditorInput( left, right, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()); CompareUtils.openInCompare( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), compareInput); } else if (selected instanceof GitFileRevision) { GitFileRevision rightRevision = (GitFileRevision) selected; left = new GitCompareFileRevisionEditorInput.EmptyTypedElement( NLS.bind( UIText.CompareTreeView_ItemNotFoundInVersionMessage, rightRevision.getName(), getBaseVersion())); right = new FileRevisionTypedElement(rightRevision); } else if (selected instanceof PathNode) { PathNode node = (PathNode) selected; switch (node.type) { case FILE_BOTH_SIDES_DIFFER: // fall through case FILE_BOTH_SIDES_SAME: { // open a compare editor with both sides filled GitFileRevision rightRevision = compareVersionMap.get(node.path); right = new FileRevisionTypedElement(rightRevision); GitFileRevision leftRevision = baseVersionMap.get(node.path); left = new FileRevisionTypedElement(leftRevision); break; } case FILE_DELETED: { // open compare editor with left side empty GitFileRevision rightRevision = compareVersionMap.get(node.path); right = new FileRevisionTypedElement(rightRevision); left = new GitCompareFileRevisionEditorInput.EmptyTypedElement( NLS.bind( UIText.CompareTreeView_ItemNotFoundInVersionMessage, rightRevision.getName(), getBaseVersion())); break; } case FILE_ADDED: { // open compare editor with right side empty GitFileRevision leftRevision = baseVersionMap.get(node.path); left = new FileRevisionTypedElement(leftRevision); right = new GitCompareFileRevisionEditorInput.EmptyTypedElement( NLS.bind( UIText.CompareTreeView_ItemNotFoundInVersionMessage, leftRevision.getName(), getCompareVersion())); break; } case FOLDER: // open/close folder TreeViewer tv = (TreeViewer) event.getViewer(); tv.setExpandedState(selected, !tv.getExpandedState(selected)); return; default: return; } } else if (selected instanceof PathNodeAdapter) { // deleted in workspace PathNodeAdapter node = (PathNodeAdapter) selected; GitFileRevision rightRevision = compareVersionMap.get(node.pathNode.path); right = new FileRevisionTypedElement(rightRevision); left = new GitCompareFileRevisionEditorInput.EmptyTypedElement( NLS.bind( UIText.CompareTreeView_ItemNotFoundInVersionMessage, node.pathNode.path.lastSegment(), getBaseVersion())); } else return; GitCompareFileRevisionEditorInput compareInput = new GitCompareFileRevisionEditorInput( left, right, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()); CompareUtils.openInCompare( PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), compareInput); }
@Override protected Object prepareInput(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { // make sure all resources belong to the same repository try (RevWalk rw = new RevWalk(repository)) { monitor.beginTask( UIText.GitCompareEditorInput_CompareResourcesTaskName, IProgressMonitor.UNKNOWN); for (IResource resource : resources) { RepositoryMapping map = RepositoryMapping.getMapping(resource.getProject()); if (map == null) { throw new InvocationTargetException( new IllegalStateException( UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge)); } if (repository != null && repository != map.getRepository()) throw new InvocationTargetException( new IllegalStateException( UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge)); String repoRelativePath = map.getRepoRelativePath(resource); filterPathStrings.add(repoRelativePath); DiffNode node = new DiffNode(Differencer.NO_CHANGE) { @Override public Image getImage() { return FOLDER_IMAGE; } }; diffRoots.put(new Path(map.getRepoRelativePath(resource)), node); repository = map.getRepository(); } if (repository == null) throw new InvocationTargetException( new IllegalStateException( UIText.GitCompareEditorInput_ResourcesInDifferentReposMessagge)); if (monitor.isCanceled()) throw new InterruptedException(); final RevCommit baseCommit; try { try { baseCommit = rw.parseCommit(repository.resolve(baseVersion)); } catch (IOException e) { throw new InvocationTargetException(e); } final RevCommit compareCommit; if (compareVersion == null) { compareCommit = null; } else { try { compareCommit = rw.parseCommit(repository.resolve(compareVersion)); } catch (IOException e) { throw new InvocationTargetException(e); } } if (monitor.isCanceled()) throw new InterruptedException(); // set the labels CompareConfiguration config = getCompareConfiguration(); config.setLeftLabel(compareVersion); config.setRightLabel(baseVersion); // set title and icon if (resources.length == 0) { Object[] titleParameters = new Object[] { Activator.getDefault().getRepositoryUtil().getRepositoryName(repository), CompareUtils.truncatedRevision(compareVersion), CompareUtils.truncatedRevision(baseVersion) }; setTitle(NLS.bind(UIText.GitCompareEditorInput_EditorTitle, titleParameters)); } else if (resources.length == 1) { Object[] titleParameters = new Object[] { resources[0].getFullPath().makeRelative().toString(), CompareUtils.truncatedRevision(compareVersion), CompareUtils.truncatedRevision(baseVersion) }; setTitle( NLS.bind(UIText.GitCompareEditorInput_EditorTitleSingleResource, titleParameters)); } else { setTitle( NLS.bind( UIText.GitCompareEditorInput_EditorTitleMultipleResources, CompareUtils.truncatedRevision(compareVersion), CompareUtils.truncatedRevision(baseVersion))); } // build the nodes try { return buildDiffContainer(baseCommit, compareCommit, monitor); } catch (IOException e) { throw new InvocationTargetException(e); } } finally { monitor.done(); } } }