view mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialAgentSideVcsSupport.java @ 213:8ec94ce51d84

Do init+pull instead of clone in agent-side checkout
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Mon, 04 Apr 2011 18:01:45 +0400
parents 0ea2ad14ce97
children 643fa1236f4e 9c06a500681c
line wrap: on
line source
/*
 * Copyright 2000-2011 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 jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.BuildAgentConfiguration;
import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.agent.vcs.AgentVcsSupport;
import jetbrains.buildServer.agent.vcs.IncludeRuleUpdater;
import jetbrains.buildServer.agent.vcs.UpdateByIncludeRules2;
import jetbrains.buildServer.agent.vcs.UpdatePolicy;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.CheckoutRules;
import jetbrains.buildServer.vcs.IncludeRule;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsRoot;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

public class MercurialAgentSideVcsSupport extends AgentVcsSupport implements UpdateByIncludeRules2 {

  private final MirrorManager myMirrorManager;

  public MercurialAgentSideVcsSupport(BuildAgentConfiguration agentConfiguration) {
    myMirrorManager = new MirrorManager(agentConfiguration.getCacheDirectory("mercurial"));
  }

  public IncludeRuleUpdater getUpdater(@NotNull final VcsRoot vcsRoot, @NotNull final CheckoutRules checkoutRules, @NotNull final String toVersion, @NotNull final File checkoutDirectory, @NotNull final AgentRunningBuild build, boolean cleanCheckoutRequested) throws VcsException {
    final BuildProgressLogger logger = build.getBuildLogger();
    final boolean useLocalMirrors = isUseLocalMirrors(build);
    return new IncludeRuleUpdater() {
      public void process(@NotNull final IncludeRule includeRule, @NotNull final File workingDir) throws VcsException {
        checkRuleIsValid(includeRule);
        Settings settings = new Settings(vcsRoot);
        if (useLocalMirrors) {
          updateLocalMirror(vcsRoot, logger);
        }
        updateRepository(workingDir, settings, logger, useLocalMirrors);
        updateWorkingDir(settings, workingDir, toVersion, logger);
      }

      public void dispose() throws VcsException {
      }
    };
  }

  @NotNull
  @Override
  public String getName() {
    return Constants.VCS_NAME;
  }

  @NotNull
  @Override
  public UpdatePolicy getUpdatePolicy() {
    return this;
  }

  private boolean isUseLocalMirrors(AgentRunningBuild build) {
    String value = build.getSharedConfigParameters().get("teamcity.hg.use.local.mirrors");
    return "true".equals(value);
  }

  private void initRepository(File workingDir, Settings settings, BuildProgressLogger logger, boolean useLocalMirrors) throws VcsException {
    try {
      logger.message("Init repository at " + workingDir.getAbsolutePath());
      String defaultPullUrl = getDefaultPullUrl(settings, useLocalMirrors);
      new Init(settings, workingDir, defaultPullUrl).execute();
    } catch (IOException e) {
      throw new VcsException("Error while initializing repository at " + workingDir.getAbsolutePath(), e);
    }
  }

  private void updateRepository(File workingDir, Settings settings, BuildProgressLogger logger, boolean useLocalMirrors) throws VcsException {
    if (!Settings.isValidRepository(workingDir)) {
      initRepository(workingDir, settings, logger, useLocalMirrors);
    } else {
      ensureUseRightRepository(workingDir, settings, logger, useLocalMirrors);
    }
    logger.message("Start pulling changes");
    new PullCommand(settings, workingDir).execute();
    logger.message("Changes successfully pulled");
  }

  private void ensureUseRightRepository(File workingDir, Settings settings, BuildProgressLogger logger, boolean useLocalMirrors) throws VcsException {
    boolean clonedFromWrongRepository = useLocalMirrors && !isClonedFromLocalMirror(settings, workingDir)
                                     || !useLocalMirrors && isClonedFromLocalMirror(settings, workingDir);

    if (clonedFromWrongRepository) {
      String rightRepository = useLocalMirrors ? "local mirror" : "remote repository";
      String wrongRepository = useLocalMirrors ? "remote repository" : "local mirror";
      logger.message("Repository in working directory is cloned from " + wrongRepository + ", clone it from " + rightRepository);
      FileUtil.delete(workingDir);
      initRepository(workingDir, settings, logger, useLocalMirrors);
    }
  }

  private void updateLocalMirror(VcsRoot root, BuildProgressLogger logger) throws VcsException {
    Settings settings = new Settings(root);
    File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl());
    if (!Settings.isValidRepository(mirrorDir)) {
      initRepository(mirrorDir, settings, logger, false);
    }
    logger.message("Start pulling changes to local mirror at " + mirrorDir);
    new PullCommand(settings, mirrorDir).execute();
    logger.message("Local mirror changes successfully pulled");
  }

  private void updateWorkingDir(final Settings settings, File workingDir, final String version, final BuildProgressLogger logger) throws VcsException {
    logger.message("Updating working directory from the local repository copy");
    UpdateCommand uc = new UpdateCommand(settings, workingDir);
    ChangeSet cs = new ChangeSet(version);
    uc.setToId(cs.getId());
    uc.execute();
    logger.message("Working directory updated successfully");
  }

  private String getDefaultPullUrl(Settings settings, boolean useLocalMirror) throws IOException {
    if (useLocalMirror) {
      File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl());
      return mirrorDir.getCanonicalPath();
    } else {
      return settings.getRepositoryUrl();
    }
  }

  private void checkRuleIsValid(IncludeRule includeRule) throws VcsException {
    if (includeRule.getTo() != null && includeRule.getTo().length() > 0) {
      if (!".".equals(includeRule.getFrom()) && includeRule.getFrom().length() != 0) {
        throw new VcsException("Invalid include rule: " + includeRule.toString() + ", Mercurial plugin supports mapping of the form: +:.=>dir only.");
      }
    }
  }

  public boolean isClonedFromLocalMirror(Settings settings, File workingDir) {
    try {
      File mirrorDir = myMirrorManager.getMirrorDir(settings.getRepositoryUrl());
      File hgrc = new File(workingDir, ".hg" + File.separator + "hgrc");
      String config = FileUtil.readText(hgrc);
      return config.contains("default = " + mirrorDir.getCanonicalPath());
    } catch (Exception e) {
      return false;
    }
  }
}