use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.
the class ExternalIdsUpdate method commit.
/** Commits updates to the external IDs. */
public static ObjectId commit(Repository repo, RevWalk rw, ObjectInserter ins, ObjectId rev, NoteMap noteMap, String commitMessage, PersonIdent committerIdent, PersonIdent authorIdent) throws IOException {
CommitBuilder cb = new CommitBuilder();
cb.setMessage(commitMessage);
cb.setTreeId(noteMap.writeTree(ins));
cb.setAuthor(authorIdent);
cb.setCommitter(committerIdent);
if (!rev.equals(ObjectId.zeroId())) {
cb.setParentId(rev);
} else {
// Ref is currently nonexistent, commit has no parents.
cb.setParentIds();
}
if (cb.getTreeId() == null) {
if (rev.equals(ObjectId.zeroId())) {
// No parent, assume empty tree.
cb.setTreeId(emptyTree(ins));
} else {
RevCommit p = rw.parseCommit(rev);
// Copy tree from parent.
cb.setTreeId(p.getTree());
}
}
ObjectId commitId = ins.insert(cb);
ins.flush();
RefUpdate u = repo.updateRef(RefNames.REFS_EXTERNAL_IDS);
u.setRefLogIdent(committerIdent);
u.setRefLogMessage("Update external IDs", false);
u.setExpectedOldObjectId(rev);
u.setNewObjectId(commitId);
RefUpdate.Result res = u.update();
switch(res) {
case NEW:
case FAST_FORWARD:
case NO_CHANGE:
case RENAMED:
case FORCED:
break;
case LOCK_FAILURE:
throw new LockFailureException("Updating external IDs failed with " + res);
case IO_FAILURE:
case NOT_ATTEMPTED:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
default:
throw new IOException("Updating external IDs failed with " + res);
}
return rw.parseCommit(commitId);
}
use of org.eclipse.jgit.lib.CommitBuilder 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);
}
use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.
the class MergeUtil method createCherryPickFromCommit.
public CodeReviewCommit createCherryPickFromCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, PersonIdent cherryPickCommitterIdent, String commitMsg, CodeReviewRevWalk rw, int parentIndex, boolean ignoreIdenticalTree) throws MissingObjectException, IncorrectObjectTypeException, IOException, MergeIdenticalTreeException, MergeConflictException {
final ThreeWayMerger m = newThreeWayMerger(inserter, repoConfig);
m.setBase(originalCommit.getParent(parentIndex));
if (m.merge(mergeTip, originalCommit)) {
ObjectId tree = m.getResultTreeId();
if (tree.equals(mergeTip.getTree()) && !ignoreIdenticalTree) {
throw new MergeIdenticalTreeException("identical tree");
}
CommitBuilder mergeCommit = new CommitBuilder();
mergeCommit.setTreeId(tree);
mergeCommit.setParentId(mergeTip);
mergeCommit.setAuthor(originalCommit.getAuthorIdent());
mergeCommit.setCommitter(cherryPickCommitterIdent);
mergeCommit.setMessage(commitMsg);
return rw.parseCommit(inserter.insert(mergeCommit));
}
throw new MergeConflictException("merge conflict");
}
use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.
the class CreateProject method createEmptyCommits.
private void createEmptyCommits(Repository repo, Project.NameKey project, List<String> refs) throws IOException {
try (ObjectInserter oi = repo.newObjectInserter()) {
CommitBuilder cb = new CommitBuilder();
cb.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
cb.setAuthor(metaDataUpdateFactory.getUserPersonIdent());
cb.setCommitter(serverIdent);
cb.setMessage("Initial empty repository\n");
ObjectId id = oi.insert(cb);
oi.flush();
for (String ref : refs) {
RefUpdate ru = repo.updateRef(ref);
ru.setNewObjectId(id);
Result result = ru.update();
switch(result) {
case NEW:
referenceUpdated.fire(project, ru, ReceiveCommand.Type.CREATE, identifiedUser.get().getAccount());
break;
case FAST_FORWARD:
case FORCED:
case IO_FAILURE:
case LOCK_FAILURE:
case NOT_ATTEMPTED:
case NO_CHANGE:
case REJECTED:
case REJECTED_CURRENT_BRANCH:
case RENAMED:
default:
{
throw new IOException(String.format("Failed to create ref \"%s\": %s", ref, result.name()));
}
}
}
} catch (IOException e) {
log.error("Cannot create empty commit for " + project.get(), e);
throw e;
}
}
use of org.eclipse.jgit.lib.CommitBuilder in project gerrit by GerritCodeReview.
the class CommitMsgHookTest method setHEAD.
private void setHEAD() throws Exception {
try (ObjectInserter oi = repository.newObjectInserter()) {
final CommitBuilder commit = new CommitBuilder();
commit.setTreeId(oi.insert(Constants.OBJ_TREE, new byte[] {}));
commit.setAuthor(author);
commit.setCommitter(committer);
commit.setMessage("test\n");
ObjectId commitId = oi.insert(commit);
final RefUpdate ref = repository.updateRef(Constants.HEAD);
ref.setNewObjectId(commitId);
Result result = ref.forceUpdate();
assert_().withFailureMessage(Constants.HEAD + " did not change: " + ref.getResult()).that(result).isAnyOf(Result.FAST_FORWARD, Result.FORCED, Result.NEW, Result.NO_CHANGE);
}
}
Aggregations