Search in sources :

Example 1 with DiffWebLinksProvider

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

PatchScript (com.google.gerrit.common.data.PatchScript)1 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 DiffInfoCreator (com.google.gerrit.server.diff.DiffInfoCreator)1 DiffSide (com.google.gerrit.server.diff.DiffSide)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 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)1 ProjectState (com.google.gerrit.server.project.ProjectState)1