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

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

Introduction

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

Prototype

public void set(AnyObjectId noteOn, ObjectId noteData) throws IOException 

Source Link

Document

Attach (or remove) a note on an object.

Usage

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithoutAccountId(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    ExternalId extId = ExternalId.create(ExternalId.Key.parse(externalId), admin.id);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = extId.key().sha1();
        Config c = new Config();
        extId.writeToConfig(c);//from   www.j a v  a2  s.  c o  m
        c.unset("externalId", extId.key().get(), "accountId");
        byte[] raw = c.toText().getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());
        return noteId.getName();
    }
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithKeyThatDoesntMatchNoteId(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    ExternalId extId = ExternalId.create(ExternalId.Key.parse(externalId), admin.id);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = ExternalId.Key.parse(externalId + "x").sha1();
        Config c = new Config();
        extId.writeToConfig(c);//from w w w . j  a v  a2s.  c  o m
        byte[] raw = c.toText().getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());
        return noteId.getName();
    }
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithInvalidConfig(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = ExternalId.Key.parse(externalId).sha1();
        byte[] raw = "bad-config".getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());// w  ww  . jav a2 s.c om
        return noteId.getName();
    }
}

From source file:com.google.gerrit.acceptance.rest.account.ExternalIdIT.java

License:Apache License

private String insertExternalIdWithEmptyNote(Repository repo, RevWalk rw, String externalId)
        throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);

    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = ExternalId.Key.parse(externalId).sha1();
        byte[] raw = "".getBytes(UTF_8);
        ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
        noteMap.set(noteId, dataBlob);

        ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(),
                admin.getIdent());/*from  w  ww  .java2  s  .com*/
        return noteId.getName();
    }
}

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

License:Apache License

/**
 * Insert or updates an new external ID and sets it in the note map.
 *
 * <p>If the external ID already exists it is overwritten.
 *///from w  w w .j ava2  s  .co  m
public static void upsert(RevWalk rw, ObjectInserter ins, NoteMap noteMap, ExternalId extId)
        throws IOException, ConfigInvalidException {
    ObjectId noteId = extId.key().sha1();
    Config c = new Config();
    if (noteMap.contains(extId.key().sha1())) {
        byte[] raw = rw.getObjectReader().open(noteMap.get(noteId), OBJ_BLOB).getCachedBytes(MAX_NOTE_SZ);
        try {
            c.fromText(new String(raw, UTF_8));
        } catch (ConfigInvalidException e) {
            throw new ConfigInvalidException(
                    String.format("Invalid external id config for note %s: %s", noteId, e.getMessage()));
        }
    }
    extId.writeToConfig(c);
    byte[] raw = c.toText().getBytes(UTF_8);
    ObjectId dataBlob = ins.insert(OBJ_BLOB, raw);
    noteMap.set(noteId, dataBlob);
}

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");
    }//from  w ww. 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.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/*from  w  w  w.  j av  a2s. co  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.notedb.CommentsInNotesUtil.java

License:Apache License

/**
 * Write comments for multiple revisions to a note map.
 * <p>/*  w w  w  . j  a  va 2  s  .  com*/
 * Mutates the map in-place. only notes for SHA-1s found as keys in the map
 * are modified; all other notes are left untouched.
 *
 * @param noteMap note map to modify.
 * @param allComments map of revision to all comments for that revision;
 *     callers are responsible for reading the original comments and applying
 *     any changes. Differs from a multimap in that present-but-empty values
 *     are significant, and indicate the note for that SHA-1 should be
 *     deleted.
 * @param inserter object inserter for writing notes.
 * @throws IOException if an error occurred.
 */
public void writeCommentsToNoteMap(NoteMap noteMap, Map<RevId, List<PatchLineComment>> allComments,
        ObjectInserter inserter) throws IOException {
    for (Map.Entry<RevId, List<PatchLineComment>> e : allComments.entrySet()) {
        List<PatchLineComment> comments = e.getValue();
        ObjectId commit = ObjectId.fromString(e.getKey().get());
        if (comments.isEmpty()) {
            noteMap.remove(commit);
            continue;
        }
        Collections.sort(comments, PLC_ORDER);
        // We allow comments for multiple commits to be written in the same
        // update, even though the rest of the metadata update is associated with
        // a single patch set.
        noteMap.set(commit, inserter.insert(OBJ_BLOB, buildNote(comments)));
    }
}