// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.submit;

import com.google.common.flogger.FluentLogger;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.SubmoduleSubscription;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo;
import com.google.gerrit.server.util.git.SubmoduleSectionParser;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.BlobBasedConfig;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.TreeWalk;

/**
 * Loads the .gitmodules file of the specified project/branch. It can be queried which submodules
 * this branch is subscribed to.
 */
public class GitModules {
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  public interface Factory {
    GitModules create(Branch.NameKey project, MergeOpRepoManager m);
  }

  private static final String GIT_MODULES = ".gitmodules";

  private final Branch.NameKey branch;
  Set<SubmoduleSubscription> subscriptions;

  @Inject
  GitModules(
      @CanonicalWebUrl @Nullable String canonicalWebUrl,
      @Assisted Branch.NameKey branch,
      @Assisted MergeOpRepoManager orm)
      throws IOException {
    this.branch = branch;
    Project.NameKey project = branch.getParentKey();
    logger.atFine().log("Loading .gitmodules of %s for project %s", branch, project);
    try {
      OpenRepo or = orm.getRepo(project);
      ObjectId id = or.repo.resolve(branch.get());
      if (id == null) {
        throw new IOException("Cannot open branch " + branch.get());
      }
      RevCommit commit = or.rw.parseCommit(id);

      try (TreeWalk tw = TreeWalk.forPath(or.repo, GIT_MODULES, commit.getTree())) {
        if (tw == null || (tw.getRawMode(0) & FileMode.TYPE_MASK) != FileMode.TYPE_FILE) {
          subscriptions = Collections.emptySet();
          logger.atFine().log("The .gitmodules file doesn't exist in %s", branch);
          return;
        }
      }
      BlobBasedConfig config;
      try {
        config = new BlobBasedConfig(null, or.repo, commit, GIT_MODULES);
      } catch (ConfigInvalidException e) {
        throw new IOException(
            "Could not read .gitmodules of super project: " + branch.getParentKey(), e);
      }
      subscriptions =
          new SubmoduleSectionParser(config, canonicalWebUrl, branch).parseAllSections();
    } catch (NoSuchProjectException e) {
      throw new IOException(e);
    }
  }

  public Collection<SubmoduleSubscription> subscribedTo(Branch.NameKey src) {
    logger.atFine().log("Checking for a subscription of %s for %s", src, branch);
    Collection<SubmoduleSubscription> ret = new ArrayList<>();
    for (SubmoduleSubscription s : subscriptions) {
      if (s.getSubmodule().equals(src)) {
        logger.atFine().log("Found %s", s);
        ret.add(s);
      }
    }
    return ret;
  }
}

@@ -2054,39 +2054,8 @@
 ate 
-final Branch.NameKey branch;%0A  
 Set%3C
@@ -2284,34 +2284,8 @@
 n %7B%0A
-    this.branch = branch;%0A
     
@@ -3502,23 +3502,16 @@
 %0A  %7D%0A%0A  
-public 
 Collecti
@@ -3575,90 +3575,8 @@
 ) %7B%0A
-    logger.atFine().log(%22Checking for a subscription of %25s for %25s%22, src, branch);%0A
     
@@ -3682,24 +3682,24 @@
 riptions) %7B%0A
+
       if (s.
@@ -3732,52 +3732,8 @@
 ) %7B%0A
-        logger.atFine().log(%22Found %25s%22, s);%0A
     

Remove unneeded submodule submit logs that spam the error log