Search in sources :

Example 1 with FileDiffCacheKey

use of com.google.gerrit.server.patch.filediff.FileDiffCacheKey in project gerrit by GerritCodeReview.

the class DiffOperationsImpl method getModifiedFilesForKeys.

/**
 * Lookup the file diffs for the input {@code keys}. For results where the cache reports negative
 * results, e.g. due to timeouts in the cache loader, this method requests the diff again using
 * the fallback algorithm {@link DiffAlgorithm#HISTOGRAM_NO_FALLBACK}.
 */
private ImmutableMap<String, FileDiffOutput> getModifiedFilesForKeys(List<FileDiffCacheKey> keys, DiffOptions diffOptions) throws DiffNotAvailableException {
    ImmutableMap<FileDiffCacheKey, FileDiffOutput> fileDiffs = fileDiffCache.getAll(keys);
    List<FileDiffCacheKey> fallbackKeys = new ArrayList<>();
    ImmutableList.Builder<FileDiffOutput> result = ImmutableList.builder();
    // Use the fallback diff algorithm for negative results
    for (FileDiffCacheKey key : fileDiffs.keySet()) {
        FileDiffOutput diff = fileDiffs.get(key);
        if (diff.isNegative()) {
            FileDiffCacheKey fallbackKey = createFileDiffCacheKey(key.project(), key.oldCommit(), key.newCommit(), key.newFilePath(), // Use the fallback diff algorithm
            DiffAlgorithm.HISTOGRAM_NO_FALLBACK, /* useTimeout= */
            false, key.whitespace());
            fallbackKeys.add(fallbackKey);
        } else {
            result.add(diff);
        }
    }
    result.addAll(fileDiffCache.getAll(fallbackKeys).values());
    return mapByFilePath(result.build(), diffOptions);
}
Also used : FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput)

Example 2 with FileDiffCacheKey

use of com.google.gerrit.server.patch.filediff.FileDiffCacheKey in project gerrit by GerritCodeReview.

the class DiffOperationsImpl method getModifiedFileAgainstParent.

@Override
public FileDiffOutput getModifiedFileAgainstParent(Project.NameKey project, ObjectId newCommit, int parent, String fileName, @Nullable DiffPreferencesInfo.Whitespace whitespace) throws DiffNotAvailableException {
    try {
        DiffParameters diffParams = computeDiffParameters(project, newCommit, parent);
        FileDiffCacheKey key = createFileDiffCacheKey(project, diffParams.baseCommit(), newCommit, fileName, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
        true, whitespace);
        return getModifiedFileForKey(key);
    } catch (IOException e) {
        throw new DiffNotAvailableException("Failed to evaluate the parent/base commit for commit " + newCommit, e);
    }
}
Also used : FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) IOException(java.io.IOException)

Example 3 with FileDiffCacheKey

use of com.google.gerrit.server.patch.filediff.FileDiffCacheKey in project gerrit by GerritCodeReview.

the class DiffOperationsImpl method getModifiedFiles.

private ImmutableMap<String, FileDiffOutput> getModifiedFiles(DiffParameters diffParams, DiffOptions diffOptions) throws DiffNotAvailableException {
    try {
        Project.NameKey project = diffParams.project();
        ObjectId newCommit = diffParams.newCommit();
        ObjectId oldCommit = diffParams.baseCommit();
        ComparisonType cmp = diffParams.comparisonType();
        ImmutableList<ModifiedFile> modifiedFiles = modifiedFilesCache.get(createModifiedFilesKey(project, oldCommit, newCommit));
        List<FileDiffCacheKey> fileCacheKeys = new ArrayList<>();
        fileCacheKeys.add(createFileDiffCacheKey(project, oldCommit, newCommit, COMMIT_MSG, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
        true, /* whitespace= */
        null));
        if (cmp.isAgainstAutoMerge() || isMergeAgainstParent(cmp, project, newCommit)) {
            fileCacheKeys.add(createFileDiffCacheKey(project, oldCommit, newCommit, MERGE_LIST, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
            true, /*whitespace = */
            null));
        }
        if (diffParams.skipFiles() == null) {
            modifiedFiles.stream().map(entity -> createFileDiffCacheKey(project, oldCommit, newCommit, entity.newPath().isPresent() ? entity.newPath().get() : entity.oldPath().get(), DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
            true, /* whitespace= */
            null)).forEach(fileCacheKeys::add);
        }
        return getModifiedFilesForKeys(fileCacheKeys, diffOptions);
    } catch (IOException e) {
        throw new DiffNotAvailableException(e);
    }
}
Also used : Patch(com.google.gerrit.entities.Patch) Module(com.google.inject.Module) ModifiedFilesCache(com.google.gerrit.server.patch.diff.ModifiedFilesCache) Whitespace(com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace) DiffAlgorithm(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheImpl.DiffAlgorithm) Inject(com.google.inject.Inject) FileDiffCache(com.google.gerrit.server.patch.filediff.FileDiffCache) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) ImmutableCollection(com.google.common.collect.ImmutableCollection) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) ChangeType(com.google.gerrit.entities.Patch.ChangeType) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Map(java.util.Map) ModifiedFilesCacheKey(com.google.gerrit.server.patch.diff.ModifiedFilesCacheKey) GitModifiedFilesCacheImpl(com.google.gerrit.server.patch.gitdiff.GitModifiedFilesCacheImpl) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) ImmutableMap(com.google.common.collect.ImmutableMap) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile) DisabledOutputStream(org.eclipse.jgit.util.io.DisabledOutputStream) FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) FileDiffCacheImpl(com.google.gerrit.server.patch.filediff.FileDiffCacheImpl) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) CacheModule(com.google.gerrit.server.cache.CacheModule) GitFileDiffCacheImpl(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheImpl) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) DiffEntry(org.eclipse.jgit.diff.DiffEntry) ModifiedFilesCacheImpl(com.google.gerrit.server.patch.diff.ModifiedFilesCacheImpl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Project(com.google.gerrit.entities.Project) FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile)

Example 4 with FileDiffCacheKey

use of com.google.gerrit.server.patch.filediff.FileDiffCacheKey in project gerrit by GerritCodeReview.

the class FileDiffCacheKeySerializerTest method roundTrip.

@Test
public void roundTrip() {
    FileDiffCacheKey key = FileDiffCacheKey.builder().project(Project.nameKey("project/x")).oldCommit(COMMIT_ID_1).newCommit(COMMIT_ID_2).newFilePath("some_file.txt").renameScore(65).diffAlgorithm(DiffAlgorithm.HISTOGRAM_WITH_FALLBACK_MYERS).whitespace(Whitespace.IGNORE_ALL).useTimeout(true).build();
    byte[] serialized = FileDiffCacheKey.Serializer.INSTANCE.serialize(key);
    assertThat(FileDiffCacheKey.Serializer.INSTANCE.deserialize(serialized)).isEqualTo(key);
}
Also used : FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) Test(org.junit.Test)

Aggregations

FileDiffCacheKey (com.google.gerrit.server.patch.filediff.FileDiffCacheKey)4 ImmutableList (com.google.common.collect.ImmutableList)2 FileDiffOutput (com.google.gerrit.server.patch.filediff.FileDiffOutput)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AutoValue (com.google.auto.value.AutoValue)1 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FluentLogger (com.google.common.flogger.FluentLogger)1 Nullable (com.google.gerrit.common.Nullable)1 Patch (com.google.gerrit.entities.Patch)1 COMMIT_MSG (com.google.gerrit.entities.Patch.COMMIT_MSG)1 ChangeType (com.google.gerrit.entities.Patch.ChangeType)1 MERGE_LIST (com.google.gerrit.entities.Patch.MERGE_LIST)1 Project (com.google.gerrit.entities.Project)1 DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)1 Whitespace (com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace)1 CacheModule (com.google.gerrit.server.cache.CacheModule)1 ModifiedFilesCache (com.google.gerrit.server.patch.diff.ModifiedFilesCache)1 ModifiedFilesCacheImpl (com.google.gerrit.server.patch.diff.ModifiedFilesCacheImpl)1