view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/CommandFactory.java @ 299:e9e7d9fcf57d

Use customized xml output from the 'hg log' command instead of running 'hg status' for every commit
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Thu, 08 Sep 2011 12:56:56 +0400
parents 8c10f5cec37d
children 33305b2022c2
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.LogCommand;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.MergeBaseCommand;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.Settings;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.VersionCommand;
import jetbrains.buildServer.serverSide.ServerPaths;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.vcs.VcsException;
import org.jetbrains.annotations.NotNull;

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

/**
 * @author dmitry.neverov
 */
public final class CommandFactory {

  //hg version which supports revsets
  private final static HgVersion REVSET_HG_VERSION = new HgVersion(1, 7, 0);
  private final static String LOG_TEMPLATE_NAME = "log.template";

  private final File myDefaultWorkingDir;
  private final File myLogTemplate;


  public CommandFactory(@NotNull final ServerPaths paths) throws IOException {
    myDefaultWorkingDir = new File(paths.getCachesDir(), "mercurial");
    myLogTemplate = createLogTemplate(paths.getPluginDataDirectory());
  }


  @NotNull
  public MergeBaseCommand createMergeBase(@NotNull Settings settings, @NotNull File workingDir) throws VcsException {
    VersionCommand versionCommand = new VersionCommand(settings, myDefaultWorkingDir);
    HgVersion hgVersion = versionCommand.execute();
    if (hgVersion.isEqualsOrGreaterThan(REVSET_HG_VERSION))
      return new MergeBaseWithRevsets(settings, workingDir, this);
    else
      return new MergeBaseNoRevsets(settings, workingDir, this);
  }


  @NotNull
  public LogCommand createLog(@NotNull final Settings settings, @NotNull final File workingDir) {
    return new LogCommand(settings, workingDir, myLogTemplate);
  }


  private File createLogTemplate(@NotNull final File templateFileDir) throws IOException {
    File template = new File(templateFileDir, LOG_TEMPLATE_NAME);
    if (!template.exists()) {
      FileUtil.copyResource(CommandFactory.class, "/buildServerResources/log.template", template);
    }
    return template;
  }
}