Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.project; 16 : 17 : import com.google.gerrit.common.data.GlobalCapability; 18 : import com.google.gerrit.extensions.annotations.RequiresCapability; 19 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 20 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 21 : import com.google.gerrit.extensions.restapi.Response; 22 : import com.google.gerrit.extensions.restapi.RestReadView; 23 : import com.google.gerrit.server.git.GitRepositoryManager; 24 : import com.google.gerrit.server.project.ProjectResource; 25 : import com.google.inject.Inject; 26 : import com.google.inject.Singleton; 27 : import java.io.IOException; 28 : import org.eclipse.jgit.api.GarbageCollectCommand; 29 : import org.eclipse.jgit.api.Git; 30 : import org.eclipse.jgit.api.errors.GitAPIException; 31 : import org.eclipse.jgit.api.errors.JGitInternalException; 32 : import org.eclipse.jgit.lib.Repository; 33 : 34 : @RequiresCapability(GlobalCapability.RUN_GC) 35 : @Singleton 36 : public class GetStatistics implements RestReadView<ProjectResource> { 37 : 38 : private final GitRepositoryManager repoManager; 39 : 40 : @Inject 41 144 : GetStatistics(GitRepositoryManager repoManager) { 42 144 : this.repoManager = repoManager; 43 144 : } 44 : 45 : @Override 46 : public Response<RepositoryStatistics> apply(ProjectResource rsrc) 47 : throws ResourceNotFoundException, ResourceConflictException { 48 1 : try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) { 49 1 : GarbageCollectCommand gc = Git.wrap(repo).gc(); 50 1 : return Response.ok(new RepositoryStatistics(gc.getStatistics())); 51 0 : } catch (GitAPIException | JGitInternalException e) { 52 0 : throw new ResourceConflictException(e.getMessage()); 53 0 : } catch (IOException e) { 54 0 : throw new ResourceNotFoundException(rsrc.getName(), e); 55 : } 56 : } 57 : }