Search in sources :

Example 1 with COMMIT_MSG

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

the class RevisionDiffIT method renamedUnrelatedFileIsIgnored_forPatchSetDiffWithRebase_whenEquallyModifiedInBoth.

@Test
@Ignore
public void renamedUnrelatedFileIsIgnored_forPatchSetDiffWithRebase_whenEquallyModifiedInBoth() throws Exception {
    // TODO(ghareeb): fix this test for the new diff cache implementation
    Function<String, String> contentModification = fileContent -> fileContent.replace("1st line\n", "First line\n");
    addModifiedPatchSet(changeId, FILE_NAME2, contentModification);
    String previousPatchSetId = gApi.changes().id(changeId).get().currentRevision;
    // Revert the modification to be able to rebase.
    addModifiedPatchSet(changeId, FILE_NAME2, fileContent -> fileContent.replace("First line\n", "1st line\n"));
    String renamedFileName = "renamed_file.txt";
    ObjectId commit2 = addCommitRenamingFile(commit1, FILE_NAME2, renamedFileName);
    rebaseChangeOn(changeId, commit2);
    addModifiedPatchSet(changeId, renamedFileName, contentModification);
    addModifiedPatchSet(changeId, FILE_NAME, "Another line\n"::concat);
    Map<String, FileInfo> changedFiles = gApi.changes().id(changeId).current().files(previousPatchSetId);
    assertThat(changedFiles.keySet()).containsExactly(COMMIT_MSG, FILE_NAME);
}
Also used : Patch(com.google.gerrit.entities.Patch) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Collectors.toMap(java.util.stream.Collectors.toMap) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) Locale(java.util.Locale) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) TestProjectUpdate.allow(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allow) FileInfoSubject.assertThat(com.google.gerrit.extensions.common.testing.FileInfoSubject.assertThat) ImmutableMap(com.google.common.collect.ImmutableMap) BufferedImage(java.awt.image.BufferedImage) Collectors(java.util.stream.Collectors) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) Collectors.joining(java.util.stream.Collectors.joining) Result(com.google.gerrit.acceptance.PushOneCommit.Result) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) RebaseInput(com.google.gerrit.extensions.api.changes.RebaseInput) DiffOperations(com.google.gerrit.server.patch.DiffOperations) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) DiffInfo(com.google.gerrit.extensions.common.DiffInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawInputUtil(com.google.gerrit.common.RawInputUtil) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PATCHSET_LEVEL(com.google.gerrit.entities.Patch.PATCHSET_LEVEL) EditWebLink(com.google.gerrit.extensions.webui.EditWebLink) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) DiffInfoSubject.assertThat(com.google.gerrit.extensions.common.testing.DiffInfoSubject.assertThat) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) GitUtil(com.google.gerrit.acceptance.GitUtil) FileApi(com.google.gerrit.extensions.api.changes.FileApi) FileInfo(com.google.gerrit.extensions.common.FileInfo) ExtensionRegistry(com.google.gerrit.acceptance.ExtensionRegistry) Before(org.junit.Before) TruthJUnit.assume(com.google.common.truth.TruthJUnit.assume) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) ObjectIds.abbreviateName(com.google.gerrit.git.ObjectIds.abbreviateName) Permission(com.google.gerrit.entities.Permission) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeType(com.google.gerrit.extensions.common.ChangeType) DiffOptions(com.google.gerrit.server.patch.DiffOptions) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) Ignore(org.junit.Ignore) ProjectOperations(com.google.gerrit.acceptance.testsuite.project.ProjectOperations) DateTimeFormatter(java.time.format.DateTimeFormatter) FileInfo(com.google.gerrit.extensions.common.FileInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) Ignore(org.junit.Ignore) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with COMMIT_MSG

use of com.google.gerrit.entities.Patch.COMMIT_MSG 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 3 with COMMIT_MSG

use of com.google.gerrit.entities.Patch.COMMIT_MSG 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 4 with COMMIT_MSG

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

the class RevisionDiffIT method unrelatedFileWhenRebasingOnAnotherChangeOrPatchSetIsIgnored.

/*
   *                change PS B
   *                   |
   * change PS A    commit4
   *    |              |
   * commit2        commit3
   *    |             /
   * commit1 --------
   */
@Test
public void unrelatedFileWhenRebasingOnAnotherChangeOrPatchSetIsIgnored() throws Exception {
    ObjectId commit2 = addCommit(commit1, FILE_NAME, FILE_CONTENT.replace("Line 5\n", "Line five\n"));
    rebaseChangeOn(changeId, commit2);
    String previousPatchSetId = gApi.changes().id(changeId).get().currentRevision;
    ObjectId commit3 = addCommit(commit1, FILE_NAME2, FILE_CONTENT2.replace("2nd line\n", "Second line\n"));
    ObjectId commit4 = addCommit(commit3, FILE_NAME, FILE_CONTENT.replace("Line 60\n", "Line sixty\n"));
    rebaseChangeOn(changeId, commit4);
    Function<String, String> contentModification = fileContent -> fileContent.replace("Line 20\n", "Line twenty\n");
    addModifiedPatchSet(changeId, FILE_NAME, contentModification);
    Map<String, FileInfo> changedFiles = gApi.changes().id(changeId).current().files(previousPatchSetId);
    assertThat(changedFiles.keySet()).containsExactly(COMMIT_MSG, FILE_NAME);
}
Also used : Patch(com.google.gerrit.entities.Patch) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) Collectors.toMap(java.util.stream.Collectors.toMap) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) Locale(java.util.Locale) Map(java.util.Map) ImageIO(javax.imageio.ImageIO) TestProjectUpdate.allow(com.google.gerrit.acceptance.testsuite.project.TestProjectUpdate.allow) FileInfoSubject.assertThat(com.google.gerrit.extensions.common.testing.FileInfoSubject.assertThat) ImmutableMap(com.google.common.collect.ImmutableMap) BufferedImage(java.awt.image.BufferedImage) Collectors(java.util.stream.Collectors) COMMIT_MSG(com.google.gerrit.entities.Patch.COMMIT_MSG) Collectors.joining(java.util.stream.Collectors.joining) Result(com.google.gerrit.acceptance.PushOneCommit.Result) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) RebaseInput(com.google.gerrit.extensions.api.changes.RebaseInput) DiffOperations(com.google.gerrit.server.patch.DiffOperations) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) GerritJUnit.assertThrows(com.google.gerrit.testing.GerritJUnit.assertThrows) Joiner(com.google.common.base.Joiner) IntStream(java.util.stream.IntStream) DiffInfo(com.google.gerrit.extensions.common.DiffInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RawInputUtil(com.google.gerrit.common.RawInputUtil) MERGE_LIST(com.google.gerrit.entities.Patch.MERGE_LIST) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PATCHSET_LEVEL(com.google.gerrit.entities.Patch.PATCHSET_LEVEL) EditWebLink(com.google.gerrit.extensions.webui.EditWebLink) BinaryResult(com.google.gerrit.extensions.restapi.BinaryResult) DiffInfoSubject.assertThat(com.google.gerrit.extensions.common.testing.DiffInfoSubject.assertThat) ImmutableList(com.google.common.collect.ImmutableList) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) GitUtil(com.google.gerrit.acceptance.GitUtil) FileApi(com.google.gerrit.extensions.api.changes.FileApi) FileInfo(com.google.gerrit.extensions.common.FileInfo) ExtensionRegistry(com.google.gerrit.acceptance.ExtensionRegistry) Before(org.junit.Before) TruthJUnit.assume(com.google.common.truth.TruthJUnit.assume) DiffPreferencesInfo(com.google.gerrit.extensions.client.DiffPreferencesInfo) ObjectIds.abbreviateName(com.google.gerrit.git.ObjectIds.abbreviateName) Permission(com.google.gerrit.entities.Permission) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeType(com.google.gerrit.extensions.common.ChangeType) DiffOptions(com.google.gerrit.server.patch.DiffOptions) WebLinkInfo(com.google.gerrit.extensions.common.WebLinkInfo) Ignore(org.junit.Ignore) ProjectOperations(com.google.gerrit.acceptance.testsuite.project.ProjectOperations) DateTimeFormatter(java.time.format.DateTimeFormatter) FileInfo(com.google.gerrit.extensions.common.FileInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)4 COMMIT_MSG (com.google.gerrit.entities.Patch.COMMIT_MSG)4 MERGE_LIST (com.google.gerrit.entities.Patch.MERGE_LIST)4 Inject (com.google.inject.Inject)4 IOException (java.io.IOException)4 ImmutableList (com.google.common.collect.ImmutableList)3 Patch (com.google.gerrit.entities.Patch)3 DiffPreferencesInfo (com.google.gerrit.extensions.client.DiffPreferencesInfo)3 FileDiffOutput (com.google.gerrit.server.patch.filediff.FileDiffOutput)3 List (java.util.List)3 Map (java.util.Map)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 AutoValue (com.google.auto.value.AutoValue)2 Joiner (com.google.common.base.Joiner)2 FluentLogger (com.google.common.flogger.FluentLogger)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 TruthJUnit.assume (com.google.common.truth.TruthJUnit.assume)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 ExtensionRegistry (com.google.gerrit.acceptance.ExtensionRegistry)2 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)2