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

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

Introduction

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

Prototype

public int getNameLength() 

Source Link

Document

Get the length of the name component of the path for the current entry.

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//from   www.j  a  v a2 s .c  o 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;
}