Example usage for org.eclipse.jgit.dircache DirCacheEntry getRawMode

List of usage examples for org.eclipse.jgit.dircache DirCacheEntry getRawMode

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCacheEntry getRawMode.

Prototype

public int getRawMode() 

Source Link

Document

Obtain the raw org.eclipse.jgit.lib.FileMode bits for this entry.

Usage

From source file:com.bacoder.scmtools.git.internal.CloneAndProcessDirCacheCheckout.java

License:Apache License

@Override
protected boolean checkoutEntries() throws IOException {
    Map<String, ObjectId> updated = getUpdated();
    List<String> paths = Lists.newArrayList(updated.keySet());
    Collections.sort(paths);/*from   w w w  .j a  v  a2  s  .com*/

    ObjectReader objectReader = repository.getObjectDatabase().newReader();
    try {
        for (String path : paths) {
            DirCacheEntry entry = dirCache.getEntry(path);
            if (FileMode.GITLINK.equals(entry.getRawMode()))
                continue;

            boolean isSubmoduleConfig = path.equals(Constants.DOT_GIT_MODULES);
            ObjectId objectId = updated.get(path);
            if (isSubmoduleConfig) {
                ObjectLoader loader = objectReader.open(objectId);
                byte[] data = loader.getBytes();
                submodulesConfig = data;
            }
            if (processor != null) {
                GitEntry gitEntry = new GitEntry(repository, objectId, pathPrefix, path);
                try {
                    processor.process(gitEntry);
                } catch (Exception e) {
                    throw new InternalRuntimeException(e);
                }
            }
        }
        // commit the index builder - a new index is persisted
        if (!getBuilder().commit())
            throw new IndexWriteException();
    } finally {
        objectReader.release();
    }
    return true;
}

From source file:com.google.gerrit.server.edit.ChangeEditModifier.java

License:Apache License

private static ObjectId writeNewTree(TreeOperation op, RevWalk rw, ObjectInserter ins, RevCommit prevEdit,
        ObjectReader reader, String fileName, @Nullable String newFile, @Nullable final ObjectId content)
        throws IOException {
    DirCache newTree = readTree(reader, prevEdit);
    DirCacheEditor dce = newTree.editor();
    switch (op) {
    case DELETE_ENTRY:
        dce.add(new DeletePath(fileName));
        break;//from   w  w w  .ja  va 2s .c om

    case RENAME_ENTRY:
        rw.parseHeaders(prevEdit);
        TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), fileName, prevEdit.getTree());
        if (tw != null) {
            dce.add(new DeletePath(fileName));
            addFileToCommit(newFile, dce, tw);
        }
        break;

    case CHANGE_ENTRY:
        checkNotNull(content, "new content required");
        dce.add(new PathEdit(fileName) {
            @Override
            public void apply(DirCacheEntry ent) {
                if (ent.getRawMode() == 0) {
                    ent.setFileMode(FileMode.REGULAR_FILE);
                }
                ent.setObjectId(content);
            }
        });
        break;

    case RESTORE_ENTRY:
        if (prevEdit.getParentCount() == 0) {
            dce.add(new DeletePath(fileName));
            break;
        }

        RevCommit base = prevEdit.getParent(0);
        rw.parseHeaders(base);
        tw = TreeWalk.forPath(rw.getObjectReader(), fileName, base.getTree());
        if (tw == null) {
            dce.add(new DeletePath(fileName));
            break;
        }

        addFileToCommit(fileName, dce, tw);
        break;
    }
    dce.finish();
    return newTree.writeTree(ins);
}

From source file:org.eclipse.egit.core.internal.merge.ResourceVariantTest.java

License:Open Source License

@Test
public void testIndexVariants() throws Exception {
    File file1 = testRepo.createFile(iProject, "file1");
    File file2 = testRepo.createFile(iProject, "file2");
    IFile iFile1 = testRepo.getIFile(iProject, file1);
    IFile iFile2 = testRepo.getIFile(iProject, file2);

    setupUnconflictingBranches();/*from ww  w.j  av  a2 s.  c  om*/

    List<String> possibleNames = Arrays.asList(iFile1.getName(), iFile2.getName());
    DirCache cache = repo.readDirCache();
    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);

        AbstractGitResourceVariant variant = IndexResourceVariant.create(repo, entry);

        assertEquals(entry.getObjectId().getName(), variant.getContentIdentifier());
        assertTrue(possibleNames.contains(variant.getName()));
        assertEquals(entry.getObjectId(), variant.getObjectId());
        assertEquals(entry.getRawMode(), variant.getRawMode());
        if (iFile1.getName().equals(variant.getName())) {
            assertContentEquals(variant, INITIAL_CONTENT_1 + MASTER_CHANGE);
        } else {
            assertContentEquals(variant, INITIAL_CONTENT_2 + MASTER_CHANGE);
        }
    }

    testRepo.checkoutBranch(BRANCH);

    cache = repo.readDirCache();
    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);

        AbstractGitResourceVariant variant = IndexResourceVariant.create(repo, entry);
        assertEquals(entry.getObjectId().getName(), variant.getContentIdentifier());
        assertTrue(possibleNames.contains(variant.getName()));
        assertEquals(entry.getObjectId(), variant.getObjectId());
        assertEquals(entry.getRawMode(), variant.getRawMode());
        if (iFile1.getName().equals(variant.getName())) {
            assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_1);
        } else {
            assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_2);
        }
    }
}

From source file:org.eclipse.egit.core.internal.merge.ResourceVariantTest.java

License:Open Source License

@Test
public void testIndexVariantsConflict() throws Exception {
    File file1 = testRepo.createFile(iProject, "file1");
    IFile iFile1 = testRepo.getIFile(iProject, file1);

    setupConflictingBranches();/*from   w  ww. j ava 2 s.c om*/
    // end setup

    // create a conflict to force multiple stages
    new MergeOperation(repo, BRANCH).execute(null);

    DirCache cache = repo.readDirCache();
    // 3 stages for file 1, 2 stages for file 2
    assertEquals(5, cache.getEntryCount());
    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);

        AbstractGitResourceVariant variant = IndexResourceVariant.create(repo, entry);
        assertEquals(entry.getObjectId().getName(), variant.getContentIdentifier());
        assertEquals(entry.getObjectId(), variant.getObjectId());
        assertEquals(entry.getRawMode(), variant.getRawMode());
        if (iFile1.getName().equals(variant.getName())) {
            switch (entry.getStage()) {
            case DirCacheEntry.STAGE_1:
                assertContentEquals(variant, INITIAL_CONTENT_1);
                break;
            case DirCacheEntry.STAGE_2:
                assertContentEquals(variant, INITIAL_CONTENT_1 + MASTER_CHANGE);
                break;
            case DirCacheEntry.STAGE_3:
                assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_1);
                break;
            case DirCacheEntry.STAGE_0:
            default:
                fail("Unexpected entry stage " + entry.getStage() + " in the index for file "
                        + entry.getPathString());
                break;
            }
        } else {
            switch (entry.getStage()) {
            case DirCacheEntry.STAGE_2:
                assertContentEquals(variant, INITIAL_CONTENT_2 + MASTER_CHANGE);
                break;
            case DirCacheEntry.STAGE_3:
                assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_2);
                break;
            case DirCacheEntry.STAGE_0:
            case DirCacheEntry.STAGE_1:
            default:
                fail("Unexpected entry stage " + entry.getStage() + " in the index for file "
                        + entry.getPathString());
                break;
            }
        }
    }
}

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

License:Open Source License

/**
 * Constructs a resource variant corresponding to the given DirCache entry.
 *
 * @param repository/*from w  w  w . j a v  a2 s  .co  m*/
 *            Repository from which this DirCacheEntry was extracted.
 * @param entry
 *            The DirCacheEntry for which content we need an
 *            IResourceVariant.
 * @return The created variant.
 */
public static IndexResourceVariant create(Repository repository, DirCacheEntry entry) {
    final String path = entry.getPathString();
    final boolean isContainer = FileMode.TREE.equals(entry.getFileMode());
    final ObjectId objectId = entry.getObjectId();
    final int rawMode = entry.getRawMode();

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

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

License:Open Source License

/**
 * Constructs the resource variant trees by iterating over the given repository's DirCache entries.
 *
 * @param repository//w w  w.  jav  a 2 s . c  o  m
 *            The repository which DirCache info we need to cache as IResourceVariantTrees.
 * @param useWorkspace
 *            Whether we should use local data instead of what's in the index for our side.
 * @throws IOException
 *             if we somehow cannot read the DirCache.
 */
public DirCacheResourceVariantTreeProvider(Repository repository, boolean useWorkspace) throws IOException {
    final DirCache cache = repository.readDirCache();
    final GitResourceVariantCache baseCache = new GitResourceVariantCache();
    final GitResourceVariantCache sourceCache = new GitResourceVariantCache();
    final GitResourceVariantCache remoteCache = new GitResourceVariantCache();

    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);
        final IResource resource = ModelEGitResourceUtil.getResourceHandleForLocation(repository,
                entry.getPathString(), FileMode.fromBits(entry.getRawMode()) == FileMode.TREE);
        // Resource variants only make sense for IResources. Do not consider
        // files outside of the workspace or otherwise non accessible.
        if (resource == null || resource.getProject() == null || !resource.getProject().isAccessible()) {
            continue;
        }
        switch (entry.getStage()) {
        case DirCacheEntry.STAGE_0:
            if (useWorkspace) {
                sourceCache.setVariant(resource, new GitLocalResourceVariant(resource));
            } else {
                sourceCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
            }
            baseCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
            remoteCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
            break;
        case DirCacheEntry.STAGE_1:
            baseCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
            break;
        case DirCacheEntry.STAGE_2:
            if (useWorkspace) {
                sourceCache.setVariant(resource, new GitLocalResourceVariant(resource));
            } else {
                sourceCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
            }
            break;
        case DirCacheEntry.STAGE_3:
            remoteCache.setVariant(resource, IndexResourceVariant.create(repository, entry));
            break;
        default:
            throw new IllegalStateException("Invalid stage: " + entry.getStage()); //$NON-NLS-1$
        }
    }

    baseTree = new GitCachedResourceVariantTree(baseCache);
    sourceTree = new GitCachedResourceVariantTree(sourceCache);
    remoteTree = new GitCachedResourceVariantTree(remoteCache);

    roots = new LinkedHashSet<IResource>();
    roots.addAll(baseCache.getRoots());
    roots.addAll(sourceCache.getRoots());
    roots.addAll(remoteCache.getRoots());

    knownResources = new LinkedHashSet<IResource>();
    knownResources.addAll(baseCache.getKnownResources());
    knownResources.addAll(sourceCache.getKnownResources());
    knownResources.addAll(remoteCache.getKnownResources());
}

From source file:org.eclipse.emf.compare.ide.ui.tests.merge.ResourceVariantTest.java

License:Open Source License

@Test
public void testIndexVariants() throws Exception {
    File file1 = repository.createFile(iProject, "file1");
    File file2 = repository.createFile(iProject, "file2");
    IFile iFile1 = repository.getIFile(iProject, file1);
    IFile iFile2 = repository.getIFile(iProject, file2);

    setupUnconflictingBranches();/* w w  w. j a  v a  2s .  co  m*/

    List<String> possibleNames = Arrays.asList(iFile1.getName(), iFile2.getName());
    DirCache cache = repo.readDirCache();
    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);

        AbstractGitResourceVariant variant = IndexResourceVariant.create(repo, entry);

        assertEquals(entry.getObjectId().getName(), variant.getContentIdentifier());
        assertTrue(possibleNames.contains(variant.getName()));
        assertEquals(entry.getObjectId(), variant.getObjectId());
        assertEquals(entry.getRawMode(), variant.getRawMode());
        if (iFile1.getName().equals(variant.getName())) {
            assertContentEquals(variant, INITIAL_CONTENT_1 + MASTER_CHANGE);
        } else {
            assertContentEquals(variant, INITIAL_CONTENT_2 + MASTER_CHANGE);
        }
    }

    repository.checkoutBranch(BRANCH);

    cache = repo.readDirCache();
    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);

        AbstractGitResourceVariant variant = IndexResourceVariant.create(repo, entry);
        assertEquals(entry.getObjectId().getName(), variant.getContentIdentifier());
        assertTrue(possibleNames.contains(variant.getName()));
        assertEquals(entry.getObjectId(), variant.getObjectId());
        assertEquals(entry.getRawMode(), variant.getRawMode());
        if (iFile1.getName().equals(variant.getName())) {
            assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_1);
        } else {
            assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_2);
        }
    }
}

From source file:org.eclipse.emf.compare.ide.ui.tests.merge.ResourceVariantTest.java

License:Open Source License

@Test
public void testIndexVariantsConflict() throws Exception {
    File file1 = repository.createFile(iProject, "file1");
    IFile iFile1 = repository.getIFile(iProject, file1);

    setupConflictingBranches();/*from w  ww. j a v  a2 s .c o m*/
    // end setup

    // create a conflict to force multiple stages
    new MergeOperation(repo, BRANCH).execute(null);

    DirCache cache = repo.readDirCache();
    // 3 stages for file 1, 2 stages for file 2
    assertEquals(5, cache.getEntryCount());
    for (int i = 0; i < cache.getEntryCount(); i++) {
        final DirCacheEntry entry = cache.getEntry(i);

        AbstractGitResourceVariant variant = IndexResourceVariant.create(repo, entry);
        assertEquals(entry.getObjectId().getName(), variant.getContentIdentifier());
        assertEquals(entry.getObjectId(), variant.getObjectId());
        assertEquals(entry.getRawMode(), variant.getRawMode());
        if (iFile1.getName().equals(variant.getName())) {
            switch (entry.getStage()) {
            case DirCacheEntry.STAGE_1:
                assertContentEquals(variant, INITIAL_CONTENT_1);
                break;
            case DirCacheEntry.STAGE_2:
                assertContentEquals(variant, INITIAL_CONTENT_1 + MASTER_CHANGE);
                break;
            case DirCacheEntry.STAGE_3:
                assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_1);
                break;
            case DirCacheEntry.STAGE_0:
            default:
                fail("Unexpected entry stage " + entry.getStage() + " in the index for file "
                        + entry.getPathString());
                break;
            }
        } else {
            switch (entry.getStage()) {
            case DirCacheEntry.STAGE_2:
                assertContentEquals(variant, INITIAL_CONTENT_2 + MASTER_CHANGE);
                break;
            case DirCacheEntry.STAGE_3:
                assertContentEquals(variant, BRANCH_CHANGE + INITIAL_CONTENT_2);
                break;
            case DirCacheEntry.STAGE_0:
            case DirCacheEntry.STAGE_1:
            default:
                fail("Unexpected entry stage " + entry.getStage() + " in the index for file "
                        + entry.getPathString());
                break;
            }
        }
    }
}