Search in sources :

Example 1 with NotIgnoredFilter

use of org.eclipse.jgit.treewalk.filter.NotIgnoredFilter 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 NotIgnoredFilter

use of org.eclipse.jgit.treewalk.filter.NotIgnoredFilter in project egit by eclipse.

the class GitSyncCache method loadDataFromGit.

static boolean loadDataFromGit(GitSynchronizeData gsd, TreeFilter filter, GitSyncObjectCache repoCache) {
    Repository repo = gsd.getRepository();
    try (TreeWalk tw = new TreeWalk(repo)) {
        if (filter != null)
            tw.setFilter(filter);
        // setup local tree
        FileTreeIterator fti = null;
        if (gsd.shouldIncludeLocal()) {
            fti = new FileTreeIterator(repo);
            tw.addTree(fti);
            if (filter != null)
                tw.setFilter(AndTreeFilter.create(filter, new NotIgnoredFilter(0)));
            else
                tw.setFilter(new NotIgnoredFilter(0));
        } else if (gsd.getSrcRevCommit() != null)
            tw.addTree(gsd.getSrcRevCommit().getTree());
        else
            tw.addTree(new EmptyTreeIterator());
        // setup base tree
        if (gsd.getCommonAncestorRev() != null)
            tw.addTree(gsd.getCommonAncestorRev().getTree());
        else
            tw.addTree(new EmptyTreeIterator());
        // setup remote tree
        if (gsd.getDstRevCommit() != null)
            tw.addTree(gsd.getDstRevCommit().getTree());
        else
            tw.addTree(new EmptyTreeIterator());
        DirCacheIterator dci = null;
        if (fti != null) {
            dci = new DirCacheIterator(DirCache.read(repo));
            tw.addTree(dci);
            fti.setDirCacheIterator(tw, 3);
        }
        List<ThreeWayDiffEntry> diffEntrys = ThreeWayDiffEntry.scan(tw, gsd);
        for (ThreeWayDiffEntry diffEntry : diffEntrys) repoCache.addMember(diffEntry);
    } catch (Exception e) {
        Activator.logError(e.getMessage(), e);
        return false;
    }
    return true;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) NotIgnoredFilter(org.eclipse.jgit.treewalk.filter.NotIgnoredFilter) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator)

Example 3 with NotIgnoredFilter

use of org.eclipse.jgit.treewalk.filter.NotIgnoredFilter 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)

Aggregations

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