List of usage examples for org.eclipse.jgit.treewalk AbstractTreeIterator getEntryObjectId
public ObjectId getEntryObjectId()
From source file:de.fkoeberle.autocommit.git.GitRepositoryAdapter.java
License:Open Source License
private ObjectId objectIdOrNullOfMatch(AbstractTreeIterator match) { ObjectId objectId = null;// w ww. j ava 2 s. c om if (match != null) { objectId = match.getEntryObjectId(); } return objectId; }
From source file:org.eclipse.egit.ui.internal.dialogs.CompareTreeView.java
License:Open Source License
private void buildMaps(Repository repository, RevCommit baseCommit, RevCommit compareCommit, IProgressMonitor monitor) throws InterruptedException, IOException { monitor.beginTask(UIText.CompareTreeView_AnalyzingRepositoryTaskText, IProgressMonitor.UNKNOWN); boolean useIndex = compareVersion.equals(INDEX_VERSION); deletedPaths.clear();//from w w w.j av a 2 s.c om equalContentPaths.clear(); baseVersionMap.clear(); compareVersionMap.clear(); compareVersionPathsWithChildren.clear(); addedPaths.clear(); baseVersionPathsWithChildren.clear(); boolean checkIgnored = false; TreeWalk tw = new TreeWalk(repository); try { int baseTreeIndex; if (baseCommit == null) { checkIgnored = true; baseTreeIndex = tw.addTree( new AdaptableFileTreeIterator(repository, ResourcesPlugin.getWorkspace().getRoot())); } 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 compareTreeIndex = tw.addTree(new DirCacheIterator(repository.readDirCache())); if (input instanceof IResource[]) { IResource[] resources = (IResource[]) input; List<TreeFilter> orFilters = new ArrayList<TreeFilter>(resources.length); for (IResource resource : resources) { String relPath = repositoryMapping.getRepoRelativePath(resource); if (relPath.length() > 0) orFilters.add(PathFilter.create(relPath)); } if (checkIgnored) { if (orFilters.size() > 1) { TreeFilter andFilter = AndTreeFilter.create(new NotIgnoredFilter(baseTreeIndex), OrTreeFilter.create(orFilters)); tw.setFilter(andFilter); } else if (orFilters.size() == 1) { TreeFilter andFilter = AndTreeFilter.create(new NotIgnoredFilter(baseTreeIndex), orFilters.get(0)); tw.setFilter(andFilter); } else tw.setFilter(new NotIgnoredFilter(baseTreeIndex)); } else if (orFilters.size() > 1) tw.setFilter(OrTreeFilter.create(orFilters)); else if (orFilters.size() == 1) tw.setFilter(orFilters.get(0)); } tw.setRecursive(true); if (monitor.isCanceled()) throw new InterruptedException(); while (tw.next()) { if (monitor.isCanceled()) throw new InterruptedException(); AbstractTreeIterator compareVersionIterator = tw.getTree(compareTreeIndex, AbstractTreeIterator.class); AbstractTreeIterator baseVersionIterator = tw.getTree(baseTreeIndex, AbstractTreeIterator.class); if (compareVersionIterator != null && baseVersionIterator != null) { monitor.setTaskName(baseVersionIterator.getEntryPathString()); IPath currentPath = new Path(baseVersionIterator.getEntryPathString()); if (!useIndex) compareVersionMap.put(currentPath, GitFileRevision.inCommit(repository, compareCommit, baseVersionIterator.getEntryPathString(), tw.getObjectId(compareTreeIndex))); else compareVersionMap.put(currentPath, GitFileRevision.inIndex(repository, baseVersionIterator.getEntryPathString())); if (baseCommit != null) baseVersionMap.put(currentPath, GitFileRevision.inCommit(repository, baseCommit, baseVersionIterator.getEntryPathString(), tw.getObjectId(baseTreeIndex))); boolean equalContent = compareVersionIterator.getEntryObjectId() .equals(baseVersionIterator.getEntryObjectId()); if (equalContent) equalContentPaths.add(currentPath); if (equalContent && !showEquals) continue; while (currentPath.segmentCount() > 0) { currentPath = currentPath.removeLastSegments(1); if (!baseVersionPathsWithChildren.add(currentPath)) break; } } else if (baseVersionIterator != null && compareVersionIterator == null) { monitor.setTaskName(baseVersionIterator.getEntryPathString()); // only on base side IPath currentPath = new Path(baseVersionIterator.getEntryPathString()); addedPaths.add(currentPath); if (baseCommit != null) baseVersionMap.put(currentPath, GitFileRevision.inCommit(repository, baseCommit, baseVersionIterator.getEntryPathString(), tw.getObjectId(baseTreeIndex))); while (currentPath.segmentCount() > 0) { currentPath = currentPath.removeLastSegments(1); if (!baseVersionPathsWithChildren.add(currentPath)) break; } } else if (compareVersionIterator != null && baseVersionIterator == null) { monitor.setTaskName(compareVersionIterator.getEntryPathString()); // only on compare side IPath currentPath = new Path(compareVersionIterator.getEntryPathString()); deletedPaths.add(currentPath); List<PathNodeAdapter> children = compareVersionPathsWithChildren .get(currentPath.removeLastSegments(1)); if (children == null) { children = new ArrayList<PathNodeAdapter>(1); compareVersionPathsWithChildren.put(currentPath.removeLastSegments(1), children); } children.add(new PathNodeAdapter(new PathNode(currentPath, Type.FILE_DELETED))); if (!useIndex) compareVersionMap.put(currentPath, GitFileRevision.inCommit(repository, compareCommit, compareVersionIterator.getEntryPathString(), tw.getObjectId(compareTreeIndex))); else compareVersionMap.put(currentPath, GitFileRevision.inIndex(repository, compareVersionIterator.getEntryPathString())); } } } finally { tw.release(); monitor.done(); } }
From source file:org.eclipse.egit.ui.internal.merge.GitCompareEditorInput.java
License:Open Source License
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); 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);//from www . j a va 2 s .c om } 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 AdaptableFileTreeIterator(repository, ResourcesPlugin.getWorkspace().getRoot())); } 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())); try { 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; } finally { tw.release(); } }
From source file:org.eclipse.egit.ui.internal.merge.GitMergeEditorInput.java
License:Open Source License
private void fileToDiffNode(final IFile file, String gitPath, RepositoryMapping map, IDiffContainer root, RevCommit rightCommit, RevCommit headCommit, RevCommit ancestorCommit, RevWalk rw, IProgressMonitor monitor) throws IOException, InterruptedException { if (monitor.isCanceled()) throw new InterruptedException(); TreeWalk tw = new TreeWalk(map.getRepository()); List<String> paths = new ArrayList<String>(); paths.add(map.getRepoRelativePath(file)); tw.setFilter(PathFilterGroup.createFromStrings(paths)); int dcindex = tw.addTree(new DirCacheIterator(map.getRepository().readDirCache())); int ftindex = tw.addTree(new FileTreeIterator(map.getRepository())); int rtindex = tw.addTree(rw.parseTree(map.getRepository().resolve(Constants.HEAD))); tw.setRecursive(tw.getFilter().shouldBeRecursive()); tw.next();//from ww w .j av a 2 s . c o m DirCacheIterator dit = tw.getTree(dcindex, DirCacheIterator.class); final DirCacheEntry indexEntry = dit == null ? null : dit.getDirCacheEntry(); boolean conflicting = indexEntry != null && indexEntry.getStage() > 0; AbstractTreeIterator rt = tw.getTree(rtindex, AbstractTreeIterator.class); FileTreeIterator fit = tw.getTree(ftindex, FileTreeIterator.class); if (fit != null && fit.isEntryIgnored()) return; // compare local file against HEAD to see if it was modified boolean modified = fit != null && rt != null && !fit.getEntryObjectId().equals(rt.getEntryObjectId()); // if this is neither conflicting nor changed, we skip it if (!conflicting && !modified) return; ITypedElement right; if (conflicting) right = CompareUtils.getFileRevisionTypedElement(gitPath, rightCommit, map.getRepository()); else right = CompareUtils.getFileRevisionTypedElement(gitPath, headCommit, map.getRepository()); // can this really happen? if (right instanceof EmptyTypedElement) return; IFileRevision rev; // if the file is not conflicting (as it was auto-merged) // we will show the auto-merged (local) version if (!conflicting || useWorkspace) rev = new LocalFileRevision(file); else rev = GitFileRevision.inCommit(map.getRepository(), headCommit, gitPath, null); EditableRevision leftEditable = new EditableRevision(rev) { @Override public void setContent(final byte[] newContent) { try { run(false, false, new IRunnableWithProgress() { public void run(IProgressMonitor myMonitor) throws InvocationTargetException, InterruptedException { try { file.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 } } }; // make sure we don't need a round trip later try { leftEditable.cacheContents(monitor); } catch (CoreException e) { throw new IOException(e.getMessage()); } int kind = Differencer.NO_CHANGE; if (conflicting) kind = Differencer.CONFLICTING; else if (modified) kind = Differencer.PSEUDO_CONFLICT; DiffNode fileParent = getFileParent(root, file); ITypedElement anc; if (ancestorCommit != null) anc = CompareUtils.getFileRevisionTypedElement(gitPath, ancestorCommit, map.getRepository()); else anc = null; // create the node as child new DiffNode(fileParent, kind, anc, leftEditable, right); }
From source file:org.eclipse.emf.compare.egit.ui.internal.merge.ModelGitMergeEditorInput.java
License:Open Source License
private IDiffContainer buildDiffContainer(Repository repository, RevCommit headCommit, RevCommit ancestorCommit, List<String> filterPaths, RevWalk rw, IProgressMonitor monitor) throws IOException, InterruptedException { monitor.setTaskName(UIText.GitMergeEditorInput_CalculatingDiffTaskName); IDiffContainer result = new DiffNode(Differencer.CONFLICTING); TreeWalk tw = new TreeWalk(repository); try {/*from www . j a va 2 s . c om*/ int dirCacheIndex = tw.addTree(new DirCacheIterator(repository.readDirCache())); int fileTreeIndex = tw.addTree(new FileTreeIterator(repository)); int repositoryTreeIndex = tw.addTree(rw.parseTree(repository.resolve(Constants.HEAD))); // skip ignored resources NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(fileTreeIndex); // filter by selected resources if (filterPaths.size() > 1) { List<TreeFilter> suffixFilters = new ArrayList<TreeFilter>(); for (String filterPath : filterPaths) { suffixFilters.add(PathFilter.create(filterPath)); } TreeFilter otf = OrTreeFilter.create(suffixFilters); tw.setFilter(AndTreeFilter.create(otf, notIgnoredFilter)); } else if (filterPaths.size() > 0) { String path = filterPaths.get(0); if (path.length() == 0) { tw.setFilter(notIgnoredFilter); } else { tw.setFilter(AndTreeFilter.create(PathFilter.create(path), notIgnoredFilter)); } } else { tw.setFilter(notIgnoredFilter); } tw.setRecursive(true); while (tw.next()) { if (monitor.isCanceled()) { throw new InterruptedException(); } String gitPath = tw.getPathString(); monitor.setTaskName(gitPath); FileTreeIterator fit = tw.getTree(fileTreeIndex, FileTreeIterator.class); if (fit == null) { continue; } DirCacheIterator dit = tw.getTree(dirCacheIndex, DirCacheIterator.class); final DirCacheEntry dirCacheEntry = dit == null ? null : dit.getDirCacheEntry(); boolean conflicting = dirCacheEntry != null && dirCacheEntry.getStage() > 0; AbstractTreeIterator rt = tw.getTree(repositoryTreeIndex, AbstractTreeIterator.class); // compare local file against HEAD to see if it was modified boolean modified = rt != null && !fit.getEntryObjectId().equals(rt.getEntryObjectId()); // if this is neither conflicting nor changed, we skip it if (!conflicting && !modified) { continue; } ITypedElement right; if (conflicting) { GitFileRevision revision = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_3); String encoding = CompareCoreUtils.getResourceEncoding(repository, gitPath); right = new FileRevisionTypedElement(revision, encoding); } else { right = CompareUtils.getFileRevisionTypedElement(gitPath, headCommit, repository); } // can this really happen? if (right instanceof EmptyTypedElement) { continue; } IFileRevision rev; // if the file is not conflicting (as it was auto-merged) // we will show the auto-merged (local) version Path repositoryPath = new Path(repository.getWorkTree().getAbsolutePath()); IPath location = repositoryPath.append(fit.getEntryPathString()); IFile file = ResourceUtil.getFileForLocation(location, false); if (!conflicting || useWorkspace) { if (file != null) { rev = new LocalFileRevision(file); } else { rev = new WorkingTreeFileRevision(location.toFile()); } } else { rev = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_2); } IRunnableContext runnableContext = getContainer(); if (runnableContext == null) { runnableContext = PlatformUI.getWorkbench().getProgressService(); } EditableRevision leftEditable; if (file != null) { leftEditable = new ResourceEditableRevision(rev, file, runnableContext); } else { leftEditable = new LocationEditableRevision(rev, location, runnableContext); } // make sure we don't need a round trip later try { leftEditable.cacheContents(monitor); } catch (CoreException e) { throw new IOException(e.getMessage()); } int kind = Differencer.NO_CHANGE; if (conflicting) { kind = Differencer.CONFLICTING; } else if (modified) { kind = Differencer.PSEUDO_CONFLICT; } IDiffContainer fileParent = getFileParent(result, repositoryPath, file, location); ITypedElement anc; if (ancestorCommit != null) { anc = CompareUtils.getFileRevisionTypedElement(gitPath, ancestorCommit, repository); } else { anc = null; } // we get an ugly black icon if we have an EmptyTypedElement // instead of null if (anc instanceof EmptyTypedElement) { anc = null; } // create the node as child new DiffNode(fileParent, kind, anc, leftEditable, right); } return result; } finally { tw.close(); } }