Search in sources :

Example 46 with PatchSet

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

the class RelatedChangesSorter method collectById.

private Map<ObjectId, PatchSetData> collectById(List<ChangeData> in) throws IOException {
    Project.NameKey project = in.get(0).change().getProject();
    Map<ObjectId, PatchSetData> result = Maps.newHashMapWithExpectedSize(in.size() * 3);
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        rw.setRetainBody(true);
        for (ChangeData cd : in) {
            checkArgument(cd.change().getProject().equals(project), "Expected change %s in project %s, found %s", cd.getId(), project, cd.change().getProject());
            for (PatchSet ps : cd.patchSets()) {
                RevCommit c = rw.parseCommit(ps.commitId());
                PatchSetData psd = PatchSetData.create(cd, ps, c);
                result.put(ps.commitId(), psd);
            }
        }
    }
    return result;
}
Also used : Project(com.google.gerrit.entities.Project) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) PatchSet(com.google.gerrit.entities.PatchSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 47 with PatchSet

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

the class WalkSorter method byCommit.

private ListMultimap<RevCommit, PatchSetData> byCommit(RevWalk rw, Collection<ChangeData> in) throws IOException {
    ListMultimap<RevCommit, PatchSetData> byCommit = MultimapBuilder.hashKeys(in.size()).arrayListValues(1).build();
    for (ChangeData cd : in) {
        PatchSet maxPs = null;
        for (PatchSet ps : cd.patchSets()) {
            if (shouldInclude(ps) && (maxPs == null || ps.id().get() > maxPs.id().get())) {
                maxPs = ps;
            }
        }
        if (maxPs == null) {
            // No patch sets matched.
            continue;
        }
        try {
            RevCommit c = rw.parseCommit(maxPs.commitId());
            byCommit.put(c, PatchSetData.create(cd, maxPs, c));
        } catch (MissingObjectException | IncorrectObjectTypeException e) {
            logger.atWarning().withCause(e).log("missing commit %s for patch set %s", maxPs.commitId().name(), maxPs.id());
        }
    }
    return byCommit;
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) ChangeData(com.google.gerrit.server.query.change.ChangeData) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 48 with PatchSet

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

the class CommentPorter method portSamePatchset.

private ImmutableList<HumanComment> portSamePatchset(Project.NameKey project, Change change, PatchSet originalPatchset, PatchSet targetPatchset, ImmutableList<HumanComment> comments) {
    try (TraceTimer ignored = TraceContext.newTimer("Porting comments same patchset", Metadata.builder().projectName(project.get()).changeId(change.getChangeId()).patchSetId(originalPatchset.number()).build())) {
        Map<Short, List<HumanComment>> commentsPerSide = comments.stream().collect(groupingBy(comment -> comment.side));
        ImmutableList.Builder<HumanComment> portedComments = ImmutableList.builder();
        for (Map.Entry<Short, List<HumanComment>> sideAndComments : commentsPerSide.entrySet()) {
            portedComments.addAll(portSamePatchsetAndSide(project, change, originalPatchset, targetPatchset, sideAndComments.getValue(), sideAndComments.getKey()));
        }
        return portedComments.build();
    }
}
Also used : Patch(com.google.gerrit.entities.Patch) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Mapping(com.google.gerrit.server.patch.GitPositionTransformer.Mapping) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) Position(com.google.gerrit.server.patch.GitPositionTransformer.Position) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Function(java.util.function.Function) BestPositionOnConflict(com.google.gerrit.server.patch.GitPositionTransformer.BestPositionOnConflict) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Description(com.google.gerrit.metrics.Description) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Metadata(com.google.gerrit.server.logging.Metadata) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TraceContext(com.google.gerrit.server.logging.TraceContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DiffMappings(com.google.gerrit.server.patch.DiffMappings) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffOptions(com.google.gerrit.server.patch.DiffOptions) TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) List(java.util.List) FileEdits(com.google.gerrit.server.patch.filediff.FileEdits) Stream(java.util.stream.Stream) GitPositionTransformer(com.google.gerrit.server.patch.GitPositionTransformer) DiffOperations(com.google.gerrit.server.patch.DiffOperations) FileMapping(com.google.gerrit.server.patch.GitPositionTransformer.FileMapping) Project(com.google.gerrit.entities.Project) PositionedEntity(com.google.gerrit.server.patch.GitPositionTransformer.PositionedEntity) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Counter0(com.google.gerrit.metrics.Counter0) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Range(com.google.gerrit.entities.Comment.Range) Singleton(com.google.inject.Singleton) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) HumanComment(com.google.gerrit.entities.HumanComment) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 49 with PatchSet

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

the class CommentPorter method port.

private ImmutableList<HumanComment> port(ChangeNotes notes, PatchSet targetPatchset, List<HumanComment> comments) {
    Map<Integer, ImmutableList<HumanComment>> commentsPerPatchset = comments.stream().collect(groupingBy(comment -> comment.key.patchSetId, toImmutableList()));
    ImmutableList.Builder<HumanComment> portedComments = ImmutableList.builderWithExpectedSize(comments.size());
    for (Integer originalPatchsetId : commentsPerPatchset.keySet()) {
        ImmutableList<HumanComment> patchsetComments = commentsPerPatchset.get(originalPatchsetId);
        PatchSet originalPatchset = notes.getPatchSets().get(PatchSet.id(notes.getChangeId(), originalPatchsetId));
        if (originalPatchset != null) {
            portedComments.addAll(portSamePatchset(notes.getProjectName(), notes.getChange(), originalPatchset, targetPatchset, patchsetComments));
        } else {
            logger.atWarning().log("Some comments which should be ported refer to the non-existent patchset %s of" + " change %d. Omitting %d affected comments.", originalPatchsetId, notes.getChangeId().get(), patchsetComments.size());
        }
    }
    return portedComments.build();
}
Also used : Patch(com.google.gerrit.entities.Patch) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Mapping(com.google.gerrit.server.patch.GitPositionTransformer.Mapping) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) Position(com.google.gerrit.server.patch.GitPositionTransformer.Position) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) Function(java.util.function.Function) BestPositionOnConflict(com.google.gerrit.server.patch.GitPositionTransformer.BestPositionOnConflict) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Description(com.google.gerrit.metrics.Description) Map(java.util.Map) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Metadata(com.google.gerrit.server.logging.Metadata) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TraceContext(com.google.gerrit.server.logging.TraceContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) DiffMappings(com.google.gerrit.server.patch.DiffMappings) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffOptions(com.google.gerrit.server.patch.DiffOptions) TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) List(java.util.List) FileEdits(com.google.gerrit.server.patch.filediff.FileEdits) Stream(java.util.stream.Stream) GitPositionTransformer(com.google.gerrit.server.patch.GitPositionTransformer) DiffOperations(com.google.gerrit.server.patch.DiffOperations) FileMapping(com.google.gerrit.server.patch.GitPositionTransformer.FileMapping) Project(com.google.gerrit.entities.Project) PositionedEntity(com.google.gerrit.server.patch.GitPositionTransformer.PositionedEntity) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Counter0(com.google.gerrit.metrics.Counter0) MetricMaker(com.google.gerrit.metrics.MetricMaker) FluentLogger(com.google.common.flogger.FluentLogger) Range(com.google.gerrit.entities.Comment.Range) Singleton(com.google.inject.Singleton) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) PatchSet(com.google.gerrit.entities.PatchSet) HumanComment(com.google.gerrit.entities.HumanComment)

Example 50 with PatchSet

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

the class GetDiff method apply.

@Override
public Response<DiffInfo> apply(FileResource resource) throws BadRequestException, ResourceConflictException, ResourceNotFoundException, AuthException, InvalidChangeOperationException, IOException, PermissionBackendException {
    DiffPreferencesInfo prefs = new DiffPreferencesInfo();
    if (whitespace != null) {
        prefs.ignoreWhitespace = whitespace;
    } else if (ignoreWhitespace != null) {
        prefs.ignoreWhitespace = ignoreWhitespace.whitespace;
    } else {
        prefs.ignoreWhitespace = Whitespace.IGNORE_LEADING_AND_TRAILING;
    }
    prefs.intralineDifference = intraline;
    logger.atFine().log("diff preferences: ignoreWhitespace = %s, intralineDifference = %s", prefs.ignoreWhitespace, prefs.intralineDifference);
    PatchScriptFactory psf;
    PatchSet basePatchSet = null;
    PatchSet.Id pId = resource.getPatchKey().patchSetId();
    String fileName = resource.getPatchKey().fileName();
    logger.atFine().log("patchSetId = %d, fileName = %s, base = %s, parentNum = %d", pId.get(), fileName, base, parentNum);
    ChangeNotes notes = resource.getRevision().getNotes();
    if (base != null) {
        RevisionResource baseResource = revisions.parse(resource.getRevision().getChangeResource(), IdString.fromDecoded(base));
        basePatchSet = baseResource.getPatchSet();
        if (basePatchSet.id().get() == 0) {
            throw new BadRequestException("edit not allowed as base");
        }
        psf = patchScriptFactoryFactory.create(notes, fileName, basePatchSet.id(), pId, prefs, currentUser.get());
    } else if (parentNum > 0) {
        psf = patchScriptFactoryFactory.create(notes, fileName, parentNum, pId, prefs, currentUser.get());
    } else {
        psf = patchScriptFactoryFactory.create(notes, fileName, null, pId, prefs, currentUser.get());
    }
    try {
        PatchScript ps = psf.call();
        Project.NameKey projectName = resource.getRevision().getChange().getProject();
        ProjectState state = projectCache.get(projectName).orElseThrow(illegalState(projectName));
        DiffSide sideA = DiffSide.create(ps.getFileInfoA(), MoreObjects.firstNonNull(ps.getOldName(), ps.getNewName()), DiffSide.Type.SIDE_A);
        DiffSide sideB = DiffSide.create(ps.getFileInfoB(), ps.getNewName(), DiffSide.Type.SIDE_B);
        DiffWebLinksProvider webLinksProvider = new DiffWebLinksProviderImpl(sideA, sideB, projectName, basePatchSet, webLinks, resource);
        DiffInfoCreator diffInfoCreator = new DiffInfoCreator(state, webLinksProvider, intraline);
        DiffInfo result = diffInfoCreator.create(ps, sideA, sideB);
        Response<DiffInfo> r = Response.ok(result);
        if (resource.isCacheable()) {
            r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
        }
        return r;
    } catch (NoSuchChangeException e) {
        throw new ResourceNotFoundException(e.getMessage(), e);
    } catch (LargeObjectException e) {
        throw new ResourceConflictException(e.getMessage(), e);
    }
}
Also used : PatchScript(com.google.gerrit.common.data.PatchScript) PatchSet(com.google.gerrit.entities.PatchSet) RevisionResource(com.google.gerrit.server.change.RevisionResource) IdString(com.google.gerrit.extensions.restapi.IdString) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) DiffSide(com.google.gerrit.server.diff.DiffSide) Project(com.google.gerrit.entities.Project) LargeObjectException(com.google.gerrit.server.git.LargeObjectException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PatchScriptFactory(com.google.gerrit.server.patch.PatchScriptFactory) DiffWebLinksProvider(com.google.gerrit.server.diff.DiffWebLinksProvider) DiffInfoCreator(com.google.gerrit.server.diff.DiffInfoCreator) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) ProjectState(com.google.gerrit.server.project.ProjectState) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) DiffInfo(com.google.gerrit.extensions.common.DiffInfo)

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