Example usage for org.eclipse.jgit.treewalk.filter NotIgnoredFilter NotIgnoredFilter

List of usage examples for org.eclipse.jgit.treewalk.filter NotIgnoredFilter NotIgnoredFilter

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk.filter NotIgnoredFilter NotIgnoredFilter.

Prototype

public NotIgnoredFilter(int workdirTreeIndex) 

Source Link

Document

Construct a filter to ignore paths known to a particular iterator.

Usage

From source file:MyDiffFormatter.java

License:Eclipse Distribution License

private static TreeFilter getDiffTreeFilterFor(AbstractTreeIterator a, AbstractTreeIterator b) {
    if (a instanceof DirCacheIterator && b instanceof WorkingTreeIterator)
        return new IndexDiffFilter(0, 1);

    if (a instanceof WorkingTreeIterator && b instanceof DirCacheIterator)
        return new IndexDiffFilter(1, 0);

    TreeFilter filter = TreeFilter.ANY_DIFF;
    if (a instanceof WorkingTreeIterator)
        filter = AndTreeFilter.create(new NotIgnoredFilter(0), filter);
    if (b instanceof WorkingTreeIterator)
        filter = AndTreeFilter.create(new NotIgnoredFilter(1), filter);
    return filter;
}

From source file:com.beck.ep.team.git.GitListBuilder.java

License:BSD License

public String[] createList(Shell shell, IProject project) throws Exception {
    error = null;/*ww  w.  j av  a 2  s  .com*/
    RepositoryMapping mapping = RepositoryMapping.getMapping(project);
    if (mapping == null) {
        error = "Git Repository not found (project: " + project.getName() + ")";
        return null;
    }
    Repository rep = mapping.getRepository();
    String pjPath = project.getLocation().toFile().getAbsolutePath();
    String repPath = rep.getDirectory().getParentFile().getAbsolutePath();
    int cutPrefixLen = 0;
    int pathDiffLen = pjPath.length() - repPath.length();
    if (pathDiffLen > 0) {
        cutPrefixLen = pathDiffLen;//for repositoy is not in "parent folder of project"
    }

    RevWalk rw = new RevWalk(rep);
    TreeWalk tw = new TreeWalk(rep);

    int ignoreOpt = pref.getInt(GitModeMenuMgr.PREF_TYPE_IGNORE, GitModeMenuMgr.IGNORE_EXCLUDE);
    if (ignoreOpt == GitModeMenuMgr.IGNORE_ONLY) {
        String[] sa = listIgnored(rep, rw, tw, cutPrefixLen);
        if (cutPrefixLen > 0) {
            for (int i = 0; i < sa.length; i++) {
                if (sa[i].length() > cutPrefixLen) {
                    sa[i] = sa[i].substring(cutPrefixLen);
                }
            }
        }
        return sa;
    }

    int mode = pref.getInt(GitModeMenuMgr.PREF_TYPE_MODE, GitModeMenuMgr.MODE_BRANCH_HEAD);
    int sourceMode = pref.getInt(GitModeMenuMgr.PREF_TYPE_SOURCE, GitModeMenuMgr.MODE_WORKING_AREA);
    if (mode == sourceMode) {
        return new String[0];
    }
    int index = addTree(mode, shell, rep, rw, tw);
    if (index == -2) {
        return null;
    }
    int stageIndex = -1;
    boolean excludeUntrackOpt = false;
    if (!pref.getBoolean(GitModeMenuMgr.PREF_TYPE_UNTRACK, true)
            && sourceMode == GitModeMenuMgr.MODE_WORKING_AREA) {
        excludeUntrackOpt = true;
        if (mode == GitModeMenuMgr.MODE_STAGING) {
            stageIndex = 0;
        }
    }

    int idx2 = addTree(sourceMode, shell, rep, rw, tw);
    if (excludeUntrackOpt && stageIndex == -1) {
        addTree(GitModeMenuMgr.MODE_STAGING, shell, rep, rw, tw);
        stageIndex = 2;
    }
    // skip ignored resources
    if (ignoreOpt == GitModeMenuMgr.IGNORE_EXCLUDE && (index != -1 || idx2 != -1)) {
        int pos = (index != -1) ? index : idx2;
        NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(pos);//index = tw.addTree(?)
        tw.setFilter(notIgnoredFilter);
    }

    tw.setRecursive(true);
    ArrayList<String> sa = new ArrayList<String>();
    while (tw.next()) {
        if (!tw.idEqual(0, 1)) {
            if (excludeUntrackOpt) {
                if (tw.getTree(stageIndex, AbstractTreeIterator.class) == null) {
                    WorkingTreeIterator t = tw.getTree(1, WorkingTreeIterator.class);
                    if (t != null && !t.isEntryIgnored()) {//file is untrack...
                        continue;
                    }
                }
            }
            String relPath = tw.getPathString();
            if (cutPrefixLen > 0 && relPath.length() > cutPrefixLen) {
                relPath = relPath.substring(cutPrefixLen);
            }
            sa.add(relPath);
        }
    }

    return sa.toArray(new String[sa.size()]);
}

From source file:org.eclipse.egit.core.synchronize.GitFolderResourceVariant.java

License:Open Source License

/**
 * @param progress/*from  ww w  .j  a v  a2s . c  o m*/
 * @return members
 * @throws IOException
 */
public IResourceVariant[] getMembers(IProgressMonitor progress) throws IOException {
    if (members != null)
        try {
            return members;
        } finally {
            progress.done();
        }

    Repository repo = getRepository();
    TreeWalk tw = new TreeWalk(repo);
    tw.reset();

    int nth = tw.addTree(getObjectId());
    int iteratorNth = tw.addTree(new FileTreeIterator(repo));

    tw.setFilter(new NotIgnoredFilter(iteratorNth));

    IProgressMonitor monitor = SubMonitor.convert(progress);
    monitor.beginTask(NLS.bind(CoreText.GitFolderResourceVariant_fetchingMembers, this), tw.getTreeCount());

    List<IResourceVariant> result = new ArrayList<IResourceVariant>();
    try {
        while (tw.next()) {
            if (monitor.isCanceled())
                throw new OperationCanceledException();

            ObjectId newObjectId = tw.getObjectId(nth);
            String path = getPath() + "/" + new String(tw.getRawPath()); //$NON-NLS-1$
            if (!newObjectId.equals(zeroId()))
                if (tw.isSubtree())
                    result.add(new GitFolderResourceVariant(repo, getRevCommit(), newObjectId, path));
                else
                    result.add(new GitBlobResourceVariant(repo, getRevCommit(), newObjectId, path));
            monitor.worked(1);
        }

        members = result.toArray(new IResourceVariant[result.size()]);
        return members;
    } finally {
        monitor.done();
    }
}

From source file:org.eclipse.egit.core.synchronize.GitResourceVariantTree.java

License:Open Source License

private TreeWalk initializeTreeWalk(Repository repo, String path) throws CorruptObjectException {
    TreeWalk tw = new TreeWalk(repo);
    tw.reset();/*w  w w  .  j  av a 2 s .c  om*/
    int ignoreNth = tw.addTree(new FileTreeIterator(repo));

    TreeFilter pathFilter = PathFilter.create(path);
    TreeFilter ignoreFilter = new NotIgnoredFilter(ignoreNth);
    tw.setFilter(AndTreeFilter.create(pathFilter, ignoreFilter));

    return tw;
}

From source file:org.eclipse.egit.core.synchronize.GitSyncCache.java

License:Open Source License

private static void loadDataFromGit(GitSynchronizeData gsd, TreeFilter filter, GitSyncObjectCache repoCache) {
    Repository repo = gsd.getRepository();
    TreeWalk tw = new TreeWalk(repo);
    if (filter != null)
        tw.setFilter(filter);/*w  ww.  j  av a2s. com*/

    try {
        // 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);
        tw.release();

        for (ThreeWayDiffEntry diffEntry : diffEntrys)
            repoCache.addMember(diffEntry);
    } catch (Exception e) {
        Activator.logError(e.getMessage(), e);
    }
}

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  a va  2s .  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.synchronize.model.GitModelWorkingTree.java

License:Open Source License

@Override
protected TreeWalk createAndConfigureTreeWalk() throws IOException {
    TreeWalk tw = createTreeWalk();//  w  ww  .  j  a va  2 s.  c om
    tw.setRecursive(true);

    Repository repo = getRepository();
    tw.addTree(new DirCacheIterator(repo.readDirCache()));
    tw.addTree(new FileTreeIterator(repo));
    dirCacheIteratorNth = 0;

    NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(1);
    tw.setFilter(AndTreeFilter.create(ANY_DIFF, notIgnoredFilter));

    return tw;
}

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  w  ww  .j  a va2 s.c o m*/
        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();
    }
}