Search in sources :

Example 1 with DiffSide

use of com.google.gerrit.server.diff.DiffSide 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)

Example 2 with DiffSide

use of com.google.gerrit.server.diff.DiffSide in project gerrit by GerritCodeReview.

the class GetFixPreview method getFixPreviewForSingleFile.

private DiffInfo getFixPreviewForSingleFile(Repository git, PatchSet patchSet, ProjectState state, ChangeNotes notes, String fileName, ImmutableList<FixReplacement> fixReplacements) throws PermissionBackendException, AuthException, LargeObjectException, InvalidChangeOperationException, IOException, ResourceNotFoundException {
    PatchScriptFactoryForAutoFix psf = patchScriptFactoryFactory.create(git, notes, fileName, patchSet, fixReplacements, DiffPreferencesInfo.defaults());
    PatchScript ps = psf.call();
    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);
    DiffInfoCreator diffInfoCreator = new DiffInfoCreator(state, new DiffWebLinksProviderImpl(), true);
    return diffInfoCreator.create(ps, sideA, sideB);
}
Also used : PatchScriptFactoryForAutoFix(com.google.gerrit.server.patch.PatchScriptFactoryForAutoFix) PatchScript(com.google.gerrit.common.data.PatchScript) DiffInfoCreator(com.google.gerrit.server.diff.DiffInfoCreator) DiffSide(com.google.gerrit.server.diff.DiffSide)

Aggregations

PatchScript (com.google.gerrit.common.data.PatchScript)2 DiffInfoCreator (com.google.gerrit.server.diff.DiffInfoCreator)2 DiffSide (com.google.gerrit.server.diff.DiffSide)2 PatchSet (com.google.gerrit.entities.PatchSet)1 Project (com.google.gerrit.entities.Project)1 DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)1 DiffInfo (com.google.gerrit.extensions.common.DiffInfo)1 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)1 IdString (com.google.gerrit.extensions.restapi.IdString)1 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)1 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)1 RevisionResource (com.google.gerrit.server.change.RevisionResource)1 DiffWebLinksProvider (com.google.gerrit.server.diff.DiffWebLinksProvider)1 LargeObjectException (com.google.gerrit.server.git.LargeObjectException)1 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)1 PatchScriptFactory (com.google.gerrit.server.patch.PatchScriptFactory)1 PatchScriptFactoryForAutoFix (com.google.gerrit.server.patch.PatchScriptFactoryForAutoFix)1 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)1 ProjectState (com.google.gerrit.server.project.ProjectState)1