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 java.util.concurrent.TimeUnit.DAYS; 18 : 19 : import com.google.common.collect.ImmutableSet; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.extensions.common.CommitInfo; 22 : import com.google.gerrit.extensions.restapi.CacheControl; 23 : import com.google.gerrit.extensions.restapi.Response; 24 : import com.google.gerrit.extensions.restapi.RestReadView; 25 : import com.google.gerrit.server.change.RevisionJson; 26 : import com.google.gerrit.server.change.RevisionResource; 27 : import com.google.gerrit.server.git.GitRepositoryManager; 28 : import com.google.inject.Inject; 29 : import java.io.IOException; 30 : import org.eclipse.jgit.lib.Repository; 31 : import org.eclipse.jgit.revwalk.RevCommit; 32 : import org.eclipse.jgit.revwalk.RevWalk; 33 : import org.kohsuke.args4j.Option; 34 : 35 : public class GetCommit implements RestReadView<RevisionResource> { 36 : private final GitRepositoryManager repoManager; 37 : private final RevisionJson.Factory json; 38 : 39 : private boolean addLinks; 40 : 41 : @Inject 42 86 : GetCommit(GitRepositoryManager repoManager, RevisionJson.Factory json) { 43 86 : this.repoManager = repoManager; 44 86 : this.json = json; 45 86 : } 46 : 47 : @Option(name = "--links", usage = "Include weblinks") 48 : public GetCommit setAddLinks(boolean addLinks) { 49 13 : this.addLinks = addLinks; 50 13 : return this; 51 : } 52 : 53 : @Override 54 : public Response<CommitInfo> apply(RevisionResource rsrc) throws IOException { 55 14 : Project.NameKey p = rsrc.getChange().getProject(); 56 14 : try (Repository repo = repoManager.openRepository(p); 57 14 : RevWalk rw = new RevWalk(repo)) { 58 14 : RevCommit commit = rw.parseCommit(rsrc.getPatchSet().commitId()); 59 14 : rw.parseBody(commit); 60 14 : CommitInfo info = 61 14 : json.create(ImmutableSet.of()) 62 14 : .getCommitInfo( 63 14 : rsrc.getProject(), 64 : rw, 65 : commit, 66 : addLinks, 67 : /* fillCommit= */ true, 68 14 : rsrc.getChange().getDest().branch(), 69 14 : rsrc.getChange().getKey().get()); 70 14 : Response<CommitInfo> r = Response.ok(info); 71 14 : if (rsrc.isCacheable()) { 72 6 : r.caching(CacheControl.PRIVATE(7, DAYS)); 73 : } 74 14 : return r; 75 : } 76 : } 77 : }