Search in sources :

Example 1 with WorkingTreeFileRevision

use of org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision in project egit by eclipse.

the class GitMergeEditorInput method buildDiffContainer.

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);
    try (TreeWalk tw = new TreeWalk(repository)) {
        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<>();
            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;
            // instead of null
            if (anc instanceof EmptyTypedElement)
                anc = null;
            // create the node as child
            new DiffNode(fileParent, kind, anc, leftEditable, right);
        }
        return result;
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NotIgnoredFilter(org.eclipse.jgit.treewalk.filter.NotIgnoredFilter) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) IFile(org.eclipse.core.resources.IFile) DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ArrayList(java.util.ArrayList) LocationEditableRevision(org.eclipse.egit.ui.internal.revision.LocationEditableRevision) IDiffContainer(org.eclipse.compare.structuremergeviewer.IDiffContainer) ResourceEditableRevision(org.eclipse.egit.ui.internal.revision.ResourceEditableRevision) GitFileRevision(org.eclipse.egit.core.internal.storage.GitFileRevision) WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) EmptyTypedElement(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput.EmptyTypedElement) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) LocalFileRevision(org.eclipse.egit.ui.internal.revision.LocalFileRevision) IPath(org.eclipse.core.runtime.IPath) IFileRevision(org.eclipse.team.core.history.IFileRevision) ITypedElement(org.eclipse.compare.ITypedElement) IOException(java.io.IOException) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) CoreException(org.eclipse.core.runtime.CoreException) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) LocationEditableRevision(org.eclipse.egit.ui.internal.revision.LocationEditableRevision) ResourceEditableRevision(org.eclipse.egit.ui.internal.revision.ResourceEditableRevision) EditableRevision(org.eclipse.egit.ui.internal.revision.EditableRevision) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator)

Example 2 with WorkingTreeFileRevision

use of org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision in project egit by eclipse.

the class CompareTreeView method buildMaps.

private void buildMaps(Repository repository, RevCommit baseCommit, RevCommit compareCommit, IProgressMonitor monitor) throws InterruptedException, IOException {
    monitor.beginTask(UIText.CompareTreeView_AnalyzingRepositoryTaskText, IProgressMonitor.UNKNOWN);
    long previousTimeMilliseconds = System.currentTimeMillis();
    boolean useIndex = compareVersion.equals(INDEX_VERSION);
    fileNodes.clear();
    containerNodes.clear();
    boolean checkIgnored = false;
    try (TreeWalk tw = new TreeWalk(repository)) {
        int baseTreeIndex;
        if (baseCommit == null) {
            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
            compareTreeIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));
        if (input instanceof IResource[]) {
            IResource[] resources = (IResource[]) input;
            List<TreeFilter> orFilters = new ArrayList<>(resources.length);
            for (IResource resource : resources) {
                String relPath = repositoryMapping.getRepoRelativePath(resource);
                if (relPath != null && 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);
            IFileRevision left = null;
            IFileRevision right = null;
            String repoRelativePath = baseVersionIterator != null ? baseVersionIterator.getEntryPathString() : compareVersionIterator.getEntryPathString();
            IPath currentPath = new Path(repoRelativePath);
            // Updating the progress bar is slow, so just sample it. To
            // make sure slow compares are reflected in the progress
            // monitor also update before comparing large files.
            long currentTimeMilliseconds = System.currentTimeMillis();
            long size1 = -1;
            long size2 = -1;
            if (compareVersionIterator != null && baseVersionIterator != null) {
                size1 = getEntrySize(tw, compareVersionIterator);
                size2 = getEntrySize(tw, baseVersionIterator);
            }
            final long REPORTSIZE = 100000;
            if (size1 > REPORTSIZE || size2 > REPORTSIZE || currentTimeMilliseconds - previousTimeMilliseconds > 500) {
                monitor.setTaskName(currentPath.toString());
                previousTimeMilliseconds = currentTimeMilliseconds;
            }
            Type type = null;
            if (compareVersionIterator != null && baseVersionIterator != null) {
                boolean equalContent = compareVersionIterator.getEntryObjectId().equals(baseVersionIterator.getEntryObjectId());
                type = equalContent ? Type.FILE_BOTH_SIDES_SAME : Type.FILE_BOTH_SIDES_DIFFER;
            } else if (compareVersionIterator != null && baseVersionIterator == null) {
                type = Type.FILE_DELETED;
            } else if (compareVersionIterator == null && baseVersionIterator != null) {
                type = Type.FILE_ADDED;
            }
            IFile file = null;
            if (type != Type.FILE_BOTH_SIDES_SAME) {
                file = ResourceUtil.getFileForLocation(repository, repoRelativePath, false);
            }
            if (baseVersionIterator != null) {
                if (baseCommit == null) {
                    if (file != null)
                        left = new LocalFileRevision(file);
                    else {
                        IPath path = getRepositoryPath().append(repoRelativePath);
                        left = new WorkingTreeFileRevision(path.toFile());
                    }
                } else {
                    left = GitFileRevision.inCommit(repository, baseCommit, repoRelativePath, tw.getObjectId(baseTreeIndex));
                }
            }
            if (compareVersionIterator != null) {
                if (!useIndex)
                    right = GitFileRevision.inCommit(repository, compareCommit, repoRelativePath, tw.getObjectId(compareTreeIndex));
                else
                    right = GitFileRevision.inIndex(repository, repoRelativePath);
            }
            IPath containerPath = currentPath.removeLastSegments(1);
            ContainerNode containerNode = getOrCreateContainerNode(containerPath, type);
            FileNode fileNode = new FileNode(currentPath, file, type, left, right);
            containerNode.addChild(fileNode);
            fileNodes.put(currentPath, fileNode);
            // change of the "only equal content" flag.
            if (type != Type.FILE_BOTH_SIDES_SAME) {
                IPath path = currentPath;
                while (path.segmentCount() > 0) {
                    path = path.removeLastSegments(1);
                    ContainerNode node = containerNodes.get(path);
                    node.setOnlyEqualContent(false);
                }
            }
        }
    } finally {
        monitor.done();
    }
}
Also used : NotIgnoredFilter(org.eclipse.jgit.treewalk.filter.NotIgnoredFilter) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) LocalFileRevision(org.eclipse.egit.ui.internal.revision.LocalFileRevision) IPath(org.eclipse.core.runtime.IPath) IFileRevision(org.eclipse.team.core.history.IFileRevision) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) Type(org.eclipse.egit.ui.internal.dialogs.CompareTreeView.PathNode.Type) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) IResource(org.eclipse.core.resources.IResource)

Example 3 with WorkingTreeFileRevision

use of org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision in project egit by eclipse.

the class CompareUtils method compareHeadWithWorkingTree.

/**
 * Opens a compare editor. The working tree version of the given file is
 * compared with the version in the HEAD commit. Use this method if the
 * given file is outide the workspace.
 *
 * @param repository
 * @param path
 */
public static void compareHeadWithWorkingTree(Repository repository, String path) {
    ITypedElement base = getHeadTypedElement(repository, path);
    if (base == null)
        return;
    IFileRevision nextFile;
    nextFile = new WorkingTreeFileRevision(new File(repository.getWorkTree(), path));
    String encoding = ResourcesPlugin.getEncoding();
    ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
    GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
    CompareUI.openCompareDialog(input);
}
Also used : WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

IFile (org.eclipse.core.resources.IFile)3 WorkingTreeFileRevision (org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision)3 IFileRevision (org.eclipse.team.core.history.IFileRevision)3 ArrayList (java.util.ArrayList)2 ITypedElement (org.eclipse.compare.ITypedElement)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 FileRevisionTypedElement (org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)2 LocalFileRevision (org.eclipse.egit.ui.internal.revision.LocalFileRevision)2 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)2 AbstractTreeIterator (org.eclipse.jgit.treewalk.AbstractTreeIterator)2 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 AndTreeFilter (org.eclipse.jgit.treewalk.filter.AndTreeFilter)2 NotIgnoredFilter (org.eclipse.jgit.treewalk.filter.NotIgnoredFilter)2 OrTreeFilter (org.eclipse.jgit.treewalk.filter.OrTreeFilter)2 TreeFilter (org.eclipse.jgit.treewalk.filter.TreeFilter)2 File (java.io.File)1 IOException (java.io.IOException)1 DiffNode (org.eclipse.compare.structuremergeviewer.DiffNode)1