Search in sources :

Example 16 with CommitBuilder

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

the class PublicKeyStoreTest method newCommitBuilder.

private CommitBuilder newCommitBuilder() {
    CommitBuilder cb = new CommitBuilder();
    PersonIdent ident = new PersonIdent("A U Thor", "author@example.com");
    cb.setAuthor(ident);
    cb.setCommitter(ident);
    return cb;
}
Also used : PersonIdent(org.eclipse.jgit.lib.PersonIdent) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder)

Example 17 with CommitBuilder

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

the class CreateChange method newCommit.

private static RevCommit newCommit(ObjectInserter oi, RevWalk rw, PersonIdent authorIdent, RevCommit mergeTip, String commitMessage) throws IOException {
    CommitBuilder commit = new CommitBuilder();
    if (mergeTip == null) {
        commit.setTreeId(emptyTreeId(oi));
    } else {
        commit.setTreeId(mergeTip.getTree().getId());
        commit.setParentId(mergeTip);
    }
    commit.setAuthor(authorIdent);
    commit.setCommitter(authorIdent);
    commit.setMessage(commitMessage);
    return rw.parseCommit(insert(oi, commit));
}
Also used : CommitBuilder(org.eclipse.jgit.lib.CommitBuilder)

Example 18 with CommitBuilder

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

the class ChangeDraftUpdate method applyImpl.

@Override
protected CommitBuilder applyImpl(RevWalk rw, ObjectInserter ins, ObjectId curr) throws OrmException, IOException {
    CommitBuilder cb = new CommitBuilder();
    cb.setMessage("Update draft comments");
    try {
        return storeCommentsInNotes(rw, ins, curr, cb);
    } catch (ConfigInvalidException e) {
        throw new OrmException(e);
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OrmException(com.google.gwtorm.server.OrmException) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder)

Example 19 with CommitBuilder

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

the class Revert method revert.

private Change.Id revert(BatchUpdate.Factory updateFactory, ChangeControl ctl, String message) throws OrmException, IOException, RestApiException, UpdateException {
    Change.Id changeIdToRevert = ctl.getChange().getId();
    PatchSet.Id patchSetId = ctl.getChange().currentPatchSetId();
    PatchSet patch = psUtil.get(db.get(), ctl.getNotes(), patchSetId);
    if (patch == null) {
        throw new ResourceNotFoundException(changeIdToRevert.toString());
    }
    Project.NameKey project = ctl.getProject().getNameKey();
    CurrentUser user = ctl.getUser();
    try (Repository git = repoManager.openRepository(project);
        ObjectInserter oi = git.newObjectInserter();
        ObjectReader reader = oi.newReader();
        RevWalk revWalk = new RevWalk(reader)) {
        RevCommit commitToRevert = revWalk.parseCommit(ObjectId.fromString(patch.getRevision().get()));
        if (commitToRevert.getParentCount() == 0) {
            throw new ResourceConflictException("Cannot revert initial commit");
        }
        Timestamp now = TimeUtil.nowTs();
        PersonIdent committerIdent = new PersonIdent(serverIdent, now);
        PersonIdent authorIdent = user.asIdentifiedUser().newCommitterIdent(now, committerIdent.getTimeZone());
        RevCommit parentToCommitToRevert = commitToRevert.getParent(0);
        revWalk.parseHeaders(parentToCommitToRevert);
        CommitBuilder revertCommitBuilder = new CommitBuilder();
        revertCommitBuilder.addParentId(commitToRevert);
        revertCommitBuilder.setTreeId(parentToCommitToRevert.getTree());
        revertCommitBuilder.setAuthor(authorIdent);
        revertCommitBuilder.setCommitter(authorIdent);
        Change changeToRevert = ctl.getChange();
        if (message == null) {
            message = MessageFormat.format(ChangeMessages.get().revertChangeDefaultMessage, changeToRevert.getSubject(), patch.getRevision().get());
        }
        ObjectId computedChangeId = ChangeIdUtil.computeChangeId(parentToCommitToRevert.getTree(), commitToRevert, authorIdent, committerIdent, message);
        revertCommitBuilder.setMessage(ChangeIdUtil.insertId(message, computedChangeId, true));
        Change.Id changeId = new Change.Id(seq.nextChangeId());
        ObjectId id = oi.insert(revertCommitBuilder);
        RevCommit revertCommit = revWalk.parseCommit(id);
        ChangeInserter ins = changeInserterFactory.create(changeId, revertCommit, ctl.getChange().getDest().get()).setTopic(changeToRevert.getTopic());
        ins.setMessage("Uploaded patch set 1.");
        Set<Account.Id> reviewers = new HashSet<>();
        reviewers.add(changeToRevert.getOwner());
        reviewers.addAll(approvalsUtil.getReviewers(db.get(), ctl.getNotes()).all());
        reviewers.remove(user.getAccountId());
        ins.setReviewers(reviewers);
        try (BatchUpdate bu = updateFactory.create(db.get(), project, user, now)) {
            bu.setRepository(git, revWalk, oi);
            bu.insertChange(ins);
            bu.addOp(changeId, new NotifyOp(ctl.getChange(), ins));
            bu.addOp(changeToRevert.getId(), new PostRevertedMessageOp(computedChangeId));
            bu.execute();
        }
        return changeId;
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(changeIdToRevert.toString(), e);
    }
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Timestamp(java.sql.Timestamp) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) HashSet(java.util.HashSet) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 20 with CommitBuilder

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

the class Schema_146 method createInitialEmptyCommit.

private ObjectId createInitialEmptyCommit(ObjectInserter oi, ObjectId emptyTree, Timestamp registrationDate) throws IOException {
    PersonIdent ident = new PersonIdent(serverIdent, registrationDate);
    CommitBuilder cb = new CommitBuilder();
    cb.setTreeId(emptyTree);
    cb.setCommitter(ident);
    cb.setAuthor(ident);
    cb.setMessage(CREATE_ACCOUNT_MSG);
    return oi.insert(cb);
}
Also used : PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder)

Aggregations

CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)37 ObjectId (org.eclipse.jgit.lib.ObjectId)24 PersonIdent (org.eclipse.jgit.lib.PersonIdent)18 RefUpdate (org.eclipse.jgit.lib.RefUpdate)13 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)11 IOException (java.io.IOException)8 RevWalk (org.eclipse.jgit.revwalk.RevWalk)8 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)7 Result (org.eclipse.jgit.lib.RefUpdate.Result)6 DirCache (org.eclipse.jgit.dircache.DirCache)5 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