com.google.gerrit.server.restapi.change.Files Maven / Gradle / Ivy
The newest version!
// 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.restapi.change;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.entities.Account;
import com.google.gerrit.entities.Change;
import com.google.gerrit.entities.PatchSet;
import com.google.gerrit.entities.Project;
import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.common.FileInfo;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.CacheControl;
import com.google.gerrit.extensions.restapi.ChildCollection;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.RestView;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.PatchSetUtil;
import com.google.gerrit.server.change.AccountPatchReviewStore;
import com.google.gerrit.server.change.AccountPatchReviewStore.PatchSetWithReviewedFiles;
import com.google.gerrit.server.change.FileInfoJson;
import com.google.gerrit.server.change.FileResource;
import com.google.gerrit.server.change.RevisionResource;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.patch.DiffNotAvailableException;
import com.google.gerrit.server.patch.DiffOperations;
import com.google.gerrit.server.patch.DiffOptions;
import com.google.gerrit.server.patch.PatchListNotAvailableException;
import com.google.gerrit.server.patch.filediff.FileDiffOutput;
import com.google.gerrit.server.permissions.PermissionBackendException;
import com.google.gerrit.server.plugincontext.PluginItemContext;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.kohsuke.args4j.Option;
@Singleton
public class Files implements ChildCollection {
private final DynamicMap> views;
private final Provider list;
@Inject
Files(DynamicMap> views, Provider list) {
this.views = views;
this.list = list;
}
@Override
public DynamicMap> views() {
return views;
}
@Override
public RestView list() throws AuthException {
return list.get();
}
@Override
public FileResource parse(RevisionResource rev, IdString id) {
return new FileResource(rev, id.get());
}
public static final class ListFiles implements RestReadView {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Option(name = "--base", metaVar = "revision-id")
String base;
@Option(name = "--parent", metaVar = "parent-number")
int parentNum;
@Option(name = "--reviewed")
boolean reviewed;
@Option(name = "-q")
String query;
private final DiffOperations diffOperations;
private final Provider self;
private final FileInfoJson fileInfoJson;
private final Revisions revisions;
private final GitRepositoryManager gitManager;
private final PatchSetUtil psUtil;
private final PluginItemContext accountPatchReviewStore;
private final GerritApi gApi;
@Inject
ListFiles(
DiffOperations diffOperations,
Provider self,
FileInfoJson fileInfoJson,
Revisions revisions,
GitRepositoryManager gitManager,
PatchSetUtil psUtil,
PluginItemContext accountPatchReviewStore,
GerritApi gApi) {
this.diffOperations = diffOperations;
this.self = self;
this.fileInfoJson = fileInfoJson;
this.revisions = revisions;
this.gitManager = gitManager;
this.psUtil = psUtil;
this.accountPatchReviewStore = accountPatchReviewStore;
this.gApi = gApi;
}
public ListFiles setReviewed(boolean r) {
this.reviewed = r;
return this;
}
@Override
public Response> apply(RevisionResource resource)
throws RestApiException, RepositoryNotFoundException, IOException,
PatchListNotAvailableException, PermissionBackendException {
checkOptions();
if (reviewed) {
return Response.ok(reviewed(resource));
} else if (query != null) {
return Response.ok(query(resource));
}
Response