Search in sources :

Example 21 with NoteMap

use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.

the class BanCommit method ban.

/**
 * Bans a list of commits from the given project.
 *
 * <p>The user must be specified, so it can be checked for the {@code BAN_COMMIT} permission.
 */
public BanCommitResult ban(Project.NameKey project, CurrentUser user, List<ObjectId> commitsToBan, String reason) throws AuthException, IOException, PermissionBackendException {
    permissionBackend.user(user).project(project).check(ProjectPermission.BAN_COMMIT);
    final BanCommitResult result = new BanCommitResult();
    NoteMap banCommitNotes = NoteMap.newEmptyMap();
    // Add a note for each banned commit to notes.
    try (Repository repo = repoManager.openRepository(project);
        RevWalk revWalk = new RevWalk(repo);
        ObjectInserter inserter = repo.newObjectInserter()) {
        ObjectId noteId = null;
        for (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;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) Note(org.eclipse.jgit.notes.Note) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException)

Example 22 with NoteMap

use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.

the class GroupNameNotes method onSave.

@Override
protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException {
    if (!oldGroupName.isPresent() && !newGroupName.isPresent()) {
        return false;
    }
    logger.atFine().log("Updating group notes");
    NoteMap noteMap = revision == null ? NoteMap.newEmptyMap() : NoteMap.read(reader, revision);
    if (oldGroupName.isPresent()) {
        removeNote(noteMap, oldGroupName.get(), inserter);
    }
    if (newGroupName.isPresent()) {
        addNote(noteMap, newGroupName.get(), groupUuid, inserter);
    }
    commit.setTreeId(noteMap.writeTree(inserter));
    commit.setMessage(getCommitMessage());
    oldGroupName = Optional.empty();
    newGroupName = Optional.empty();
    return true;
}
Also used : NoteMap(org.eclipse.jgit.notes.NoteMap)

Example 23 with NoteMap

use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.

the class GroupNameNotes method loadAllGroups.

/**
 * Loads the {@code GroupReference}s (name/UUID pairs) for all groups.
 *
 * <p>Even though group UUIDs should be unique, this class doesn't enforce it. For this reason,
 * it's technically possible that two of the {@code GroupReference}s have a duplicate UUID but a
 * different name. In practice, this shouldn't occur unless we introduce a bug in the future.
 *
 * @param repository the repository which holds the commits of the notes
 * @return the {@code GroupReference}s of all existing groups/notes
 * @throws IOException if the repository can't be accessed for some reason
 * @throws ConfigInvalidException if one of the notes is in an invalid state
 */
public static ImmutableList<GroupReference> loadAllGroups(Repository repository) throws IOException, ConfigInvalidException {
    Ref ref = repository.exactRef(RefNames.REFS_GROUPNAMES);
    if (ref == null) {
        return ImmutableList.of();
    }
    try (TraceTimer ignored = TraceContext.newTimer("Loading all groups", Metadata.builder().noteDbRefName(RefNames.REFS_GROUPNAMES).build());
        RevWalk revWalk = new RevWalk(repository);
        ObjectReader reader = revWalk.getObjectReader()) {
        RevCommit notesCommit = revWalk.parseCommit(ref.getObjectId());
        NoteMap noteMap = NoteMap.read(reader, notesCommit);
        Multiset<GroupReference> groupReferences = HashMultiset.create();
        for (Note note : noteMap) {
            GroupReference groupReference = getGroupReference(reader, note.getData());
            int numOfOccurrences = groupReferences.add(groupReference, 1);
            if (numOfOccurrences > 1) {
                GroupsNoteDbConsistencyChecker.logConsistencyProblemAsWarning("The UUID of group %s (%s) is duplicate in group name notes", groupReference.getName(), groupReference.getUUID());
            }
        }
        return ImmutableList.copyOf(groupReferences);
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) Note(org.eclipse.jgit.notes.Note) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) GroupReference(com.google.gerrit.entities.GroupReference) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 24 with NoteMap

use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.

the class GroupNameNotes method onLoad.

@Override
protected void onLoad() throws IOException, ConfigInvalidException {
    nameConflicting = false;
    logger.atFine().log("Reading group notes");
    if (revision != null) {
        NoteMap noteMap = NoteMap.read(reader, revision);
        if (newGroupName.isPresent()) {
            ObjectId newNameId = getNoteKey(newGroupName.get());
            nameConflicting = noteMap.contains(newNameId);
        }
        ensureOldNameIsPresent(noteMap);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) NoteMap(org.eclipse.jgit.notes.NoteMap)

Example 25 with NoteMap

use of org.eclipse.jgit.notes.NoteMap in project gerrit by GerritCodeReview.

the class GroupNameNotes method loadGroup.

/**
 * Loads the {@code GroupReference} (name/UUID pair) for the group with the specified name.
 *
 * @param repository the repository which holds the commits of the notes
 * @param groupName the name of the group
 * @return the corresponding {@code GroupReference} if a group/note with the given name exists
 * @throws IOException if the repository can't be accessed for some reason
 * @throws ConfigInvalidException if the note for the specified group is in an invalid state
 */
public static Optional<GroupReference> loadGroup(Repository repository, AccountGroup.NameKey groupName) throws IOException, ConfigInvalidException {
    Ref ref = repository.exactRef(RefNames.REFS_GROUPNAMES);
    if (ref == null) {
        return Optional.empty();
    }
    try (RevWalk revWalk = new RevWalk(repository);
        ObjectReader reader = revWalk.getObjectReader()) {
        RevCommit notesCommit = revWalk.parseCommit(ref.getObjectId());
        NoteMap noteMap = NoteMap.read(reader, notesCommit);
        ObjectId noteDataBlobId = noteMap.get(getNoteKey(groupName));
        if (noteDataBlobId == null) {
            return Optional.empty();
        }
        return Optional.of(getGroupReference(reader, noteDataBlobId));
    }
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

NoteMap (org.eclipse.jgit.notes.NoteMap)29 ObjectId (org.eclipse.jgit.lib.ObjectId)23 RevWalk (org.eclipse.jgit.revwalk.RevWalk)15 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)11 Repository (org.eclipse.jgit.lib.Repository)9 Note (org.eclipse.jgit.notes.Note)9 IOException (java.io.IOException)8 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)7 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)6 Ref (org.eclipse.jgit.lib.Ref)5 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)4 ConsistencyProblemInfo (com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo)3 AllUsersName (com.google.gerrit.server.config.AllUsersName)3 Inject (com.google.inject.Inject)3 Singleton (com.google.inject.Singleton)3 ListMultimap (com.google.common.collect.ListMultimap)2 MultimapBuilder (com.google.common.collect.MultimapBuilder)2 AccountGroup (com.google.gerrit.entities.AccountGroup)2 GroupReference (com.google.gerrit.entities.GroupReference)2