Search in sources :

Example 1 with Text

use of com.google.gerrit.server.patch.Text 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 Text

use of com.google.gerrit.server.patch.Text 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 Text

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

the class CommentContextLoader method getContextForMergeList.

private CommentContext getContextForMergeList(ObjectReader reader, RevCommit commit, Range commentRange, int contextPadding) throws IOException {
    ComparisonType cmp = ComparisonType.againstParent(1);
    Text text = Text.forMergeList(cmp, reader, commit);
    return createContext(text, commentRange, contextPadding, FileContentUtil.TEXT_X_GERRIT_MERGE_LIST);
}
Also used : ComparisonType(com.google.gerrit.server.patch.ComparisonType) Text(com.google.gerrit.server.patch.Text)

Example 4 with Text

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

the class CommentContextLoader method getContextForFilePath.

private CommentContext getContextForFilePath(Repository repo, RevWalk rw, RevCommit commit, String filePath, Range commentRange, int contextPadding) throws IOException {
    // the same file multiple times.
    try (TreeWalk tw = TreeWalk.forPath(rw.getObjectReader(), filePath, commit.getTree())) {
        if (tw == null) {
            logger.atWarning().log("Could not find path %s in the git tree of ID %s.", filePath, commit.getTree().getId());
            return CommentContext.empty();
        }
        ObjectId id = tw.getObjectId(0);
        byte[] sourceContent = SrcContentResolver.getSourceContent(repo, id, tw.getFileMode(0));
        Text textSrc = new Text(sourceContent);
        String contentType = getContentType(tw, filePath, textSrc);
        return createContext(textSrc, commentRange, contextPadding, contentType);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) Text(com.google.gerrit.server.patch.Text) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 5 with Text

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

the class PatchListCacheIT method harmfulMutationsOfEditsAreNotPossibleForIntraLineDiffArgsAndCachedValue.

@Test
public void harmfulMutationsOfEditsAreNotPossibleForIntraLineDiffArgsAndCachedValue() throws Exception {
    String a = "First line\nSecond line\n";
    String b = "1st line\n2nd line\n";
    Text aText = new Text(a.getBytes(UTF_8));
    Text bText = new Text(b.getBytes(UTF_8));
    Edit inputEdit = new Edit(0, 2, 0, 2);
    List<Edit> inputEdits = new ArrayList<>(ImmutableList.of(inputEdit));
    IntraLineDiffKey diffKey = IntraLineDiffKey.create(ObjectId.zeroId(), ObjectId.zeroId(), Whitespace.IGNORE_NONE);
    IntraLineDiffArgs diffArgs = IntraLineDiffArgs.create(aText, bText, inputEdits, project, ObjectId.zeroId(), "file.txt");
    IntraLineDiff intraLineDiff = patchListCache.getIntraLineDiff(diffKey, diffArgs);
    Edit outputEdit = Iterables.getOnlyElement(intraLineDiff.getEdits());
    outputEdit.shift(5);
    inputEdit.shift(7);
    inputEdits.add(new Edit(43, 47, 50, 51));
    Edit originalEdit = new Edit(0, 2, 0, 2);
    assertThat(diffArgs.edits()).containsExactly(originalEdit);
    assertThat(intraLineDiff.getEdits()).containsExactly(originalEdit);
}
Also used : IntraLineDiffArgs(com.google.gerrit.server.patch.IntraLineDiffArgs) IntraLineDiffKey(com.google.gerrit.server.patch.IntraLineDiffKey) IntraLineDiff(com.google.gerrit.server.patch.IntraLineDiff) ArrayList(java.util.ArrayList) Text(com.google.gerrit.server.patch.Text) Edit(org.eclipse.jgit.diff.Edit) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Aggregations

Text (com.google.gerrit.server.patch.Text)6 Edit (org.eclipse.jgit.diff.Edit)4 FileDiffOutput (com.google.gerrit.server.patch.filediff.FileDiffOutput)2 TaggedEdit (com.google.gerrit.server.patch.filediff.TaggedEdit)2 IOException (java.io.IOException)2 Pattern (java.util.regex.Pattern)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 Test (org.junit.Test)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 Range (com.google.gerrit.entities.Comment.Range)1 FixReplacement (com.google.gerrit.entities.FixReplacement)1 FixResult (com.google.gerrit.server.fixes.FixCalculator.FixResult)1 ComparisonType (com.google.gerrit.server.patch.ComparisonType)1 DiffNotAvailableException (com.google.gerrit.server.patch.DiffNotAvailableException)1 IntraLineDiff (com.google.gerrit.server.patch.IntraLineDiff)1 IntraLineDiffArgs (com.google.gerrit.server.patch.IntraLineDiffArgs)1 IntraLineDiffKey (com.google.gerrit.server.patch.IntraLineDiffKey)1