Search in sources :

Example 1 with MERGE_LIST

use of com.google.gerrit.entities.Patch.MERGE_LIST in project gerrit by GerritCodeReview.

the class CommentContextLoader method getContext.

/**
 * Load the comment context for multiple contextInputs at once. This method will open the
 * repository and read the source files for all necessary contextInputs' file paths.
 *
 * @param contextInputs a list of contextInputs.
 * @return a Map where all entries consist of the input contextInputs and the values are their
 *     corresponding {@link CommentContext}.
 */
public Map<ContextInput, CommentContext> getContext(Collection<ContextInput> contextInputs) throws IOException {
    ImmutableMap.Builder<ContextInput, CommentContext> result = ImmutableMap.builderWithExpectedSize(Iterables.size(contextInputs));
    // Group contextInputs by commit ID so that each commit is parsed only once
    Map<ObjectId, List<ContextInput>> commentsByCommitId = contextInputs.stream().collect(groupingBy(ContextInput::commitId));
    try (Repository repo = repoManager.openRepository(project);
        RevWalk rw = new RevWalk(repo)) {
        for (ObjectId commitId : commentsByCommitId.keySet()) {
            RevCommit commit;
            try {
                commit = rw.parseCommit(commitId);
            } catch (IncorrectObjectTypeException | MissingObjectException e) {
                logger.atWarning().log("Commit %s is missing or has an incorrect object type", commitId);
                commentsByCommitId.get(commitId).forEach(contextInput -> result.put(contextInput, CommentContext.empty()));
                continue;
            }
            for (ContextInput contextInput : commentsByCommitId.get(commitId)) {
                Optional<Range> range = getStartAndEndLines(contextInput);
                if (!range.isPresent()) {
                    result.put(contextInput, CommentContext.empty());
                    continue;
                }
                String filePath = contextInput.filePath();
                switch(filePath) {
                    case COMMIT_MSG:
                        result.put(contextInput, getContextForCommitMessage(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
                        break;
                    case MERGE_LIST:
                        result.put(contextInput, getContextForMergeList(rw.getObjectReader(), commit, range.get(), contextInput.contextPadding()));
                        break;
                    default:
                        result.put(contextInput, getContextForFilePath(repo, rw, commit, filePath, range.get(), contextInput.contextPadding()));
                }
            }
        }
        return result.build();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) ComparisonType(com.google.gerrit.server.patch.ComparisonType) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ProjectCache(com.google.gerrit.server.project.ProjectCache) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) SrcContentResolver(com.google.gerrit.server.patch.SrcContentResolver) Inject(com.google.inject.Inject) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) Comment(com.google.gerrit.entities.Comment) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) MimeType(eu.medsea.mimeutil.MimeType) Assisted(com.google.inject.assistedinject.Assisted) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchScript(com.google.gerrit.common.data.PatchScript) ContextLineInfo(com.google.gerrit.extensions.common.ContextLineInfo) FileContentUtil(com.google.gerrit.server.change.FileContentUtil) Map(java.util.Map) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) ImmutableMap(com.google.common.collect.ImmutableMap) Text(com.google.gerrit.server.patch.Text) Collection(java.util.Collection) FileTypeRegistry(com.google.gerrit.server.mime.FileTypeRegistry) ProjectState(com.google.gerrit.server.project.ProjectState) IOException(java.io.IOException) CommentContext(com.google.gerrit.entities.CommentContext) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) ObjectId(org.eclipse.jgit.lib.ObjectId) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) AutoValue(com.google.auto.value.AutoValue) Project(com.google.gerrit.entities.Project) Optional(java.util.Optional) MimeUtil2(eu.medsea.mimeutil.MimeUtil2) ObjectReader(org.eclipse.jgit.lib.ObjectReader) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) CommentContext(com.google.gerrit.entities.CommentContext) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ImmutableMap(com.google.common.collect.ImmutableMap) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Repository(org.eclipse.jgit.lib.Repository) List(java.util.List) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 2 with MERGE_LIST

use of com.google.gerrit.entities.Patch.MERGE_LIST 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)

Aggregations

AutoValue (com.google.auto.value.AutoValue)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 FluentLogger (com.google.common.flogger.FluentLogger)2 Nullable (com.google.gerrit.common.Nullable)2 COMMIT_MSG (com.google.gerrit.entities.Patch.COMMIT_MSG)2 MERGE_LIST (com.google.gerrit.entities.Patch.MERGE_LIST)2 Project (com.google.gerrit.entities.Project)2 Inject (com.google.inject.Inject)2 IOException (java.io.IOException)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 RevWalk (org.eclipse.jgit.revwalk.RevWalk)2 ImmutableCollection (com.google.common.collect.ImmutableCollection)1 ImmutableList (com.google.common.collect.ImmutableList)1 Iterables (com.google.common.collect.Iterables)1 PatchScript (com.google.gerrit.common.data.PatchScript)1 Comment (com.google.gerrit.entities.Comment)1