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;
}
}
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();
}
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);
}
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());
}
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);
}
}
Aggregations