Example usage for org.eclipse.jgit.treewalk AbstractTreeIterator getEntryRawMode

List of usage examples for org.eclipse.jgit.treewalk AbstractTreeIterator getEntryRawMode

Introduction

In this page you can find the example usage for org.eclipse.jgit.treewalk AbstractTreeIterator getEntryRawMode.

Prototype

public int getEntryRawMode() 

Source Link

Document

Get the file mode of the current entry as bits.

Usage

From source file:jetbrains.buildServer.buildTriggers.vcs.git.submodules.SubmoduleAwareTreeIteratorFactory.java

License:Apache License

/**
 * Scan current tree and build mapping that reorders submodule entries if needed.
 *
 * @param w wrapped tree iterator/*www. j  av a 2s.co m*/
 * @return a mapping or null if the mapping is not needed
 * @throws CorruptObjectException in case if navigation fails
 */
private static int[] buildMapping(final AbstractTreeIterator w) throws CorruptObjectException {
    class SubmoduleEntry {
        final ByteRange name;
        final int position;

        public SubmoduleEntry(int position) {
            this.position = position;
            byte[] n = new byte[w.getNameLength() + 1];
            w.getName(n, 0);
            n[n.length - 1] = '/';
            name = new ByteRange(n);
        }
    }
    IntArrayList rc = new IntArrayList();
    boolean reordered = false;
    LinkedList<SubmoduleEntry> stack = new LinkedList<SubmoduleEntry>();
    int actual = 0;
    assert w.first();
    if (w.eof()) {
        return null;
    }
    final int INITIAL_NAME_SIZE = 32;
    byte[] name = new byte[INITIAL_NAME_SIZE];
    while (!w.eof()) {
        if (!stack.isEmpty()) {
            int l = w.getNameLength();
            if (l > name.length) {
                int nl = name.length;
                while (nl < l) {
                    nl <<= 1;
                }
                name = new byte[nl];
            }
            w.getName(name, 0);
            ByteRange currentName = new ByteRange(name, 0, l);
            while (!stack.isEmpty()) {
                final SubmoduleEntry top = stack.getLast();
                final int result = top.name.compareTo(currentName);
                assert result != 0;
                if (result < 0) {
                    if (top.position != rc.size()) {
                        reordered = true;
                    }
                    rc.add(top.position);
                    stack.removeLast();
                } else {
                    break;
                }
            }
        }
        if (w.getEntryRawMode() == SubmoduleAwareTreeIterator.GITLINK_MODE_BITS) {
            stack.add(new SubmoduleEntry(actual));
        } else {
            rc.add(actual);
        }
        w.next(1);
        actual++;
    }
    while (!stack.isEmpty()) {
        final SubmoduleEntry top = stack.removeLast();
        if (top.position != rc.size()) {
            reordered = true;
        }
        rc.add(top.position);
    }
    w.back(actual);
    assert w.first();
    return reordered ? rc.toArray() : null;
}

From source file:org.exist.git.xquery.Status.java

License:Open Source License

public void diff(final Repository repository, final String revstr,
        final WorkingTreeIterator initialWorkingTreeIterator, final StatusBuilder builder, final String folder,
        boolean recursive) throws IOException {

    RevTree tree = null;/*from w  w w. j a v  a2 s . c  om*/

    ObjectId objectId = repository.resolve(revstr);
    if (objectId != null)
        tree = new RevWalk(repository).parseTree(objectId);
    else
        tree = null;

    PathFilter filter = folder == null || folder.isEmpty() ? null : PathFilter.create(folder);
    IndexDiffFilter indexDiffFilter;

    DirCache dirCache = repository.readDirCache();

    TreeWalk treeWalk = new TreeWalk(repository);
    //      TreeWalk treeWalk = TreeWalk.forPath(repository, folder, tree);
    treeWalk.setRecursive(recursive);
    // add the trees (tree, dirchache, workdir)
    if (tree != null)
        treeWalk.addTree(tree);
    else
        treeWalk.addTree(new EmptyTreeIterator());
    treeWalk.addTree(new DirCacheIterator(dirCache));
    treeWalk.addTree(initialWorkingTreeIterator);
    Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);

    if (filter != null)
        filters.add(filter);
    filters.add(new SkipWorkTreeFilter(INDEX));
    indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
    filters.add(indexDiffFilter);
    treeWalk.setFilter(AndTreeFilter.create(filters));
    if (filter != null) {
        while (treeWalk.next()) {
            if (filter.include(treeWalk)) {
                if (filter.isDone(treeWalk)) {
                    if (treeWalk.isSubtree()) {
                        treeWalk.enterSubtree();
                    }
                    break;
                } else if (treeWalk.isSubtree()) {
                    treeWalk.enterSubtree();
                }
            }
        }
    }

    while (treeWalk.next()) {
        AbstractTreeIterator treeIterator = treeWalk.getTree(TREE, AbstractTreeIterator.class);
        DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator.class);
        WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR, WorkingTreeIterator.class);

        if (dirCacheIterator != null) {
            final DirCacheEntry dirCacheEntry = dirCacheIterator.getDirCacheEntry();
            if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) {
                builder.conflict(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            }
        }

        if (treeIterator != null) {
            if (dirCacheIterator != null) {
                if (!treeIterator.idEqual(dirCacheIterator)
                        || treeIterator.getEntryRawMode() != dirCacheIterator.getEntryRawMode()) {
                    // in repo, in index, content diff => changed
                    builder.changed(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            } else {
                // in repo, not in index => removed
                builder.removed(treeWalk.getFileMode(2), treeWalk.getPathString());
                if (workingTreeIterator != null) //XXX: 2 statuses
                    builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            }
        } else {
            if (dirCacheIterator != null) {
                // not in repo, in index => added
                builder.added(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            } else {
                // not in repo, not in index => untracked
                if (workingTreeIterator != null && !workingTreeIterator.isEntryIgnored()) {
                    builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            }
        }

        if (dirCacheIterator != null) {
            if (workingTreeIterator == null) {
                // in index, not in workdir => missing
                builder.missing(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            } else {
                if (dirCacheIterator.getDirCacheEntry() == null) {
                    //XXX: null on collections - to fix
                    //                  builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString());

                } else if (workingTreeIterator.isModified(dirCacheIterator.getDirCacheEntry(), true)) {
                    // in index, in workdir, content differs => modified
                    builder.modified(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            }
        }
        builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString());
    }

    //      for (String path : indexDiffFilter.getUntrackedFolders()) {
    //         builder.untrackedFolders(path);
    //      }
    //      
    for (String path : indexDiffFilter.getIgnoredPaths()) {
        //XXX: to fix FileMode
        builder.ignored(FileMode.REGULAR_FILE, path);
    }
}