use of org.eclipse.jgit.dircache.DirCache in project blueocean-plugin by jenkinsci.
the class GitUtils method commit.
public static void commit(final Repository repo, final String refName, final String path, final byte[] contents, final String name, final String email, final String message, final TimeZone timeZone, final Date when) {
final PersonIdent author = buildPersonIdent(repo, name, email, timeZone, when);
try (final ObjectInserter odi = repo.newObjectInserter()) {
// Create the in-memory index of the new/updated issue.
final ObjectId headId = repo.resolve(refName + "^{commit}");
final DirCache index = createTemporaryIndex(repo, headId, path, contents);
final ObjectId indexTreeId = index.writeTree(odi);
// Create a commit object
final CommitBuilder commit = new CommitBuilder();
commit.setAuthor(author);
commit.setCommitter(author);
commit.setEncoding(Constants.CHARACTER_ENCODING);
commit.setMessage(message);
// headId can be null if the repository has no commit yet
if (headId != null) {
commit.setParentId(headId);
}
commit.setTreeId(indexTreeId);
// Insert the commit into the repository
final ObjectId commitId = odi.insert(commit);
odi.flush();
try (RevWalk revWalk = new RevWalk(repo)) {
final RevCommit revCommit = revWalk.parseCommit(commitId);
final RefUpdate ru = repo.updateRef(refName);
if (headId == null) {
ru.setExpectedOldObjectId(ObjectId.zeroId());
} else {
ru.setExpectedOldObjectId(headId);
}
ru.setNewObjectId(commitId);
ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
final RefUpdate.Result rc = ru.forceUpdate();
switch(rc) {
case NEW:
case FORCED:
case FAST_FORWARD:
break;
case REJECTED:
case LOCK_FAILURE:
throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
default:
throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, commitId.toString(), rc));
}
}
} catch (ConcurrentRefUpdateException | IOException | JGitInternalException ex) {
throw new RuntimeException(ex);
}
}
Aggregations