Example usage for org.eclipse.jgit.notes NoteMap read

List of usage examples for org.eclipse.jgit.notes NoteMap read

Introduction

In this page you can find the example usage for org.eclipse.jgit.notes NoteMap read.

Prototype

public static NoteMap read(ObjectReader reader, RevTree tree)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException 

Source Link

Document

Load a collection of notes from a tree.

Usage

From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java

License:Open Source License

/**
 * Merges the notes from local and origin commits with the given merge base.
 *///  ww w.  ja va 2 s . c  om
private void mergeNotesAndPush(RevWalk revWalk, String refName, RevCommit baseCommit, RevCommit localCommit,
        RevCommit originCommit) throws GitClientException {
    int remainingLockFailureCalls = JgitUtils.MAX_LOCK_FAILURE_CALLS;

    // Merge and commit.
    while (true) {
        try {
            NoteMap theirNoteMap = NoteMap.read(revWalk.getObjectReader(), originCommit);
            NoteMap ourNoteMap = NoteMap.read(revWalk.getObjectReader(), localCommit);
            NoteMap baseNoteMap;
            if (baseCommit != null) {
                baseNoteMap = NoteMap.read(revWalk.getObjectReader(), baseCommit);
            } else {
                baseNoteMap = NoteMap.newEmptyMap();
            }

            NoteMapMerger merger = new NoteMapMerger(repo, new DefaultNoteMerger(), MergeStrategy.RESOLVE);
            NoteMap merged = merger.merge(baseNoteMap, ourNoteMap, theirNoteMap);
            try (ObjectInserter inserter = repo.newObjectInserter()) {
                RevCommit mergeCommit = createNotesCommit(merged, inserter, revWalk, "Merged note commits\n",
                        localCommit, originCommit);

                RefUpdate update = JgitUtils.updateRef(repo, mergeCommit, localCommit, refName);
                Result result = update.update();
                if (result == Result.LOCK_FAILURE) {
                    if (--remainingLockFailureCalls > 0) {
                        Thread.sleep(JgitUtils.SLEEP_ON_LOCK_FAILURE_MS);
                    } else {
                        throw new GitClientException("Failed to lock the ref: " + refName);
                    }
                } else if (result == Result.REJECTED) {
                    throw new GitClientException("Rejected update to " + refName + ", this is unexpected");
                } else if (result == Result.IO_FAILURE) {
                    throw new GitClientException("I/O failure merging notes");
                } else {
                    // OK.
                    break;
                }
            }
        } catch (Exception e) {
            throw new GitClientException("Error merging notes commits", e);
        }
    }

    // And push.
    try {
        pushCommentsAndReviews();
    } catch (Exception e) {
        throw new GitClientException("Error pushing merge commit", e);
    }
}

From source file:com.google.appraise.eclipse.core.client.git.GitNoteWriter.java

License:Open Source License

private void loadBase() throws IOException {
    Ref notesBranch = repo.getRef(ref);
    if (notesBranch != null) {
        baseCommit = revWalk.parseCommit(notesBranch.getObjectId());
        base = NoteMap.read(revWalk.getObjectReader(), baseCommit);
    }/*w w  w.j  ava 2 s. c  om*/
    if (baseCommit != null) {
        ours = NoteMap.read(repo.newObjectReader(), baseCommit);
    } else {
        ours = NoteMap.newEmptyMap();
    }
}

From source file:com.google.appraise.eclipse.core.client.git.GitNoteWriter.java

License:Open Source License

private void updateRef() throws IOException, InterruptedException, RuntimeException, MissingObjectException,
        IncorrectObjectTypeException, CorruptObjectException {
    if (baseCommit != null && oursCommit.getTree().equals(baseCommit.getTree())) {
        // If the trees are identical, there is no change in the notes.
        // Avoid saving this commit as it has no new information.
        return;/*from  www .  j ava  2  s .  com*/
    }

    int remainingLockFailureCalls = JgitUtils.MAX_LOCK_FAILURE_CALLS;
    RefUpdate refUpdate = JgitUtils.updateRef(repo, oursCommit, baseCommit, ref);

    for (;;) {
        Result result = refUpdate.update();

        if (result == Result.LOCK_FAILURE) {
            if (--remainingLockFailureCalls > 0) {
                Thread.sleep(JgitUtils.SLEEP_ON_LOCK_FAILURE_MS);
            } else {
                throw new RuntimeException("Failed to lock the ref: " + ref);
            }

        } else if (result == Result.REJECTED) {
            RevCommit theirsCommit = revWalk.parseCommit(refUpdate.getOldObjectId());
            NoteMap theirs = NoteMap.read(revWalk.getObjectReader(), theirsCommit);
            NoteMapMerger merger = new NoteMapMerger(repo);
            NoteMap merged = merger.merge(base, ours, theirs);
            RevCommit mergeCommit = createCommit(merged, author, "Merged note records\n", theirsCommit,
                    oursCommit);
            refUpdate = JgitUtils.updateRef(repo, mergeCommit, theirsCommit, ref);
            remainingLockFailureCalls = JgitUtils.MAX_LOCK_FAILURE_CALLS;

        } else if (result == Result.IO_FAILURE) {
            throw new RuntimeException("Couldn't create notes because of IO_FAILURE");
        } else {
            break;
        }
    }
}

From source file:com.google.gerrit.gpg.PublicKeyStore.java

License:Apache License

private void load() throws IOException {
    reset();// w  w  w .j  ava 2s .c om
    reader = repo.newObjectReader();

    Ref ref = repo.getRefDatabase().exactRef(REFS_GPG_KEYS);
    if (ref == null) {
        return;
    }
    try (RevWalk rw = new RevWalk(reader)) {
        tip = rw.parseCommit(ref.getObjectId());
        notes = NoteMap.read(reader, tip);
    }
}

From source file:com.google.gerrit.gpg.PublicKeyStoreTest.java

License:Apache License

@Test
public void saveAppendsToExistingList() throws Exception {
    TestKey key1 = TestKey.key1();/*from  www . jav  a2  s . c  o  m*/
    TestKey key2 = TestKey.key2();
    tr.branch(REFS_GPG_KEYS).commit()
            // Mismatched for this key ID, but we can still read it out.
            .add(keyObjectId(key1.getKeyId()).name(), key2.getPublicKeyArmored()).create();

    store.add(key1.getPublicKeyRing());
    assertEquals(RefUpdate.Result.FAST_FORWARD, store.save(newCommitBuilder()));

    assertKeys(key1.getKeyId(), key1, key2);

    try (ObjectReader reader = tr.getRepository().newObjectReader(); RevWalk rw = new RevWalk(reader)) {
        NoteMap notes = NoteMap.read(reader,
                tr.getRevWalk().parseCommit(tr.getRepository().getRef(REFS_GPG_KEYS).getObjectId()));
        String contents = new String(reader.open(notes.get(keyObjectId(key1.getKeyId()))).getBytes(), UTF_8);
        String header = "-----BEGIN PGP PUBLIC KEY BLOCK-----";
        int i1 = contents.indexOf(header);
        assertTrue(i1 >= 0);
        int i2 = contents.indexOf(header, i1 + header.length());
        assertTrue(i2 >= 0);
    }
}

From source file:com.google.gerrit.server.account.externalids.ExternalIdReader.java

License:Apache License

public static NoteMap readNoteMap(RevWalk rw, ObjectId rev) throws IOException {
    if (!rev.equals(ObjectId.zeroId())) {
        return NoteMap.read(rw.getObjectReader(), rw.parseCommit(rev));
    }/*  w  w  w  .  ja va 2  s  .  c  om*/
    return NoteMap.newEmptyMap();
}

From source file:com.google.gerrit.server.git.BanCommit.java

License:Apache License

/**
 * Loads a list of commits to reject from {@code refs/meta/reject-commits}.
 *
 * @param repo repository from which the rejected commits should be loaded
 * @param walk open revwalk on repo.//from www  .  j a  v  a  2  s  .  c  om
 * @return NoteMap of commits to be rejected, null if there are none.
 * @throws IOException the map cannot be loaded.
 */
public static NoteMap loadRejectCommitsMap(Repository repo, RevWalk walk) throws IOException {
    try {
        Ref ref = repo.getRefDatabase().exactRef(RefNames.REFS_REJECT_COMMITS);
        if (ref == null) {
            return NoteMap.newEmptyMap();
        }

        RevCommit map = walk.parseCommit(ref.getObjectId());
        return NoteMap.read(walk.getObjectReader(), map);
    } catch (IOException badMap) {
        throw new IOException("Cannot load " + RefNames.REFS_REJECT_COMMITS, badMap);
    }
}

From source file:com.google.gerrit.server.git.CreateCodeReviewNotes.java

License:Apache License

public void loadBase() throws IOException {
    Ref notesBranch = db.getRef(REFS_NOTES_REVIEW);
    if (notesBranch != null) {
        baseCommit = revWalk.parseCommit(notesBranch.getObjectId());
        base = NoteMap.read(revWalk.getObjectReader(), baseCommit);
    }/* w w  w  .j  a va2s  .c  o  m*/
    if (baseCommit != null) {
        ours = NoteMap.read(db.newObjectReader(), baseCommit);
    } else {
        ours = NoteMap.newEmptyMap();
    }
}

From source file:com.google.gerrit.server.git.CreateCodeReviewNotes.java

License:Apache License

public void updateRef() throws IOException, InterruptedException, CodeReviewNoteCreationException,
        MissingObjectException, IncorrectObjectTypeException, CorruptObjectException {
    if (baseCommit != null && oursCommit.getTree().equals(baseCommit.getTree())) {
        // If the trees are identical, there is no change in the notes.
        // Avoid saving this commit as it has no new information.
        return;/*from   w w  w.  j  av  a 2s. co  m*/
    }

    int remainingLockFailureCalls = MAX_LOCK_FAILURE_CALLS;
    RefUpdate refUpdate = createRefUpdate(oursCommit, baseCommit);

    for (;;) {
        Result result = refUpdate.update();

        if (result == Result.LOCK_FAILURE) {
            if (--remainingLockFailureCalls > 0) {
                Thread.sleep(SLEEP_ON_LOCK_FAILURE_MS);
            } else {
                throw new CodeReviewNoteCreationException("Failed to lock the ref: " + REFS_NOTES_REVIEW);
            }

        } else if (result == Result.REJECTED) {
            RevCommit theirsCommit = revWalk.parseCommit(refUpdate.getOldObjectId());
            NoteMap theirs = NoteMap.read(revWalk.getObjectReader(), theirsCommit);
            NoteMapMerger merger = new NoteMapMerger(db);
            NoteMap merged = merger.merge(base, ours, theirs);
            RevCommit mergeCommit = createCommit(merged, gerritIdent, "Merged note commits\n", theirsCommit,
                    oursCommit);
            refUpdate = createRefUpdate(mergeCommit, theirsCommit);
            remainingLockFailureCalls = MAX_LOCK_FAILURE_CALLS;

        } else if (result == Result.IO_FAILURE) {
            throw new CodeReviewNoteCreationException(
                    "Couldn't create code review notes because of IO_FAILURE");
        } else {
            break;
        }
    }
}

From source file:com.google.gerrit.server.git.gpg.PublicKeyStore.java

License:Apache License

private void load() throws IOException {
    close();/*from  w w  w .  j a  va2  s  .  co m*/
    reader = repo.newObjectReader();

    Ref ref = repo.getRefDatabase().exactRef(RefNames.REFS_GPG_KEYS);
    if (ref == null) {
        return;
    }
    try (RevWalk rw = new RevWalk(reader)) {
        notes = NoteMap.read(reader, rw.parseCommit(ref.getObjectId()));
    }
}