Search in sources :

Example 1 with PatchList

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

the class FileInfoJson method toFileInfoMap.

private Map<String, FileInfo> toFileInfoMap(Change change, PatchListKey key) throws PatchListNotAvailableException {
    PatchList list = patchListCache.get(key, change.getProject());
    Map<String, FileInfo> files = new TreeMap<>();
    for (PatchListEntry e : list.getPatches()) {
        FileInfo d = new FileInfo();
        d.status = e.getChangeType() != Patch.ChangeType.MODIFIED ? e.getChangeType().getCode() : null;
        d.oldPath = e.getOldName();
        d.sizeDelta = e.getSizeDelta();
        d.size = e.getSize();
        if (e.getPatchType() == Patch.PatchType.BINARY) {
            d.binary = true;
        } else {
            d.linesInserted = e.getInsertions() > 0 ? e.getInsertions() : null;
            d.linesDeleted = e.getDeletions() > 0 ? e.getDeletions() : null;
        }
        FileInfo o = files.put(e.getNewName(), d);
        if (o != null) {
            // This should only happen on a delete-add break created by JGit
            // when the file was rewritten and too little content survived. Write
            // a single record with data from both sides.
            d.status = Patch.ChangeType.REWRITE.getCode();
            d.sizeDelta = o.sizeDelta;
            d.size = o.size;
            if (o.binary != null && o.binary) {
                d.binary = true;
            }
            if (o.linesInserted != null) {
                d.linesInserted = o.linesInserted;
            }
            if (o.linesDeleted != null) {
                d.linesDeleted = o.linesDeleted;
            }
        }
    }
    return files;
}
Also used : PatchList(com.google.gerrit.server.patch.PatchList) FileInfo(com.google.gerrit.extensions.common.FileInfo) PatchListEntry(com.google.gerrit.server.patch.PatchListEntry) TreeMap(java.util.TreeMap)

Example 2 with PatchList

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

the class ChangeEmail method getUnifiedDiff.

/** Show patch set as unified difference. */
public String getUnifiedDiff() {
    PatchList patchList;
    try {
        patchList = getPatchList();
        if (patchList.getOldId() == null) {
            // Currently these always have a null oldId in the PatchList.
            return "[Octopus merge; cannot be formatted as a diff.]\n";
        }
    } catch (PatchListNotAvailableException e) {
        log.error("Cannot format patch", e);
        return "";
    }
    int maxSize = args.settings.maximumDiffSize;
    TemporaryBuffer.Heap buf = new TemporaryBuffer.Heap(Math.min(HEAP_EST_SIZE, maxSize), maxSize);
    try (DiffFormatter fmt = new DiffFormatter(buf)) {
        try (Repository git = args.server.openRepository(change.getProject())) {
            try {
                fmt.setRepository(git);
                fmt.setDetectRenames(true);
                fmt.format(patchList.getOldId(), patchList.getNewId());
                return RawParseUtils.decode(buf.toByteArray());
            } catch (IOException e) {
                if (JGitText.get().inMemoryBufferLimitExceeded.equals(e.getMessage())) {
                    return "";
                }
                log.error("Cannot format patch", e);
                return "";
            }
        } catch (IOException e) {
            log.error("Cannot open repository to format patch", e);
            return "";
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) PatchList(com.google.gerrit.server.patch.PatchList) TemporaryBuffer(org.eclipse.jgit.util.TemporaryBuffer) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) IOException(java.io.IOException) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter)

Example 3 with PatchList

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

the class PRED_commit_delta_4 method exec.

@Override
public Operation exec(Prolog engine) throws PrologException {
    engine.cont = cont;
    engine.setB0();
    Term a1 = arg1.dereference();
    if (a1 instanceof VariableTerm) {
        throw new PInstantiationException(this, 1);
    }
    if (!(a1 instanceof SymbolTerm)) {
        throw new IllegalTypeException(this, 1, "symbol", a1);
    }
    Pattern regex = Pattern.compile(a1.name());
    engine.r1 = new JavaObjectTerm(regex);
    engine.r2 = arg2;
    engine.r3 = arg3;
    engine.r4 = arg4;
    PatchList pl = StoredValues.PATCH_LIST.get(engine);
    Iterator<PatchListEntry> iter = pl.getPatches().iterator();
    engine.r5 = new JavaObjectTerm(iter);
    return engine.jtry5(commit_delta_check, commit_delta_next);
}
Also used : Pattern(java.util.regex.Pattern) PatchList(com.google.gerrit.server.patch.PatchList) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) PatchListEntry(com.google.gerrit.server.patch.PatchListEntry) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) Term(com.googlecode.prolog_cafe.lang.Term) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) PInstantiationException(com.googlecode.prolog_cafe.exceptions.PInstantiationException)

Example 4 with PatchList

use of com.google.gerrit.server.patch.PatchList 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);
    PatchList pl = StoredValues.PATCH_LIST.get(engine);
    Repository repo = StoredValues.REPOSITORY.get(engine);
    try (ObjectReader reader = repo.newObjectReader();
        RevWalk rw = new RevWalk(reader)) {
        final RevTree aTree;
        final RevTree bTree;
        final RevCommit bCommit = rw.parseCommit(pl.getNewId());
        if (pl.getOldId() != null) {
            aTree = rw.parseTree(pl.getOldId());
        } else {
            // web UI returns no files to match against, just fail.
            return engine.fail();
        }
        bTree = bCommit.getTree();
        for (PatchListEntry entry : pl.getPatches()) {
            String newName = entry.getNewName();
            String oldName = entry.getOldName();
            if (newName.equals("/COMMIT_MSG")) {
                continue;
            }
            if (fileRegex.matcher(newName).find() || (oldName != null && fileRegex.matcher(oldName).find())) {
                List<Edit> edits = entry.getEdits();
                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) PatchListEntry(com.google.gerrit.server.patch.PatchListEntry) Edit(org.eclipse.jgit.diff.Edit) 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) Repository(org.eclipse.jgit.lib.Repository) PatchList(com.google.gerrit.server.patch.PatchList) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with PatchList

use of com.google.gerrit.server.patch.PatchList 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();
    PatchList pl = StoredValues.PATCH_LIST.get(engine);
    // Account for magic files
    if (!a1.unify(new IntegerTerm(pl.getPatches().size() - countMagicFiles(pl.getPatches())), engine.trail)) {
        return engine.fail();
    }
    if (!a2.unify(new IntegerTerm(pl.getInsertions()), engine.trail)) {
        return engine.fail();
    }
    if (!a3.unify(new IntegerTerm(pl.getDeletions()), engine.trail)) {
        return engine.fail();
    }
    return cont;
}
Also used : IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) PatchList(com.google.gerrit.server.patch.PatchList) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm)

Aggregations

PatchList (com.google.gerrit.server.patch.PatchList)8 PatchListEntry (com.google.gerrit.server.patch.PatchListEntry)5 PatchListNotAvailableException (com.google.gerrit.server.patch.PatchListNotAvailableException)4 IOException (java.io.IOException)4 Term (com.googlecode.prolog_cafe.lang.Term)3 EmailException (com.google.gerrit.common.errors.EmailException)2 OrmException (com.google.gwtorm.server.OrmException)2 SymbolTerm (com.googlecode.prolog_cafe.lang.SymbolTerm)2 VariableTerm (com.googlecode.prolog_cafe.lang.VariableTerm)2 Pattern (java.util.regex.Pattern)2 Repository (org.eclipse.jgit.lib.Repository)2 Strings (com.google.common.base.Strings)1 Ordering (com.google.common.collect.Ordering)1 FilenameComparator (com.google.gerrit.common.data.FilenameComparator)1 NoSuchEntityException (com.google.gerrit.common.errors.NoSuchEntityException)1 NotifyHandling (com.google.gerrit.extensions.api.changes.NotifyHandling)1 FileInfo (com.google.gerrit.extensions.common.FileInfo)1 Change (com.google.gerrit.reviewdb.client.Change)1 Comment (com.google.gerrit.reviewdb.client.Comment)1 Patch (com.google.gerrit.reviewdb.client.Patch)1