LCOV - code coverage report
Current view: top level - server/restapi/change - GetMergeList.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 32 34 94.1 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2016 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.ImmutableList;
      20             : import com.google.common.collect.ImmutableSet;
      21             : import com.google.gerrit.entities.Project;
      22             : import com.google.gerrit.extensions.common.CommitInfo;
      23             : import com.google.gerrit.extensions.restapi.BadRequestException;
      24             : import com.google.gerrit.extensions.restapi.CacheControl;
      25             : import com.google.gerrit.extensions.restapi.Response;
      26             : import com.google.gerrit.extensions.restapi.RestReadView;
      27             : import com.google.gerrit.server.change.RevisionJson;
      28             : import com.google.gerrit.server.change.RevisionResource;
      29             : import com.google.gerrit.server.git.GitRepositoryManager;
      30             : import com.google.gerrit.server.patch.MergeListBuilder;
      31             : import com.google.inject.Inject;
      32             : import java.io.IOException;
      33             : import java.util.ArrayList;
      34             : import java.util.List;
      35             : import org.eclipse.jgit.lib.Repository;
      36             : import org.eclipse.jgit.revwalk.RevCommit;
      37             : import org.eclipse.jgit.revwalk.RevWalk;
      38             : import org.kohsuke.args4j.Option;
      39             : 
      40             : public class GetMergeList implements RestReadView<RevisionResource> {
      41             :   private final GitRepositoryManager repoManager;
      42             :   private final RevisionJson.Factory json;
      43             : 
      44          58 :   @Option(name = "--parent", usage = "Uninteresting parent (1-based, default = 1)")
      45             :   private int uninterestingParent = 1;
      46             : 
      47             :   @Option(name = "--links", usage = "Include weblinks")
      48             :   private boolean addLinks;
      49             : 
      50             :   @Inject
      51          58 :   GetMergeList(GitRepositoryManager repoManager, RevisionJson.Factory json) {
      52          58 :     this.repoManager = repoManager;
      53          58 :     this.json = json;
      54          58 :   }
      55             : 
      56             :   public void setUninterestingParent(int uninterestingParent) {
      57           1 :     this.uninterestingParent = uninterestingParent;
      58           1 :   }
      59             : 
      60             :   public void setAddLinks(boolean addLinks) {
      61           1 :     this.addLinks = addLinks;
      62           1 :   }
      63             : 
      64             :   @Override
      65             :   public Response<List<CommitInfo>> apply(RevisionResource rsrc)
      66             :       throws BadRequestException, IOException {
      67           2 :     Project.NameKey p = rsrc.getChange().getProject();
      68           2 :     try (Repository repo = repoManager.openRepository(p);
      69           2 :         RevWalk rw = new RevWalk(repo)) {
      70           2 :       RevCommit commit = rw.parseCommit(rsrc.getPatchSet().commitId());
      71           2 :       rw.parseBody(commit);
      72             : 
      73           2 :       if (uninterestingParent < 1 || uninterestingParent > commit.getParentCount()) {
      74           0 :         throw new BadRequestException("No such parent: " + uninterestingParent);
      75             :       }
      76             : 
      77           2 :       if (commit.getParentCount() < 2) {
      78           1 :         return createResponse(rsrc, ImmutableList.of());
      79             :       }
      80             : 
      81           1 :       ImmutableList<RevCommit> commits = MergeListBuilder.build(rw, commit, uninterestingParent);
      82           1 :       List<CommitInfo> result = new ArrayList<>(commits.size());
      83           1 :       RevisionJson changeJson = json.create(ImmutableSet.of());
      84           1 :       for (RevCommit c : commits) {
      85           1 :         result.add(
      86           1 :             changeJson.getCommitInfo(
      87           1 :                 rsrc.getProject(),
      88             :                 rw,
      89             :                 c,
      90             :                 addLinks,
      91             :                 /* fillCommit= */ true,
      92           1 :                 rsrc.getChange().getDest().branch(),
      93           1 :                 rsrc.getChange().getKey().get()));
      94           1 :       }
      95           1 :       return createResponse(rsrc, result);
      96           1 :     }
      97             :   }
      98             : 
      99             :   private static Response<List<CommitInfo>> createResponse(
     100             :       RevisionResource rsrc, List<CommitInfo> result) {
     101           2 :     Response<List<CommitInfo>> r = Response.ok(result);
     102           2 :     if (rsrc.isCacheable()) {
     103           0 :       r.caching(CacheControl.PRIVATE(7, DAYS));
     104             :     }
     105           2 :     return r;
     106             :   }
     107             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750