Example usage for org.eclipse.jgit.dircache DirCacheIterator getEntryFileMode

List of usage examples for org.eclipse.jgit.dircache DirCacheIterator getEntryFileMode

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCacheIterator getEntryFileMode.

Prototype

public FileMode getEntryFileMode() 

Source Link

Document

Get the file mode of the current entry.

Usage

From source file:org.eclipse.emf.compare.egit.internal.merge.RecursiveModelMerger.java

License:Open Source License

/**
 * Add files modified by model mergers to the index.
 *
 * @throws CorruptObjectException/*from  w  w w  .  ja va2  s .  c om*/
 * @throws MissingObjectException
 * @throws IncorrectObjectTypeException
 * @throws IOException
 */
private void indexModelMergedFiles()
        throws CorruptObjectException, MissingObjectException, IncorrectObjectTypeException, IOException {
    TreeWalk syncingTreeWalk = new TreeWalk(getRepository());
    try {
        syncingTreeWalk.addTree(new DirCacheIterator(dircache));
        syncingTreeWalk.addTree(new FileTreeIterator(getRepository()));
        syncingTreeWalk.setRecursive(true);
        syncingTreeWalk.setFilter(PathFilterGroup.createFromStrings(makeInSync));
        String lastAdded = null;
        while (syncingTreeWalk.next()) {
            String path = syncingTreeWalk.getPathString();
            if (path.equals(lastAdded)) {
                continue;
            }

            WorkingTreeIterator workingTree = syncingTreeWalk.getTree(1, WorkingTreeIterator.class);
            DirCacheIterator dirCache = syncingTreeWalk.getTree(0, DirCacheIterator.class);
            if (dirCache == null && workingTree != null && workingTree.isEntryIgnored()) {
                // nothing to do on this one
            } else if (workingTree != null) {
                if (dirCache == null || dirCache.getDirCacheEntry() == null
                        || !dirCache.getDirCacheEntry().isAssumeValid()) {
                    final DirCacheEntry dce = new DirCacheEntry(path);
                    final FileMode mode = workingTree.getIndexFileMode(dirCache);
                    dce.setFileMode(mode);

                    if (FileMode.GITLINK != mode) {
                        dce.setLength(workingTree.getEntryLength());
                        dce.setLastModified(workingTree.getEntryLastModified());
                        InputStream is = workingTree.openEntryStream();
                        try {
                            dce.setObjectId(getObjectInserter().insert(Constants.OBJ_BLOB,
                                    workingTree.getEntryContentLength(), is));
                        } finally {
                            is.close();
                        }
                    } else {
                        dce.setObjectId(workingTree.getEntryObjectId());
                    }
                    builder.add(dce);
                    lastAdded = path;
                } else {
                    builder.add(dirCache.getDirCacheEntry());
                }
            } else if (dirCache != null && FileMode.GITLINK == dirCache.getEntryFileMode()) {
                builder.add(dirCache.getDirCacheEntry());
            }
        }
    } finally {
        syncingTreeWalk.close();
    }
}

From source file:org.oecd.ant.git.custom.CustomAddCommand.java

License:Eclipse Distribution License

/**
 * Executes the {@code Add} command. Each instance of this class should only be used for one invocation of the command. Don't call this method twice on an
 * instance.//ww  w  . j  a  va  2  s  . c o  m
 *
 * @return the DirCache after Add
 */
@Override
public DirCache call() throws GitAPIException, NoFilepatternException {

    if (filepatterns.isEmpty())
        throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
    checkCallable();
    DirCache dc = null;
    boolean addAll = false;
    if (filepatterns.contains(".")) //$NON-NLS-1$
        addAll = true;

    try (ObjectInserter inserter = repo.newObjectInserter(); final TreeWalk tw = new TreeWalk(repo)) {
        dc = repo.lockDirCache();
        DirCacheIterator c;

        DirCacheBuilder builder = dc.builder();
        tw.addTree(new DirCacheBuildIterator(builder));
        if (workingTreeIterator == null)
            workingTreeIterator = new FileTreeIterator(repo);
        tw.addTree(workingTreeIterator);
        tw.setRecursive(true);
        if (!addAll)
            tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));

        String lastAddedFile = null;

        while (tw.next()) {
            String path = tw.getPathString();

            WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
            if (tw.getTree(0, DirCacheIterator.class) == null && f != null && f.isEntryIgnored()) {
                // file is not in index but is ignored, do nothing
            }
            // In case of an existing merge conflict the
            // DirCacheBuildIterator iterates over all stages of
            // this path, we however want to add only one
            // new DirCacheEntry per path.
            else if (!(path.equals(lastAddedFile))) {
                if (all || !(update && tw.getTree(0, DirCacheIterator.class) == null)) {
                    c = tw.getTree(0, DirCacheIterator.class);
                    if (f != null) { // the file exists
                        long sz = f.getEntryLength();
                        DirCacheEntry entry = new DirCacheEntry(path);
                        if (c == null || c.getDirCacheEntry() == null
                                || !c.getDirCacheEntry().isAssumeValid()) {
                            FileMode mode = f.getIndexFileMode(c);
                            entry.setFileMode(mode);

                            if (FileMode.GITLINK != mode) {
                                entry.setLength(sz);
                                entry.setLastModified(f.getEntryLastModified());
                                long contentSize = f.getEntryContentLength();
                                InputStream in = f.openEntryStream();
                                try {
                                    entry.setObjectId(inserter.insert(Constants.OBJ_BLOB, contentSize, in));
                                } finally {
                                    in.close();
                                }
                            } else
                                entry.setObjectId(f.getEntryObjectId());
                            builder.add(entry);
                            lastAddedFile = path;
                        } else {
                            builder.add(c.getDirCacheEntry());
                        }

                    } else if (c != null && (!(all || update) || FileMode.GITLINK == c.getEntryFileMode()))
                        builder.add(c.getDirCacheEntry());
                }
            }
        }
        inserter.flush();
        builder.commit();
        setCallable(false);
    } catch (IOException e) {
        throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
    } finally {
        if (dc != null)
            dc.unlock();
    }

    return dc;
}