Example usage for org.eclipse.jgit.treewalk TreeWalk getFilter

List of usage examples for org.eclipse.jgit.treewalk TreeWalk getFilter

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk TreeWalk getFilter.

Prototype

public TreeFilter getFilter() 

Source Link

Document

Get the currently configured filter.

Usage

From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitVcsFileContentProvider.java

License:Apache License

@NotNull
public byte[] getContent(@NotNull String filePath, @NotNull VcsRoot root, @NotNull String version)
        throws VcsException {
    OperationContext context = myVcs.createContext(root,
            "retrieving content, file: '" + filePath + "', version: '" + version + "'");
    try {//from  ww  w  .java 2 s. c o  m
        final long start = System.currentTimeMillis();
        Repository r = context.getRepository();
        final TreeWalk tw = new TreeWalk(r);
        final GitVcsRoot gitRoot = context.getGitRoot();
        try {
            logStartProcessingFile(gitRoot, version, filePath);
            final String rev = GitUtils.versionRevision(version);
            RevCommit c = myCommitLoader.loadCommit(context, gitRoot, rev);
            tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(filePath)));
            tw.setRecursive(tw.getFilter().shouldBeRecursive());
            context.addTree(gitRoot, tw, r, c, true);
            if (!tw.next()) {
                throw new VcsFileNotFoundException(
                        "The file " + filePath + " could not be found in " + rev + gitRoot.debugInfo());
            }
            final byte[] data = loadObject(gitRoot, r, tw, 0);
            logFileContentLoaded(gitRoot, version, filePath, tw);
            return data;
        } finally {
            logPerformance(gitRoot, filePath, start);
            tw.release();
        }
    } catch (Exception e) {
        throw context.wrapException(e);
    } finally {
        context.close();
    }
}

From source file:org.eclipse.egit.ui.internal.decorators.DecoratableResourceAdapter.java

License:Open Source License

private void extractContainerProperties(TreeWalk treeWalk) throws IOException {
    treeWalk.setFilter(AndTreeFilter.create(treeWalk.getFilter(), new RecursiveStateFilter()));
    treeWalk.setRecursive(true);//from   ww  w .  j  a v  a  2s  . c  o m

    treeWalk.next();
}

From source file:org.eclipse.egit.ui.internal.decorators.DecoratableResourceAdapter.java

License:Open Source License

/**
 * Helper method to create a new tree walk between the repository, the
 * index, and the working tree./*from w w w .  j a v  a  2s.  c  om*/
 *
 * @return the created tree walk, or null if it could not be created
 * @throws IOException
 *             if there were errors when creating the tree walk
 */
private TreeWalk createThreeWayTreeWalk() throws IOException {
    final TreeWalk treeWalk = new TreeWalk(repository);
    if (!addResourceFilter(treeWalk, resource))
        return null;

    treeWalk.setRecursive(treeWalk.getFilter().shouldBeRecursive());
    treeWalk.reset();

    // Repository
    if (headId != null)
        treeWalk.addTree(new RevWalk(repository).parseTree(headId));
    else
        treeWalk.addTree(new EmptyTreeIterator());

    // Index
    treeWalk.addTree(new DirCacheIterator(repository.readDirCache()));

    // Working directory
    treeWalk.addTree(IteratorService.createInitialIterator(repository));
    return treeWalk;
}

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();//  w  w  w  .j  ava2  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);
}