use of com.google.gerrit.server.patch.filediff.TaggedEdit in project gerrit by GerritCodeReview.
the class FileEditsPredicate method match.
@Override
public boolean match(ChangeData cd) {
try {
Map<String, FileDiffOutput> modifiedFiles = diffOperations.listModifiedFilesAgainstParent(cd.project(), cd.currentPatchSet().commitId(), /* parentNum= */
0, DiffOptions.DEFAULTS);
FileDiffOutput firstDiff = Iterables.getFirst(modifiedFiles.values(), /* defaultValue= */
null);
if (firstDiff == null) {
// engine.fail();
return false;
}
Pattern filePattern = null;
Pattern editPattern = null;
if (fileEditsArgs.filePattern().startsWith("^")) {
// We validated the pattern before creating this predicate. No need to revalidate.
String pattern = fileEditsArgs.filePattern();
filePattern = Pattern.compile(pattern);
}
if (fileEditsArgs.editPattern().startsWith("^")) {
// We validated the pattern before creating this predicate. No need to revalidate.
String pattern = fileEditsArgs.editPattern();
editPattern = Pattern.compile(pattern);
}
try (Repository repo = repoManager.openRepository(cd.project());
ObjectReader reader = repo.newObjectReader();
RevWalk rw = new RevWalk(reader)) {
RevTree aTree = firstDiff.oldCommitId().equals(ObjectId.zeroId()) ? null : rw.parseTree(firstDiff.oldCommitId());
RevTree bTree = rw.parseCommit(firstDiff.newCommitId()).getTree();
for (FileDiffOutput entry : modifiedFiles.values()) {
String newName = FilePathAdapter.getNewPath(entry.oldPath(), entry.newPath(), entry.changeType());
String oldName = FilePathAdapter.getOldPath(entry.oldPath(), entry.changeType());
if (Patch.isMagic(newName)) {
continue;
}
if (match(newName, fileEditsArgs.filePattern(), filePattern) || (oldName != null && match(oldName, fileEditsArgs.filePattern(), filePattern))) {
List<Edit> edits = entry.edits().stream().map(TaggedEdit::jgitEdit).collect(Collectors.toList());
if (edits.isEmpty()) {
continue;
}
Text tA;
if (oldName != null) {
tA = load(aTree, oldName, reader);
} else {
tA = load(aTree, newName, reader);
}
Text tB = load(bTree, newName, reader);
for (Edit edit : edits) {
if (tA != Text.EMPTY) {
String aDiff = tA.getString(edit.getBeginA(), edit.getEndA(), true);
if (match(aDiff, fileEditsArgs.editPattern(), editPattern)) {
return true;
}
}
if (tB != Text.EMPTY) {
String bDiff = tB.getString(edit.getBeginB(), edit.getEndB(), true);
if (match(bDiff, fileEditsArgs.editPattern(), editPattern)) {
return true;
}
}
}
}
}
} catch (IOException e) {
logger.atSevere().withCause(e).log("Error while evaluating commit edits.");
return false;
}
} catch (DiffNotAvailableException e) {
logger.atSevere().withCause(e).log("Diff error while evaluating commit edits.");
return false;
}
return false;
}
use of com.google.gerrit.server.patch.filediff.TaggedEdit in project gerrit by GerritCodeReview.
the class PRED_commit_edits_2 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
Term a2 = arg2.dereference();
Pattern fileRegex = getRegexParameter(a1);
Pattern editRegex = getRegexParameter(a2);
Map<String, FileDiffOutput> modifiedFiles = StoredValues.DIFF_LIST.get(engine);
FileDiffOutput firstDiff = Iterables.getFirst(modifiedFiles.values(), /* defaultValue= */
null);
if (firstDiff == null) {
// No available diffs. We cannot identify old and new commit IDs.
engine.fail();
}
Repository repo = StoredValues.REPOSITORY.get(engine);
try (ObjectReader reader = repo.newObjectReader();
RevWalk rw = new RevWalk(reader)) {
final RevTree aTree = firstDiff.oldCommitId().equals(ObjectId.zeroId()) ? null : rw.parseTree(firstDiff.oldCommitId());
final RevTree bTree = rw.parseCommit(firstDiff.newCommitId()).getTree();
for (FileDiffOutput entry : modifiedFiles.values()) {
String newName = FilePathAdapter.getNewPath(entry.oldPath(), entry.newPath(), entry.changeType());
String oldName = FilePathAdapter.getOldPath(entry.oldPath(), entry.changeType());
if (Patch.isMagic(newName)) {
continue;
}
if (fileRegex.matcher(newName).find() || (oldName != null && fileRegex.matcher(oldName).find())) {
List<Edit> edits = entry.edits().stream().map(TaggedEdit::jgitEdit).collect(Collectors.toList());
if (edits.isEmpty()) {
continue;
}
Text tA;
if (oldName != null) {
tA = load(aTree, oldName, reader);
} else {
tA = load(aTree, newName, reader);
}
Text tB = load(bTree, newName, reader);
for (Edit edit : edits) {
if (tA != Text.EMPTY) {
String aDiff = tA.getString(edit.getBeginA(), edit.getEndA(), true);
if (editRegex.matcher(aDiff).find()) {
return cont;
}
}
if (tB != Text.EMPTY) {
String bDiff = tB.getString(edit.getBeginB(), edit.getEndB(), true);
if (editRegex.matcher(bDiff).find()) {
return cont;
}
}
}
}
}
} catch (IOException err) {
throw new JavaException(this, 1, err);
}
return engine.fail();
}
use of com.google.gerrit.server.patch.filediff.TaggedEdit in project gerrit by GerritCodeReview.
the class FileDiffOutputSerializerTest method roundTrip.
@Test
public void roundTrip() {
ImmutableList<TaggedEdit> edits = ImmutableList.of(TaggedEdit.create(Edit.create(1, 5, 3, 4), true), TaggedEdit.create(Edit.create(21, 30, 150, 158), false));
FileDiffOutput fileDiff = FileDiffOutput.builder().oldCommitId(ObjectId.fromString("dd4d2a1498870ca5fe415b33f65d052d69d9eaf5")).newCommitId(ObjectId.fromString("0cfaab3f2ba76f71798da0a2651f41be8d45f842")).comparisonType(ComparisonType.againstOtherPatchSet()).oldPath(Optional.of("old_file_path.txt")).newPath(Optional.empty()).changeType(ChangeType.DELETED).patchType(Optional.of(PatchType.UNIFIED)).size(23).sizeDelta(10).headerLines(ImmutableList.of("header line 1", "header line 2")).edits(edits).negative(Optional.of(true)).build();
byte[] serialized = FileDiffOutput.Serializer.INSTANCE.serialize(fileDiff);
assertThat(FileDiffOutput.Serializer.INSTANCE.deserialize(serialized)).isEqualTo(fileDiff);
}
Aggregations