LCOV - code coverage report
Current view: top level - server/patch - PatchSetInfoFactory.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 27 31 87.1 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 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.patch;
      16             : 
      17             : import com.google.gerrit.entities.PatchSet;
      18             : import com.google.gerrit.entities.PatchSetInfo;
      19             : import com.google.gerrit.entities.Project;
      20             : import com.google.gerrit.exceptions.StorageException;
      21             : import com.google.gerrit.server.PatchSetUtil;
      22             : import com.google.gerrit.server.account.Emails;
      23             : import com.google.gerrit.server.git.GitRepositoryManager;
      24             : import com.google.gerrit.server.notedb.ChangeNotes;
      25             : import com.google.inject.Inject;
      26             : import com.google.inject.Singleton;
      27             : import java.io.IOException;
      28             : import java.util.ArrayList;
      29             : import java.util.List;
      30             : import org.eclipse.jgit.errors.MissingObjectException;
      31             : import org.eclipse.jgit.lib.Repository;
      32             : import org.eclipse.jgit.revwalk.RevCommit;
      33             : import org.eclipse.jgit.revwalk.RevWalk;
      34             : 
      35             : /** Factory class creating PatchSetInfo from meta-data found in Git repository. */
      36             : @Singleton
      37             : public class PatchSetInfoFactory {
      38             :   private final GitRepositoryManager repoManager;
      39             :   private final PatchSetUtil psUtil;
      40             :   private final Emails emails;
      41             : 
      42             :   @Inject
      43         146 :   public PatchSetInfoFactory(GitRepositoryManager repoManager, PatchSetUtil psUtil, Emails emails) {
      44         146 :     this.repoManager = repoManager;
      45         146 :     this.psUtil = psUtil;
      46         146 :     this.emails = emails;
      47         146 :   }
      48             : 
      49             :   public PatchSetInfo get(RevWalk rw, RevCommit src, PatchSet.Id psi) throws IOException {
      50         103 :     rw.parseBody(src);
      51         103 :     PatchSetInfo info = new PatchSetInfo(psi);
      52         103 :     info.setSubject(src.getShortMessage());
      53         103 :     info.setMessage(src.getFullMessage());
      54         103 :     info.setAuthor(emails.toUserIdentity(src.getAuthorIdent()));
      55         103 :     info.setCommitter(emails.toUserIdentity(src.getCommitterIdent()));
      56         103 :     info.setCommitId(src);
      57         103 :     return info;
      58             :   }
      59             : 
      60             :   public PatchSetInfo get(ChangeNotes notes, PatchSet.Id psId)
      61             :       throws PatchSetInfoNotAvailableException {
      62             :     try {
      63          64 :       PatchSet patchSet = psUtil.get(notes, psId);
      64          64 :       return get(notes.getProjectName(), patchSet);
      65           0 :     } catch (StorageException e) {
      66           0 :       throw new PatchSetInfoNotAvailableException(e);
      67             :     }
      68             :   }
      69             : 
      70             :   public PatchSetInfo get(Project.NameKey project, PatchSet patchSet)
      71             :       throws PatchSetInfoNotAvailableException {
      72          87 :     try (Repository repo = repoManager.openRepository(project);
      73          87 :         RevWalk rw = new RevWalk(repo)) {
      74          87 :       RevCommit src = rw.parseCommit(patchSet.commitId());
      75          87 :       PatchSetInfo info = get(rw, src, patchSet.id());
      76          87 :       info.setParents(toParentInfos(src.getParents(), rw));
      77          87 :       return info;
      78           0 :     } catch (IOException | StorageException e) {
      79           0 :       throw new PatchSetInfoNotAvailableException(e);
      80             :     }
      81             :   }
      82             : 
      83             :   private List<PatchSetInfo.ParentInfo> toParentInfos(RevCommit[] parents, RevWalk walk)
      84             :       throws IOException, MissingObjectException {
      85          87 :     List<PatchSetInfo.ParentInfo> pInfos = new ArrayList<>(parents.length);
      86          87 :     for (RevCommit parent : parents) {
      87          85 :       walk.parseBody(parent);
      88          85 :       String msg = parent.getShortMessage();
      89          85 :       pInfos.add(new PatchSetInfo.ParentInfo(parent, msg));
      90             :     }
      91          87 :     return pInfos;
      92             :   }
      93             : }

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