Search in sources :

Example 1 with TaggedEdit

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;
}
Also used : Pattern(java.util.regex.Pattern) Edit(org.eclipse.jgit.diff.Edit) TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) Text(com.google.gerrit.server.patch.Text) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Repository(org.eclipse.jgit.lib.Repository) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 2 with TaggedEdit

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();
}
Also used : Pattern(java.util.regex.Pattern) JavaException(com.googlecode.prolog_cafe.exceptions.JavaException) Edit(org.eclipse.jgit.diff.Edit) TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) Text(com.google.gerrit.server.patch.Text) Term(com.googlecode.prolog_cafe.lang.Term) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Repository(org.eclipse.jgit.lib.Repository) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 3 with TaggedEdit

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);
}
Also used : TaggedEdit(com.google.gerrit.server.patch.filediff.TaggedEdit) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Test(org.junit.Test)

Aggregations

FileDiffOutput (com.google.gerrit.server.patch.filediff.FileDiffOutput)3 TaggedEdit (com.google.gerrit.server.patch.filediff.TaggedEdit)3 Text (com.google.gerrit.server.patch.Text)2 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)2 Edit (org.eclipse.jgit.diff.Edit)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 Repository (org.eclipse.jgit.lib.Repository)2 RevTree (org.eclipse.jgit.revwalk.RevTree)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 DiffNotAvailableException (com.google.gerrit.server.patch.DiffNotAvailableException)1 JavaException (com.googlecode.prolog_cafe.exceptions.JavaException)1 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)1 Term (com.googlecode.prolog_cafe.lang.Term)1 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)1 Test (org.junit.Test)1