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;
}
}
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;
}
}
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;
}
}
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;
}
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);
}
Aggregations