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

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

Introduction

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

Prototype

public ObjectId getObjectId() 

Source Link

Document

Obtain the ObjectId for the entry.

Usage

From source file:com.github.kaitoy.goslings.server.dao.jgit.RepositoryDaoImpl.java

License:Open Source License

@Override
public Index getIndex(String token) {
    try {/* ww  w .j  av  a 2 s  .co  m*/
        DirCache index = resolver.getRepository(token).readDirCache();
        int numEntries = index.getEntryCount();
        List<IndexEntry> entries = new ArrayList<>(numEntries);
        for (int i = 0; i < numEntries; i++) {
            DirCacheEntry dce = index.getEntry(i);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            dce.getFileMode().copyTo(baos);
            entries.add(new IndexEntry(dce.getObjectId().getName(), dce.getPathString(), baos.toString(),
                    dce.getStage()));
        }
        return new Index(entries.toArray(new IndexEntry[numEntries]));
    } catch (NoWorkTreeException e) {
        String message = new StringBuilder().append("The repository ").append(token)
                .append(" is bare and so doesn't have index.").toString();
        LOG.error(message, e);
        throw new DaoException(message, e);
    } catch (CorruptObjectException e) {
        String message = new StringBuilder().append("Filed to get index of the repository ").append(token)
                .append(" due to an internal error.").toString();
        LOG.error(message, e);
        throw new DaoException(message, e);
    } catch (IOException e) {
        String message = new StringBuilder().append("Filed to get index of the repository ").append(token)
                .append(" due to an I/O error.").toString();
        LOG.error(message, e);
        throw new DaoException(message, e);
    }

}

From source file:com.google.gerrit.server.git.SubmoduleOp.java

License:Apache License

/**
 * Update the submodules in one branch of one repository.
 *
 * @param subscriber the branch of the repository which should be changed.
 * @param updates submodule updates which should be updated to.
 * @throws SubmoduleException//from  w ww . j  ava  2s. co  m
 */
private void updateGitlinks(ReviewDb db, Branch.NameKey subscriber, Collection<SubmoduleSubscription> updates)
        throws SubmoduleException {
    PersonIdent author = null;
    StringBuilder msgbuf = new StringBuilder("Updated git submodules\n\n");
    boolean sameAuthorForAll = true;

    try (Repository pdb = repoManager.openRepository(subscriber.getParentKey())) {
        if (pdb.getRef(subscriber.get()) == null) {
            throw new SubmoduleException("The branch was probably deleted from the subscriber repository");
        }

        DirCache dc = readTree(pdb, pdb.getRef(subscriber.get()));
        DirCacheEditor ed = dc.editor();

        for (SubmoduleSubscription s : updates) {
            try (Repository subrepo = repoManager.openRepository(s.getSubmodule().getParentKey());
                    RevWalk rw = CodeReviewCommit.newRevWalk(subrepo)) {
                Ref ref = subrepo.getRefDatabase().exactRef(s.getSubmodule().get());
                if (ref == null) {
                    ed.add(new DeletePath(s.getPath()));
                    continue;
                }

                final ObjectId updateTo = ref.getObjectId();
                RevCommit newCommit = rw.parseCommit(updateTo);

                if (author == null) {
                    author = newCommit.getAuthorIdent();
                } else if (!author.equals(newCommit.getAuthorIdent())) {
                    sameAuthorForAll = false;
                }

                DirCacheEntry dce = dc.getEntry(s.getPath());
                ObjectId oldId;
                if (dce != null) {
                    if (!dce.getFileMode().equals(FileMode.GITLINK)) {
                        log.error("Requested to update gitlink " + s.getPath() + " in "
                                + s.getSubmodule().getParentKey().get() + " but entry "
                                + "doesn't have gitlink file mode.");
                        continue;
                    }
                    oldId = dce.getObjectId();
                } else {
                    // This submodule did not exist before. We do not want to add
                    // the full submodule history to the commit message, so omit it.
                    oldId = updateTo;
                }

                ed.add(new PathEdit(s.getPath()) {
                    @Override
                    public void apply(DirCacheEntry ent) {
                        ent.setFileMode(FileMode.GITLINK);
                        ent.setObjectId(updateTo);
                    }
                });
                if (verboseSuperProject) {
                    msgbuf.append("Project: " + s.getSubmodule().getParentKey().get());
                    msgbuf.append(" " + s.getSubmodule().getShortName());
                    msgbuf.append(" " + updateTo.getName());
                    msgbuf.append("\n\n");

                    try {
                        rw.markStart(newCommit);
                        rw.markUninteresting(rw.parseCommit(oldId));
                        for (RevCommit c : rw) {
                            msgbuf.append(c.getFullMessage() + "\n\n");
                        }
                    } catch (IOException e) {
                        logAndThrowSubmoduleException(
                                "Could not perform a revwalk to " + "create superproject commit message", e);
                    }
                }
            }
        }
        ed.finish();

        if (!sameAuthorForAll || author == null) {
            author = myIdent;
        }

        ObjectInserter oi = pdb.newObjectInserter();
        ObjectId tree = dc.writeTree(oi);

        ObjectId currentCommitId = pdb.getRef(subscriber.get()).getObjectId();

        CommitBuilder commit = new CommitBuilder();
        commit.setTreeId(tree);
        commit.setParentIds(new ObjectId[] { currentCommitId });
        commit.setAuthor(author);
        commit.setCommitter(myIdent);
        commit.setMessage(msgbuf.toString());
        oi.insert(commit);
        oi.flush();

        ObjectId commitId = oi.idFor(Constants.OBJ_COMMIT, commit.build());

        final RefUpdate rfu = pdb.updateRef(subscriber.get());
        rfu.setForceUpdate(false);
        rfu.setNewObjectId(commitId);
        rfu.setExpectedOldObjectId(currentCommitId);
        rfu.setRefLogMessage("Submit to " + subscriber.getParentKey().get(), true);

        switch (rfu.update()) {
        case NEW:
        case FAST_FORWARD:
            gitRefUpdated.fire(subscriber.getParentKey(), rfu);
            changeHooks.doRefUpdatedHook(subscriber, rfu, account);
            // TODO since this is performed "in the background" no mail will be
            // sent to inform users about the updated branch
            break;

        default:
            throw new IOException(rfu.getResult().name());
        }
        // Recursive call: update subscribers of the subscriber
        updateSuperProjects(db, Sets.newHashSet(subscriber));
    } catch (IOException e) {
        throw new SubmoduleException("Cannot update gitlinks for " + subscriber.get(), e);
    }
}

From source file:com.itemis.maven.plugins.unleash.scm.providers.merge.UnleashGitMerger.java

License:Eclipse Distribution License

/**
 * adds a entry to the index builder which is a copy of the specified
 * DirCacheEntry/*from w  w  w  .  j  a  va 2  s  .  c  o m*/
 *
 * @param e
 *          the entry which should be copied
 *
 * @return the entry which was added to the index
 */
private DirCacheEntry keep(DirCacheEntry e) {
    DirCacheEntry newEntry = new DirCacheEntry(e.getPathString(), e.getStage());
    newEntry.setFileMode(e.getFileMode());
    newEntry.setObjectId(e.getObjectId());
    newEntry.setLastModified(e.getLastModified());
    newEntry.setLength(e.getLength());
    this.builder.add(newEntry);
    return newEntry;
}

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();/*w  ww  . j a va2s .c o  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);
        }
    }

    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();// w w  w.  j  a  va  2  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;
            }
        }
    }
}

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

License:Open Source License

private ObjectId locateBlobObjectId() throws CoreException {
    try {//from  www . ja v  a  2 s  .c  o  m
        DirCacheEntry entry = db.readDirCache().getEntry(path);
        if (entry == null)
            throw new CoreException(
                    Activator.error(NLS.bind(CoreText.IndexFileRevision_indexEntryNotFound, path), null));
        return entry.getObjectId();
    } catch (IOException e) {
        throw new CoreException(
                Activator.error(NLS.bind(CoreText.IndexFileRevision_errorLookingUpPath, path), e));
    }
}

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//  ww w .j av  a  2  s . c  o  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.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();/*from   w ww . j  a v  a 2 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);
        }
    }

    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 w w  .  ja v a  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.orion.server.git.objects.Index.java

License:Open Source License

public ObjectStream toObjectStream() throws IOException {
    DirCache cache = db.readDirCache();//from  w  w  w  .j a v  a 2  s . com
    DirCacheEntry entry = cache.getEntry(pattern);
    if (entry == null) {
        return null;
    }
    ObjectId blobId = entry.getObjectId();
    return db.open(blobId, Constants.OBJ_BLOB).openStream();
}