From 258cb8d98ee343135e8c8b44f98d4f28bb6e54bf Mon Sep 17 00:00:00 2001 From: David Ostrovsky Date: Mon, 15 Dec 2014 01:28:54 +0100 Subject: [PATCH] Factor out DiffInfo classes to extensions package Change-Id: Ia447d9a6bfe09c7e7b41216141b806d300d27fd4 --- .../gerrit/extensions/common/ChangeType.java | 36 +++++++++ .../gerrit/extensions/common/DiffInfo.java | 74 ++++++++++++++++++ .../google/gerrit/server/change/GetDiff.java | 77 +++++++------------ 3 files changed, 137 insertions(+), 50 deletions(-) create mode 100644 gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ChangeType.java create mode 100644 gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/DiffInfo.java diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ChangeType.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ChangeType.java new file mode 100644 index 0000000000..d55580c681 --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/ChangeType.java @@ -0,0 +1,36 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.extensions.common; + +/** Type of modification made to the file path. */ +public enum ChangeType { + /** Path is being created/introduced by this patch. */ + ADDED, + + /** Path already exists, and has updated content. */ + MODIFIED, + + /** Path existed, but is being removed by this patch. */ + DELETED, + + /** Path existed but was moved. */ + RENAMED, + + /** Path was copied from source. */ + COPIED, + + /** Sufficient amount of content changed to claim the file was rewritten. */ + REWRITE; +} diff --git a/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/DiffInfo.java b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/DiffInfo.java new file mode 100644 index 0000000000..e58ffc51ed --- /dev/null +++ b/gerrit-extension-api/src/main/java/com/google/gerrit/extensions/common/DiffInfo.java @@ -0,0 +1,74 @@ +// Copyright (C) 2014 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.extensions.common; + +import java.util.List; + +/* This entity contains information about the diff of a file in a revision. */ +public class DiffInfo { + // Meta information about the file on side A + public FileMeta metaA; + // Meta information about the file on side B + public FileMeta metaB; + // Intraline status + public IntraLineStatus intralineStatus; + // The type of change + public ChangeType changeType; + // A list of strings representing the patch set diff header + public List diffHeader; + // The content differences in the file as a list of entities + public List content; + // Links to the file diff in external sites + public List webLinks; + + public static enum IntraLineStatus { + OK, + TIMEOUT, + FAILURE + } + + public static class FileMeta { + // The name of the file + public String name; + // The content type of the file + public String contentType; + // The total number of lines in the file + public Integer lines; + // Links to the file in external sites + public List webLinks; + } + + public static final class ContentEntry { + // Common lines to both sides. + public List ab; + // Lines of a. + public List a; + // Lines of b. + public List b; + + // A list of changed sections of the corresponding line list. + // Each entry is a character pair. The offset is from the + // beginning of the first line in the list. Also, the offset includes an + // implied trailing newline character for each line. + public List> editA; + public List> editB; + + // a and b are actually common with this whitespace ignore setting. + public Boolean common; + + // Number of lines to skip on both sides. + public Integer skip; + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/change/GetDiff.java b/gerrit-server/src/main/java/com/google/gerrit/server/change/GetDiff.java index c183d5d2f5..8ffc21b64a 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/change/GetDiff.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/change/GetDiff.java @@ -19,10 +19,17 @@ import static com.google.common.base.Preconditions.checkState; import com.google.common.base.MoreObjects; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; import com.google.gerrit.common.data.PatchScript; import com.google.gerrit.common.data.PatchScript.DisplayMethod; import com.google.gerrit.common.data.PatchScript.FileMode; +import com.google.gerrit.extensions.common.ChangeType; +import com.google.gerrit.extensions.common.DiffInfo; +import com.google.gerrit.extensions.common.DiffInfo.ContentEntry; +import com.google.gerrit.extensions.common.DiffInfo.FileMeta; +import com.google.gerrit.extensions.common.DiffInfo.IntraLineStatus; import com.google.gerrit.extensions.common.DiffWebLinkInfo; import com.google.gerrit.extensions.common.WebLinkInfo; import com.google.gerrit.extensions.restapi.AuthException; @@ -36,7 +43,6 @@ import com.google.gerrit.prettify.common.SparseFileContent; import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.AccountDiffPreference; import com.google.gerrit.reviewdb.client.Patch; -import com.google.gerrit.reviewdb.client.Patch.ChangeType; import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.server.WebLinks; @@ -65,6 +71,17 @@ import java.util.List; import java.util.concurrent.TimeUnit; public class GetDiff implements RestReadView { + private final static ImmutableMap CHANGE_TYPE = + Maps.immutableEnumMap( + new ImmutableMap.Builder() + .put(Patch.ChangeType.ADDED, ChangeType.ADDED) + .put(Patch.ChangeType.MODIFIED, ChangeType.MODIFIED) + .put(Patch.ChangeType.DELETED, ChangeType.DELETED) + .put(Patch.ChangeType.RENAMED, ChangeType.RENAMED) + .put(Patch.ChangeType.COPIED, ChangeType.COPIED) + .put(Patch.ChangeType.REWRITE, ChangeType.REWRITE) + .build()); + private final ProjectCache projectCache; private final PatchScriptFactory.Factory patchScriptFactoryFactory; private final Revisions revisions; @@ -97,7 +114,7 @@ public class GetDiff implements RestReadView { } @Override - public Response apply(FileResource resource) + public Response apply(FileResource resource) throws ResourceConflictException, ResourceNotFoundException, OrmException, AuthException, InvalidChangeOperationException, IOException { PatchSet basePatchSet = null; @@ -151,7 +168,7 @@ public class GetDiff implements RestReadView { ProjectState state = projectCache.get(resource.getRevision().getChange().getProject()); - Result result = new Result(); + DiffInfo result = new DiffInfo(); // TODO referring to the parent commit by refs/changes/12/60012/1^1 // will likely not work for inline edits String revA = basePatchSet != null @@ -202,14 +219,19 @@ public class GetDiff implements RestReadView { } } - result.changeType = ps.getChangeType(); + result.changeType = CHANGE_TYPE.get(ps.getChangeType()); + if (result.changeType == null) { + throw new IllegalStateException( + "unknown change type: " + ps.getChangeType()); + } + if (ps.getPatchHeader().size() > 0) { result.diffHeader = ps.getPatchHeader(); } result.content = content.lines; } - Response r = Response.ok(result); + Response r = Response.ok(result); if (resource.isCacheable()) { r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS)); } @@ -228,23 +250,6 @@ public class GetDiff implements RestReadView { return links.isEmpty() ? null : links.toList(); } - static class Result { - FileMeta metaA; - FileMeta metaB; - IntraLineStatus intralineStatus; - ChangeType changeType; - List diffHeader; - List content; - List webLinks; - } - - static class FileMeta { - String name; - String contentType; - Integer lines; - List webLinks; - } - private void setContentType(FileMeta meta, ProjectState project, FileMode fileMode, String mimeType) { switch (fileMode) { @@ -273,12 +278,6 @@ public class GetDiff implements RestReadView { } } - enum IntraLineStatus { - OK, - TIMEOUT, - FAILURE - } - private static class Content { final List lines; final SparseFileContent fileA; @@ -393,28 +392,6 @@ public class GetDiff implements RestReadView { } } - static final class ContentEntry { - // Common lines to both sides. - List ab; - // Lines of a. - List a; - // Lines of b. - List b; - - // A list of changed sections of the corresponding line list. - // Each entry is a character pair. The offset is from the - // beginning of the first line in the list. Also, the offset includes an - // implied trailing newline character for each line. - List> editA; - List> editB; - - // a and b are actually common with this whitespace ignore setting. - Boolean common; - - // Number of lines to skip on both sides. - Integer skip; - } - public static class ContextOptionHandler extends OptionHandler { public ContextOptionHandler( CmdLineParser parser, OptionDef option, Setter setter) {