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;
}
}
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;
}
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);
}
}
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);
}
}
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));
}
}
Aggregations