Search in sources :

Example 41 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class ConsistencyCheckerIT method expectedMergedCommitIsDanglingPatchSetNewerThanCurrent.

@Test
public void expectedMergedCommitIsDanglingPatchSetNewerThanCurrent() throws Exception {
    ChangeControl ctl = insertChange();
    PatchSet ps1 = psUtil.current(db, ctl.getNotes());
    // Create dangling ref with no patch set.
    PatchSet.Id psId2 = new PatchSet.Id(ctl.getId(), 2);
    RevCommit commit2 = patchSetCommit(psId2);
    String rev2 = commit2.name();
    testRepo.branch(psId2.toRefName()).update(commit2);
    testRepo.branch(ctl.getChange().getDest().get()).update(testRepo.getRevWalk().parseCommit(ObjectId.fromString(rev2)));
    FixInput fix = new FixInput();
    fix.expectMergedAs = rev2;
    assertProblems(ctl, fix, problem("No patch set found for merged commit " + rev2, FIXED, "Marked change as merged"), problem("Expected merge commit " + rev2 + " corresponds to patch set 2," + " not the current patch set 1", FIXED, "Inserted as patch set 2"));
    ctl = reload(ctl);
    assertThat(ctl.getChange().currentPatchSetId()).isEqualTo(psId2);
    assertThat(ctl.getChange().getStatus()).isEqualTo(Change.Status.MERGED);
    assertThat(psUtil.byChangeAsMap(db, ctl.getNotes()).keySet()).containsExactly(ps1.getId(), psId2);
    assertThat(psUtil.get(db, ctl.getNotes(), psId2).getRevision().get()).isEqualTo(rev2);
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) TestChanges.newPatchSet(com.google.gerrit.testutil.TestChanges.newPatchSet) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) ObjectId(org.eclipse.jgit.lib.ObjectId) FixInput(com.google.gerrit.extensions.api.changes.FixInput) RevCommit(org.eclipse.jgit.revwalk.RevCommit) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 42 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class CatServlet method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse rsp) throws IOException {
    String keyStr = req.getPathInfo();
    // We shouldn't have to do this extra decode pass, but somehow we
    // are now receiving our "^1" suffix as "%5E1", which confuses us
    // downstream. Other times we get our embedded "," as "%2C", which
    // is equally bad. And yet when these happen a "%2F" is left as-is,
    // rather than escaped as "%252F", which makes me feel really really
    // uncomfortable with a blind decode right here.
    //
    keyStr = Url.decode(keyStr);
    if (!keyStr.startsWith("/")) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    keyStr = keyStr.substring(1);
    final Patch.Key patchKey;
    final int side;
    {
        final int c = keyStr.lastIndexOf('^');
        if (c == 0) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        if (c < 0) {
            side = 0;
        } else {
            try {
                side = Integer.parseInt(keyStr.substring(c + 1));
                keyStr = keyStr.substring(0, c);
            } catch (NumberFormatException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        }
        try {
            patchKey = Patch.Key.parse(keyStr);
        } catch (NumberFormatException e) {
            rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
    final Change.Id changeId = patchKey.getParentKey().getParentKey();
    String revision;
    try {
        final ReviewDb db = requestDb.get();
        final ChangeControl control = changeControl.validateFor(db, changeId, userProvider.get());
        if (patchKey.getParentKey().get() == 0) {
            // change edit
            try {
                Optional<ChangeEdit> edit = changeEditUtil.byChange(control.getChange());
                if (edit.isPresent()) {
                    revision = ObjectId.toString(edit.get().getEditCommit());
                } else {
                    rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
            } catch (AuthException e) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
        } else {
            PatchSet patchSet = psUtil.get(db, control.getNotes(), patchKey.getParentKey());
            if (patchSet == null) {
                rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            revision = patchSet.getRevision().get();
        }
    } catch (NoSuchChangeException e) {
        rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    } catch (OrmException e) {
        getServletContext().log("Cannot query database", e);
        rsp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
    }
    String path = patchKey.getFileName();
    String restUrl = String.format("%s/changes/%d/revisions/%s/files/%s/download?parent=%d", req.getContextPath(), changeId.get(), revision, Url.encode(path), side);
    rsp.sendRedirect(restUrl);
}
Also used : ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) AuthException(com.google.gerrit.extensions.restapi.AuthException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) ChangeControl(com.google.gerrit.server.project.ChangeControl) Patch(com.google.gerrit.reviewdb.client.Patch) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 43 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class CherryPickChange method insertPatchSet.

private Change.Id insertPatchSet(BatchUpdate bu, Repository git, ChangeControl destCtl, CodeReviewCommit cherryPickCommit, CherryPickInput input) throws IOException, OrmException, BadRequestException {
    Change destChange = destCtl.getChange();
    PatchSet.Id psId = ChangeUtil.nextPatchSetId(git, destChange.currentPatchSetId());
    PatchSet current = psUtil.current(db.get(), destCtl.getNotes());
    PatchSetInserter inserter = patchSetInserterFactory.create(destCtl, psId, cherryPickCommit);
    inserter.setMessage("Uploaded patch set " + inserter.getPatchSetId().get() + ".").setDraft(current.isDraft()).setNotify(input.notify).setAccountsToNotify(notifyUtil.resolveAccounts(input.notifyDetails));
    bu.addOp(destChange.getId(), inserter);
    return destChange.getId();
}
Also used : PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change)

Example 44 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet 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)

Example 45 with PatchSet

use of com.google.gerrit.reviewdb.client.PatchSet in project gerrit by GerritCodeReview.

the class ApplyFix method apply.

@Override
public Response<EditInfo> apply(FixResource fixResource, Void nothing) throws AuthException, OrmException, ResourceConflictException, IOException, ResourceNotFoundException, PermissionBackendException {
    RevisionResource revisionResource = fixResource.getRevisionResource();
    Project.NameKey project = revisionResource.getProject();
    ProjectState projectState = revisionResource.getControl().getProjectControl().getProjectState();
    PatchSet patchSet = revisionResource.getPatchSet();
    ObjectId patchSetCommitId = ObjectId.fromString(patchSet.getRevision().get());
    try (Repository repository = gitRepositoryManager.openRepository(project)) {
        List<TreeModification> treeModifications = fixReplacementInterpreter.toTreeModifications(repository, projectState, patchSetCommitId, fixResource.getFixReplacements());
        ChangeEdit changeEdit = changeEditModifier.combineWithModifiedPatchSetTree(repository, revisionResource.getControl(), patchSet, treeModifications);
        return Response.ok(changeEditJson.toEditInfo(changeEdit, false));
    } catch (InvalidChangeOperationException e) {
        throw new ResourceConflictException(e.getMessage());
    }
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) Repository(org.eclipse.jgit.lib.Repository) ChangeEdit(com.google.gerrit.server.edit.ChangeEdit) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) ObjectId(org.eclipse.jgit.lib.ObjectId) ProjectState(com.google.gerrit.server.project.ProjectState) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) TreeModification(com.google.gerrit.server.edit.tree.TreeModification)

Aggregations

PatchSet (com.google.gerrit.reviewdb.client.PatchSet)124 Change (com.google.gerrit.reviewdb.client.Change)51 Test (org.junit.Test)44 ObjectId (org.eclipse.jgit.lib.ObjectId)35 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)27 RevCommit (org.eclipse.jgit.revwalk.RevCommit)26 Repository (org.eclipse.jgit.lib.Repository)21 RevId (com.google.gerrit.reviewdb.client.RevId)20 ChangeControl (com.google.gerrit.server.project.ChangeControl)20 ChangeData (com.google.gerrit.server.query.change.ChangeData)19 OrmException (com.google.gwtorm.server.OrmException)19 Timestamp (java.sql.Timestamp)18 RevWalk (org.eclipse.jgit.revwalk.RevWalk)15 Project (com.google.gerrit.reviewdb.client.Project)14 PatchSetApproval (com.google.gerrit.reviewdb.client.PatchSetApproval)11 TestChanges.newPatchSet (com.google.gerrit.testutil.TestChanges.newPatchSet)11 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)10 Account (com.google.gerrit.reviewdb.client.Account)10