Factor out DiffInfo classes to extensions package

Change-Id: Ia447d9a6bfe09c7e7b41216141b806d300d27fd4
This commit is contained in:
David Ostrovsky 2014-12-15 01:28:54 +01:00 committed by David Ostrovsky
parent 29993f3430
commit 258cb8d98e
3 changed files with 137 additions and 50 deletions

View File

@ -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;
}

View File

@ -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<String> diffHeader;
// The content differences in the file as a list of entities
public List<ContentEntry> content;
// Links to the file diff in external sites
public List<DiffWebLinkInfo> 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<WebLinkInfo> webLinks;
}
public static final class ContentEntry {
// Common lines to both sides.
public List<String> ab;
// Lines of a.
public List<String> a;
// Lines of b.
public List<String> b;
// A list of changed sections of the corresponding line list.
// Each entry is a character <offset, length> 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<List<Integer>> editA;
public List<List<Integer>> 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;
}
}

View File

@ -19,10 +19,17 @@ import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists; 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;
import com.google.gerrit.common.data.PatchScript.DisplayMethod; import com.google.gerrit.common.data.PatchScript.DisplayMethod;
import com.google.gerrit.common.data.PatchScript.FileMode; 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.DiffWebLinkInfo;
import com.google.gerrit.extensions.common.WebLinkInfo; import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.restapi.AuthException; 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.Account;
import com.google.gerrit.reviewdb.client.AccountDiffPreference; import com.google.gerrit.reviewdb.client.AccountDiffPreference;
import com.google.gerrit.reviewdb.client.Patch; 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.PatchSet;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.WebLinks; import com.google.gerrit.server.WebLinks;
@ -65,6 +71,17 @@ import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class GetDiff implements RestReadView<FileResource> { public class GetDiff implements RestReadView<FileResource> {
private final static ImmutableMap<Patch.ChangeType, ChangeType> CHANGE_TYPE =
Maps.immutableEnumMap(
new ImmutableMap.Builder<Patch.ChangeType, ChangeType>()
.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 ProjectCache projectCache;
private final PatchScriptFactory.Factory patchScriptFactoryFactory; private final PatchScriptFactory.Factory patchScriptFactoryFactory;
private final Revisions revisions; private final Revisions revisions;
@ -97,7 +114,7 @@ public class GetDiff implements RestReadView<FileResource> {
} }
@Override @Override
public Response<Result> apply(FileResource resource) public Response<DiffInfo> apply(FileResource resource)
throws ResourceConflictException, ResourceNotFoundException, throws ResourceConflictException, ResourceNotFoundException,
OrmException, AuthException, InvalidChangeOperationException, IOException { OrmException, AuthException, InvalidChangeOperationException, IOException {
PatchSet basePatchSet = null; PatchSet basePatchSet = null;
@ -151,7 +168,7 @@ public class GetDiff implements RestReadView<FileResource> {
ProjectState state = ProjectState state =
projectCache.get(resource.getRevision().getChange().getProject()); 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 // TODO referring to the parent commit by refs/changes/12/60012/1^1
// will likely not work for inline edits // will likely not work for inline edits
String revA = basePatchSet != null String revA = basePatchSet != null
@ -202,14 +219,19 @@ public class GetDiff implements RestReadView<FileResource> {
} }
} }
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) { if (ps.getPatchHeader().size() > 0) {
result.diffHeader = ps.getPatchHeader(); result.diffHeader = ps.getPatchHeader();
} }
result.content = content.lines; result.content = content.lines;
} }
Response<Result> r = Response.ok(result); Response<DiffInfo> r = Response.ok(result);
if (resource.isCacheable()) { if (resource.isCacheable()) {
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS)); r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
} }
@ -228,23 +250,6 @@ public class GetDiff implements RestReadView<FileResource> {
return links.isEmpty() ? null : links.toList(); return links.isEmpty() ? null : links.toList();
} }
static class Result {
FileMeta metaA;
FileMeta metaB;
IntraLineStatus intralineStatus;
ChangeType changeType;
List<String> diffHeader;
List<ContentEntry> content;
List<DiffWebLinkInfo> webLinks;
}
static class FileMeta {
String name;
String contentType;
Integer lines;
List<WebLinkInfo> webLinks;
}
private void setContentType(FileMeta meta, ProjectState project, private void setContentType(FileMeta meta, ProjectState project,
FileMode fileMode, String mimeType) { FileMode fileMode, String mimeType) {
switch (fileMode) { switch (fileMode) {
@ -273,12 +278,6 @@ public class GetDiff implements RestReadView<FileResource> {
} }
} }
enum IntraLineStatus {
OK,
TIMEOUT,
FAILURE
}
private static class Content { private static class Content {
final List<ContentEntry> lines; final List<ContentEntry> lines;
final SparseFileContent fileA; final SparseFileContent fileA;
@ -393,28 +392,6 @@ public class GetDiff implements RestReadView<FileResource> {
} }
} }
static final class ContentEntry {
// Common lines to both sides.
List<String> ab;
// Lines of a.
List<String> a;
// Lines of b.
List<String> b;
// A list of changed sections of the corresponding line list.
// Each entry is a character <offset, length> 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<List<Integer>> editA;
List<List<Integer>> 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<Short> { public static class ContextOptionHandler extends OptionHandler<Short> {
public ContextOptionHandler( public ContextOptionHandler(
CmdLineParser parser, OptionDef option, Setter<Short> setter) { CmdLineParser parser, OptionDef option, Setter<Short> setter) {