Search in sources :

Example 31 with CommitBuilder

use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.

the class ChangeNotesParserTest method writeCommit.

private RevCommit writeCommit(String body, PersonIdent author) throws Exception {
    Change change = newChange();
    ChangeNotes notes = newNotes(change).load();
    try (ObjectInserter ins = testRepo.getRepository().newObjectInserter()) {
        CommitBuilder cb = new CommitBuilder();
        cb.setParentId(notes.getRevision());
        cb.setAuthor(author);
        cb.setCommitter(new PersonIdent(serverIdent, author.getWhen()));
        cb.setTreeId(testRepo.tree());
        cb.setMessage(body);
        ObjectId id = ins.insert(cb);
        ins.flush();
        RevCommit commit = walk.parseCommit(id);
        walk.parseBody(commit);
        return commit;
    }
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Change(com.google.gerrit.reviewdb.client.Change) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 32 with CommitBuilder

use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.

the class VersionedMetaDataOnInit method save.

protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
    File path = getPath();
    if (path == null) {
        throw new IOException(project + " does not exist.");
    }
    try (Repository repo = new FileRepository(path);
        ObjectInserter i = repo.newObjectInserter();
        ObjectReader r = repo.newObjectReader();
        RevWalk rw = new RevWalk(r)) {
        inserter = i;
        reader = r;
        RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
        newTree = readTree(srcTree);
        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(ident);
        commit.setCommitter(ident);
        commit.setMessage(msg);
        onSave(commit);
        ObjectId res = newTree.writeTree(inserter);
        if (res.equals(srcTree)) {
            return;
        }
        commit.setTreeId(res);
        if (revision != null) {
            commit.addParentId(revision);
        }
        ObjectId newRevision = inserter.insert(commit);
        updateRef(repo, ident, newRevision, "commit: " + msg);
        revision = rw.parseCommit(newRevision);
    } finally {
        inserter = null;
        reader = null;
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 33 with CommitBuilder

use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.

the class ChangeEditModifier method createCommit.

private ObjectId createCommit(Repository repository, RevCommit basePatchSetCommit, ObjectId tree, String commitMessage, Timestamp timestamp) throws IOException {
    try (ObjectInserter objectInserter = repository.newObjectInserter()) {
        CommitBuilder builder = new CommitBuilder();
        builder.setTreeId(tree);
        builder.setParentIds(basePatchSetCommit.getParents());
        builder.setAuthor(basePatchSetCommit.getAuthorIdent());
        builder.setCommitter(getCommitterIdent(timestamp));
        builder.setMessage(commitMessage);
        ObjectId newCommitId = objectInserter.insert(builder);
        objectInserter.flush();
        return newCommitId;
    }
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder)

Example 34 with CommitBuilder

use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.

the class AbstractChangeUpdate method apply.

/**
   * Apply this update to the given inserter.
   *
   * @param rw walk for reading back any objects needed for the update.
   * @param ins inserter to write to; callers should not flush.
   * @param curr the current tip of the branch prior to this update.
   * @return commit ID produced by inserting this update's commit, or null if this update is a no-op
   *     and should be skipped. The zero ID is a valid return value, and indicates the ref should be
   *     deleted.
   * @throws OrmException if a Gerrit-level error occurred.
   * @throws IOException if a lower-level error occurred.
   */
final ObjectId apply(RevWalk rw, ObjectInserter ins, ObjectId curr) throws OrmException, IOException {
    if (isEmpty()) {
        return null;
    }
    // Allow this method to proceed even if migration.failChangeWrites() = true.
    // This may be used by an auto-rebuilding step that the caller does not plan
    // to actually store.
    checkArgument(rw.getObjectReader().getCreatedFromInserter() == ins);
    checkNotReadOnly();
    ObjectId z = ObjectId.zeroId();
    CommitBuilder cb = applyImpl(rw, ins, curr);
    if (cb == null) {
        result = z;
        // Impl intends to delete the ref.
        return z;
    } else if (cb == NO_OP_UPDATE) {
        // Impl is a no-op.
        return null;
    }
    cb.setAuthor(authorIdent);
    cb.setCommitter(new PersonIdent(serverIdent, when));
    if (!curr.equals(z)) {
        cb.setParentId(curr);
    } else {
        // Ref is currently nonexistent, commit has no parents.
        cb.setParentIds();
    }
    if (cb.getTreeId() == null) {
        if (curr.equals(z)) {
            // No parent, assume empty tree.
            cb.setTreeId(emptyTree(ins));
        } else {
            RevCommit p = rw.parseCommit(curr);
            // Copy tree from parent.
            cb.setTreeId(p.getTree());
        }
    }
    result = ins.insert(cb);
    return result;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) PersonIdent(org.eclipse.jgit.lib.PersonIdent) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 35 with CommitBuilder

use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.

the class NotesBranchUtil method createCommit.

private RevCommit createCommit(NoteMap map, PersonIdent author, String message, RevCommit... parents) throws IOException {
    CommitBuilder b = new CommitBuilder();
    b.setTreeId(map.writeTree(inserter));
    b.setAuthor(author != null ? author : gerritIdent);
    b.setCommitter(gerritIdent);
    if (parents.length > 0) {
        b.setParentIds(parents);
    }
    b.setMessage(message);
    ObjectId commitId = inserter.insert(b);
    inserter.flush();
    return revWalk.parseCommit(commitId);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder)

Aggregations

CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)38 ObjectId (org.eclipse.jgit.lib.ObjectId)25 PersonIdent (org.eclipse.jgit.lib.PersonIdent)19 RefUpdate (org.eclipse.jgit.lib.RefUpdate)14 RevCommit (org.eclipse.jgit.revwalk.RevCommit)13 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)12 IOException (java.io.IOException)9 RevWalk (org.eclipse.jgit.revwalk.RevWalk)9 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)7 DirCache (org.eclipse.jgit.dircache.DirCache)6 Result (org.eclipse.jgit.lib.RefUpdate.Result)6 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)5 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)4 ObjectReader (org.eclipse.jgit.lib.ObjectReader)4 Repository (org.eclipse.jgit.lib.Repository)4 ThreeWayMerger (org.eclipse.jgit.merge.ThreeWayMerger)4 MergeConflictException (com.google.gerrit.extensions.restapi.MergeConflictException)3 OrmException (com.google.gwtorm.server.OrmException)3 ArrayList (java.util.ArrayList)3