Search in sources :

Example 1 with DirCacheBuildIterator

use of org.eclipse.jgit.dircache.DirCacheBuildIterator in project VoxelGamesLibv2 by VoxelGamesLib.

the class CustomAddCommand method call.

/**
 * 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.
 *
 * @return the DirCache after Add
 */
@Override
@Nonnull
public DirCache call() throws GitAPIException {
    if (filepatterns.isEmpty())
        throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
    checkCallable();
    DirCache dc = null;
    // $NON-NLS-1$
    boolean addAll = filepatterns.contains(".");
    try (ObjectInserter inserter = repo.newObjectInserter();
        NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) {
        tw.setOperationType(OperationType.CHECKIN_OP);
        dc = repo.lockDirCache();
        DirCacheBuilder builder = dc.builder();
        tw.addTree(new DirCacheBuildIterator(builder));
        if (workingTreeIterator == null)
            workingTreeIterator = new FileTreeIterator(repo);
        workingTreeIterator.setDirCacheIterator(tw, 0);
        tw.addTree(workingTreeIterator);
        if (!addAll)
            tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
        byte[] lastAdded = null;
        while (tw.next()) {
            DirCacheIterator c = tw.getTree(0, DirCacheIterator.class);
            WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
            // if (c == null && f != null && f.isEntryIgnored()) {
            // // file is not in index but is ignored, do nothing
            // continue;
            // } else if (c == null && update) {
            // // Only update of existing entries was requested.
            // continue;
            // }
            // JUST FUCK YOU
            DirCacheEntry entry = c != null ? c.getDirCacheEntry() : null;
            if (entry != null && entry.getStage() > 0 && lastAdded != null && lastAdded.length == tw.getPathLength() && tw.isPathPrefix(lastAdded, lastAdded.length) == 0) {
                // new DirCacheEntry per path.
                continue;
            }
            if (tw.isSubtree() && !tw.isDirectoryFileConflict()) {
                tw.enterSubtree();
                continue;
            }
            if (f == null) {
                // working tree file does not exist
                if (entry != null && (!update || GITLINK == entry.getFileMode())) {
                    builder.add(entry);
                }
                continue;
            }
            if (entry != null && entry.isAssumeValid()) {
                // Index entry is marked assume valid. Even though
                // the user specified the file to be added JGit does
                // not consider the file for addition.
                builder.add(entry);
                continue;
            }
            if ((f.getEntryRawMode() == TYPE_TREE && f.getIndexFileMode(c) != FileMode.GITLINK) || (f.getEntryRawMode() == TYPE_GITLINK && f.getIndexFileMode(c) == FileMode.TREE)) {
                // Index entry exists and is symlink, gitlink or file,
                // otherwise the tree would have been entered above.
                // Replace the index entry by diving into tree of files.
                tw.enterSubtree();
                continue;
            }
            byte[] path = tw.getRawPath();
            if (entry == null || entry.getStage() > 0) {
                entry = new DirCacheEntry(path);
            }
            FileMode mode = f.getIndexFileMode(c);
            entry.setFileMode(mode);
            if (GITLINK != mode) {
                entry.setLength(f.getEntryLength());
                entry.setLastModified(f.getEntryLastModified());
                long len = f.getEntryContentLength();
                // inserting. TODO: Fix this by using Buffers.
                try (InputStream in = f.openEntryStream()) {
                    ObjectId id = inserter.insert(OBJ_BLOB, len, in);
                    entry.setObjectId(id);
                }
            } else {
                entry.setLength(0);
                entry.setLastModified(0);
                entry.setObjectId(f.getEntryObjectId());
            }
            builder.add(entry);
            lastAdded = path;
        }
        inserter.flush();
        builder.commit();
        setCallable(false);
    } catch (IOException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause instanceof FilterFailedException)
            throw (FilterFailedException) cause;
        throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
    } finally {
        if (dc != null)
            dc.unlock();
    }
    return dc;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) WorkingTreeIterator(org.eclipse.jgit.treewalk.WorkingTreeIterator) ObjectId(org.eclipse.jgit.lib.ObjectId) InputStream(java.io.InputStream) NoFilepatternException(org.eclipse.jgit.api.errors.NoFilepatternException) DirCacheBuildIterator(org.eclipse.jgit.dircache.DirCacheBuildIterator) IOException(java.io.IOException) NameConflictTreeWalk(org.eclipse.jgit.treewalk.NameConflictTreeWalk) DirCache(org.eclipse.jgit.dircache.DirCache) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) FilterFailedException(org.eclipse.jgit.api.errors.FilterFailedException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) Nonnull(javax.annotation.Nonnull)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Nonnull (javax.annotation.Nonnull)1 FilterFailedException (org.eclipse.jgit.api.errors.FilterFailedException)1 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)1 NoFilepatternException (org.eclipse.jgit.api.errors.NoFilepatternException)1 DirCache (org.eclipse.jgit.dircache.DirCache)1 DirCacheBuildIterator (org.eclipse.jgit.dircache.DirCacheBuildIterator)1 DirCacheBuilder (org.eclipse.jgit.dircache.DirCacheBuilder)1 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)1 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)1 FileMode (org.eclipse.jgit.lib.FileMode)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)1 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)1 NameConflictTreeWalk (org.eclipse.jgit.treewalk.NameConflictTreeWalk)1 WorkingTreeIterator (org.eclipse.jgit.treewalk.WorkingTreeIterator)1