Search in sources :

Example 21 with DirCache

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

DirCache (org.eclipse.jgit.dircache.DirCache)21 DirCacheBuilder (org.eclipse.jgit.dircache.DirCacheBuilder)10 IOException (java.io.IOException)9 ObjectId (org.eclipse.jgit.lib.ObjectId)9 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)8 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)7 RevWalk (org.eclipse.jgit.revwalk.RevWalk)7 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 ConcurrentRefUpdateException (org.eclipse.jgit.api.errors.ConcurrentRefUpdateException)4 RefUpdate (org.eclipse.jgit.lib.RefUpdate)4 Repository (org.eclipse.jgit.lib.Repository)4 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)3 DiffEntry (org.eclipse.jgit.diff.DiffEntry)3 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)3 ObjectReader (org.eclipse.jgit.lib.ObjectReader)3 PersonIdent (org.eclipse.jgit.lib.PersonIdent)3 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 RefModel (com.gitblit.models.RefModel)2 SubmoduleSubscription (com.google.gerrit.reviewdb.client.SubmoduleSubscription)2