view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 388:3b799724b82b Eluru-6.5.x

TW-20304 port fix from branch default
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 17 Feb 2012 10:55:58 +0400
parents 55c2c88a2d82
children e182bb6b5921
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.BuildAgent;
import jetbrains.buildServer.Used;
import jetbrains.buildServer.buildTriggers.vcs.AbstractVcsPropertiesProcessor;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.*;
import jetbrains.buildServer.util.EventDispatcher;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.util.filters.Filter;
import jetbrains.buildServer.util.filters.FilterUtil;
import jetbrains.buildServer.vcs.*;
import jetbrains.buildServer.vcs.impl.VcsRootImpl;
import jetbrains.buildServer.vcs.patches.PatchBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.intellij.openapi.util.text.StringUtil.isEmptyOrSpaces;

/**
 * Mercurial VCS plugin for TeamCity works as follows:
 * <ul>
 * <li>clones repository to internal storage
 * <li>before any operation with working copy of repository pulls changes from the original repository
 * <li>executes corresponding hg command
 * </ul>
 *
 * <p>Working copy of repository is created in the $TEAMCITY_DATA_PATH/system/caches/hg_&lt;hash code> folder.
 * <p>Personal builds (remote runs) are not yet supported, they require corresponding functionality from the IDE.
 */
public class MercurialVcsSupport extends ServerVcsSupport implements LabelingSupport, VcsFileContentProvider, BranchSupport {

  private final String LOG_TEMPLATE_NAME = "log.template";
  private ConcurrentMap<String, Lock> myWorkDirLocks= new ConcurrentHashMap<String, Lock>();
  private VcsManager myVcsManager;
  private File myDefaultWorkFolderParent;
  private MirrorManager myMirrorManager;
  private final ServerPluginConfig myConfig;
  private File myLogTemplate;
  private final FileFilter myIgnoreDotHgFilter = new IgnoreDotHgFilter();
  private final FileFilter myAcceptAllFilter = new AcceptAllFilter();

  public MercurialVcsSupport(@NotNull final VcsManager vcsManager,
                             @NotNull final ServerPaths paths,
                             @NotNull final SBuildServer server,
                             @NotNull final EventDispatcher<BuildServerListener> dispatcher,
                             @NotNull final ServerPluginConfig config) throws IOException {
    myLogTemplate = createLogTemplate(paths.getPluginDataDirectory());
    myVcsManager = vcsManager;
    myDefaultWorkFolderParent = new File(paths.getCachesDir(), "mercurial");
    myMirrorManager = new MirrorManager(myDefaultWorkFolderParent);
    myConfig = config;
    dispatcher.addListener(new BuildServerAdapter() {
      @Override
      public void cleanupFinished() {
        super.cleanupFinished();
        server.getExecutor().submit(new Runnable() {
          public void run() {
            removeOldWorkFolders();
          }
        });
      }

      @Override
      public void sourcesVersionReleased(@NotNull final BuildAgent agent) {
        super.sourcesVersionReleased(agent);
        server.getExecutor().submit(new Runnable() {
          public void run() {
            deleteWithLocking(myMirrorManager.getMirrors());
          }
        });
      }
    });
  }

  private void deleteWithLocking(Collection<File> filesForDelete) {
    for (File f : filesForDelete) {
      lockWorkDir(f);
      try {
        FileUtil.delete(f);
      } finally {
        unlockWorkDir(f);
      }
    }
  }

  private Collection<ModifiedFile> computeModifiedFilesForMergeCommit(final Settings settings, final ChangeSet cur) throws VcsException {
    File workingDir = getWorkingDir(settings);
    ChangedFilesCommand cfc = new ChangedFilesCommand(settings, workingDir);
    cfc.setRevId(cur.getId());
    return cfc.execute();
  }

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

  private List<VcsChange> toVcsChanges(final List<ModifiedFile> modifiedFiles, String prevVer, String curVer, CheckoutRules rules) {
    List<VcsChange> files = new ArrayList<VcsChange>();
    for (ModifiedFile mf: modifiedFiles) {
      final String path = rules.map(mf.getPath());
      if (shouldInclude(path))
        files.add(toVcsChange(mf, prevVer, curVer, path));
    }
    return files;
  }

  private boolean shouldInclude(String path) {
    return path != null;
  }

  private VcsChange toVcsChange(ModifiedFile mf, String prevVer, String curVer, String mappedPath) {
    String normalizedPath = PathUtil.normalizeSeparator(mf.getPath());
    VcsChangeInfo.Type changeType = getChangeType(mf.getStatus());
    if (changeType == null) {
      Loggers.VCS.warn("Unable to convert status: " + mf.getStatus() + " to VCS change type");
      changeType = VcsChangeInfo.Type.NOT_CHANGED;
    }
    return new VcsChange(changeType, mf.getStatus().getName(), normalizedPath, mappedPath, prevVer, curVer);
  }

  private VcsChangeInfo.Type getChangeType(final ModifiedFile.Status status) {
    switch (status) {
      case ADDED:return VcsChangeInfo.Type.ADDED;
      case MODIFIED:return VcsChangeInfo.Type.CHANGED;
      case REMOVED:return VcsChangeInfo.Type.REMOVED;
    }
    return null;
  }

  @NotNull
  public byte[] getContent(@NotNull final VcsModification vcsModification,
                           @NotNull final VcsChangeInfo change,
                           @NotNull final VcsChangeInfo.ContentType contentType,
                           @NotNull final VcsRoot vcsRoot) throws VcsException {
    String version = contentType == VcsChangeInfo.ContentType.AFTER_CHANGE ? change.getAfterChangeRevisionNumber() : change.getBeforeChangeRevisionNumber();
    return getContent(change.getRelativeFileName(), vcsRoot, version);
  }

  @NotNull
  public byte[] getContent(@NotNull final String filePath, @NotNull final VcsRoot vcsRoot, @NotNull final String version) throws VcsException {
    ChangeSet cset = new ChangeSet(version);
    Settings settings = createSettings(vcsRoot);
    syncRepository(settings, cset);
    File workingDir = getWorkingDir(settings);
    CatCommand cc = new CatCommand(settings, workingDir);
    cc.setRevId(cset.getId());
    File parentDir = cc.execute(Collections.singletonList(filePath));
    try {
      File file = new File(parentDir, filePath);
      if (file.isFile()) {
        try {
          return FileUtil.loadFileBytes(file);
        } catch (IOException e) {
          throw new VcsException("Failed to load content of file: " + file.getAbsolutePath(), e);
        }
      } else {
        Loggers.VCS.warn("Unable to obtain content of the file: " + filePath);
      }
    } finally {
      deleteTmpDir(parentDir);
    }
    return new byte[0];
  }

  /**
   * Returns file's content or empty string if it doesn't exist.
   * @param path path of the file of interest
   * @param settings root settings
   * @param cset repository cset (should be present in the repository)
   * @return see above
   */
  @NotNull
  private String getFileContent(@NotNull final String path, @NotNull final Settings settings, @NotNull final ChangeSet cset) throws VcsException {
    File dir = getWorkingDir(settings);
    CatCommand cat = new CatCommand(settings, dir);
    cat.setRevId(cset.getId());
    cat.checkForFailure(false);
    File parentDir = null;
    try {
      parentDir = cat.execute(Collections.singletonList(path));
      File f = new File(parentDir, path);
      if (f.isFile())
        return FileUtil.readText(f);
      else
        return "";
    } catch (Exception e) {
      return "";
    } finally {
      if (parentDir != null)
        deleteTmpDir(parentDir);
    }
  }

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

  @NotNull
  @Used("jsp")
  public String getDisplayName() {
    return "Mercurial";
  }

  @Nullable
  public PropertiesProcessor getVcsPropertiesProcessor() {
    return new AbstractVcsPropertiesProcessor() {
      public Collection<InvalidProperty> process(final Map<String, String> properties) {
        List<InvalidProperty> result = new ArrayList<InvalidProperty>();
        if (isEmpty(properties.get(Constants.HG_COMMAND_PATH_PROP))) {
          result.add(new InvalidProperty(Constants.HG_COMMAND_PATH_PROP, "Path to 'hg' command must be specified"));
        }
        if (isEmpty(properties.get(Constants.REPOSITORY_PROP))) {
          result.add(new InvalidProperty(Constants.REPOSITORY_PROP, "Repository must be specified"));
        }
        return result;
      }
    };
  }

  @NotNull
  public String getVcsSettingsJspFilePath() {
    return "mercurialSettings.jsp";
  }

  @NotNull
  public String getCurrentVersion(@NotNull final VcsRoot root) throws VcsException {
    Settings settings = createSettings(root);
    syncRepository(settings);
    File workingDir = getWorkingDir(settings);
    BranchesCommand branches = new BranchesCommand(settings, workingDir);
    Map<String, ChangeSet> result = branches.execute();
    if (!result.containsKey(settings.getBranchName())) {
      throw new VcsException("Unable to find current version for the branch: " + settings.getBranchName());
    }

    return result.get(settings.getBranchName()).getFullVersion();
  }

  public boolean sourcesUpdatePossibleIfChangesNotFound(@NotNull final VcsRoot root) {
    return false;
  }

  @NotNull
  public String describeVcsRoot(final VcsRoot vcsRoot) {
    return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP);
  }

  @Override
  public TestConnectionSupport getTestConnectionSupport() {
    return new TestConnectionSupport() {
      public String testConnection(@NotNull final VcsRoot vcsRoot) throws VcsException {
        Settings settings = createSettings(vcsRoot);
        File workingDir = getWorkingDir(settings);
        IdentifyCommand id = new IdentifyCommand(settings, workingDir);
        StringBuilder res = new StringBuilder();
        res.append(quoteIfNeeded(settings.getHgCommandPath()));
        res.append(" identify ");
        final String obfuscatedUrl = CommandUtil.removePrivateData(settings.getRepositoryUrl(), Collections.singleton(settings.getPassword()));
        res.append(quoteIfNeeded(obfuscatedUrl));
        res.append('\n').append(id.execute());
        return res.toString();
      }
    };
  }

  private String quoteIfNeeded(@NotNull String str) {
    if (str.indexOf(' ') != -1) {
      return "\"" + str + "\"";
    }

    return str;
  }

  @Nullable
  public Map<String, String> getDefaultVcsProperties() {
    Map<String, String> defaults = new HashMap<String, String>();
    defaults.put(Constants.HG_COMMAND_PATH_PROP, "hg");
    defaults.put(Constants.UNCOMPRESSED_TRANSFER, "false");
    return defaults;
  }

  public String getVersionDisplayName(@NotNull final String version, @NotNull final VcsRoot root) throws VcsException {
    return new ChangeSet(version).getId();
  }

  @NotNull
  public Comparator<String> getVersionComparator() {
    // comparator is called when TeamCity needs to sort modifications in the order of their appearance,
    // currently we sort changes by revision number, not sure however that this is a good idea,
    // probably it would be better to sort them by timestamp (and to add timestamp into the version).
    return new Comparator<String>() {
      public int compare(final String o1, final String o2) {
        try {
          return new ChangeSet(o1).getRevNumber() - new ChangeSet(o2).getRevNumber();
        } catch (Exception e) {
          return 1;
        }
      }
    };
  }

  // builds patch from version to version
  private void buildIncrementalPatch(final Settings settings, @NotNull final ChangeSet fromVer, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules)
    throws VcsException, IOException {
    File workingDir = getWorkingDir(settings);
    StatusCommand st = new StatusCommand(settings, workingDir);
    st.setFromRevId(fromVer.getId());
    st.setToRevId(toVer.getId());
    List<ModifiedFile> modifiedFiles = st.execute();
    List<String> notDeletedFiles = new ArrayList<String>();
    for (ModifiedFile f: modifiedFiles) {
      if (f.getStatus() != ModifiedFile.Status.REMOVED) {
        notDeletedFiles.add(f.getPath());
      }
    }

    if (notDeletedFiles.isEmpty()) return;

    CatCommand cc = new CatCommand(settings, workingDir);
    cc.setRevId(toVer.getId());
    File parentDir = cc.execute(notDeletedFiles);

    try {
      for (ModifiedFile f: modifiedFiles) {
        String mappedPath = checkoutRules.map(f.getPath());
        if (mappedPath == null) continue; // skip
        final File virtualFile = new File(mappedPath);
        if (f.getStatus() == ModifiedFile.Status.REMOVED) {
          builder.deleteFile(virtualFile, true);
        } else {
          File realFile = new File(parentDir, f.getPath());
          FileInputStream is = new FileInputStream(realFile);
          try {
            builder.changeOrCreateBinaryFile(virtualFile, null, is, realFile.length());
          } finally {
            is.close();
          }
        }
      }
    } finally {
      deleteTmpDir(parentDir);
    }
  }

  private void deleteTmpDir(File parentDir) {
    boolean dirDeleted = FileUtil.delete(parentDir);
    if (!dirDeleted) {
      Loggers.VCS.warn("Can not delete directory \"" + parentDir.getAbsolutePath() + "\"");
    }
  }

  // builds patch by exporting files using specified version
  private void buildFullPatch(final Settings settings, @NotNull final ChangeSet toVer, final PatchBuilder builder, final CheckoutRules checkoutRules)
    throws IOException, VcsException {
    File tempDir = FileUtil.createTempDirectory("mercurial", toVer.getId());
    try {
      File mirrorDir = getWorkingDir(settings);
      if (hasSubrepositories(settings, toVer)) {
        Loggers.VCS.debug("Repository '" + settings.getRepository() + "' has submodules at revision " + toVer.getId() + ", use 'hg clone' to build clean patch");
        CloneCommand cl = new CloneCommand(settings, tempDir);
        cl.setRepository(mirrorDir.getAbsolutePath());
        cl.setToId(toVer.getId());
        cl.setUpdateWorkingDir(false);
        cl.setUsePullProtocol(myConfig.isUsePullProtocol());
        cl.execute();

        UpdateCommand up = new UpdateCommand(settings, tempDir);
        up.setToId(toVer.getId());
        up.execute();

        buildPatchFromDirectory(builder, tempDir, checkoutRules, myIgnoreDotHgFilter);
      } else {
        Loggers.VCS.debug("Repository '" + settings.getRepository() + "' doesn't have submodules at revision " + toVer.getId() + ", use 'hg archive' to build clean patch");
        ArchiveCommand archive = new ArchiveCommand(settings, mirrorDir);
        archive.setDestDir(tempDir);
        archive.setToId(toVer.getId());
        archive.execute();
        buildPatchFromDirectory(builder, tempDir, checkoutRules, myAcceptAllFilter);
      }
    } finally {
      FileUtil.delete(tempDir);
    }
  }

  private boolean hasSubrepositories(@NotNull final Settings settings, @NotNull final ChangeSet cset) throws VcsException {
    String hgsub = getFileContent(".hgsub", settings, cset);
    return !isEmptyOrSpaces(hgsub);
  }

  private void buildPatchFromDirectory(final PatchBuilder builder, final File repRoot, final CheckoutRules checkoutRules, @NotNull final FileFilter filter) throws IOException {
    buildPatchFromDirectory(repRoot, builder, repRoot, checkoutRules, filter);
  }

  private void buildPatchFromDirectory(File curDir, final PatchBuilder builder, final File repRoot, final CheckoutRules checkoutRules, @NotNull final FileFilter filter) throws IOException {
    File[] files = curDir.listFiles(filter);
    if (files != null) {
      for (File realFile: files) {
        String relPath = realFile.getAbsolutePath().substring(repRoot.getAbsolutePath().length());
        String mappedPath = checkoutRules.map(relPath);
        if (mappedPath != null && mappedPath.length() > 0) {
          final File virtualFile = new File(mappedPath);
          if (realFile.isDirectory()) {
            builder.createDirectory(virtualFile);
            buildPatchFromDirectory(realFile, builder, repRoot, checkoutRules, filter);
          } else {
            final FileInputStream is = new FileInputStream(realFile);
            try {
              builder.createBinaryFile(virtualFile, null, is, realFile.length());
            } finally {
              is.close();
            }
          }
        } else {
          if (realFile.isDirectory()) {
            buildPatchFromDirectory(realFile, builder, repRoot, checkoutRules, filter);
          }
        }
      }
    }
  }

  /**
   * clone the repo if it doesn't exist, pull the repo if it doesn't contain specified changeSet
   */
  private void syncRepository(final Settings settings, final ChangeSet cset) throws VcsException {
    File workingDir = getWorkingDir(settings);
    lockWorkDir(workingDir);
    try {
      if (Settings.isValidRepository(workingDir)) {
        if (!isChangeSetExist(settings, workingDir, cset)) {
          PullCommand pull = new PullCommand(settings, workingDir);
          pull.execute(myConfig.getPullTimeout());
        }
      } else {
        CloneCommand cl = new CloneCommand(settings, workingDir);
        cl.setUpdateWorkingDir(false);
        cl.execute();
      }
    } finally {
      unlockWorkDir(workingDir);
    }
  }

  private void syncRepository(final Settings settings) throws VcsException {
    File workingDir = getWorkingDir(settings);
    lockWorkDir(workingDir);
    try {
      if (Settings.isValidRepository(workingDir)) {
        PullCommand pull = new PullCommand(settings, workingDir);
        pull.execute(myConfig.getPullTimeout());
      } else {
        CloneCommand cl = new CloneCommand(settings, workingDir);
        cl.setUpdateWorkingDir(false);
        cl.execute();
      }
    } finally {
      unlockWorkDir(workingDir);
    }
  }

  /**
   * Check if changeSet is present in local repository.
   * @param settings root settings
   * @param cset change set of interest
   * @return true if changeSet is present in local repository
   */
  private boolean isChangeSetExist(Settings settings, File workDir, ChangeSet cset) {
    try {
      IdentifyCommand identify = new IdentifyCommand(settings, workDir);
      identify.setInLocalRepository(true);
      identify.setChangeSet(cset);
      identify.execute();
      return true;
    } catch (VcsException e) {
      return false;
    }
  }

  @Override
  public LabelingSupport getLabelingSupport() {
    return this;
  }

  @NotNull
  public VcsFileContentProvider getContentProvider() {
    return this;
  }

  @NotNull
  public String getRemoteRunOnBranchPattern() {
    return "remote-run/*";
  }

  @NotNull
  public Map<String, String> getBranchesRevisions(@NotNull VcsRoot root) throws VcsException {
    Settings settings = createSettings(root);
    syncRepository(settings);
    File workingDir = getWorkingDir(settings);
    BranchesCommand branches = new BranchesCommand(settings, workingDir);
    Map<String, String> result = new HashMap<String, String>();
    for (Map.Entry<String, ChangeSet> entry : branches.execute().entrySet()) {
      result.put(entry.getKey(), entry.getValue().getId());
    }
    return result;
  }

  @NotNull
  public Map<String, String> getBranchRootOptions(@NotNull VcsRoot root, @NotNull String branchName) {
    final Map<String, String> options = new HashMap<String, String>(root.getProperties());
    options.put(Constants.BRANCH_NAME_PROP, branchName);
    return options;
  }


  @Nullable
  public PersonalBranchDescription getPersonalBranchDescription(@NotNull VcsRoot root, @NotNull String branchName) throws VcsException {
    Settings settings = createSettings(root);
    VcsRoot branchRoot = createBranchRoot(root, branchName);
    String baseVersion = getCurrentVersion(root);
    String branchVersion = getCurrentVersion(branchRoot);
    String branchPoint = getBranchPoint(settings, baseVersion, branchVersion);

    LogCommand lc = new LogCommand(settings, getWorkingDir(settings), myLogTemplate);
    lc.setFromRevId(new ChangeSetRevision(branchPoint).getId());
    lc.setToRevId(new ChangeSetRevision(branchVersion).getId());
    lc.setBranchName(null);//do not limit output to particular branch, return all commits
    List<ChangeSet> changeSets = lc.execute();
    if (changeSets.size() > 1) {//when branch points to the commit in original branch we get 1 cset
      String branchId = changeSets.get(1).getId();
      String username = changeSets.get(changeSets.size() - 1).getUser();
      return new PersonalBranchDescription(branchId, username);
    } else {
      return null;
    }
  }


  private VcsRoot createBranchRoot(VcsRoot original, String branchName) {
    VcsRootImpl result = new VcsRootImpl(original.getId(), original.getProperties());
    result.addProperty(Constants.BRANCH_NAME_PROP, branchName);
    return result;
  }

  @NotNull
  public List<ModificationData> collectChanges(@NotNull VcsRoot fromRoot, @NotNull String fromRootRevision,
                                               @NotNull VcsRoot toRoot, @Nullable String toRootRevision,
                                               @NotNull CheckoutRules checkoutRules) throws VcsException {
    //we get all branches while clone, if vcs roots are related it is doesn't matter in which one search for branch point
    Settings settings = createSettings(fromRoot);
    syncRepository(settings);
    String branchPoint = getBranchPoint(settings, fromRootRevision, toRootRevision);
    return ((CollectChangesByCheckoutRules) getCollectChangesPolicy()).collectChanges(toRoot, branchPoint, toRootRevision, checkoutRules);
  }


  private String getBranchPoint(@NotNull Settings settings, String branchOneRev, String branchTwoRev) throws VcsException {
    if (branchOneRev.equals(branchTwoRev))
      return branchOneRev;
    File workingDir = getWorkingDir(settings);
    LogCommand lc = new LogCommand(settings, workingDir, myLogTemplate);
    lc.setFromRevId(new ChangeSetRevision(branchOneRev).getId());
    lc.setToRevId(new ChangeSetRevision(branchTwoRev).getId());
    lc.setLimit(1);
    List<ChangeSet> changeSets = lc.execute();
    ChangeSet cs = changeSets.get(0);
    if (cs.isInitial()) {
      return cs.getId();
    } else {
      return cs.getParents().get(0).getId();
    }
  }

  @NotNull
  public CollectChangesPolicy getCollectChangesPolicy() {
    return new CollectChangesByCheckoutRules() {
      @NotNull
      public List<ModificationData> collectChanges(@NotNull VcsRoot root, @NotNull String fromVersion, @Nullable String currentVersion, @NotNull CheckoutRules checkoutRules) throws VcsException {
        Settings settings = createSettings(root);
        syncRepository(settings);

        // first obtain changes between specified versions
        List<ModificationData> result = new ArrayList<ModificationData>();
        if (currentVersion == null)
          return result;

        File workingDir = getWorkingDir(settings);
        LogCommand lc = new LogCommand(settings, workingDir, myLogTemplate);
        String fromId = new ChangeSetRevision(fromVersion).getId();
        lc.setFromRevId(fromId);
        lc.setToRevId(new ChangeSetRevision(currentVersion).getId());
        List<ChangeSet> changeSets = lc.execute();
        if (changeSets.isEmpty()) {
          return result;
        }

        ChangeSet prev = new ChangeSet(fromVersion);
        for (ChangeSet cur : changeSets) {
          if (cur.getId().equals(fromId))
            continue; // skip already reported changeset

          boolean merge = cur.getParents().size() > 1;
          List<ModifiedFile> modifiedFiles = cur.getModifiedFiles();
          List<VcsChange> files = toVcsChanges(modifiedFiles, prev.getFullVersion(), cur.getFullVersion(), checkoutRules);
          if (files.isEmpty() && !merge)
            continue;
          ModificationData md = new ModificationData(cur.getTimestamp(), files, cur.getDescription(), cur.getUser(), root, cur.getFullVersion(), cur.getId());
          if (merge)
            md.setCanBeIgnored(false);
          result.add(md);
          prev = cur;
        }

        return result;
      }
    };
  }

  @NotNull
  public BuildPatchPolicy getBuildPatchPolicy() {
    return new BuildPatchByCheckoutRules() {
      public void buildPatch(@NotNull final VcsRoot root,
                             @Nullable final String fromVersion,
                             @NotNull final String toVersion,
                             @NotNull final PatchBuilder builder,
                             @NotNull final CheckoutRules checkoutRules) throws IOException, VcsException {
        Settings settings = createSettings(root);
        syncRepository(settings);
        if (fromVersion == null) {
          buildFullPatch(settings, new ChangeSet(toVersion), builder, checkoutRules);
        } else {
          buildIncrementalPatch(settings, new ChangeSet(fromVersion), new ChangeSet(toVersion), builder, checkoutRules);
        }
      }
    };
  }

  private void lockWorkDir(@NotNull File workDir) {
    getWorkDirLock(workDir).lock();
  }

  private void unlockWorkDir(@NotNull File workDir) {
    getWorkDirLock(workDir).unlock();
  }

  @Override
  public boolean allowSourceCaching() {
    // since a copy of repository for each VCS root is already stored on disk
    // we do not need separate cache for our patches
    return false;
  }

  private Lock getWorkDirLock(final File workDir) {
    String path = workDir.getAbsolutePath();
    Lock lock = myWorkDirLocks.get(path);
    if (lock == null) {
      lock = new ReentrantLock();
      Lock curLock = myWorkDirLocks.putIfAbsent(path, lock);
      if (curLock != null) {
        lock = curLock;
      }
    }
    return lock;
  }

  private void removeOldWorkFolders() {
    Set<File> workDirs = new HashSet<File>(myMirrorManager.getMirrors());

    for (VcsRoot vcsRoot: getMercurialVcsRoots()) {
      try {
        Settings s = createSettings(vcsRoot);
        File workingDir = getWorkingDir(s);
        workDirs.remove(PathUtil.getCanonicalFile(workingDir));
      } catch (VcsException e) {
        Loggers.VCS.error(e);
      }
    }

    deleteWithLocking(workDirs);
  }

  private Collection<VcsRoot> getMercurialVcsRoots() {
    List<VcsRoot> res = new ArrayList<VcsRoot>(myVcsManager.getAllRegisteredVcsRoots());
    FilterUtil.filterCollection(res, new Filter<VcsRoot>() {
      public boolean accept(@NotNull final VcsRoot data) {
        return getName().equals(data.getVcsName());
      }
    });
    return res;
  }

  public String label(@NotNull String label, @NotNull String version, @NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules) throws VcsException {
    File tmpDir = null;
    try {
      tmpDir = createLabelingTmpDir();
      final Settings settings = createSettings(root);
      settings.setCustomWorkingDir(tmpDir);
      syncRepository(settings);
      File workingDir = getWorkingDir(settings);
      new UpdateCommand(settings, workingDir).execute();

      String fixedTagname = fixTagName(label);
      TagCommand tc = new TagCommand(settings, workingDir);
      tc.setRevId(new ChangeSet(version).getId());
      tc.setTag(fixedTagname);
      tc.execute();

      PushCommand pc = new PushCommand(settings, workingDir);
      pc.execute();
      return fixedTagname;
    } finally {
      if (tmpDir != null)
        FileUtil.delete(tmpDir);
    }
  }

  private String fixTagName(final String label) {
    // according to Mercurial documentation http://hgbook.red-bean.com/hgbookch8.html#x12-1570008
    // tag name must not contain:
    // Colon (ASCII 58, ':')
    // Carriage return (ASCII 13, '\r')
    // Newline (ASCII 10, '\n')
    // all these characters will be replaced with _ (underscore)
    return label.replace(':', '_').replace('\r', '_').replace('\n', '_');
  }

  private File getWorkingDir(Settings s) {
    File customDir = s.getCustomWorkingDir();
    return customDir != null ? customDir : myMirrorManager.getMirrorDir(s.getRepositoryUrl());
  }

  private Settings createSettings(final VcsRoot root) throws VcsException {
    Settings settings = new Settings(root);
    String customClonePath = settings.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 = settings.getRepositoryUrl();
      String[] splitted = repPath.split("[/\\\\]");
      if (splitted.length > 0) {
        repPath = splitted[splitted.length-1];
      }

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

  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());
    }
  }

  public boolean isAgentSideCheckoutAvailable() {
    return true;
  }


  private File createLabelingTmpDir() throws VcsException {
    try {
      return FileUtil.createTempDirectory("mercurial", "label");
    } catch (IOException e) {
      throw new VcsException("Unable to create temporary directory");
    }
  }


  /* for tests only */
  public MirrorManager getMirrorManager() {
    return myMirrorManager;
  }


  private static class IgnoreDotHgFilter implements FileFilter {
    public boolean accept(final File file) {
      return !(file.isDirectory() && ".hg".equals(file.getName()));
    }
  }

  private static class AcceptAllFilter implements FileFilter {
    public boolean accept(File pathname) {
      return true;
    }
  }
}