Example usage for org.eclipse.jgit.lib ObjectInserter idFor

List of usage examples for org.eclipse.jgit.lib ObjectInserter idFor

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectInserter idFor.

Prototype

public ObjectId idFor(int type, byte[] data) 

Source Link

Document

Compute the name of an object, without inserting it.

Usage

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/*  www.j a  v a  2s .  com*/
 */
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.microsoft.gittf.core.util.DirectoryUtil.java

License:Open Source License

private static String getUniqueAbbreviatedFolderName(final Repository repository, GUID guid) {
    ObjectInserter objIns = null;
    try {/*from   w  w  w.  j  a v  a  2  s . c  om*/
        objIns = repository.newObjectInserter();

        return ObjectIdUtil.abbreviate(repository, objIns.idFor(OBJ_BLOB, guid.getGUIDBytes()));
    } catch (Exception e) {
        return guid.getGUIDString();
    } finally {
        if (objIns != null) {
            objIns.release();
        }
    }

}

From source file:org.eclipse.mylyn.internal.gerrit.ui.egit.GitFileRevisionUtils.java

License:Open Source License

public static IFileRevision getFileRevision(final Repository repository, final IFileVersion reviewFileVersion) {
    IFileRevision gitFileRevision = null;

    if (reviewFileVersion != null && reviewFileVersion.getPath() != null) {
        //Get SHA-1 for the file revision to look for the correct file revision in the Git repository
        ObjectInserter inserter = repository.newObjectInserter();
        String id = inserter.idFor(Constants.OBJ_BLOB, CompareUtil.getContent(reviewFileVersion)).getName();
        inserter.release();//from  w  w  w . j a  v a  2 s. co m
        if (id != null) {
            final ObjectId objId = ObjectId.fromString(id);
            if (objId != null) {
                final IPath path = Path.fromPortableString(reviewFileVersion.getPath());
                gitFileRevision = new org.eclipse.team.core.history.provider.FileRevision() {

                    public IFileRevision withAllProperties(IProgressMonitor monitor) throws CoreException {
                        return this;
                    }

                    public boolean isPropertyMissing() {
                        return false;
                    }

                    public IStorage getStorage(IProgressMonitor monitor) throws CoreException {
                        return getFileRevisionStorage(null, repository, path, objId);
                    }

                    public String getName() {
                        return path.lastSegment();
                    }
                };
            }
        }
    }
    return gitFileRevision;
}