Search in sources :

Example 6 with FileDiffOutput

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

the class DiffOperationsTest method diffAgainstAutoMergePersistsAutoMergeInRepo.

@Test
public void diffAgainstAutoMergePersistsAutoMergeInRepo() throws Exception {
    ObjectId parent1 = createCommit(repo, null, ImmutableList.of(new FileEntity("file_1.txt", "file 1 content")));
    ObjectId parent2 = createCommit(repo, null, ImmutableList.of(new FileEntity("file_2.txt", "file 2 content")));
    ObjectId merge = createMergeCommit(repo, ImmutableList.of(new FileEntity("file_1.txt", "file 1 content"), new FileEntity("file_2.txt", "file 2 content"), new FileEntity("file_3.txt", "file 3 content")), parent1, parent2);
    String autoMergeRef = RefNames.refsCacheAutomerge(merge.name());
    assertThat(repo.getRefDatabase().exactRef(autoMergeRef)).isNull();
    Map<String, FileDiffOutput> changedFiles = diffOperations.listModifiedFilesAgainstParent(testProjectName, merge, /* parentNum=*/
    0, DiffOptions.DEFAULTS);
    assertThat(changedFiles.keySet()).containsExactly("/COMMIT_MSG", "/MERGE_LIST", "file_3.txt");
    // Requesting diff against auto-merge had the side effect of updating the auto-merge ref
    assertThat(repo.getRefDatabase().exactRef(autoMergeRef)).isNotNull();
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Test(org.junit.Test)

Example 7 with FileDiffOutput

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

the class EventFactory method asPatchSetAttribute.

/**
 * Create a PatchSetAttribute for the given patchset suitable for serialization to JSON.
 */
public PatchSetAttribute asPatchSetAttribute(RevWalk revWalk, Change change, PatchSet patchSet) {
    PatchSetAttribute p = new PatchSetAttribute();
    p.revision = patchSet.commitId().name();
    p.number = patchSet.number();
    p.ref = patchSet.refName();
    p.uploader = asAccountAttribute(patchSet.uploader());
    p.createdOn = patchSet.createdOn().getEpochSecond();
    PatchSet.Id pId = patchSet.id();
    try {
        p.parents = new ArrayList<>();
        RevCommit c = revWalk.parseCommit(ObjectId.fromString(p.revision));
        for (RevCommit parent : c.getParents()) {
            p.parents.add(parent.name());
        }
        UserIdentity author = emails.toUserIdentity(c.getAuthorIdent());
        if (author.getAccount() == null) {
            p.author = new AccountAttribute();
            p.author.email = author.getEmail();
            p.author.name = author.getName();
            p.author.username = "";
        } else {
            p.author = asAccountAttribute(author.getAccount());
        }
        Map<String, FileDiffOutput> modifiedFiles = diffOperations.listModifiedFilesAgainstParent(change.getProject(), patchSet.commitId(), /* parentNum= */
        0, DiffOptions.DEFAULTS);
        for (FileDiffOutput fileDiff : modifiedFiles.values()) {
            p.sizeDeletions += fileDiff.deletions();
            p.sizeInsertions += fileDiff.insertions();
        }
        p.kind = changeKindCache.getChangeKind(change, patchSet);
    } catch (IOException | StorageException e) {
        logger.atSevere().withCause(e).log("Cannot load patch set data for %s", patchSet.id());
    } catch (DiffNotAvailableException e) {
        logger.atSevere().withCause(e).log("Cannot get size information for %s.", pId);
    }
    return p;
}
Also used : AccountAttribute(com.google.gerrit.server.data.AccountAttribute) UserIdentity(com.google.gerrit.entities.UserIdentity) PatchSet(com.google.gerrit.entities.PatchSet) IOException(java.io.IOException) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) StorageException(com.google.gerrit.exceptions.StorageException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with FileDiffOutput

use of com.google.gerrit.server.patch.filediff.FileDiffOutput 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 9 with FileDiffOutput

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

the class PRED_files_1 method getAllSubmodulePaths.

/**
 * Returns the paths for all {@code GITLINK} files.
 */
private static Set<String> getAllSubmodulePaths(Repository repository, RevCommit commit, Collection<FileDiffOutput> modifiedFiles) throws PrologException, IOException {
    Set<String> submodules = new HashSet<>();
    try (TreeWalk treeWalk = new TreeWalk(repository)) {
        treeWalk.addTree(commit.getTree());
        Set<String> allPaths = modifiedFiles.stream().map(f -> FilePathAdapter.getNewPath(f.oldPath(), f.newPath(), f.changeType())).filter(f -> !Patch.isMagic(f)).collect(Collectors.toSet());
        treeWalk.setFilter(PathFilterGroup.createFromStrings(allPaths));
        while (treeWalk.next()) {
            if (treeWalk.getFileMode() == FileMode.GITLINK) {
                submodules.add(treeWalk.getPathString());
            }
        }
        return submodules;
    }
}
Also used : Patch(com.google.gerrit.entities.Patch) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Term(com.googlecode.prolog_cafe.lang.Term) FileMode(org.eclipse.jgit.lib.FileMode) Collection(java.util.Collection) PrologException(com.googlecode.prolog_cafe.exceptions.PrologException) Set(java.util.Set) Prolog(com.googlecode.prolog_cafe.lang.Prolog) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) HashSet(java.util.HashSet) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) FilePathAdapter(com.google.gerrit.server.patch.FilePathAdapter) Predicate(com.googlecode.prolog_cafe.lang.Predicate) ListTerm(com.googlecode.prolog_cafe.lang.ListTerm) Operation(com.googlecode.prolog_cafe.lang.Operation) Repository(org.eclipse.jgit.lib.Repository) StoredValues(com.google.gerrit.server.rules.StoredValues) PathFilterGroup(org.eclipse.jgit.treewalk.filter.PathFilterGroup) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) HashSet(java.util.HashSet)

Example 10 with FileDiffOutput

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

the class PRED_commit_stats_3 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.setB0();
    Term a1 = arg1.dereference();
    Term a2 = arg2.dereference();
    Term a3 = arg3.dereference();
    Map<String, FileDiffOutput> modifiedFiles = StoredValues.DIFF_LIST.get(engine);
    // Account for magic files
    if (!a1.unify(new IntegerTerm(modifiedFiles.size() - countMagicFiles(modifiedFiles.values())), engine.trail)) {
        return engine.fail();
    }
    Integer insertions = modifiedFiles.values().stream().map(FileDiffOutput::insertions).reduce(0, Integer::sum);
    Integer deletions = modifiedFiles.values().stream().map(FileDiffOutput::deletions).reduce(0, Integer::sum);
    if (!a2.unify(new IntegerTerm(insertions), engine.trail)) {
        return engine.fail();
    }
    if (!a3.unify(new IntegerTerm(deletions), engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput)

Aggregations

FileDiffOutput (com.google.gerrit.server.patch.filediff.FileDiffOutput)20 IOException (java.io.IOException)10 DiffNotAvailableException (com.google.gerrit.server.patch.DiffNotAvailableException)6 Repository (org.eclipse.jgit.lib.Repository)6 ArrayList (java.util.ArrayList)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 Patch (com.google.gerrit.entities.Patch)4 StorageException (com.google.gerrit.exceptions.StorageException)4 Test (org.junit.Test)4 Project (com.google.gerrit.entities.Project)3 Inject (com.google.inject.Inject)3 Term (com.googlecode.prolog_cafe.lang.Term)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Config (org.eclipse.jgit.lib.Config)3 ImmutableList (com.google.common.collect.ImmutableList)2 FluentLogger (com.google.common.flogger.FluentLogger)2 Nullable (com.google.gerrit.common.Nullable)2