Search in sources :

Example 66 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ReviewCommand method addPatchSetId.

@Argument(index = 0, required = true, multiValued = true, metaVar = "{COMMIT | CHANGE,PATCHSET}", usage = "list of commits or patch sets to review")
void addPatchSetId(String token) {
    try {
        PatchSet ps = psParser.parsePatchSet(token, projectState, branch);
        patchSets.add(ps);
    } catch (UnloggedFailure e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    } catch (StorageException e) {
        throw new IllegalArgumentException("database error", e);
    }
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) StorageException(com.google.gerrit.exceptions.StorageException) Argument(org.kohsuke.args4j.Argument)

Example 67 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class PatchSetParser method parsePatchSet.

public PatchSet parsePatchSet(String token, ProjectState projectState, String branch) throws UnloggedFailure {
    // 
    if (token.matches("^([0-9a-fA-F]{4," + ObjectIds.STR_LEN + "})$")) {
        InternalChangeQuery query = queryProvider.get();
        List<ChangeData> cds;
        if (projectState != null) {
            Project.NameKey p = projectState.getNameKey();
            if (branch != null) {
                cds = query.byBranchCommit(p.get(), branch, token);
            } else {
                cds = query.byProjectCommit(p, token);
            }
        } else {
            cds = query.byCommit(token);
        }
        List<PatchSet> matches = new ArrayList<>(cds.size());
        for (ChangeData cd : cds) {
            Change c = cd.change();
            if (!(inProject(c, projectState) && inBranch(c, branch))) {
                continue;
            }
            for (PatchSet ps : cd.patchSets()) {
                if (ObjectIds.matchesAbbreviation(ps.commitId(), token)) {
                    matches.add(ps);
                }
            }
        }
        switch(matches.size()) {
            case 1:
                return matches.iterator().next();
            case 0:
                throw error("\"" + token + "\" no such patch set");
            default:
                throw error("\"" + token + "\" matches multiple patch sets");
        }
    }
    // 
    if (token.matches("^[1-9][0-9]*,[1-9][0-9]*$")) {
        PatchSet.Id patchSetId;
        try {
            patchSetId = PatchSet.Id.parse(token);
        } catch (IllegalArgumentException e) {
            throw error("\"" + token + "\" is not a valid patch set", e);
        }
        ChangeNotes notes = getNotes(projectState, patchSetId.changeId());
        PatchSet patchSet = psUtil.get(notes, patchSetId);
        if (patchSet == null) {
            throw error("\"" + token + "\" no such patch set");
        }
        if (projectState != null || branch != null) {
            Change change = notes.getChange();
            if (!inProject(change, projectState)) {
                throw error("change " + change.getId() + " not in project " + projectState.getName());
            }
            if (!inBranch(change, branch)) {
                throw error("change " + change.getId() + " not in branch " + branch);
            }
        }
        return patchSet;
    }
    throw error("\"" + token + "\" is not a valid patch set");
}
Also used : InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) Project(com.google.gerrit.entities.Project) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 68 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ChangeEditIT method rebaseEdit.

@Test
public void rebaseEdit() throws Exception {
    PatchSet previousPatchSet = getCurrentPatchSet(changeId2);
    createEmptyEditFor(changeId2);
    gApi.changes().id(changeId2).edit().modifyFile(FILE_NAME, RawInputUtil.create(CONTENT_NEW));
    addNewPatchSet(changeId2);
    PatchSet currentPatchSet = getCurrentPatchSet(changeId2);
    Optional<EditInfo> originalEdit = getEdit(changeId2);
    assertThat(originalEdit).value().baseRevision().isEqualTo(previousPatchSet.commitId().name());
    Timestamp beforeRebase = originalEdit.get().commit.committer.date;
    gApi.changes().id(changeId2).edit().rebase();
    ensureSameBytes(getFileContentOfEdit(changeId2, FILE_NAME), CONTENT_NEW);
    ensureSameBytes(getFileContentOfEdit(changeId2, FILE_NAME2), CONTENT_NEW2);
    Optional<EditInfo> rebasedEdit = getEdit(changeId2);
    assertThat(rebasedEdit).value().baseRevision().isEqualTo(currentPatchSet.commitId().name());
    assertThat(rebasedEdit).value().commit().committer().date().isNotEqualTo(beforeRebase);
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) EditInfo(com.google.gerrit.extensions.common.EditInfo) Timestamp(java.sql.Timestamp) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 69 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class ApprovalInference method getForPatchSetWithoutNormalization.

private Collection<PatchSetApproval> getForPatchSetWithoutNormalization(ChangeNotes notes, ProjectState project, PatchSet patchSet, RevWalk rw, Config repoConfig) {
    checkState(project.getNameKey().equals(notes.getProjectName()), "project must match %s, %s", project.getNameKey(), notes.getProjectName());
    PatchSet.Id psId = patchSet.id();
    // Add approvals on the given patch set to the result
    Table<String, Account.Id, PatchSetApproval> resultByUser = HashBasedTable.create();
    ImmutableList<PatchSetApproval> nonCopiedApprovalsForGivenPatchSet = notes.load().getApprovals().get(patchSet.id());
    nonCopiedApprovalsForGivenPatchSet.forEach(psa -> resultByUser.put(psa.label(), psa.accountId(), psa));
    // given patch set.
    if (psId.get() == 1) {
        return resultByUser.values();
    }
    Map.Entry<PatchSet.Id, PatchSet> priorPatchSet = notes.load().getPatchSets().lowerEntry(psId);
    if (priorPatchSet == null) {
        return resultByUser.values();
    }
    ImmutableList<PatchSetApproval> priorApprovalsIncludingCopied = notes.load().getApprovalsWithCopied().get(priorPatchSet.getKey());
    // Add labels from the previous patch set to the result in case the label isn't already there
    // and settings as well as change kind allow copying.
    ChangeKind changeKind = changeKindCache.getChangeKind(project.getNameKey(), rw, repoConfig, priorPatchSet.getValue().commitId(), patchSet.commitId());
    logger.atFine().log("change kind for patch set %d of change %d against prior patch set %s is %s", patchSet.id().get(), patchSet.id().changeId().get(), priorPatchSet.getValue().id().changeId(), changeKind);
    Map<String, ModifiedFile> baseVsCurrent = null;
    Map<String, ModifiedFile> baseVsPrior = null;
    Map<String, ModifiedFile> priorVsCurrent = null;
    LabelTypes labelTypes = project.getLabelTypes();
    for (PatchSetApproval psa : priorApprovalsIncludingCopied) {
        if (resultByUser.contains(psa.label(), psa.accountId())) {
            continue;
        }
        Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
        // Only compute modified files if there is a relevant label, since this is expensive.
        if (baseVsCurrent == null && type.isPresent() && type.get().isCopyAllScoresIfListOfFilesDidNotChange()) {
            baseVsCurrent = listModifiedFiles(project, patchSet, rw, repoConfig);
            baseVsPrior = listModifiedFiles(project, priorPatchSet.getValue(), rw, repoConfig);
            priorVsCurrent = listModifiedFiles(project, priorPatchSet.getValue().commitId(), patchSet.commitId(), rw, repoConfig);
        }
        if (!type.isPresent()) {
            logger.atFine().log("approval %d on label %s of patch set %d of change %d cannot be copied" + " to patch set %d because the label no longer exists on project %s", psa.value(), psa.label(), psa.key().patchSetId().get(), psa.key().patchSetId().changeId().get(), psId.get(), project.getName());
            continue;
        }
        if (!canCopyBasedOnBooleanLabelConfigs(project, psa, patchSet.id(), changeKind, type.get(), baseVsCurrent, baseVsPrior, priorVsCurrent) && !canCopyBasedOnCopyCondition(notes, psa, patchSet, type.get(), changeKind, rw, repoConfig)) {
            continue;
        }
        resultByUser.put(psa.label(), psa.accountId(), psa.copyWithPatchSet(patchSet.id()));
    }
    return resultByUser.values();
}
Also used : LabelTypes(com.google.gerrit.entities.LabelTypes) PatchSet(com.google.gerrit.entities.PatchSet) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelType(com.google.gerrit.entities.LabelType) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile) ObjectId(org.eclipse.jgit.lib.ObjectId) Map(java.util.Map) ChangeKind(com.google.gerrit.extensions.client.ChangeKind)

Example 70 with PatchSet

use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.

the class CommitUtil method createRevertCommit.

/**
 * Creates a revert commit.
 *
 * @param message Commit message for the revert commit.
 * @param notes ChangeNotes of the change being reverted.
 * @param user Current User performing the revert.
 * @param ts Timestamp of creation for the commit.
 * @param oi ObjectInserter for inserting the newly created commit.
 * @param revWalk Used for parsing the original commit.
 * @param generatedChangeId The changeId for the commit message, can be null since it is not
 *     needed for commits, only for changes.
 * @return ObjectId that represents the newly created commit.
 * @throws ResourceConflictException Can't revert the initial commit.
 * @throws IOException Thrown in case of I/O errors.
 */
private ObjectId createRevertCommit(String message, ChangeNotes notes, CurrentUser user, Instant ts, ObjectInserter oi, RevWalk revWalk, @Nullable ObjectId generatedChangeId) throws ResourceConflictException, IOException {
    PatchSet patch = notes.getCurrentPatchSet();
    RevCommit commitToRevert = revWalk.parseCommit(patch.commitId());
    if (commitToRevert.getParentCount() == 0) {
        throw new ResourceConflictException("Cannot revert initial commit");
    }
    PersonIdent committerIdent = serverIdent.get();
    PersonIdent authorIdent = user.asIdentifiedUser().newCommitterIdent(ts, committerIdent.getTimeZone());
    RevCommit parentToCommitToRevert = commitToRevert.getParent(0);
    revWalk.parseHeaders(parentToCommitToRevert);
    CommitBuilder revertCommitBuilder = new CommitBuilder();
    revertCommitBuilder.addParentId(commitToRevert);
    revertCommitBuilder.setTreeId(parentToCommitToRevert.getTree());
    revertCommitBuilder.setAuthor(authorIdent);
    revertCommitBuilder.setCommitter(authorIdent);
    Change changeToRevert = notes.getChange();
    String subject = changeToRevert.getSubject();
    if (subject.length() > 63) {
        subject = subject.substring(0, 59) + "...";
    }
    if (message == null) {
        message = MessageFormat.format(ChangeMessages.get().revertChangeDefaultMessage, subject, patch.commitId().name());
    }
    if (generatedChangeId != null) {
        revertCommitBuilder.setMessage(ChangeIdUtil.insertId(message, generatedChangeId, true));
    }
    ObjectId id = oi.insert(revertCommitBuilder);
    oi.flush();
    return id;
}
Also used : ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) PatchSet(com.google.gerrit.entities.PatchSet) Change(com.google.gerrit.entities.Change) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

PatchSet (com.google.gerrit.entities.PatchSet)123 Change (com.google.gerrit.entities.Change)61 Test (org.junit.Test)48 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)41 ObjectId (org.eclipse.jgit.lib.ObjectId)35 RevCommit (org.eclipse.jgit.revwalk.RevCommit)29 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)28 Project (com.google.gerrit.entities.Project)25 StorageException (com.google.gerrit.exceptions.StorageException)25 Repository (org.eclipse.jgit.lib.Repository)22 IOException (java.io.IOException)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)19 ChangeData (com.google.gerrit.server.query.change.ChangeData)18 HumanComment (com.google.gerrit.entities.HumanComment)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)16 Inject (com.google.inject.Inject)14 Map (java.util.Map)14 List (java.util.List)13 ImmutableList (com.google.common.collect.ImmutableList)12 AuthException (com.google.gerrit.extensions.restapi.AuthException)11