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