Search in sources :

Example 1 with PS_ID_ORDER

use of com.google.gerrit.server.ChangeUtil.PS_ID_ORDER in project gerrit by GerritCodeReview.

the class ConsistencyChecker method checkPatchSets.

private boolean checkPatchSets() {
    List<PatchSet> all;
    try {
        // Iterate in descending order.
        all = PS_ID_ORDER.sortedCopy(psUtil.byChange(db.get(), ctl.getNotes()));
    } catch (OrmException e) {
        return error("Failed to look up patch sets", e);
    }
    patchSetsBySha = MultimapBuilder.hashKeys(all.size()).treeSetValues(PS_ID_ORDER).build();
    Map<String, Ref> refs;
    try {
        refs = repo.getRefDatabase().exactRef(all.stream().map(ps -> ps.getId().toRefName()).toArray(String[]::new));
    } catch (IOException e) {
        error("error reading refs", e);
        refs = Collections.emptyMap();
    }
    List<DeletePatchSetFromDbOp> deletePatchSetOps = new ArrayList<>();
    for (PatchSet ps : all) {
        // Check revision format.
        int psNum = ps.getId().get();
        String refName = ps.getId().toRefName();
        ObjectId objId = parseObjectId(ps.getRevision().get(), "patch set " + psNum);
        if (objId == null) {
            continue;
        }
        patchSetsBySha.put(objId, ps);
        // Check ref existence.
        ProblemInfo refProblem = null;
        Ref ref = refs.get(refName);
        if (ref == null) {
            refProblem = problem("Ref missing: " + refName);
        } else if (!objId.equals(ref.getObjectId())) {
            String actual = ref.getObjectId() != null ? ref.getObjectId().name() : "null";
            refProblem = problem(String.format("Expected %s to point to %s, found %s", ref.getName(), objId.name(), actual));
        }
        // Check object existence.
        RevCommit psCommit = parseCommit(objId, String.format("patch set %d", psNum));
        if (psCommit == null) {
            if (fix != null && fix.deletePatchSetIfCommitMissing) {
                deletePatchSetOps.add(new DeletePatchSetFromDbOp(lastProblem(), ps.getId()));
            }
            continue;
        } else if (refProblem != null && fix != null) {
            fixPatchSetRef(refProblem, ps);
        }
        if (ps.getId().equals(change().currentPatchSetId())) {
            currPsCommit = psCommit;
        }
    }
    // Delete any bad patch sets found above, in a single update.
    deletePatchSets(deletePatchSetOps);
    // Check for duplicates.
    for (Map.Entry<ObjectId, Collection<PatchSet>> e : patchSetsBySha.asMap().entrySet()) {
        if (e.getValue().size() > 1) {
            problem(String.format("Multiple patch sets pointing to %s: %s", e.getKey().name(), Collections2.transform(e.getValue(), PatchSet::getPatchSetId)));
        }
    }
    return currPs != null && currPsCommit != null;
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) OrmException(com.google.gwtorm.server.OrmException) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) DynamicItem(com.google.gerrit.extensions.registration.DynamicItem) MultimapBuilder(com.google.common.collect.MultimapBuilder) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Collections2(com.google.common.collect.Collections2) PatchSetInfoFactory(com.google.gerrit.server.patch.PatchSetInfoFactory) UpdateException(com.google.gerrit.server.update.UpdateException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) Map(java.util.Map) RetryHelper(com.google.gerrit.server.update.RetryHelper) Collection(java.util.Collection) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Status(com.google.gerrit.extensions.common.ProblemInfo.Status) NotifyHandling(com.google.gerrit.extensions.api.changes.NotifyHandling) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) ReviewDbUtil.intKeyOrdering(com.google.gerrit.reviewdb.server.ReviewDbUtil.intKeyOrdering) PS_ID_ORDER(com.google.gerrit.server.ChangeUtil.PS_ID_ORDER) Nullable(com.google.gerrit.common.Nullable) Ref(org.eclipse.jgit.lib.Ref) AutoValue(com.google.auto.value.AutoValue) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) RevCommit(org.eclipse.jgit.revwalk.RevCommit) FixInput(com.google.gerrit.extensions.api.changes.FixInput) Change(com.google.gerrit.reviewdb.client.Change) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) PatchSetState(com.google.gerrit.server.notedb.PatchSetState) RepoContext(com.google.gerrit.server.update.RepoContext) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ChangeContext(com.google.gerrit.server.update.ChangeContext) Project(com.google.gerrit.reviewdb.client.Project) PatchSetInfoNotAvailableException(com.google.gerrit.server.patch.PatchSetInfoNotAvailableException) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) CurrentUser(com.google.gerrit.server.CurrentUser) Logger(org.slf4j.Logger) REFS_CHANGES(com.google.gerrit.reviewdb.client.RefNames.REFS_CHANGES) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) TimeUtil(com.google.gerrit.common.TimeUtil) ReviewDbUtil(com.google.gerrit.reviewdb.server.ReviewDbUtil) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) SetMultimap(com.google.common.collect.SetMultimap) ObjectId(org.eclipse.jgit.lib.ObjectId) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) FooterConstants(com.google.gerrit.common.FooterConstants) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Collections(java.util.Collections) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ProblemInfo(com.google.gerrit.extensions.common.ProblemInfo) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) IOException(java.io.IOException) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) Collection(java.util.Collection) Map(java.util.Map) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

AutoValue (com.google.auto.value.AutoValue)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Collections2 (com.google.common.collect.Collections2)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 MultimapBuilder (com.google.common.collect.MultimapBuilder)1 SetMultimap (com.google.common.collect.SetMultimap)1 FooterConstants (com.google.gerrit.common.FooterConstants)1 Nullable (com.google.gerrit.common.Nullable)1 TimeUtil (com.google.gerrit.common.TimeUtil)1 FixInput (com.google.gerrit.extensions.api.changes.FixInput)1 NotifyHandling (com.google.gerrit.extensions.api.changes.NotifyHandling)1 ProblemInfo (com.google.gerrit.extensions.common.ProblemInfo)1 Status (com.google.gerrit.extensions.common.ProblemInfo.Status)1 DynamicItem (com.google.gerrit.extensions.registration.DynamicItem)1 RestApiException (com.google.gerrit.extensions.restapi.RestApiException)1 Change (com.google.gerrit.reviewdb.client.Change)1 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)1 Project (com.google.gerrit.reviewdb.client.Project)1