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.change; 16 : 17 : import static com.google.gerrit.server.project.ProjectCache.illegalState; 18 : 19 : import com.google.gerrit.entities.Change; 20 : import com.google.gerrit.entities.Patch; 21 : import com.google.gerrit.entities.PatchSet; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 23 : import com.google.gerrit.extensions.restapi.BinaryResult; 24 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 25 : import com.google.gerrit.extensions.restapi.Response; 26 : import com.google.gerrit.extensions.restapi.RestReadView; 27 : import com.google.gerrit.server.PatchSetUtil; 28 : import com.google.gerrit.server.change.FileContentUtil; 29 : import com.google.gerrit.server.change.FileResource; 30 : import com.google.gerrit.server.git.GitRepositoryManager; 31 : import com.google.gerrit.server.notedb.ChangeNotes; 32 : import com.google.gerrit.server.patch.ComparisonType; 33 : import com.google.gerrit.server.patch.Text; 34 : import com.google.gerrit.server.project.NoSuchChangeException; 35 : import com.google.gerrit.server.project.ProjectCache; 36 : import com.google.inject.Inject; 37 : import java.io.IOException; 38 : import org.eclipse.jgit.errors.RepositoryNotFoundException; 39 : import org.eclipse.jgit.lib.Repository; 40 : import org.eclipse.jgit.revwalk.RevCommit; 41 : import org.eclipse.jgit.revwalk.RevWalk; 42 : import org.kohsuke.args4j.Option; 43 : 44 : public class GetContent implements RestReadView<FileResource> { 45 : private final GitRepositoryManager gitManager; 46 : private final PatchSetUtil psUtil; 47 : private final FileContentUtil fileContentUtil; 48 : private final ProjectCache projectCache; 49 : 50 : @Option(name = "--parent") 51 : private Integer parent; 52 : 53 : @Inject 54 : GetContent( 55 : GitRepositoryManager gitManager, 56 : PatchSetUtil psUtil, 57 : FileContentUtil fileContentUtil, 58 15 : ProjectCache projectCache) { 59 15 : this.gitManager = gitManager; 60 15 : this.psUtil = psUtil; 61 15 : this.fileContentUtil = fileContentUtil; 62 15 : this.projectCache = projectCache; 63 15 : } 64 : 65 : @Override 66 : public Response<BinaryResult> apply(FileResource rsrc) 67 : throws ResourceNotFoundException, IOException, BadRequestException { 68 12 : String path = rsrc.getPatchKey().fileName(); 69 12 : if (Patch.COMMIT_MSG.equals(path)) { 70 2 : String msg = getMessage(rsrc.getRevision().getChangeResource().getNotes()); 71 2 : return Response.ok( 72 2 : BinaryResult.create(msg) 73 2 : .setContentType(FileContentUtil.TEXT_X_GERRIT_COMMIT_MESSAGE) 74 2 : .base64()); 75 12 : } else if (Patch.MERGE_LIST.equals(path)) { 76 1 : byte[] mergeList = getMergeList(rsrc.getRevision().getChangeResource().getNotes()); 77 1 : return Response.ok( 78 1 : BinaryResult.create(mergeList) 79 1 : .setContentType(FileContentUtil.TEXT_X_GERRIT_MERGE_LIST) 80 1 : .base64()); 81 : } 82 11 : return Response.ok( 83 11 : fileContentUtil.getContent( 84 : projectCache 85 11 : .get(rsrc.getRevision().getProject()) 86 11 : .orElseThrow(illegalState(rsrc.getRevision().getProject())), 87 11 : rsrc.getRevision().getPatchSet().commitId(), 88 : path, 89 : parent)); 90 : } 91 : 92 : private String getMessage(ChangeNotes notes) throws IOException { 93 2 : Change.Id changeId = notes.getChangeId(); 94 2 : PatchSet ps = psUtil.current(notes); 95 2 : if (ps == null) { 96 0 : throw new NoSuchChangeException(changeId); 97 : } 98 : 99 2 : try (Repository git = gitManager.openRepository(notes.getProjectName()); 100 2 : RevWalk revWalk = new RevWalk(git)) { 101 2 : RevCommit commit = revWalk.parseCommit(ps.commitId()); 102 2 : return commit.getFullMessage(); 103 0 : } catch (RepositoryNotFoundException e) { 104 0 : throw new NoSuchChangeException(changeId, e); 105 : } 106 : } 107 : 108 : private byte[] getMergeList(ChangeNotes notes) throws IOException { 109 1 : Change.Id changeId = notes.getChangeId(); 110 1 : PatchSet ps = psUtil.current(notes); 111 1 : if (ps == null) { 112 0 : throw new NoSuchChangeException(changeId); 113 : } 114 : 115 1 : try (Repository git = gitManager.openRepository(notes.getProjectName()); 116 1 : RevWalk revWalk = new RevWalk(git)) { 117 1 : return Text.forMergeList( 118 1 : ComparisonType.againstAutoMerge(), revWalk.getObjectReader(), ps.commitId()) 119 1 : .getContent(); 120 0 : } catch (RepositoryNotFoundException e) { 121 0 : throw new NoSuchChangeException(changeId, e); 122 : } 123 : } 124 : }