view mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialAgentSideVcsSupport.java @ 188:5013242d3af7 Eluru-6.0.x

Do clone directly to working dir instead of tmp dir
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Wed, 16 Mar 2011 10:17:53 +0300
parents bcc04a74783d
children ebcca5974cb4
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.BuildProgressLogger;
import jetbrains.buildServer.agent.vcs.AgentVcsSupport;
import jetbrains.buildServer.agent.vcs.IncludeRuleUpdater;
import jetbrains.buildServer.agent.vcs.UpdateByIncludeRules;
import jetbrains.buildServer.agent.vcs.UpdatePolicy;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
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;

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

  private void doClone(final Settings settings, final File workingDir) throws VcsException {
    CloneCommand cc = new CloneCommand(settings);
    cc.setDestDir(workingDir.getAbsolutePath());
    cc.setUpdateWorkingDir(false);
    cc.execute();
  }

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

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

  public IncludeRuleUpdater getUpdater(@NotNull final VcsRoot vcsRoot, @NotNull final CheckoutRules checkoutRules, @NotNull final String toVersion, @NotNull final File checkoutDirectory, @NotNull final BuildProgressLogger logger) throws VcsException {
    return new IncludeRuleUpdater() {
      public void process(@NotNull final IncludeRule includeRule, @NotNull final File workingDir) 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.");
          }
        }

        Settings settings = new Settings(workingDir, vcsRoot);
        settings.setWorkingDir(workingDir);
        if (settings.hasCopyOfRepository()) {
          logger.message("Repository in working directory found, start pulling changes");
          PullCommand pc = new PullCommand(settings);
          pc.execute();
          logger.message("Changes successfully pulled");
        } else {
          logger.message("No repository in working directory found, start cloning repository to temporary folder");
          doClone(settings, workingDir);
          logger.message("Repository successfully cloned to: " + workingDir.getAbsolutePath());
        }
        updateWorkingDir(settings, toVersion, logger);
      }

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