view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/CommandFactoryImpl.java @ 380:3bc8125671b8

Merge branch Eluru-6.5.x
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Wed, 15 Feb 2012 13:23:53 +0400
parents e0464f11206c
children
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.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 CommandFactoryImpl implements 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 CommandFactoryImpl(@NotNull final ServerPluginConfig config) throws IOException {
    myDefaultWorkingDir = config.getCachesDir();
    myLogTemplate = createLogTemplate(config.getPluginDataDir());
  }


  @NotNull
  public MergeBaseCommand createMergeBase(@NotNull Settings settings, @NotNull File workingDir) throws VcsException {
    HgVersion hgVersion = getHgVersion(settings);
    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);
  }

  @NotNull
  public CollectChangesCommand getCollectChangesCommand(@NotNull final Settings settings, @NotNull final File workingDir) throws VcsException {
    HgVersion hgVersion = getHgVersion(settings);
    if (hgVersion.isEqualsOrGreaterThan(REVSET_HG_VERSION)) {
      return new CollectChangesWithRevsets(settings, workingDir, myLogTemplate);
    } else {
      return new CollectChangesNoRevsets(settings, workingDir, myLogTemplate);
    }
  }

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

  private HgVersion getHgVersion(@NotNull final Settings settings) throws VcsException {
    VersionCommand versionCommand = new VersionCommand(settings, myDefaultWorkingDir);
    return versionCommand.execute();
  }
}