Search in sources :

Example 16 with NoteMap

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

the class ExternalIdsUpdate method updateNoteMap.

private RefsMetaExternalIdsUpdate updateNoteMap(MyConsumer<OpenRepo> update) throws IOException, ConfigInvalidException, OrmException {
    try {
        return retryer.call(() -> {
            try (Repository repo = repoManager.openRepository(allUsersName);
                ObjectInserter ins = repo.newObjectInserter()) {
                ObjectId rev = readRevision(repo);
                afterReadRevision.run();
                try (RevWalk rw = new RevWalk(repo)) {
                    NoteMap noteMap = readNoteMap(rw, rev);
                    update.accept(OpenRepo.create(repo, rw, ins, noteMap));
                    return commit(repo, rw, ins, rev, noteMap);
                }
            }
        });
    } catch (ExecutionException | RetryException e) {
        if (e.getCause() != null) {
            Throwables.throwIfInstanceOf(e.getCause(), IOException.class);
            Throwables.throwIfInstanceOf(e.getCause(), ConfigInvalidException.class);
            Throwables.throwIfInstanceOf(e.getCause(), OrmException.class);
        }
        throw new OrmException(e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) OrmException(com.google.gwtorm.server.OrmException) NoteMap(org.eclipse.jgit.notes.NoteMap) ExternalIdReader.readNoteMap(com.google.gerrit.server.account.externalids.ExternalIdReader.readNoteMap) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ExecutionException(java.util.concurrent.ExecutionException) RetryException(com.github.rholder.retry.RetryException)

Example 17 with NoteMap

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

the class BanCommit method ban.

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");
    }
    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;
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) Note(org.eclipse.jgit.notes.Note) PermissionDeniedException(com.google.gerrit.common.errors.PermissionDeniedException)

Example 18 with NoteMap

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

the class ExternalIdReader method parse.

private static ExternalId parse(ExternalId.Key key, RevWalk rw, ObjectId rev) throws IOException, ConfigInvalidException {
    NoteMap noteMap = readNoteMap(rw, rev);
    ObjectId noteId = key.sha1();
    if (!noteMap.contains(noteId)) {
        return null;
    }
    byte[] raw = rw.getObjectReader().open(noteMap.get(noteId), OBJ_BLOB).getCachedBytes(MAX_NOTE_SZ);
    return ExternalId.parse(noteId.name(), raw);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) NoteMap(org.eclipse.jgit.notes.NoteMap)

Example 19 with NoteMap

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

the class ExternalIdsConsistencyChecker method check.

private List<ConsistencyProblemInfo> check(Repository repo, ObjectId commit) throws IOException {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    ListMultimap<String, ExternalId.Key> emails = MultimapBuilder.hashKeys().arrayListValues().build();
    try (RevWalk rw = new RevWalk(repo)) {
        NoteMap noteMap = ExternalIdReader.readNoteMap(rw, commit);
        for (Note note : noteMap) {
            byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
            try {
                ExternalId extId = ExternalId.parse(note.getName(), raw);
                problems.addAll(validateExternalId(extId));
                if (extId.email() != null) {
                    emails.put(extId.email(), extId.key());
                }
            } catch (ConfigInvalidException e) {
                addError(String.format(e.getMessage()), problems);
            }
        }
    }
    emails.asMap().entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> addError(String.format("Email '%s' is not unique, it's used by the following external IDs: %s", e.getKey(), e.getValue().stream().map(k -> "'" + k.get() + "'").sorted().collect(joining(", "))), problems));
    return problems;
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OutgoingEmailValidator(com.google.gerrit.server.mail.send.OutgoingEmailValidator) Note(org.eclipse.jgit.notes.Note) OBJ_BLOB(org.eclipse.jgit.lib.Constants.OBJ_BLOB) ListMultimap(com.google.common.collect.ListMultimap) DecoderException(org.apache.commons.codec.DecoderException) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) IOException(java.io.IOException) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) HashedPassword(com.google.gerrit.server.account.HashedPassword) Repository(org.eclipse.jgit.lib.Repository) NoteMap(org.eclipse.jgit.notes.NoteMap) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ArrayList(java.util.ArrayList) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Note(org.eclipse.jgit.notes.Note) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo)

Example 20 with NoteMap

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

the class ExternalIdTestUtil method insertExternalId.

private static String insertExternalId(Repository repo, RevWalk rw, PersonIdent ident, ExternalIdInserter extIdInserter) throws IOException {
    ObjectId rev = ExternalIdReader.readRevision(repo);
    NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
    try (ObjectInserter ins = repo.newObjectInserter()) {
        ObjectId noteId = extIdInserter.addNote(ins, noteMap);
        CommitBuilder cb = new CommitBuilder();
        cb.setMessage("Update external IDs");
        cb.setTreeId(noteMap.writeTree(ins));
        cb.setAuthor(ident);
        cb.setCommitter(ident);
        if (!rev.equals(ObjectId.zeroId())) {
            cb.setParentId(rev);
        } else {
            // Ref is currently nonexistent, commit has no parents.
            cb.setParentIds();
        }
        if (cb.getTreeId() == null) {
            if (rev.equals(ObjectId.zeroId())) {
                // No parent, assume empty tree.
                cb.setTreeId(ins.insert(OBJ_TREE, new byte[] {}));
            } else {
                RevCommit p = rw.parseCommit(rev);
                // Copy tree from parent.
                cb.setTreeId(p.getTree());
            }
        }
        ObjectId commitId = ins.insert(cb);
        ins.flush();
        RefUpdate u = repo.updateRef(RefNames.REFS_EXTERNAL_IDS);
        u.setExpectedOldObjectId(rev);
        u.setNewObjectId(commitId);
        RefUpdate.Result res = u.update();
        switch(res) {
            case NEW:
            case FAST_FORWARD:
            case NO_CHANGE:
            case RENAMED:
            case FORCED:
                break;
            case LOCK_FAILURE:
            case IO_FAILURE:
            case NOT_ATTEMPTED:
            case REJECTED:
            case REJECTED_CURRENT_BRANCH:
            case REJECTED_MISSING_OBJECT:
            case REJECTED_OTHER_REASON:
            default:
                throw new IOException("Updating external IDs failed with " + res);
        }
        return noteId.getName();
    }
}
Also used : ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) NoteMap(org.eclipse.jgit.notes.NoteMap) IOException(java.io.IOException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

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