Move FileInfo to FileInfoJson and implement Files.list().

Change-Id: I893dd097f2372f9a0e2a788a973fa30b53b3e0e4
This commit is contained in:
Colby Ranger
2013-05-07 19:55:34 -07:00
parent a41f1d5ae2
commit 42be1d8206
5 changed files with 146 additions and 58 deletions

View File

@@ -25,6 +25,11 @@ public class IdString {
return new IdString(id); return new IdString(id);
} }
/** Construct an identifier from an already decoded string. */
public static IdString fromDecoded(String id) {
return new IdString(Url.encode(id));
}
private final String urlEncoded; private final String urlEncoded;
private IdString(String s) { private IdString(String s) {

View File

@@ -64,9 +64,6 @@ import com.google.gerrit.server.account.AccountInfo;
import com.google.gerrit.server.config.CanonicalWebUrl; import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.GerritServerConfig; import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.git.LabelNormalizer; import com.google.gerrit.server.git.LabelNormalizer;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.patch.PatchListEntry;
import com.google.gerrit.server.patch.PatchListNotAvailableException; import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gerrit.server.patch.PatchSetInfoFactory; import com.google.gerrit.server.patch.PatchSetInfoFactory;
import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException; import com.google.gerrit.server.patch.PatchSetInfoNotAvailableException;
@@ -124,7 +121,7 @@ public class ChangeJson {
private final IdentifiedUser.GenericFactory userFactory; private final IdentifiedUser.GenericFactory userFactory;
private final ChangeControl.GenericFactory changeControlGenericFactory; private final ChangeControl.GenericFactory changeControlGenericFactory;
private final PatchSetInfoFactory patchSetInfoFactory; private final PatchSetInfoFactory patchSetInfoFactory;
private final PatchListCache patchListCache; private final FileInfoJson fileInfoJson;
private final AccountInfo.Loader.Factory accountLoaderFactory; private final AccountInfo.Loader.Factory accountLoaderFactory;
private final Provider<String> urlProvider; private final Provider<String> urlProvider;
private final Urls urls; private final Urls urls;
@@ -143,7 +140,7 @@ public class ChangeJson {
IdentifiedUser.GenericFactory uf, IdentifiedUser.GenericFactory uf,
ChangeControl.GenericFactory ccf, ChangeControl.GenericFactory ccf,
PatchSetInfoFactory psi, PatchSetInfoFactory psi,
PatchListCache plc, FileInfoJson fileInfoJson,
AccountInfo.Loader.Factory ailf, AccountInfo.Loader.Factory ailf,
@CanonicalWebUrl Provider<String> curl, @CanonicalWebUrl Provider<String> curl,
Urls urls) { Urls urls) {
@@ -154,7 +151,7 @@ public class ChangeJson {
this.userFactory = uf; this.userFactory = uf;
this.changeControlGenericFactory = ccf; this.changeControlGenericFactory = ccf;
this.patchSetInfoFactory = psi; this.patchSetInfoFactory = psi;
this.patchListCache = plc; this.fileInfoJson = fileInfoJson;
this.accountLoaderFactory = ailf; this.accountLoaderFactory = ailf;
this.urlProvider = curl; this.urlProvider = curl;
this.urls = urls; this.urls = urls;
@@ -770,49 +767,11 @@ public class ChangeJson {
} }
if (has(ALL_FILES) || (out.isCurrent && has(CURRENT_FILES))) { if (has(ALL_FILES) || (out.isCurrent && has(CURRENT_FILES))) {
PatchList list;
try { try {
list = patchListCache.get(cd.change(db), in); out.files = fileInfoJson.toFileInfoMap(cd.change(db), in);
out.files.remove(Patch.COMMIT_MSG);
} catch (PatchListNotAvailableException e) { } catch (PatchListNotAvailableException e) {
log.warn("Cannot load PatchList " + in.getId(), e); log.warn("Cannot load PatchList " + in.getId(), e);
list = null;
}
if (list != null) {
out.files = Maps.newTreeMap();
for (PatchListEntry e : list.getPatches()) {
if (Patch.COMMIT_MSG.equals(e.getNewName())) {
continue;
}
FileInfo d = new FileInfo();
d.status = e.getChangeType() != Patch.ChangeType.MODIFIED
? e.getChangeType().getCode()
: null;
d.oldPath = e.getOldName();
if (e.getPatchType() == Patch.PatchType.BINARY) {
d.binary = true;
} else {
d.linesInserted = e.getInsertions() > 0 ? e.getInsertions() : null;
d.linesDeleted = e.getDeletions() > 0 ? e.getDeletions() : null;
}
FileInfo o = out.files.put(e.getNewName(), d);
if (o != null) {
// This should only happen on a delete-add break created by JGit
// when the file was rewritten and too little content survived. Write
// a single record with data from both sides.
d.status = Patch.ChangeType.REWRITE.getCode();
if (o.binary != null && o.binary) {
d.binary = true;
}
if (o.linesInserted != null) {
d.linesInserted = o.linesInserted;
}
if (o.linesDeleted != null) {
d.linesDeleted = o.linesDeleted;
}
}
}
} }
} }
return out; return out;
@@ -902,7 +861,7 @@ public class ChangeJson {
int _number; int _number;
Map<String, FetchInfo> fetch; Map<String, FetchInfo> fetch;
CommitInfo commit; CommitInfo commit;
Map<String, FileInfo> files; Map<String, FileInfoJson.FileInfo> files;
} }
static class FetchInfo { static class FetchInfo {
@@ -931,14 +890,6 @@ public class ChangeJson {
String message; String message;
} }
static class FileInfo {
Character status;
Boolean binary;
String oldPath;
Integer linesInserted;
Integer linesDeleted;
}
static class LabelInfo { static class LabelInfo {
transient SubmitRecord.Label.Status _status; transient SubmitRecord.Label.Status _status;

View File

@@ -0,0 +1,97 @@
// Copyright (C) 2013 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.server.change;
import com.google.common.collect.Maps;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.Patch;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.reviewdb.client.AccountDiffPreference.Whitespace;
import com.google.gerrit.server.patch.PatchList;
import com.google.gerrit.server.patch.PatchListCache;
import com.google.gerrit.server.patch.PatchListEntry;
import com.google.gerrit.server.patch.PatchListKey;
import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.inject.Inject;
import org.eclipse.jgit.lib.ObjectId;
import java.util.Map;
import javax.annotation.Nullable;
public class FileInfoJson {
private final PatchListCache patchListCache;
@Inject
FileInfoJson(PatchListCache patchListCache) {
this.patchListCache = patchListCache;
}
Map<String, FileInfo> toFileInfoMap(Change change, PatchSet patchSet)
throws PatchListNotAvailableException {
return toFileInfoMap(change, patchSet, null);
}
Map<String, FileInfo> toFileInfoMap(Change change, PatchSet patchSet, @Nullable PatchSet base)
throws PatchListNotAvailableException {
ObjectId a = (base == null)
? null
: ObjectId.fromString(base.getRevision().get());
ObjectId b = ObjectId.fromString(patchSet.getRevision().get());
PatchList list = patchListCache.get(
new PatchListKey(change.getProject(), a, b, Whitespace.IGNORE_NONE));
Map<String, FileInfo> files = Maps.newTreeMap();
for (PatchListEntry e : list.getPatches()) {
FileInfoJson.FileInfo d = new FileInfoJson.FileInfo();
d.status = e.getChangeType() != Patch.ChangeType.MODIFIED
? e.getChangeType().getCode() : null;
d.oldPath = e.getOldName();
if (e.getPatchType() == Patch.PatchType.BINARY) {
d.binary = true;
} else {
d.linesInserted = e.getInsertions() > 0 ? e.getInsertions() : null;
d.linesDeleted = e.getDeletions() > 0 ? e.getDeletions() : null;
}
FileInfoJson.FileInfo o = files.put(e.getNewName(), d);
if (o != null) {
// This should only happen on a delete-add break created by JGit
// when the file was rewritten and too little content survived. Write
// a single record with data from both sides.
d.status = Patch.ChangeType.REWRITE.getCode();
if (o.binary != null && o.binary) {
d.binary = true;
}
if (o.linesInserted != null) {
d.linesInserted = o.linesInserted;
}
if (o.linesDeleted != null) {
d.linesDeleted = o.linesDeleted;
}
}
}
return files;
}
static class FileInfo {
Character status;
Boolean binary;
String oldPath;
Integer linesInserted;
Integer linesDeleted;
}
}

View File

@@ -19,16 +19,28 @@ import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ChildCollection; import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString; import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.RestView; import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider;
import org.kohsuke.args4j.Option;
class Files implements ChildCollection<RevisionResource, FileResource> { class Files implements ChildCollection<RevisionResource, FileResource> {
private final DynamicMap<RestView<FileResource>> views; private final DynamicMap<RestView<FileResource>> views;
private final FileInfoJson fileInfoJson;
private final Provider<Revisions> revisions;
@Inject @Inject
Files(DynamicMap<RestView<FileResource>> views) { Files(DynamicMap<RestView<FileResource>> views,
FileInfoJson fileInfoJson,
Provider<Revisions> revisions) {
this.views = views; this.views = views;
this.fileInfoJson = fileInfoJson;
this.revisions = revisions;
} }
@Override @Override
@@ -38,7 +50,7 @@ class Files implements ChildCollection<RevisionResource, FileResource> {
@Override @Override
public RestView<RevisionResource> list() throws AuthException { public RestView<RevisionResource> list() throws AuthException {
throw new UnsupportedOperationException(); return new List();
} }
@Override @Override
@@ -46,4 +58,23 @@ class Files implements ChildCollection<RevisionResource, FileResource> {
throws ResourceNotFoundException, OrmException, AuthException { throws ResourceNotFoundException, OrmException, AuthException {
return new FileResource(rev, id.get()); return new FileResource(rev, id.get());
} }
private final class List implements RestReadView<RevisionResource> {
@Option(name = "--base", metaVar = "revision-id")
String base;
@Override
public Object apply(RevisionResource resource)
throws ResourceNotFoundException, OrmException,
PatchListNotAvailableException {
PatchSet basePatchSet = null;
if (base != null) {
RevisionResource baseResource = revisions.get().parse(
resource.getChangeResource(), IdString.fromDecoded(base));
basePatchSet = baseResource.getPatchSet();
}
return fileInfoJson.toFileInfoMap(
resource.getChange(), resource.getPatchSet(), basePatchSet);
}
}
} }

View File

@@ -35,8 +35,12 @@ public class RevisionResource implements RestResource {
this.ps = ps; this.ps = ps;
} }
public ChangeResource getChangeResource() {
return change;
}
public ChangeControl getControl() { public ChangeControl getControl() {
return change.getControl(); return getChangeResource().getControl();
} }
public Change getChange() { public Change getChange() {