private IDiffContainer buildDiffContainer( RevCommit baseCommit, RevCommit compareCommit, IProgressMonitor monitor) throws IOException, InterruptedException { boolean useIndex = compareVersion.equals(CompareTreeView.INDEX_VERSION); boolean checkIgnored = false; IDiffContainer result = new DiffNode(Differencer.CONFLICTING); try (TreeWalk tw = new TreeWalk(repository)) { // filter by selected resources if (filterPathStrings.size() > 1) { List<TreeFilter> suffixFilters = new ArrayList<TreeFilter>(); for (String filterPath : filterPathStrings) suffixFilters.add(PathFilter.create(filterPath)); TreeFilter otf = OrTreeFilter.create(suffixFilters); tw.setFilter(otf); } else if (filterPathStrings.size() > 0) { String path = filterPathStrings.get(0); if (path.length() != 0) tw.setFilter(PathFilter.create(path)); } tw.setRecursive(true); int baseTreeIndex; if (baseCommit == null) { // compare workspace with something checkIgnored = true; baseTreeIndex = tw.addTree(new FileTreeIterator(repository)); } else baseTreeIndex = tw.addTree( new CanonicalTreeParser(null, repository.newObjectReader(), baseCommit.getTree())); int compareTreeIndex; if (!useIndex) compareTreeIndex = tw.addTree( new CanonicalTreeParser( null, repository.newObjectReader(), compareCommit.getTree())); else // compare something with the index compareTreeIndex = tw.addTree(new DirCacheIterator(repository.readDirCache())); while (tw.next()) { if (monitor.isCanceled()) throw new InterruptedException(); AbstractTreeIterator compareVersionIterator = tw.getTree(compareTreeIndex, AbstractTreeIterator.class); AbstractTreeIterator baseVersionIterator = tw.getTree(baseTreeIndex, AbstractTreeIterator.class); if (checkIgnored && baseVersionIterator != null && ((WorkingTreeIterator) baseVersionIterator).isEntryIgnored()) continue; if (compareVersionIterator != null && baseVersionIterator != null) { boolean equalContent = compareVersionIterator .getEntryObjectId() .equals(baseVersionIterator.getEntryObjectId()); if (equalContent) continue; } String encoding = null; GitFileRevision compareRev = null; if (compareVersionIterator != null) { String entryPath = compareVersionIterator.getEntryPathString(); encoding = CompareCoreUtils.getResourceEncoding(repository, entryPath); if (!useIndex) compareRev = GitFileRevision.inCommit( repository, compareCommit, entryPath, tw.getObjectId(compareTreeIndex)); else compareRev = GitFileRevision.inIndex(repository, entryPath); } GitFileRevision baseRev = null; if (baseVersionIterator != null) { String entryPath = baseVersionIterator.getEntryPathString(); if (encoding == null) { encoding = CompareCoreUtils.getResourceEncoding(repository, entryPath); } baseRev = GitFileRevision.inCommit( repository, baseCommit, entryPath, tw.getObjectId(baseTreeIndex)); } if (compareVersionIterator != null && baseVersionIterator != null) { monitor.setTaskName(baseVersionIterator.getEntryPathString()); // content exists on both sides add( result, baseVersionIterator.getEntryPathString(), new DiffNode( new FileRevisionTypedElement(compareRev, encoding), new FileRevisionTypedElement(baseRev, encoding))); } else if (baseVersionIterator != null && compareVersionIterator == null) { monitor.setTaskName(baseVersionIterator.getEntryPathString()); // only on base side add( result, baseVersionIterator.getEntryPathString(), new DiffNode( Differencer.DELETION | Differencer.RIGHT, null, null, new FileRevisionTypedElement(baseRev, encoding))); } else if (compareVersionIterator != null && baseVersionIterator == null) { monitor.setTaskName(compareVersionIterator.getEntryPathString()); // only on compare side add( result, compareVersionIterator.getEntryPathString(), new DiffNode( Differencer.ADDITION | Differencer.RIGHT, null, new FileRevisionTypedElement(compareRev, encoding), null)); } if (monitor.isCanceled()) throw new InterruptedException(); } return result; } }
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); }
/** * Write diff * * @param repository * @param diff * @return this formatter * @throws IOException */ public DiffStyleRangeFormatter write(Repository repository, FileDiff diff) throws IOException { this.stream.charset = CompareCoreUtils.getResourceEncoding(repository, diff.getPath()); diff.outputDiff(null, repository, this, true); flush(); return this; }