LCOV - code coverage report
Current view: top level - server/git - HookUtil.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 17 26 65.4 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2017 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.git;
      16             : 
      17             : import static java.util.stream.Collectors.toMap;
      18             : 
      19             : import java.io.IOException;
      20             : import java.util.Map;
      21             : import org.eclipse.jgit.lib.Ref;
      22             : import org.eclipse.jgit.transport.ReceivePack;
      23             : import org.eclipse.jgit.transport.ServiceMayNotContinueException;
      24             : import org.eclipse.jgit.transport.UploadPack;
      25             : 
      26             : /** Static utilities for writing git protocol hooks. */
      27             : public class HookUtil {
      28             :   /**
      29             :    * Scan and advertise all refs in the repo if refs have not already been advertised; otherwise,
      30             :    * just return the advertised map.
      31             :    *
      32             :    * @param rp receive-pack handler.
      33             :    * @return map of refs that were advertised.
      34             :    * @throws ServiceMayNotContinueException if a problem occurred.
      35             :    */
      36             :   public static Map<String, Ref> ensureAllRefsAdvertised(ReceivePack rp)
      37             :       throws ServiceMayNotContinueException {
      38          97 :     Map<String, Ref> refs = rp.getAdvertisedRefs();
      39          97 :     if (refs != null) {
      40          97 :       return refs;
      41             :     }
      42             :     try {
      43          97 :       refs =
      44          97 :           rp.getRepository().getRefDatabase().getRefs().stream()
      45          97 :               .collect(toMap(Ref::getName, r -> r));
      46          97 :       rp.setAdvertisedRefs(refs, rp.getAdvertisedObjects());
      47           0 :     } catch (ServiceMayNotContinueException e) {
      48           0 :       throw e;
      49           0 :     } catch (IOException e) {
      50           0 :       throw new ServiceMayNotContinueException(e);
      51          97 :     }
      52          97 :     return refs;
      53             :   }
      54             : 
      55             :   /**
      56             :    * Scan and advertise all refs in the repo if refs have not already been advertised; otherwise,
      57             :    * just return the advertised map.
      58             :    *
      59             :    * @param up upload-pack handler.
      60             :    * @return map of refs that were advertised.
      61             :    * @throws ServiceMayNotContinueException if a problem occurred.
      62             :    */
      63             :   public static Map<String, Ref> ensureAllRefsAdvertised(UploadPack up)
      64             :       throws ServiceMayNotContinueException {
      65           6 :     Map<String, Ref> refs = up.getAdvertisedRefs();
      66           6 :     if (refs != null) {
      67           0 :       return refs;
      68             :     }
      69             :     try {
      70           6 :       refs =
      71           6 :           up.getRepository().getRefDatabase().getRefs().stream()
      72           6 :               .collect(toMap(Ref::getName, r -> r));
      73           6 :       up.setAdvertisedRefs(refs);
      74           0 :     } catch (ServiceMayNotContinueException e) {
      75           0 :       throw e;
      76           0 :     } catch (IOException e) {
      77           0 :       throw new ServiceMayNotContinueException(e);
      78           6 :     }
      79           6 :     return refs;
      80             :   }
      81             : 
      82             :   private HookUtil() {}
      83             : }

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