Search in sources :

Example 76 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.

the class ChangeOperationsImpl method createChange.

private Change.Id createChange(TestChangeCreation changeCreation) throws Exception {
    Change.Id changeId = Change.id(seq.nextChangeId());
    Project.NameKey project = getTargetProject(changeCreation);
    try (Repository repository = repositoryManager.openRepository(project);
        ObjectInserter objectInserter = repository.newObjectInserter();
        RevWalk revWalk = new RevWalk(objectInserter.newReader())) {
        Instant now = TimeUtil.now();
        IdentifiedUser changeOwner = getChangeOwner(changeCreation);
        PersonIdent authorAndCommitter = changeOwner.newCommitterIdent(now, serverIdent.getTimeZone());
        ObjectId commitId = createCommit(repository, revWalk, objectInserter, changeCreation, authorAndCommitter);
        String refName = RefNames.fullName(changeCreation.branch());
        ChangeInserter inserter = getChangeInserter(changeId, refName, commitId);
        try (BatchUpdate batchUpdate = batchUpdateFactory.create(project, changeOwner, now)) {
            batchUpdate.setRepository(repository, revWalk, objectInserter);
            batchUpdate.insertChange(inserter);
            batchUpdate.execute();
        }
        return changeId;
    }
}
Also used : AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) Change(com.google.gerrit.entities.Change) RevWalk(org.eclipse.jgit.revwalk.RevWalk) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ChangeInserter(com.google.gerrit.server.change.ChangeInserter)

Example 77 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter 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;
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 78 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.

the class IntBlob method tryStore.

public static RefUpdate tryStore(Repository repo, RevWalk rw, Project.NameKey projectName, String refName, @Nullable ObjectId oldId, int val, GitReferenceUpdated gitRefUpdated) throws IOException {
    ObjectId newId;
    try (ObjectInserter ins = repo.newObjectInserter()) {
        logger.atFine().log("storing value %d on %s in %s (oldId: %s)", val, refName, projectName, oldId == null ? "null" : oldId.name());
        newId = ins.insert(OBJ_BLOB, Integer.toString(val).getBytes(UTF_8));
        ins.flush();
        logger.atFine().log("successfully stored %d on %s as %s in %s", val, refName, newId.name(), projectName);
    }
    RefUpdate ru = repo.updateRef(refName);
    if (oldId != null) {
        ru.setExpectedOldObjectId(oldId);
    }
    ru.disableRefLog();
    ru.setNewObjectId(newId);
    // Required for non-commitish updates.
    ru.setForceUpdate(true);
    RefUpdate.Result result = ru.update(rw);
    if (refUpdated(result)) {
        gitRefUpdated.fire(projectName, ru, null);
    }
    return ru;
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 79 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.

the class PublicKeyStore method rebuildSubkeyMasterKeyMap.

public void rebuildSubkeyMasterKeyMap() throws MissingObjectException, IncorrectObjectTypeException, IOException, PGPException {
    if (reader == null) {
        load();
    }
    if (notes != null) {
        try (ObjectInserter ins = repo.newObjectInserter()) {
            for (Note note : notes) {
                for (PGPPublicKeyRing keyRing : new PGPPublicKeyRingCollection(readKeysFromNote(note, null))) {
                    long masterKeyId = keyRing.getPublicKey().getKeyID();
                    ObjectId masterKeyObjectId = keyObjectId(masterKeyId);
                    saveSubkeyMapping(ins, keyRing, masterKeyId, masterKeyObjectId);
                }
            }
        }
    }
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPPublicKeyRingCollection(org.bouncycastle.openpgp.PGPPublicKeyRingCollection) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) Note(org.eclipse.jgit.notes.Note)

Example 80 with ObjectInserter

use of org.eclipse.jgit.lib.ObjectInserter in project gerrit by GerritCodeReview.

the class PublicKeyStore method save.

/**
 * Save pending keys to the store.
 *
 * <p>One commit is created and the ref updated. The pending list is cleared if and only if the
 * ref update succeeds, which allows for easy retries in case of lock failure.
 *
 * @param cb commit builder with at least author and identity populated; tree and parent are
 *     ignored.
 * @return result of the ref update.
 */
public RefUpdate.Result save(CommitBuilder cb) throws PGPException, IOException {
    if (toAdd.isEmpty() && toRemove.isEmpty()) {
        return RefUpdate.Result.NO_CHANGE;
    }
    if (reader == null) {
        load();
    }
    if (notes == null) {
        notes = NoteMap.newEmptyMap();
    }
    ObjectId newTip;
    try (ObjectInserter ins = repo.newObjectInserter()) {
        for (PGPPublicKeyRing keyRing : toAdd.values()) {
            saveToNotes(ins, keyRing);
        }
        for (Fingerprint fp : toRemove) {
            deleteFromNotes(ins, fp);
        }
        cb.setTreeId(notes.writeTree(ins));
        if (cb.getTreeId().equals(tip != null ? tip.getTree() : EMPTY_TREE_ID)) {
            return RefUpdate.Result.NO_CHANGE;
        }
        if (tip != null) {
            cb.setParentId(tip);
        }
        if (cb.getMessage() == null) {
            int n = toAdd.size() + toRemove.size();
            cb.setMessage(String.format("Update %d public key%s", n, n != 1 ? "s" : ""));
        }
        newTip = ins.insert(cb);
        ins.flush();
    }
    RefUpdate ru = repo.updateRef(PublicKeyStore.REFS_GPG_KEYS);
    ru.setExpectedOldObjectId(tip);
    ru.setNewObjectId(newTip);
    ru.setRefLogIdent(cb.getCommitter());
    ru.setRefLogMessage("Store public keys", true);
    RefUpdate.Result result = ru.update();
    reset();
    switch(result) {
        case FAST_FORWARD:
        case NEW:
        case NO_CHANGE:
            toAdd.clear();
            toRemove.clear();
            break;
        case LOCK_FAILURE:
            throw new LockFailureException("Failed to store public keys", ru);
        case FORCED:
        case IO_FAILURE:
        case NOT_ATTEMPTED:
        case REJECTED:
        case REJECTED_CURRENT_BRANCH:
        case RENAMED:
        case REJECTED_MISSING_OBJECT:
        case REJECTED_OTHER_REASON:
        default:
            break;
    }
    return result;
}
Also used : PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) LockFailureException(com.google.gerrit.git.LockFailureException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Aggregations

ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)108 ObjectId (org.eclipse.jgit.lib.ObjectId)77 RevWalk (org.eclipse.jgit.revwalk.RevWalk)55 Repository (org.eclipse.jgit.lib.Repository)49 RevCommit (org.eclipse.jgit.revwalk.RevCommit)35 IOException (java.io.IOException)31 ObjectReader (org.eclipse.jgit.lib.ObjectReader)31 PersonIdent (org.eclipse.jgit.lib.PersonIdent)30 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)28 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)24 DirCache (org.eclipse.jgit.dircache.DirCache)15 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)14 Change (com.google.gerrit.entities.Change)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)13 Change (com.google.gerrit.reviewdb.client.Change)13 DirCacheEntry (org.eclipse.jgit.dircache.DirCacheEntry)13 Ref (org.eclipse.jgit.lib.Ref)13 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)12 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)10