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

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

Introduction

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

Prototype

public int getEntryRawMode() 

Source Link

Document

Get the file mode of the current entry as bits.

Usage

From source file:com.mangosolutions.rcloud.rawgist.repository.git.BareCommitCommand.java

private DirCache createTemporaryIndex(ObjectId headId, DirCache index, RevWalk rw) throws IOException {
    ObjectInserter inserter = null;//from w  w w.  j av a2 s  .c  o m

    // get DirCacheBuilder for existing index
    DirCacheBuilder existingBuilder = index.builder();

    // get DirCacheBuilder for newly created in-core index to build a
    // temporary index for this commit
    DirCache inCoreIndex = DirCache.newInCore();
    DirCacheBuilder tempBuilder = inCoreIndex.builder();

    onlyProcessed = new boolean[only.size()];
    boolean emptyCommit = true;

    try (TreeWalk treeWalk = new TreeWalk(repo)) {
        treeWalk.setOperationType(OperationType.CHECKIN_OP);
        int dcIdx = treeWalk.addTree(new DirCacheBuildIterator(existingBuilder));

        FileModeStrategy fileModeStrategy = this.getRepository().getConfig().get(WorkingTreeOptions.KEY)
                .isDirNoGitLinks() ? NoGitlinksStrategy.INSTANCE : DefaultFileModeStrategy.INSTANCE;

        FileTreeIterator fti = new FileTreeIterator(this.workingFolder, this.getRepository().getFS(),
                this.getRepository().getConfig().get(WorkingTreeOptions.KEY), fileModeStrategy);

        fti.setDirCacheIterator(treeWalk, 0);
        int fIdx = treeWalk.addTree(fti);
        int hIdx = -1;
        if (headId != null) {
            hIdx = treeWalk.addTree(rw.parseTree(headId));
        }
        treeWalk.setRecursive(true);

        String lastAddedFile = null;
        while (treeWalk.next()) {
            String path = treeWalk.getPathString();
            // check if current entry's path matches a specified path
            int pos = lookupOnly(path);

            CanonicalTreeParser hTree = null;
            if (hIdx != -1) {
                hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
            }

            DirCacheIterator dcTree = treeWalk.getTree(dcIdx, DirCacheIterator.class);

            if (pos >= 0) {
                // include entry in commit

                FileTreeIterator fTree = treeWalk.getTree(fIdx, FileTreeIterator.class);

                // check if entry refers to a tracked file
                boolean tracked = dcTree != null || hTree != null;
                if (!tracked) {
                    continue;
                }

                // for an unmerged path, DirCacheBuildIterator will yield 3
                // entries, we only want to add one
                if (path.equals(lastAddedFile)) {
                    continue;
                }

                lastAddedFile = path;

                if (fTree != null) {
                    // create a new DirCacheEntry with data retrieved from
                    // disk
                    final DirCacheEntry dcEntry = new DirCacheEntry(path);
                    long entryLength = fTree.getEntryLength();
                    dcEntry.setLength(entryLength);
                    dcEntry.setLastModified(fTree.getEntryLastModified());
                    dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));

                    boolean objectExists = (dcTree != null && fTree.idEqual(dcTree))
                            || (hTree != null && fTree.idEqual(hTree));
                    if (objectExists) {
                        dcEntry.setObjectId(fTree.getEntryObjectId());
                    } else {
                        if (FileMode.GITLINK.equals(dcEntry.getFileMode())) {
                            dcEntry.setObjectId(fTree.getEntryObjectId());
                        } else {
                            // insert object
                            if (inserter == null) {
                                inserter = repo.newObjectInserter();
                            }
                            long contentLength = fTree.getEntryContentLength();
                            InputStream inputStream = fTree.openEntryStream();
                            try {
                                dcEntry.setObjectId(
                                        inserter.insert(Constants.OBJ_BLOB, contentLength, inputStream));
                            } finally {
                                inputStream.close();
                            }
                        }
                    }

                    // add to existing index
                    existingBuilder.add(dcEntry);
                    // add to temporary in-core index
                    tempBuilder.add(dcEntry);

                    if (emptyCommit && (hTree == null || !hTree.idEqual(fTree)
                            || hTree.getEntryRawMode() != fTree.getEntryRawMode())) {
                        // this is a change
                        emptyCommit = false;
                    }
                } else {
                    // if no file exists on disk, neither add it to
                    // index nor to temporary in-core index

                    if (emptyCommit && hTree != null) {
                        // this is a change
                        emptyCommit = false;
                    }
                }

                // keep track of processed path
                onlyProcessed[pos] = true;
            } else {
                // add entries from HEAD for all other paths
                if (hTree != null) {
                    // create a new DirCacheEntry with data retrieved from
                    // HEAD
                    final DirCacheEntry dcEntry = new DirCacheEntry(path);
                    dcEntry.setObjectId(hTree.getEntryObjectId());
                    dcEntry.setFileMode(hTree.getEntryFileMode());

                    // add to temporary in-core index
                    tempBuilder.add(dcEntry);
                }

                // preserve existing entry in index
                if (dcTree != null) {
                    existingBuilder.add(dcTree.getDirCacheEntry());
                }
            }
        }
    }

    // there must be no unprocessed paths left at this point; otherwise an
    // untracked or unknown path has been specified
    for (int i = 0; i < onlyProcessed.length; i++) {
        if (!onlyProcessed[i]) {
            throw new JGitInternalException(
                    MessageFormat.format(JGitText.get().entryNotFoundByPath, only.get(i)));
        }
    }

    // there must be at least one change
    if (emptyCommit) {
        // Would like to throw a EmptyCommitException. But this would break the API
        // TODO(ch): Change this in the next release
        //         throw new JGitInternalException(JGitText.get().emptyCommit);
    }

    // update index
    existingBuilder.commit();
    // finish temporary in-core index used for this commit
    tempBuilder.finish();
    return inCoreIndex;
}

From source file:org.eclipse.egit.core.internal.storage.TreeParserResourceVariant.java

License:Open Source License

/**
 * Constructs a resource variant corresponding to the current entry of the
 * given CanonicalTreeParser./*ww  w  .  j a va  2  s.c  o m*/
 *
 * @param repository
 *            Repository from which this CanonicalTreeParser was created.
 * @param treeParser
 *            A CanonicalTreeParser to retrieve information from. This will
 *            only read information about the current entry on which this
 *            parser is positioned and will not change its state.
 * @return The created variant.
 */
public static TreeParserResourceVariant create(Repository repository, CanonicalTreeParser treeParser) {
    final String path = treeParser.getEntryPathString();
    final boolean isContainer = FileMode.TREE.equals(treeParser.getEntryFileMode());
    final ObjectId objectId = treeParser.getEntryObjectId();
    final int rawMode = treeParser.getEntryRawMode();

    return new TreeParserResourceVariant(repository, path, isContainer, objectId, rawMode);
}

From source file:org.eclipse.emf.compare.egit.internal.storage.TreeParserResourceVariant.java

License:Open Source License

/**
 * Constructs a resource variant corresponding to the current entry of the given CanonicalTreeParser.
 *
 * @param repository//w w  w .  j ava 2  s  . c o m
 *            Repository from which this CanonicalTreeParser was created.
 * @param treeParser
 *            A CanonicalTreeParser to retrieve information from. This will only read information about
 *            the current entry on which this parser is positioned and will not change its state.
 * @param workspacePath
 *            The workspace-relative path of this resource.
 * @return The created variant.
 */
public static TreeParserResourceVariant create(Repository repository, CanonicalTreeParser treeParser,
        IPath workspacePath) {
    final String path = treeParser.getEntryPathString();
    final boolean isContainer = FileMode.TREE.equals(treeParser.getEntryFileMode());
    final ObjectId objectId = treeParser.getEntryObjectId();
    final int rawMode = treeParser.getEntryRawMode();

    return new TreeParserResourceVariant(repository, path, workspacePath, isContainer, objectId, rawMode);
}