Example usage for org.eclipse.jgit.lib Repository readDirCache

List of usage examples for org.eclipse.jgit.lib Repository readDirCache

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository readDirCache.

Prototype

@NonNull
public DirCache readDirCache() throws NoWorkTreeException, CorruptObjectException, IOException 

Source Link

Document

Create a new in-core index representation and read an index from disk.

Usage

From source file:org.eclipse.ptp.internal.rdt.sync.git.core.JGitRepo.java

License:Open Source License

private boolean anyDiffInIndex() throws IOException {
    final Repository repo = getRepository();
    final TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.setRecursive(true); //recursive is required because a hash for a (sub)tree might not be available
    final ObjectId rev = repo.resolve("HEAD"); //$NON-NLS-1$
    if (rev != null) { //HEAD doesn't exist for a new repo, then we want to compare against empty tree
        treeWalk.addTree(new RevWalk(repo).parseTree(rev));
    }/*from ww  w .j a  v  a  2 s.  c om*/
    treeWalk.addTree(new DirCacheIterator(repo.readDirCache()));
    treeWalk.setFilter(TreeFilter.ANY_DIFF);
    return treeWalk.next();
}

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  va2s . c  o  m*/

    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);
    }
}