Search in sources :

Example 86 with PersonIdent

use of org.eclipse.jgit.lib.PersonIdent 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);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DirCache(org.eclipse.jgit.dircache.DirCache) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) ConcurrentRefUpdateException(org.eclipse.jgit.api.errors.ConcurrentRefUpdateException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate)

Aggregations

PersonIdent (org.eclipse.jgit.lib.PersonIdent)86 RevCommit (org.eclipse.jgit.revwalk.RevCommit)23 Test (org.junit.Test)21 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)19 ObjectId (org.eclipse.jgit.lib.ObjectId)18 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)15 RevWalk (org.eclipse.jgit.revwalk.RevWalk)13 Change (com.google.gerrit.reviewdb.client.Change)12 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)12 Repository (org.eclipse.jgit.lib.Repository)11 Account (com.google.gerrit.reviewdb.client.Account)10 IOException (java.io.IOException)9 RefUpdate (org.eclipse.jgit.lib.RefUpdate)9 Ref (org.eclipse.jgit.lib.Ref)8 TestRepository (org.eclipse.jgit.junit.TestRepository)7 Date (java.util.Date)6 Result (org.eclipse.jgit.lib.RefUpdate.Result)6 ArrayList (java.util.ArrayList)5 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)4 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)4