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

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

Introduction

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

Prototype

public static NoteMap newEmptyMap() 

Source Link

Document

Construct a new empty note map.

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.
 *///from w ww  . j  a  v a2  s  .c o m
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 ww  .j a  v  a  2 s  .c  o  m
    if (baseCommit != null) {
        ours = NoteMap.read(repo.newObjectReader(), baseCommit);
    } else {
        ours = NoteMap.newEmptyMap();
    }
}

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

License:Apache License

/**
 * Save pending keys to the store.//w  w w  .ja  v a  2s  .co m
 * <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)) {
            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;
    default:
        break;
    }
    return result;
}

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));
    }//from  www  .  j  a  v  a2  s.  c o m
    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.// w  w w .j  ava 2s.c  o  m
 * @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.BanCommit.java

License:Apache License

public BanCommitResult ban(final ProjectControl projectControl, final List<ObjectId> commitsToBan,
        final String reason) throws PermissionDeniedException, IOException, ConcurrentRefUpdateException {
    if (!projectControl.isOwner()) {
        throw new PermissionDeniedException("Not project owner: not permitted to ban commits");
    }//  www.  j  a v a 2 s.  co m

    final BanCommitResult result = new BanCommitResult();
    NoteMap banCommitNotes = NoteMap.newEmptyMap();
    // Add a note for each banned commit to notes.
    final Project.NameKey project = projectControl.getProject().getNameKey();
    try (Repository repo = repoManager.openRepository(project);
            RevWalk revWalk = new RevWalk(repo);
            ObjectInserter inserter = repo.newObjectInserter()) {
        ObjectId noteId = null;
        for (final ObjectId commitToBan : commitsToBan) {
            try {
                revWalk.parseCommit(commitToBan);
            } catch (MissingObjectException e) {
                // Ignore exception, non-existing commits can be banned.
            } catch (IncorrectObjectTypeException e) {
                result.notACommit(commitToBan);
                continue;
            }
            if (noteId == null) {
                noteId = createNoteContent(reason, inserter);
            }
            banCommitNotes.set(commitToBan, noteId);
        }
        NotesBranchUtil notesBranchUtil = notesBranchUtilFactory.create(project, repo, inserter);
        NoteMap newlyCreated = notesBranchUtil.commitNewNotes(banCommitNotes, REFS_REJECT_COMMITS,
                createPersonIdent(), buildCommitMessage(commitsToBan, reason));

        for (Note n : banCommitNotes) {
            if (newlyCreated.contains(n)) {
                result.commitBanned(n);
            } else {
                result.commitAlreadyBanned(n);
            }
        }
        return result;
    }
}

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 ww.  ja  va  2s.  co  m
    if (baseCommit != null) {
        ours = NoteMap.read(db.newObjectReader(), baseCommit);
    } else {
        ours = NoteMap.newEmptyMap();
    }
}

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

License:Apache License

/**
 * Create a new commit in the {@code notesBranch} by creating not yet
 * existing notes from the {@code notes} map. The notes from the
 * {@code notes} map which already exist in the note-tree of the
 * tip of the {@code notesBranch} will not be updated.
 *
 * @param notes map of notes/*  w  w  w . j av  a2 s .  c o m*/
 * @param notesBranch notes branch to update
 * @param commitAuthor author of the commit in the notes branch
 * @param commitMessage for the commit in the notes branch
 * @return map with those notes from the {@code notes} that were newly
 *         created
 * @throws IOException
 * @throws ConcurrentRefUpdateException
 */
public final NoteMap commitNewNotes(NoteMap notes, String notesBranch, PersonIdent commitAuthor,
        String commitMessage) throws IOException, ConcurrentRefUpdateException {
    this.overwrite = false;
    commitNotes(notes, notesBranch, commitAuthor, commitMessage);
    NoteMap newlyCreated = NoteMap.newEmptyMap();
    for (Note n : notes) {
        if (base == null || !base.contains(n)) {
            newlyCreated.set(n, n.getData());
        }
    }
    return newlyCreated;
}

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

License:Apache License

private void loadBase(String notesBranch) throws IOException {
    Ref branch = db.getRefDatabase().exactRef(notesBranch);
    if (branch != null) {
        baseCommit = revWalk.parseCommit(branch.getObjectId());
        base = NoteMap.read(revWalk.getObjectReader(), baseCommit);
    }//from ww w  .  j av a  2  s  .co  m
    if (baseCommit != null) {
        ours = NoteMap.read(revWalk.getObjectReader(), baseCommit);
    } else {
        ours = NoteMap.newEmptyMap();
    }
}

From source file:com.google.gerrit.server.notedb.ChangeDraftUpdate.java

License:Apache License

/** @return the tree id for the updated tree */
private ObjectId storeCommentsInNotes(AtomicBoolean removedAllComments) throws OrmException, IOException {
    if (isEmpty()) {
        return null;
    }//  w  w  w  .j a v  a2  s .c  om

    NoteMap noteMap = draftNotes.load().getNoteMap();
    if (noteMap == null) {
        noteMap = NoteMap.newEmptyMap();
    }

    Map<RevId, List<PatchLineComment>> allComments = new HashMap<>();

    boolean hasComments = false;
    int n = deleteComments.size() + upsertComments.size();
    Set<RevId> updatedRevs = Sets.newHashSetWithExpectedSize(n);
    Set<PatchLineComment.Key> updatedKeys = Sets.newHashSetWithExpectedSize(n);
    for (PatchLineComment c : deleteComments) {
        allComments.put(c.getRevId(), new ArrayList<PatchLineComment>());
        updatedRevs.add(c.getRevId());
        updatedKeys.add(c.getKey());
    }

    for (PatchLineComment c : upsertComments) {
        hasComments = true;
        addCommentToMap(allComments, c);
        updatedRevs.add(c.getRevId());
        updatedKeys.add(c.getKey());
    }

    // Re-add old comments for updated revisions so the new note contents
    // includes both old and new comments merged in the right order.
    //
    // writeCommentsToNoteMap doesn't touch notes for SHA-1s that are not
    // mentioned in the input map, so by omitting comments for those revisions,
    // we avoid the work of having to re-serialize identical comment data for
    // those revisions.
    ListMultimap<RevId, PatchLineComment> existing = draftNotes.getComments();
    for (Map.Entry<RevId, PatchLineComment> e : existing.entries()) {
        PatchLineComment c = e.getValue();
        if (updatedRevs.contains(c.getRevId()) && !updatedKeys.contains(c.getKey())) {
            hasComments = true;
            addCommentToMap(allComments, e.getValue());
        }
    }

    // If we touched every revision and there are no comments left, set the flag
    // for the caller to delete the entire ref.
    boolean touchedAllRevs = updatedRevs.equals(existing.keySet());
    if (touchedAllRevs && !hasComments) {
        removedAllComments.set(touchedAllRevs && !hasComments);
        return null;
    }

    commentsUtil.writeCommentsToNoteMap(noteMap, allComments, inserter);
    return noteMap.writeTree(inserter);
}