Line data Source code
1 : // Copyright (C) 2014 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.entities.Patch; 18 : import com.google.gerrit.extensions.common.FileInfo; 19 : import com.google.gerrit.extensions.registration.DynamicMap; 20 : import com.google.gerrit.extensions.restapi.ChildCollection; 21 : import com.google.gerrit.extensions.restapi.IdString; 22 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 23 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestReadView; 26 : import com.google.gerrit.extensions.restapi.RestView; 27 : import com.google.gerrit.server.change.FileInfoJson; 28 : import com.google.gerrit.server.git.GitRepositoryManager; 29 : import com.google.gerrit.server.patch.PatchListNotAvailableException; 30 : import com.google.gerrit.server.project.CommitResource; 31 : import com.google.gerrit.server.project.FileResource; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Provider; 34 : import com.google.inject.Singleton; 35 : import java.io.IOException; 36 : import java.util.Map; 37 : import org.eclipse.jgit.revwalk.RevCommit; 38 : import org.kohsuke.args4j.Option; 39 : 40 : /** 41 : * like {@link FilesCollection}, but for commits that are specified as hex ID, rather than branch 42 : * names. 43 : */ 44 : @Singleton 45 : public class FilesInCommitCollection implements ChildCollection<CommitResource, FileResource> { 46 : private final DynamicMap<RestView<FileResource>> views; 47 : private final Provider<ListFiles> list; 48 : private final GitRepositoryManager repoManager; 49 : 50 : @Inject 51 : FilesInCommitCollection( 52 : DynamicMap<RestView<FileResource>> views, 53 : Provider<ListFiles> list, 54 138 : GitRepositoryManager repoManager) { 55 138 : this.views = views; 56 138 : this.list = list; 57 138 : this.repoManager = repoManager; 58 138 : } 59 : 60 : @Override 61 : public RestView<CommitResource> list() throws ResourceNotFoundException { 62 2 : return list.get(); 63 : } 64 : 65 : @Override 66 : public FileResource parse(CommitResource parent, IdString id) 67 : throws ResourceNotFoundException, IOException { 68 1 : if (Patch.isMagic(id.get())) { 69 0 : return new FileResource(parent.getProjectState(), parent.getCommit(), id.get()); 70 : } 71 1 : return FileResource.create(repoManager, parent.getProjectState(), parent.getCommit(), id.get()); 72 : } 73 : 74 : @Override 75 : public DynamicMap<RestView<FileResource>> views() { 76 1 : return views; 77 : } 78 : 79 : public static final class ListFiles implements RestReadView<CommitResource> { 80 : /** 81 : * The 1-based parent number. If zero, the default base commit will be used, which is the only 82 : * parent for commits having one parent or the auto-merge commit otherwise. 83 : */ 84 : @Option(name = "--parent", metaVar = "parent-number") 85 : int parentNum; 86 : 87 : private final FileInfoJson fileInfoJson; 88 : 89 : @Inject 90 5 : public ListFiles(FileInfoJson fileInfoJson) { 91 5 : this.fileInfoJson = fileInfoJson; 92 5 : } 93 : 94 : public ListFiles setParent(int parentNum) { 95 1 : this.parentNum = parentNum; 96 1 : return this; 97 : } 98 : 99 : @Override 100 : public Response<Map<String, FileInfo>> apply(CommitResource resource) 101 : throws ResourceConflictException, PatchListNotAvailableException { 102 3 : RevCommit commit = resource.getCommit(); 103 3 : return Response.ok( 104 3 : fileInfoJson.getFileInfoMap(resource.getProjectState().getNameKey(), commit, parentNum)); 105 : } 106 : } 107 : }