Search in sources :

Example 41 with ObjectId

use of org.eclipse.jgit.lib.ObjectId 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);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) LockFailureException(com.google.gerrit.server.git.LockFailureException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 42 with ObjectId

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

the class ExternalIdsBatchUpdate method commit.

/**
   * Commits this batch.
   *
   * <p>This means external ID replacements which were prepared by invoking {@link
   * #replace(ExternalId, ExternalId)} are now executed. Deletion of external IDs is done before
   * adding the new external IDs. This means if an external ID is specified for deletion and an
   * external ID with the same key is specified to be added, the old external ID with that key is
   * deleted first and then the new external ID is added (so the external ID for that key is
   * replaced).
   *
   * <p>For NoteDb a single commit is created that contains all the external ID updates.
   */
public void commit(String commitMessage) throws IOException, OrmException, ConfigInvalidException {
    if (toDelete.isEmpty() && toAdd.isEmpty()) {
        return;
    }
    try (Repository repo = repoManager.openRepository(allUsersName);
        RevWalk rw = new RevWalk(repo);
        ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId rev = ExternalIdReader.readRevision(repo);
        NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
        for (ExternalId extId : toDelete) {
            ExternalIdsUpdate.remove(rw, noteMap, extId);
        }
        for (ExternalId extId : toAdd) {
            ExternalIdsUpdate.insert(rw, ins, noteMap, extId);
        }
        ObjectId newRev = ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, commitMessage, serverIdent, serverIdent);
        externalIdCache.onReplace(rev, newRev, toDelete, toAdd);
    }
    toAdd.clear();
    toDelete.clear();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 43 with ObjectId

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

the class ReplaceOp method findMergedInto.

private static String findMergedInto(Context ctx, String first, RevCommit commit) {
    try {
        RevWalk rw = ctx.getRevWalk();
        Optional<ObjectId> firstId = ctx.getRepoView().getRef(first);
        if (firstId.isPresent() && rw.isMergedInto(commit, rw.parseCommit(firstId.get()))) {
            return first;
        }
        for (Map.Entry<String, ObjectId> e : ctx.getRepoView().getRefs(R_HEADS).entrySet()) {
            if (rw.isMergedInto(commit, rw.parseCommit(e.getValue()))) {
                return R_HEADS + e.getKey();
            }
        }
        return null;
    } catch (IOException e) {
        log.warn("Can't check for already submitted change", e);
        return null;
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Map(java.util.Map) HashMap(java.util.HashMap)

Example 44 with ObjectId

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

the class RepoRefCache method get.

@Override
public Optional<ObjectId> get(String refName) throws IOException {
    Optional<ObjectId> id = ids.get(refName);
    if (id != null) {
        return id;
    }
    Ref ref = refdb.exactRef(refName);
    id = Optional.ofNullable(ref).map(Ref::getObjectId);
    ids.put(refName, id);
    return id;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId)

Example 45 with ObjectId

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

the class ReviewNoteMerger method merge.

@Override
public Note merge(Note base, Note ours, Note theirs, ObjectReader reader, ObjectInserter inserter) throws IOException {
    if (ours == null) {
        return theirs;
    }
    if (theirs == null) {
        return ours;
    }
    if (ours.getData().equals(theirs.getData())) {
        return ours;
    }
    ObjectLoader lo = reader.open(ours.getData());
    byte[] sep = new byte[] { '\n' };
    ObjectLoader lt = reader.open(theirs.getData());
    try (ObjectStream os = lo.openStream();
        ByteArrayInputStream b = new ByteArrayInputStream(sep);
        ObjectStream ts = lt.openStream();
        UnionInputStream union = new UnionInputStream(os, b, ts)) {
        ObjectId noteData = inserter.insert(Constants.OBJ_BLOB, lo.getSize() + sep.length + lt.getSize(), union);
        return new Note(ours, noteData);
    }
}
Also used : UnionInputStream(org.eclipse.jgit.util.io.UnionInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectStream(org.eclipse.jgit.lib.ObjectStream) Note(org.eclipse.jgit.notes.Note) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader)

Aggregations

ObjectId (org.eclipse.jgit.lib.ObjectId)357 Test (org.junit.Test)128 RevCommit (org.eclipse.jgit.revwalk.RevCommit)125 RevWalk (org.eclipse.jgit.revwalk.RevWalk)86 IOException (java.io.IOException)63 Repository (org.eclipse.jgit.lib.Repository)63 Change (com.google.gerrit.reviewdb.client.Change)41 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)40 ArrayList (java.util.ArrayList)36 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)34 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)34 Ref (org.eclipse.jgit.lib.Ref)34 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)33 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)26 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)23 NoteMap (org.eclipse.jgit.notes.NoteMap)22 Map (java.util.Map)21 OrmException (com.google.gwtorm.server.OrmException)20 ObjectReader (org.eclipse.jgit.lib.ObjectReader)20