Search in sources :

Example 1 with DiffPreferencesInfo

use of com.google.gerrit.extensions.client.DiffPreferencesInfo in project gerrit by GerritCodeReview.

the class GetDiffPreferences method readFromGit.

static DiffPreferencesInfo readFromGit(Account.Id id, GitRepositoryManager gitMgr, AllUsersName allUsersName, DiffPreferencesInfo in) throws IOException, ConfigInvalidException, RepositoryNotFoundException {
    try (Repository git = gitMgr.openRepository(allUsersName)) {
        VersionedAccountPreferences p = VersionedAccountPreferences.forUser(id);
        p.load(git);
        DiffPreferencesInfo prefs = new DiffPreferencesInfo();
        loadSection(p.getConfig(), UserConfigSections.DIFF, null, prefs, readDefaultsFromGit(git, in), in);
        return prefs;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo)

Example 2 with DiffPreferencesInfo

use of com.google.gerrit.extensions.client.DiffPreferencesInfo in project gerrit by GerritCodeReview.

the class SubmitWithStickyApprovalDiff method getDiffForFile.

private String getDiffForFile(ChangeNotes notes, PatchSet.Id currentPatchsetId, PatchSet.Id latestApprovedPatchsetId, FileDiffOutput fileDiffOutput, CurrentUser currentUser, @Nullable List<String> formatterResult, boolean isDiffTooLarge) throws AuthException, InvalidChangeOperationException, IOException, PermissionBackendException {
    StringBuilder diff = new StringBuilder(String.format("```\nThe name of the file: %s\nInsertions: %d, Deletions: %d.\n\n", fileDiffOutput.newPath().isPresent() ? fileDiffOutput.newPath().get() : fileDiffOutput.oldPath().get(), fileDiffOutput.insertions(), fileDiffOutput.deletions()));
    DiffPreferencesInfo diffPreferencesInfo = createDefaultDiffPreferencesInfo();
    PatchScriptFactory patchScriptFactory = patchScriptFactoryFactory.create(notes, fileDiffOutput.newPath().isPresent() ? fileDiffOutput.newPath().get() : fileDiffOutput.oldPath().get(), latestApprovedPatchsetId, currentPatchsetId, diffPreferencesInfo, currentUser);
    PatchScript patchScript = null;
    try {
        // TODO(paiking): we can get rid of this call to optimize by checking the diff for renames.
        patchScript = patchScriptFactory.call();
    } catch (LargeObjectException exception) {
        diff.append("The file content is too large for showing the full diff. \n\n");
        return diff.toString();
    }
    if (patchScript.getChangeType() == ChangeType.RENAMED) {
        diff.append(String.format("The file %s was renamed to %s\n", fileDiffOutput.oldPath().get(), fileDiffOutput.newPath().get()));
    }
    if (isDiffTooLarge) {
        diff.append("The diff is too large to show. Please review the diff.");
        diff.append("\n```\n");
        return diff.toString();
    }
    // This filters only the file we need.
    // TODO(paiking): we can make this more efficient by mapping the files to their respective
    // diffs prior to this method, such that we need to go over the diff only once.
    diff.append(getDiffForFile(patchScript, formatterResult));
    // This line (and the ``` above) are useful for formatting in the web UI.
    diff.append("\n```\n");
    return diff.toString();
}
Also used : LargeObjectException(com.google.gerrit.server.git.LargeObjectException) PatchScript(com.google.gerrit.common.data.PatchScript) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo)

Example 3 with DiffPreferencesInfo

use of com.google.gerrit.extensions.client.DiffPreferencesInfo in project gerrit by GerritCodeReview.

the class PreferencesParserUtil method parseDefaultDiffPreferences.

/**
 * Returns a {@link DiffPreferencesInfo} that is the result of parsing {@code defaultCfg} for the
 * server's default configs. These configs are then overlaid to inherit values (default -> input
 * (if provided).
 */
public static DiffPreferencesInfo parseDefaultDiffPreferences(Config defaultCfg, DiffPreferencesInfo input) throws ConfigInvalidException {
    DiffPreferencesInfo allUserPrefs = new DiffPreferencesInfo();
    loadSection(defaultCfg, UserConfigSections.DIFF, null, allUserPrefs, DiffPreferencesInfo.defaults(), input);
    return updateDiffPreferencesDefaults(allUserPrefs);
}
Also used : DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo)

Example 4 with DiffPreferencesInfo

use of com.google.gerrit.extensions.client.DiffPreferencesInfo in project gerrit by GerritCodeReview.

the class DiffPreferencesIT method getDiffPreferences.

@Test
public void getDiffPreferences() throws Exception {
    DiffPreferencesInfo result = gApi.config().server().getDefaultDiffPreferences();
    assertPrefs(result, DiffPreferencesInfo.defaults());
}
Also used : DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 5 with DiffPreferencesInfo

use of com.google.gerrit.extensions.client.DiffPreferencesInfo 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

DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 Test (org.junit.Test)8 PatchScript (com.google.gerrit.common.data.PatchScript)3 VersionedAccountPreferences (com.google.gerrit.server.account.VersionedAccountPreferences)3 LargeObjectException (com.google.gerrit.server.git.LargeObjectException)3 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)3 Repository (org.eclipse.jgit.lib.Repository)3 DiffInfo (com.google.gerrit.extensions.common.DiffInfo)2 IdString (com.google.gerrit.extensions.restapi.IdString)2 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 CachedPreferences (com.google.gerrit.server.config.CachedPreferences)2 PatchScriptFactory (com.google.gerrit.server.patch.PatchScriptFactory)2 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)2 ProjectState (com.google.gerrit.server.project.ProjectState)2 PatchSet (com.google.gerrit.entities.PatchSet)1 Project (com.google.gerrit.entities.Project)1 EditPreferencesInfo (com.google.gerrit.extensions.client.EditPreferencesInfo)1 GeneralPreferencesInfo (com.google.gerrit.extensions.client.GeneralPreferencesInfo)1