Search in sources :

Example 96 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 97 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 98 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 99 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)

Example 100 with ObjectId

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

the class ReceiveCommits method validateNewCommits.

private void validateNewCommits(RefControl ctl, ReceiveCommand cmd) {
    if (ctl.canForgeAuthor() && ctl.canForgeCommitter() && ctl.canForgeGerritServerIdentity() && ctl.canUploadMerges() && !projectControl.getProjectState().isUseSignedOffBy() && Iterables.isEmpty(rejectCommits) && !RefNames.REFS_CONFIG.equals(ctl.getRefName()) && !(MagicBranch.isMagicBranch(cmd.getRefName()) || NEW_PATCHSET.matcher(cmd.getRefName()).matches())) {
        logDebug("Short-circuiting new commit validation");
        return;
    }
    boolean defaultName = Strings.isNullOrEmpty(user.getAccount().getFullName());
    RevWalk walk = rp.getRevWalk();
    walk.reset();
    walk.sort(RevSort.NONE);
    try {
        RevObject parsedObject = walk.parseAny(cmd.getNewId());
        if (!(parsedObject instanceof RevCommit)) {
            return;
        }
        ListMultimap<ObjectId, Ref> existing = changeRefsById();
        walk.markStart((RevCommit) parsedObject);
        markHeadsAsUninteresting(walk, cmd.getRefName());
        int i = 0;
        for (RevCommit c; (c = walk.next()) != null; ) {
            i++;
            if (existing.keySet().contains(c)) {
                continue;
            } else if (!validCommit(walk, ctl, cmd, c)) {
                break;
            }
            if (defaultName && user.hasEmailAddress(c.getCommitterIdent().getEmailAddress())) {
                try {
                    Account a = db.accounts().get(user.getAccountId());
                    if (a != null && Strings.isNullOrEmpty(a.getFullName())) {
                        a.setFullName(c.getCommitterIdent().getName());
                        db.accounts().update(Collections.singleton(a));
                        user.getAccount().setFullName(a.getFullName());
                        accountCache.evict(a.getId());
                    }
                } catch (OrmException e) {
                    logWarn("Cannot default full_name", e);
                } finally {
                    defaultName = false;
                }
            }
        }
        logDebug("Validated {} new commits", i);
    } catch (IOException err) {
        cmd.setResult(REJECTED_MISSING_OBJECT);
        logError("Invalid pack upload; one or more objects weren't sent", err);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Ref(org.eclipse.jgit.lib.Ref) RevObject(org.eclipse.jgit.revwalk.RevObject) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectId (org.eclipse.jgit.lib.ObjectId)355 Test (org.junit.Test)128 RevCommit (org.eclipse.jgit.revwalk.RevCommit)123 RevWalk (org.eclipse.jgit.revwalk.RevWalk)84 Repository (org.eclipse.jgit.lib.Repository)63 IOException (java.io.IOException)61 Change (com.google.gerrit.reviewdb.client.Change)41 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)39 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)25 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)22 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)19