Search in sources :

Example 11 with FileDiffOutput

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

the class DiffOperationsImpl method getModifiedFiles.

private ImmutableMap<String, FileDiffOutput> getModifiedFiles(DiffParameters diffParams, DiffOptions diffOptions) throws DiffNotAvailableException {
    try {
        Project.NameKey project = diffParams.project();
        ObjectId newCommit = diffParams.newCommit();
        ObjectId oldCommit = diffParams.baseCommit();
        ComparisonType cmp = diffParams.comparisonType();
        ImmutableList<ModifiedFile> modifiedFiles = modifiedFilesCache.get(createModifiedFilesKey(project, oldCommit, newCommit));
        List<FileDiffCacheKey> fileCacheKeys = new ArrayList<>();
        fileCacheKeys.add(createFileDiffCacheKey(project, oldCommit, newCommit, COMMIT_MSG, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
        true, /* whitespace= */
        null));
        if (cmp.isAgainstAutoMerge() || isMergeAgainstParent(cmp, project, newCommit)) {
            fileCacheKeys.add(createFileDiffCacheKey(project, oldCommit, newCommit, MERGE_LIST, DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
            true, /*whitespace = */
            null));
        }
        if (diffParams.skipFiles() == null) {
            modifiedFiles.stream().map(entity -> createFileDiffCacheKey(project, oldCommit, newCommit, entity.newPath().isPresent() ? entity.newPath().get() : entity.oldPath().get(), DEFAULT_DIFF_ALGORITHM, /* useTimeout= */
            true, /* whitespace= */
            null)).forEach(fileCacheKeys::add);
        }
        return getModifiedFilesForKeys(fileCacheKeys, diffOptions);
    } catch (IOException e) {
        throw new DiffNotAvailableException(e);
    }
}
Also used : Patch(com.google.gerrit.entities.Patch) Module(com.google.inject.Module) ModifiedFilesCache(com.google.gerrit.server.patch.diff.ModifiedFilesCache) Whitespace(com.google.gerrit.extensions.client.DiffPreferencesInfo.Whitespace) DiffAlgorithm(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheImpl.DiffAlgorithm) Inject(com.google.inject.Inject) FileDiffCache(com.google.gerrit.server.patch.filediff.FileDiffCache) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) ImmutableCollection(com.google.common.collect.ImmutableCollection) Function(java.util.function.Function) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) ChangeType(com.google.gerrit.entities.Patch.ChangeType) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Map(java.util.Map) ModifiedFilesCacheKey(com.google.gerrit.server.patch.diff.ModifiedFilesCacheKey) GitModifiedFilesCacheImpl(com.google.gerrit.server.patch.gitdiff.GitModifiedFilesCacheImpl) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) ImmutableMap(com.google.common.collect.ImmutableMap) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile) DisabledOutputStream(org.eclipse.jgit.util.io.DisabledOutputStream) FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) FileDiffCacheImpl(com.google.gerrit.server.patch.filediff.FileDiffCacheImpl) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) CacheModule(com.google.gerrit.server.cache.CacheModule) GitFileDiffCacheImpl(com.google.gerrit.server.patch.gitfilediff.GitFileDiffCacheImpl) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) DiffEntry(org.eclipse.jgit.diff.DiffEntry) ModifiedFilesCacheImpl(com.google.gerrit.server.patch.diff.ModifiedFilesCacheImpl) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Project(com.google.gerrit.entities.Project) FileDiffCacheKey(com.google.gerrit.server.patch.filediff.FileDiffCacheKey) ModifiedFile(com.google.gerrit.server.patch.gitdiff.ModifiedFile)

Example 12 with FileDiffOutput

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

the class DiffSummaryLoader method call.

@Override
public DiffSummary call() throws Exception {
    ObjectId oldId = key.toPatchListKey().getOldId();
    ObjectId newId = key.toPatchListKey().getNewId();
    Map<String, FileDiffOutput> diffList = oldId == null ? diffOperations.listModifiedFilesAgainstParent(project, newId, /* parentNum= */
    0, DiffOptions.DEFAULTS) : diffOperations.listModifiedFiles(project, oldId, newId, DiffOptions.DEFAULTS);
    return toDiffSummary(diffList);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput)

Example 13 with FileDiffOutput

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

the class DiffSummaryLoader method toDiffSummary.

private DiffSummary toDiffSummary(Map<String, FileDiffOutput> fileDiffs) {
    List<String> r = new ArrayList<>(fileDiffs.size());
    int linesInserted = 0;
    int linesDeleted = 0;
    for (String path : fileDiffs.keySet()) {
        if (Patch.isMagic(path)) {
            continue;
        }
        FileDiffOutput fileDiff = fileDiffs.get(path);
        linesInserted += fileDiff.insertions();
        linesDeleted += fileDiff.deletions();
        switch(fileDiff.changeType()) {
            case ADDED:
            case MODIFIED:
            case DELETED:
            case COPIED:
            case REWRITE:
                r.add(FilePathAdapter.getNewPath(fileDiff.oldPath(), fileDiff.newPath(), fileDiff.changeType()));
                break;
            case RENAMED:
                r.add(FilePathAdapter.getOldPath(fileDiff.oldPath(), fileDiff.changeType()));
                r.add(FilePathAdapter.getNewPath(fileDiff.oldPath(), fileDiff.newPath(), fileDiff.changeType()));
                break;
        }
    }
    return new DiffSummary(r.stream().sorted().toArray(String[]::new), linesInserted, linesDeleted);
}
Also used : ArrayList(java.util.ArrayList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput)

Example 14 with FileDiffOutput

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

the class ChangeEmail method getChangeDetail.

/**
 * Create the change message and the affected file list.
 */
public String getChangeDetail() {
    try {
        StringBuilder detail = new StringBuilder();
        if (patchSetInfo != null) {
            detail.append(patchSetInfo.getMessage().trim()).append("\n");
        } else {
            detail.append(change.getSubject().trim()).append("\n");
        }
        if (patchSet != null) {
            detail.append("---\n");
            // Sort files by name.
            TreeMap<String, FileDiffOutput> modifiedFiles = new TreeMap<>(listModifiedFiles());
            for (FileDiffOutput fileDiff : modifiedFiles.values()) {
                if (fileDiff.newPath().isPresent() && Patch.isMagic(fileDiff.newPath().get())) {
                    continue;
                }
                detail.append(fileDiff.changeType().getCode()).append(" ").append(FilePathAdapter.getNewPath(fileDiff.oldPath(), fileDiff.newPath(), fileDiff.changeType())).append("\n");
            }
            Integer insertions = modifiedFiles.values().stream().map(FileDiffOutput::insertions).reduce(0, Integer::sum);
            Integer deletions = modifiedFiles.values().stream().map(FileDiffOutput::deletions).reduce(0, Integer::sum);
            detail.append(MessageFormat.format(// 
            "" + // 
            "{0,choice,0#0 files|1#1 file|1<{0} files} changed, " + // 
            "{1,choice,0#0 insertions|1#1 insertion|1<{1} insertions}(+), " + // 
            "{2,choice,0#0 deletions|1#1 deletion|1<{2} deletions}(-)" + "\n", // 
            modifiedFiles.size() - 1, // 
            insertions, deletions));
            detail.append("\n");
        }
        return detail.toString();
    } catch (Exception err) {
        logger.atWarning().withCause(err).log("Cannot format change detail");
        return "";
    }
}
Also used : TreeMap(java.util.TreeMap) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) EmailException(com.google.gerrit.exceptions.EmailException) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) PatchSetInfoNotAvailableException(com.google.gerrit.server.patch.PatchSetInfoNotAvailableException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException)

Example 15 with FileDiffOutput

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

the class CommentSender method getGroupedInlineComments.

/**
 * Returns a list of FileCommentGroup objects representing the inline comments grouped by the
 * file.
 */
private List<CommentSender.FileCommentGroup> getGroupedInlineComments(Repository repo) {
    List<CommentSender.FileCommentGroup> groups = new ArrayList<>();
    // Loop over the comments and collect them into groups based on the file
    // location of the comment.
    FileCommentGroup currentGroup = null;
    for (Comment c : inlineComments) {
        // If it's a new group:
        if (currentGroup == null || !c.key.filename.equals(currentGroup.filename) || c.key.patchSetId != currentGroup.patchSetId) {
            currentGroup = new FileCommentGroup();
            currentGroup.filename = c.key.filename;
            currentGroup.patchSetId = c.key.patchSetId;
            // Get the modified files:
            Map<String, FileDiffOutput> modifiedFiles = null;
            try {
                modifiedFiles = listModifiedFiles(c.key.patchSetId);
            } catch (DiffNotAvailableException e) {
                logger.atSevere().withCause(e).log("Failed to get modified files");
            }
            groups.add(currentGroup);
            if (modifiedFiles != null && !modifiedFiles.isEmpty()) {
                try {
                    currentGroup.fileData = new PatchFile(repo, modifiedFiles, c.key.filename);
                } catch (IOException e) {
                    logger.atWarning().withCause(e).log("Cannot load %s from %s in %s", c.key.filename, modifiedFiles.values().iterator().next().newCommitId().name(), projectState.getName());
                    currentGroup.fileData = null;
                }
            }
        }
        if (currentGroup.filename.equals(PATCHSET_LEVEL) || currentGroup.fileData != null) {
            currentGroup.comments.add(c);
        }
    }
    groups.sort(Comparator.comparing(g -> g.filename, FilenameComparator.INSTANCE));
    return groups;
}
Also used : Patch(com.google.gerrit.entities.Patch) EmailException(com.google.gerrit.exceptions.EmailException) Protocol(com.google.gerrit.server.mail.receive.Protocol) Inject(com.google.inject.Inject) ZonedDateTime(java.time.ZonedDateTime) Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) CommentsUtil(com.google.gerrit.server.CommentsUtil) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Assisted(com.google.inject.assistedinject.Assisted) HashSet(java.util.HashSet) PATCHSET_LEVEL(com.google.gerrit.entities.Patch.PATCHSET_LEVEL) Strings(com.google.common.base.Strings) Config(org.eclipse.jgit.lib.Config) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Map(java.util.Map) RobotComment(com.google.gerrit.entities.RobotComment) NoSuchEntityException(com.google.gerrit.exceptions.NoSuchEntityException) Change(com.google.gerrit.entities.Change) FieldName(org.apache.james.mime4j.dom.field.FieldName) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) StorageException(com.google.gerrit.exceptions.StorageException) Account(com.google.gerrit.entities.Account) MailHeader(com.google.gerrit.mail.MailHeader) IOException(java.io.IOException) ZoneId(java.time.ZoneId) NotifyHandling(com.google.gerrit.extensions.api.changes.NotifyHandling) Collectors.toList(java.util.stream.Collectors.toList) FilenameComparator(com.google.gerrit.common.data.FilenameComparator) List(java.util.List) NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) Project(com.google.gerrit.entities.Project) MailProcessingUtil(com.google.gerrit.mail.MailProcessingUtil) LabelVote(com.google.gerrit.server.util.LabelVote) Optional(java.util.Optional) PatchFile(com.google.gerrit.server.patch.PatchFile) Comparator(java.util.Comparator) Collections(java.util.Collections) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) Comment(com.google.gerrit.entities.Comment) HumanComment(com.google.gerrit.entities.HumanComment) RobotComment(com.google.gerrit.entities.RobotComment) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) PatchFile(com.google.gerrit.server.patch.PatchFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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