view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactory.java @ 430:3600b68a4c0c

TW-21403 better error message when hg not found
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 11 May 2012 19:33:48 +0400
parents
children 81fa236998d4
line wrap: on
line source
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsRoot;
import org.jetbrains.annotations.NotNull;

import java.io.File;

/**
 * @author dmitry.neverov
 */
public class HgVcsRootFactory {

  private File myDefaultWorkFolderParent;

  public HgVcsRootFactory(@NotNull final ServerPluginConfig config) {
    myDefaultWorkFolderParent = config.getCachesDir();
  }


  public HgVcsRoot createHgRoot(@NotNull VcsRoot root) throws VcsException {
    HgVcsRoot hgRoot = new HgVcsRoot(root);
    String customClonePath = hgRoot.getCustomClonePath();
    if (!StringUtil.isEmptyOrSpaces(customClonePath) && !myDefaultWorkFolderParent.equals(new File(customClonePath).getAbsoluteFile())) {
      File parentDir = new File(customClonePath);
      createClonedRepositoryParentDir(parentDir);

      // take last part of repository path
      String repPath = hgRoot.getRepositoryUrlWithCredentials();
      String[] splitted = repPath.split("[/\\\\]");
      if (splitted.length > 0) {
        repPath = splitted[splitted.length-1];
      }

      File customWorkingDir = new File(parentDir, repPath);
      hgRoot.setCustomWorkingDir(customWorkingDir);
    }
    return hgRoot;
  }

  private void createClonedRepositoryParentDir(final File parentDir) throws VcsException {
    if (!parentDir.exists() && !parentDir.mkdirs())
      throw new VcsException("Failed to create parent directory for cloned repository: " + parentDir.getAbsolutePath());
  }

}