use of com.google.gerrit.server.patch.gitdiff.ModifiedFile in project gerrit by GerritCodeReview.
the class ListOfFilesUnchangedPredicate method match.
/**
* returns {@code true} if the files that were modified are the same in both inputs, and the
* {@link ChangeType} matches for each modified file.
*/
public boolean match(Map<String, ModifiedFile> baseVsCurrent, Map<String, ModifiedFile> baseVsPrior, Map<String, ModifiedFile> priorVsCurrent) {
Set<String> allFiles = new HashSet<>();
allFiles.addAll(baseVsCurrent.keySet());
allFiles.addAll(baseVsPrior.keySet());
for (String file : allFiles) {
if (Patch.isMagic(file)) {
continue;
}
ModifiedFile modifiedFile1 = baseVsCurrent.get(file);
ModifiedFile modifiedFile2 = baseVsPrior.get(file);
if (!priorVsCurrent.containsKey(file)) {
// it. The file might have been modified due to rebase.
continue;
}
if (modifiedFile1 == null || modifiedFile2 == null) {
return false;
}
if (!modifiedFile2.changeType().equals(modifiedFile1.changeType())) {
return false;
}
}
return true;
}
use of com.google.gerrit.server.patch.gitdiff.ModifiedFile in project gerrit by GerritCodeReview.
the class DiffOperationsImpl method loadModifiedFilesWithoutCache.
/**
* Loads the modified file paths between two commits without inspecting the diff cache.
*/
private static Map<String, ModifiedFile> loadModifiedFilesWithoutCache(Project.NameKey project, DiffParameters diffParams, RevWalk revWalk, Config repoConfig) throws DiffNotAvailableException {
ObjectId newCommit = diffParams.newCommit();
ObjectId oldCommit = diffParams.baseCommit();
try {
ObjectReader reader = revWalk.getObjectReader();
List<DiffEntry> diffEntries;
try (DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
df.setReader(reader, repoConfig);
df.setDetectRenames(false);
diffEntries = df.scan(oldCommit.equals(ObjectId.zeroId()) ? null : oldCommit, newCommit);
}
List<ModifiedFile> modifiedFiles = diffEntries.stream().map(entry -> ModifiedFile.builder().changeType(toChangeType(entry.getChangeType())).oldPath(getGitPath(entry.getOldPath())).newPath(getGitPath(entry.getNewPath())).build()).collect(Collectors.toList());
return DiffUtil.mergeRewrittenModifiedFiles(modifiedFiles).stream().collect(ImmutableMap.toImmutableMap(ModifiedFile::getDefaultPath, Function.identity()));
} catch (IOException e) {
throw new DiffNotAvailableException(String.format("Failed to compute the modified files for project '%s'," + " old commit '%s', new commit '%s'.", project, oldCommit.name(), newCommit.name()), e);
}
}
use of com.google.gerrit.server.patch.gitdiff.ModifiedFile in project gerrit by GerritCodeReview.
the class DiffOperationsTest method loadModifiedFiles.
@Test
public void loadModifiedFiles() throws Exception {
ImmutableList<FileEntity> oldFiles = ImmutableList.of(new FileEntity(fileName1, fileContent1), new FileEntity(fileName2, fileContent2));
ObjectId oldCommitId = createCommit(repo, null, oldFiles);
ImmutableList<FileEntity> newFiles = ImmutableList.of(new FileEntity(fileName1, fileContent1), new FileEntity(fileName2, fileContent2 + "\nnew line here"));
ObjectId newCommitId = createCommit(repo, oldCommitId, newFiles);
Repository repository = repoManager.openRepository(testProjectName);
ObjectReader objectReader = repository.newObjectReader();
RevWalk rw = new RevWalk(objectReader);
StoredConfig repoConfig = repository.getConfig();
// This call loads modified files directly without going through the diff cache.
Map<String, ModifiedFile> modifiedFiles = diffOperations.loadModifiedFiles(testProjectName, newCommitId, oldCommitId, DiffOptions.DEFAULTS, rw, repoConfig);
assertThat(modifiedFiles).containsExactly(fileName2, ModifiedFile.builder().changeType(ChangeType.MODIFIED).oldPath(Optional.of(fileName2)).newPath(Optional.of(fileName2)).build());
}
use of com.google.gerrit.server.patch.gitdiff.ModifiedFile 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);
}
}
use of com.google.gerrit.server.patch.gitdiff.ModifiedFile in project gerrit by GerritCodeReview.
the class ApprovalInference method getForPatchSetWithoutNormalization.
private Collection<PatchSetApproval> getForPatchSetWithoutNormalization(ChangeNotes notes, ProjectState project, PatchSet patchSet, RevWalk rw, Config repoConfig) {
checkState(project.getNameKey().equals(notes.getProjectName()), "project must match %s, %s", project.getNameKey(), notes.getProjectName());
PatchSet.Id psId = patchSet.id();
// Add approvals on the given patch set to the result
Table<String, Account.Id, PatchSetApproval> resultByUser = HashBasedTable.create();
ImmutableList<PatchSetApproval> nonCopiedApprovalsForGivenPatchSet = notes.load().getApprovals().get(patchSet.id());
nonCopiedApprovalsForGivenPatchSet.forEach(psa -> resultByUser.put(psa.label(), psa.accountId(), psa));
// given patch set.
if (psId.get() == 1) {
return resultByUser.values();
}
Map.Entry<PatchSet.Id, PatchSet> priorPatchSet = notes.load().getPatchSets().lowerEntry(psId);
if (priorPatchSet == null) {
return resultByUser.values();
}
ImmutableList<PatchSetApproval> priorApprovalsIncludingCopied = notes.load().getApprovalsWithCopied().get(priorPatchSet.getKey());
// Add labels from the previous patch set to the result in case the label isn't already there
// and settings as well as change kind allow copying.
ChangeKind changeKind = changeKindCache.getChangeKind(project.getNameKey(), rw, repoConfig, priorPatchSet.getValue().commitId(), patchSet.commitId());
logger.atFine().log("change kind for patch set %d of change %d against prior patch set %s is %s", patchSet.id().get(), patchSet.id().changeId().get(), priorPatchSet.getValue().id().changeId(), changeKind);
Map<String, ModifiedFile> baseVsCurrent = null;
Map<String, ModifiedFile> baseVsPrior = null;
Map<String, ModifiedFile> priorVsCurrent = null;
LabelTypes labelTypes = project.getLabelTypes();
for (PatchSetApproval psa : priorApprovalsIncludingCopied) {
if (resultByUser.contains(psa.label(), psa.accountId())) {
continue;
}
Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
// Only compute modified files if there is a relevant label, since this is expensive.
if (baseVsCurrent == null && type.isPresent() && type.get().isCopyAllScoresIfListOfFilesDidNotChange()) {
baseVsCurrent = listModifiedFiles(project, patchSet, rw, repoConfig);
baseVsPrior = listModifiedFiles(project, priorPatchSet.getValue(), rw, repoConfig);
priorVsCurrent = listModifiedFiles(project, priorPatchSet.getValue().commitId(), patchSet.commitId(), rw, repoConfig);
}
if (!type.isPresent()) {
logger.atFine().log("approval %d on label %s of patch set %d of change %d cannot be copied" + " to patch set %d because the label no longer exists on project %s", psa.value(), psa.label(), psa.key().patchSetId().get(), psa.key().patchSetId().changeId().get(), psId.get(), project.getName());
continue;
}
if (!canCopyBasedOnBooleanLabelConfigs(project, psa, patchSet.id(), changeKind, type.get(), baseVsCurrent, baseVsPrior, priorVsCurrent) && !canCopyBasedOnCopyCondition(notes, psa, patchSet, type.get(), changeKind, rw, repoConfig)) {
continue;
}
resultByUser.put(psa.label(), psa.accountId(), psa.copyWithPatchSet(patchSet.id()));
}
return resultByUser.values();
}
Aggregations