Search in sources :

Example 1 with DirCacheEditor

use of org.eclipse.jgit.dircache.DirCacheEditor in project gerrit by GerritCodeReview.

the class SubmoduleOp method composeGitlinksCommit.

/** Create a separate gitlink commit */
public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber) throws IOException, SubmoduleException {
    OpenRepo or;
    try {
        or = orm.getRepo(subscriber.getParentKey());
    } catch (NoSuchProjectException | IOException e) {
        throw new SubmoduleException("Cannot access superproject", e);
    }
    CodeReviewCommit currentCommit;
    if (branchTips.containsKey(subscriber)) {
        currentCommit = branchTips.get(subscriber);
    } else {
        Ref r = or.repo.exactRef(subscriber.get());
        if (r == null) {
            throw new SubmoduleException("The branch was probably deleted from the subscriber repository");
        }
        currentCommit = or.rw.parseCommit(r.getObjectId());
        addBranchTip(subscriber, currentCommit);
    }
    StringBuilder msgbuf = new StringBuilder("");
    PersonIdent author = null;
    DirCache dc = readTree(or.rw, currentCommit);
    DirCacheEditor ed = dc.editor();
    for (SubmoduleSubscription s : targets.get(subscriber)) {
        RevCommit newCommit = updateSubmodule(dc, ed, msgbuf, s);
        if (newCommit != null) {
            if (author == null) {
                author = newCommit.getAuthorIdent();
            } else if (!author.equals(newCommit.getAuthorIdent())) {
                author = myIdent;
            }
        }
    }
    ed.finish();
    ObjectId newTreeId = dc.writeTree(or.ins);
    // Gitlinks are already in the branch, return null
    if (newTreeId.equals(currentCommit.getTree())) {
        return null;
    }
    CommitBuilder commit = new CommitBuilder();
    commit.setTreeId(newTreeId);
    commit.setParentId(currentCommit);
    StringBuilder commitMsg = new StringBuilder("Update git submodules\n\n");
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        commitMsg.append(msgbuf);
    }
    commit.setMessage(commitMsg.toString());
    commit.setAuthor(author);
    commit.setCommitter(myIdent);
    ObjectId id = or.ins.insert(commit);
    return or.rw.parseCommit(id);
}
Also used : NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) DirCache(org.eclipse.jgit.dircache.DirCache) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with DirCacheEditor

use of org.eclipse.jgit.dircache.DirCacheEditor in project gerrit by GerritCodeReview.

the class VersionedMetaData method saveFile.

protected void saveFile(String fileName, byte[] raw) throws IOException {
    DirCacheEditor editor = newTree.editor();
    if (raw != null && 0 < raw.length) {
        final ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, raw);
        editor.add(new PathEdit(fileName) {

            @Override
            public void apply(DirCacheEntry ent) {
                ent.setFileMode(FileMode.REGULAR_FILE);
                ent.setObjectId(blobId);
            }
        });
    } else {
        editor.add(new DeletePath(fileName));
    }
    editor.finish();
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) PathEdit(org.eclipse.jgit.dircache.DirCacheEditor.PathEdit) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) DeletePath(org.eclipse.jgit.dircache.DirCacheEditor.DeletePath)

Example 3 with DirCacheEditor

use of org.eclipse.jgit.dircache.DirCacheEditor in project gerrit by GerritCodeReview.

the class SubmoduleOp method composeGitlinksCommit.

/** Amend an existing commit with gitlink updates */
public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber, CodeReviewCommit currentCommit) throws IOException, SubmoduleException {
    OpenRepo or;
    try {
        or = orm.getRepo(subscriber.getParentKey());
    } catch (NoSuchProjectException | IOException e) {
        throw new SubmoduleException("Cannot access superproject", e);
    }
    StringBuilder msgbuf = new StringBuilder("");
    DirCache dc = readTree(or.rw, currentCommit);
    DirCacheEditor ed = dc.editor();
    for (SubmoduleSubscription s : targets.get(subscriber)) {
        updateSubmodule(dc, ed, msgbuf, s);
    }
    ed.finish();
    ObjectId newTreeId = dc.writeTree(or.ins);
    // Gitlinks are already updated, just return the commit
    if (newTreeId.equals(currentCommit.getTree())) {
        return currentCommit;
    }
    or.rw.parseBody(currentCommit);
    CommitBuilder commit = new CommitBuilder();
    commit.setTreeId(newTreeId);
    commit.setParentIds(currentCommit.getParents());
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        // TODO(czhen): handle cherrypick footer
        commit.setMessage(currentCommit.getFullMessage() + "\n\n* submodules:\n" + msgbuf.toString());
    } else {
        commit.setMessage(currentCommit.getFullMessage());
    }
    commit.setAuthor(currentCommit.getAuthorIdent());
    commit.setCommitter(myIdent);
    ObjectId id = or.ins.insert(commit);
    CodeReviewCommit newCommit = or.rw.parseCommit(id);
    newCommit.copyFrom(currentCommit);
    return newCommit;
}
Also used : DirCache(org.eclipse.jgit.dircache.DirCache) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) IOException(java.io.IOException) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor)

Example 4 with DirCacheEditor

use of org.eclipse.jgit.dircache.DirCacheEditor in project gerrit by GerritCodeReview.

the class TreeCreator method applyPathEdits.

private static void applyPathEdits(DirCache tree, List<DirCacheEditor.PathEdit> pathEdits) {
    DirCacheEditor dirCacheEditor = tree.editor();
    pathEdits.forEach(dirCacheEditor::add);
    dirCacheEditor.finish();
}
Also used : DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor)

Aggregations

DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)4 ObjectId (org.eclipse.jgit.lib.ObjectId)3 SubmoduleSubscription (com.google.gerrit.reviewdb.client.SubmoduleSubscription)2 OpenRepo (com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo)2 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)2 IOException (java.io.IOException)2 DirCache (org.eclipse.jgit.dircache.DirCache)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)1 DeletePath (org.eclipse.jgit.dircache.DirCacheEditor.DeletePath)1 PathEdit (org.eclipse.jgit.dircache.DirCacheEditor.PathEdit)1 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)1 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)1 PersonIdent (org.eclipse.jgit.lib.PersonIdent)1 Ref (org.eclipse.jgit.lib.Ref)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1