view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ServerHgRepo.java @ 949:32bfdf49827f

disable failing test The tested logic is not used
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Thu, 17 Mar 2016 11:51:14 +0100
parents c0c1a3f4b6f7
children e4332192fc0d 38adef4f1b8f
line wrap: on
line source
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 jetbrains.buildServer.buildTriggers.vcs.mercurial;

import com.intellij.openapi.util.Pair;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
import jetbrains.buildServer.util.graph.DAG;
import jetbrains.buildServer.util.graph.DAGs;
import jetbrains.buildServer.vcs.ModificationData;
import jetbrains.buildServer.vcs.VcsChange;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.List;
import java.util.Map;

import static java.util.Collections.emptyList;

/**
 * @author dmitry.neverov
 */
public class ServerHgRepo extends HgRepo {

  private final static HgVersion REVSET_HG_VERSION = new HgVersion(1, 7, 0);
  private final CommandSettingsFactory myCommandSettingsFactory;
  private final ServerPluginConfig myConfig;
  protected final MercurialClasspathTemplate myLogTemplate = new MercurialClasspathTemplate("/buildServerResources/log.template", "hg.log.template");
  protected final MercurialClasspathTemplate myLogNoFilesTemplate = new MercurialClasspathTemplate("/buildServerResources/log.no.files.template", "hg.short.log.template");
  protected final MercurialClasspathTemplate myDagTemplate = new MercurialClasspathTemplate("/buildServerResources/dag.template", "hg.dag.template");
  protected final MercurialClasspathTemplate myFastLogTemplate = new MercurialClasspathTemplate("/buildServerResources/fastlog.template", "hg.fastlog.template");
  private CollectChangesContext myContext;

  public ServerHgRepo(@NotNull CommandSettingsFactory commandSettingsFactory,
                      @NotNull ServerPluginConfig config,
                      @NotNull File workingDir,
                      @NotNull String hgPath,
                      @NotNull AuthSettings authSettings) {
    super(commandSettingsFactory, workingDir, hgPath, authSettings);
    myCommandSettingsFactory = commandSettingsFactory;
    myConfig = config;
  }

  public void setOperationContext(@NotNull CollectChangesContext context) {
    myContext = context;
  }

  public LogCommand log() {
    return new LogCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings).withTemplate(myLogTemplate);
  }

  @NotNull
  public LogCommand logNoFiles() {
    return log().withTemplate(myLogNoFilesTemplate);
  }

  public LogCommand log(@NotNull HgVcsRoot root) {
    final MercurialTemplate template = root.isSubrepo() && !myConfig.reportSubrepoChangesFileStatus() ? myFastLogTemplate : myLogTemplate;
    return new LogCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings).withTemplate(template);
  }

  public MergeBaseCommand mergeBase() throws VcsException {
    HgVersion hgVersion = getHgVersion();
    if (hgVersion.isEqualsOrGreaterThan(REVSET_HG_VERSION))
      return new MergeBaseWithRevsets(this);
    else
      return new MergeBaseNoRevsets(this);
  }

  @NotNull
  public CollectChangesCommand collectChanges(@NotNull HgVcsRoot root) throws VcsException {
    if (myConfig.dontUseRevsets())
      return new CollectChangesNoRevsets(root, this, myLogNoFilesTemplate);

    HgVersion hgVersion = getHgVersion();
    if (hgVersion.isEqualsOrGreaterThan(REVSET_HG_VERSION)) {
      return new CollectChangesWithRevsets(root, this);
    } else {
      return new CollectChangesNoRevsets(root, this, myLogNoFilesTemplate);
    }
  }

  public boolean supportRevsets() throws VcsException {
    HgVersion hgVersion = getHgVersion();
    return hgVersion.isEqualsOrGreaterThan(REVSET_HG_VERSION);
  }

  private HgVersion getHgVersion() throws VcsException {
    if (myContext != null)
      return myContext.getHgVersion(this);
    return version().call();
  }

  @NotNull
  public DAG<String> loadDag() throws VcsException {
    LoadDagCommand loadDag = new LoadDagCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings, myDagTemplate);
    loadDag.setMaxDagNodesCount(myConfig.getMaxDagNodesCount());
    List<Pair<String, String>> edges = loadDag.call();
    return DAGs.createFromEdges(edges);
  }

  public Map<String, SubRepo> getSubrepositories(@NotNull ModificationData m) {
    if (hasSubrepoConfigChanges(m))
      return getSubrepositories(m.getVersion());
    for (String p : m.getParentRevisions()) {
      Map<String, SubRepo> cached = mySubreposCache.get(p);
      if (cached != null) {
        mySubreposCache.put(m.getVersion(), cached);
        return cached;
      }
    }
    return getSubrepositories(m.getVersion());
  }

  public List<HgSubrepoConfigChange> getSubrepoConfigChanges(@NotNull ModificationData m) {
    if (hasSubrepoConfigChanges(m))
      return getSubrepoConfigChanges(m.getVersion(), m.getParentRevisions());
    return emptyList();
  }

  private boolean hasSubrepoConfigChanges(@NotNull ModificationData m) {
    for (VcsChange c : m.getChanges()) {
      //use endsWith instead of equals because files are mapped by checkout rules
      if (c.getFileName().endsWith(".hgsub") || c.getFileName().endsWith(".hgsubstate"))
        return true;
    }
    return false;
  }
}