Search in sources :

Example 1 with GitFileDiff

use of com.google.gerrit.server.patch.gitfilediff.GitFileDiff in project gerrit by GerritCodeReview.

the class AllDiffsEvaluator method execute.

Map<AugmentedFileDiffCacheKey, AllFileGitDiffs> execute(List<AugmentedFileDiffCacheKey> augmentedKeys) throws DiffNotAvailableException {
    ImmutableMap.Builder<AugmentedFileDiffCacheKey, AllFileGitDiffs> keyToAllDiffs = ImmutableMap.builderWithExpectedSize(augmentedKeys.size());
    List<AugmentedFileDiffCacheKey> keysWithRebaseEdits = augmentedKeys.stream().filter(k -> !k.ignoreRebase()).collect(Collectors.toList());
    // TODO(ghareeb): as an enhancement, you can batch these calls as follows.
    // First batch: "old commit vs. new commit" and "new parent vs. new commit"
    // Second batch: "old parent vs. old commit" and "old parent vs. new parent"
    Map<FileDiffCacheKey, GitDiffEntity> mainDiffs = computeGitFileDiffs(createGitKeys(augmentedKeys, k -> k.key().oldCommit(), k -> k.key().newCommit(), k -> k.key().newFilePath()));
    Map<FileDiffCacheKey, GitDiffEntity> oldVsParentDiffs = computeGitFileDiffs(createGitKeys(keysWithRebaseEdits, // oldParent is set for keysWithRebaseEdits
    k -> k.oldParentId().get(), k -> k.key().oldCommit(), k -> mainDiffs.get(k.key()).gitDiff().oldPath().orElse(null)));
    Map<FileDiffCacheKey, GitDiffEntity> newVsParentDiffs = computeGitFileDiffs(createGitKeys(keysWithRebaseEdits, // newParent is set for keysWithRebaseEdits
    k -> k.newParentId().get(), k -> k.key().newCommit(), k -> k.key().newFilePath()));
    Map<FileDiffCacheKey, GitDiffEntity> parentsDiffs = computeGitFileDiffs(createGitKeys(keysWithRebaseEdits, k -> k.oldParentId().get(), k -> k.newParentId().get(), k -> {
        GitFileDiff newVsParDiff = newVsParentDiffs.get(k.key()).gitDiff();
        // the first situation described?
        return newVsParDiff.oldPath().orElse(k.key().newFilePath());
    }));
    for (AugmentedFileDiffCacheKey augmentedKey : augmentedKeys) {
        FileDiffCacheKey key = augmentedKey.key();
        AllFileGitDiffs.Builder builder = AllFileGitDiffs.builder().augmentedKey(augmentedKey).mainDiff(mainDiffs.get(key));
        if (augmentedKey.ignoreRebase()) {
            keyToAllDiffs.put(augmentedKey, builder.build());
            continue;
        }
        if (oldVsParentDiffs.containsKey(key) && !oldVsParentDiffs.get(key).gitDiff().isEmpty()) {
            builder.oldVsParentDiff(Optional.of(oldVsParentDiffs.get(key)));
        }
        if (newVsParentDiffs.containsKey(key) && !newVsParentDiffs.get(key).gitDiff().isEmpty()) {
            builder.newVsParentDiff(Optional.of(newVsParentDiffs.get(key)));
        }
        if (parentsDiffs.containsKey(key) && !parentsDiffs.get(key).gitDiff().isEmpty()) {
            builder.parentVsParentDiff(Optional.of(parentsDiffs.get(key)));
        }
        keyToAllDiffs.put(augmentedKey, builder.build());
    }
    return keyToAllDiffs.build();
}
Also used : GitFileDiffCacheKey(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheKey) GitFileDiffCache(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCache) ImmutableMap(com.google.common.collect.ImmutableMap) Inject(com.google.inject.Inject) IOException(java.io.IOException) HashMap(java.util.HashMap) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ObjectId(org.eclipse.jgit.lib.ObjectId) Assisted(com.google.inject.assistedinject.Assisted) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffUtil(com.google.gerrit.server.patch.DiffUtil) GitFileDiff(com.google.gerrit.server.patch.gitfilediff.GitFileDiff) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) GitFileDiffCacheKey(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheKey) GitFileDiff(com.google.gerrit.server.patch.gitfilediff.GitFileDiff) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with GitFileDiff

use of com.google.gerrit.server.patch.gitfilediff.GitFileDiff in project gerrit by GerritCodeReview.

the class GitFileDiffSerializerTest method roundTrip.

@Test
public void roundTrip() {
    ImmutableList<Edit> edits = ImmutableList.of(Edit.create(1, 5, 3, 4), Edit.create(21, 30, 150, 158));
    GitFileDiff gitFileDiff = GitFileDiff.builder().edits(edits).fileHeader("file_header").oldPath(Optional.of("old_file_path.txt")).newPath(Optional.empty()).oldId(AbbreviatedObjectId.fromObjectId(OLD_ID)).newId(AbbreviatedObjectId.fromObjectId(NEW_ID)).changeType(ChangeType.DELETED).patchType(Optional.of(PatchType.UNIFIED)).oldMode(Optional.of(FileMode.REGULAR_FILE)).newMode(Optional.of(FileMode.REGULAR_FILE)).negative(Optional.of(true)).build();
    byte[] serialized = Serializer.INSTANCE.serialize(gitFileDiff);
    assertThat(Serializer.INSTANCE.deserialize(serialized)).isEqualTo(gitFileDiff);
}
Also used : GitFileDiff(com.google.gerrit.server.patch.gitfilediff.GitFileDiff) Edit(com.google.gerrit.server.patch.filediff.Edit) Test(org.junit.Test)

Example 3 with GitFileDiff

use of com.google.gerrit.server.patch.gitfilediff.GitFileDiff in project gerrit by GerritCodeReview.

the class AllDiffsEvaluator method computeGitFileDiffs.

/**
 * Computes the git diff for the git keys of the input map {@code keys} parameter. The computation
 * uses the underlying {@link GitFileDiffCache}.
 */
private Map<FileDiffCacheKey, GitDiffEntity> computeGitFileDiffs(Map<FileDiffCacheKey, GitFileDiffCacheKey> keys) throws DiffNotAvailableException {
    ImmutableMap.Builder<FileDiffCacheKey, GitDiffEntity> result = ImmutableMap.builderWithExpectedSize(keys.size());
    ImmutableMap<GitFileDiffCacheKey, GitFileDiff> gitDiffs = gitCache.getAll(keys.values());
    for (FileDiffCacheKey key : keys.keySet()) {
        GitFileDiffCacheKey gitKey = keys.get(key);
        GitFileDiff gitFileDiff = gitDiffs.get(gitKey);
        result.put(key, GitDiffEntity.create(gitKey, gitFileDiff));
    }
    return result.build();
}
Also used : GitFileDiffCacheKey(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheKey) GitFileDiff(com.google.gerrit.server.patch.gitfilediff.GitFileDiff) GitFileDiffCacheKey(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheKey) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

GitFileDiff (com.google.gerrit.server.patch.gitfilediff.GitFileDiff)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 GitFileDiffCacheKey (com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheKey)2 FluentLogger (com.google.common.flogger.FluentLogger)1 DiffNotAvailableException (com.google.gerrit.server.patch.DiffNotAvailableException)1 DiffUtil (com.google.gerrit.server.patch.DiffUtil)1 Edit (com.google.gerrit.server.patch.filediff.Edit)1 GitFileDiffCache (com.google.gerrit.server.patch.gitfilediff.GitFileDiffCache)1 Inject (com.google.inject.Inject)1 Assisted (com.google.inject.assistedinject.Assisted)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1 Test (org.junit.Test)1