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

          Line data    Source code
       1             : // Copyright (C) 2020 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.submit;
      16             : 
      17             : import com.google.common.collect.ImmutableSet;
      18             : import com.google.gerrit.entities.BranchNameKey;
      19             : import com.google.gerrit.entities.Project;
      20             : import com.google.gerrit.entities.SubmoduleSubscription;
      21             : import java.util.Collection;
      22             : import java.util.HashSet;
      23             : import java.util.LinkedHashSet;
      24             : import java.util.Set;
      25             : 
      26             : /**
      27             :  * Sorts the projects or branches affected by the update.
      28             :  *
      29             :  * <p>The subscription graph contains all branches (and projects) affected by the update, but the
      30             :  * updates must be executed in the right order, so no superproject reference is updated before its
      31             :  * target.
      32             :  */
      33             : class UpdateOrderCalculator {
      34             : 
      35             :   private final SubscriptionGraph subscriptionGraph;
      36             : 
      37          68 :   UpdateOrderCalculator(SubscriptionGraph subscriptionGraph) {
      38          68 :     this.subscriptionGraph = subscriptionGraph;
      39          68 :   }
      40             : 
      41             :   ImmutableSet<Project.NameKey> getProjectsInOrder() throws SubmoduleConflictException {
      42          68 :     LinkedHashSet<Project.NameKey> projects = new LinkedHashSet<>();
      43          68 :     for (Project.NameKey project : subscriptionGraph.getAffectedSuperProjects()) {
      44           2 :       addAllSubmoduleProjects(project, new LinkedHashSet<>(), projects);
      45           2 :     }
      46             : 
      47          68 :     for (BranchNameKey branch : subscriptionGraph.getUpdatedBranches()) {
      48          68 :       projects.add(branch.project());
      49          68 :     }
      50          68 :     return ImmutableSet.copyOf(projects);
      51             :   }
      52             : 
      53             :   private void addAllSubmoduleProjects(
      54             :       Project.NameKey project,
      55             :       LinkedHashSet<Project.NameKey> current,
      56             :       LinkedHashSet<Project.NameKey> projects)
      57             :       throws SubmoduleConflictException {
      58           2 :     if (current.contains(project)) {
      59           1 :       throw new SubmoduleConflictException(
      60             :           "Project level circular subscriptions detected:  "
      61           1 :               + CircularPathFinder.printCircularPath(current, project));
      62             :     }
      63             : 
      64           2 :     if (projects.contains(project)) {
      65           1 :       return;
      66             :     }
      67             : 
      68           2 :     current.add(project);
      69           2 :     Set<Project.NameKey> subprojects = new HashSet<>();
      70           2 :     for (BranchNameKey branch : subscriptionGraph.getAffectedSuperBranches(project)) {
      71           2 :       Collection<SubmoduleSubscription> subscriptions = subscriptionGraph.getSubscriptions(branch);
      72           2 :       for (SubmoduleSubscription s : subscriptions) {
      73           2 :         subprojects.add(s.getSubmodule().project());
      74           2 :       }
      75           2 :     }
      76             : 
      77           2 :     for (Project.NameKey p : subprojects) {
      78           2 :       addAllSubmoduleProjects(p, current, projects);
      79           2 :     }
      80             : 
      81           2 :     current.remove(project);
      82           2 :     projects.add(project);
      83           2 :   }
      84             : 
      85             :   ImmutableSet<BranchNameKey> getBranchesInOrder() {
      86          53 :     LinkedHashSet<BranchNameKey> branches = new LinkedHashSet<>();
      87          53 :     branches.addAll(subscriptionGraph.getSortedSuperprojectAndSubmoduleBranches());
      88          53 :     branches.addAll(subscriptionGraph.getUpdatedBranches());
      89          53 :     return ImmutableSet.copyOf(branches);
      90             :   }
      91             : }

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