view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java @ 560:a1c15a8cec64

Optimization works only for hg with revsets
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 01 Mar 2013 18:59:41 +0400
parents 2b8299ba321d
children 35098f0c8fda
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.Used;
import jetbrains.buildServer.buildTriggers.vcs.AbstractVcsPropertiesProcessor;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.UnknownRevisionException;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.UnrelatedRepositoryException;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.*;
import jetbrains.buildServer.serverSide.impl.LogUtil;
import jetbrains.buildServer.util.EventDispatcher;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.util.cache.ResetCacheRegister;
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.net.URISyntaxException;
import java.util.*;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil.deleteDir;

/**
 * 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,
        CollectChangesBetweenRoots, CollectChangesBetweenRepositories, BuildPatchByCheckoutRules {
  private final MirrorManager myMirrorManager;
  private final ServerPluginConfig myConfig;
  private final HgPathProvider myHgPathProvider;
  private final RepoFactory myRepoFactory;
  private final HgVcsRootFactory myHgVcsRootFactory;
  private final FileFilter myIgnoreDotHgFilter = new IgnoreDotHgFilter();
  private final FileFilter myAcceptAllFilter = new AcceptAllFilter();
  private final HgTestConnectionSupport myTestConnection;
  private Collection<MercurialServerExtension> myExtensions;

  public MercurialVcsSupport(@NotNull final EventDispatcher<ServerListener> dispatcher,
                             @NotNull final ResetCacheRegister resetCacheHandlerManager,
                             @NotNull final ServerPluginConfig config,
                             @NotNull final HgPathProvider hgPathProvider,
                             @NotNull final RepoFactory repoFactory,
                             @NotNull final MirrorManager mirrorManager,
                             @NotNull final HgVcsRootFactory hgVcsRootFactory,
                             @NotNull final HgTestConnectionSupport testConnection) {
    myConfig = config;
    myMirrorManager = mirrorManager;
    myHgPathProvider = hgPathProvider;
    myRepoFactory = repoFactory;
    myHgVcsRootFactory = hgVcsRootFactory;
    myTestConnection = testConnection;
    resetCacheHandlerManager.registerHandler(new MercurialResetCacheHandler(myMirrorManager));
    dispatcher.addListener(new ServerListenerAdapter() {
      @Override
      public void serverShutdown() {
        myRepoFactory.dispose();
      }
    });
    logUsedHg();
  }

  public void setExtensions(@NotNull Collection<MercurialServerExtension> extensions) {
    myExtensions = extensions;
  }

  private void logUsedHg() {
    String hgPath = myConfig.getHgPath();
    if (hgPath != null)
      Loggers.VCS.info("Use server-wide hg path " + hgPath + ", path in the VCS root settings will be ignored");
    else
      Loggers.VCS.info("Server-wide hg path is not set, will use path from the VCS root settings");
  }

  private List<VcsChange> toVcsChanges(final List<FileStatus> modifiedFiles, String prevVer, String curVer, CheckoutRules rules) {
    List<VcsChange> files = new ArrayList<VcsChange>();
    for (FileStatus 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(FileStatus 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 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);
    HgVcsRoot root = myHgVcsRootFactory.createHgRoot(vcsRoot);
    syncRepository(root, cset);
    HgRepo repo = createRepo(root);
    File parentDir = repo.cat().files(filePath).atRevision(cset).call();
    File file = new File(parentDir, filePath);
    try {
      return FileUtil.loadFileBytes(file);
    } catch (IOException e) {
      throw new VcsException("Failed to load content of file " + filePath + " at revision " + version, e);
    } finally {
      deleteDir(parentDir, Loggers.VCS);
    }
  }

  @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 {
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    syncRepository(hgRoot);
    HgRepo repo = createRepo(hgRoot);
    Map<String, String> result = repo.branches().call();
    String revision = result.get(hgRoot.getBranchName());
    if (revision == null)
      throw new VcsException("Unable to find current version for the branch: " + hgRoot.getBranchName());
    return revision;
  }

  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 myTestConnection;
  }

  @Nullable
  public Map<String, String> getDefaultVcsProperties() {
    Map<String, String> defaults = new HashMap<String, String>();
    defaults.put(Constants.BRANCH_NAME_PROP, "default");
    defaults.put(Constants.HG_COMMAND_PATH_PROP, "hg");
    defaults.put(Constants.UNCOMPRESSED_TRANSFER, "true");
    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(@NotNull final HgVcsRoot root,
                                     @NotNull final ChangeSet fromVer,
                                     @NotNull final ChangeSet toVer,
                                     @NotNull final PatchBuilder builder,
                                     @NotNull final CheckoutRules checkoutRules)
    throws VcsException, IOException {
    HgRepo repo = createRepo(root);
    List<FileStatus> modifiedFiles = repo.status().fromRevision(fromVer).toRevision(toVer).call();
    List<String> notDeletedFiles = new ArrayList<String>();
    for (FileStatus f: modifiedFiles) {
      if (f.getStatus() != Status.REMOVED) {
        notDeletedFiles.add(f.getPath());
      }
    }

    if (notDeletedFiles.isEmpty())
      return;

    File parentDir = repo.cat().files(notDeletedFiles).atRevision(toVer).call();
    try {
      for (FileStatus f: modifiedFiles) {
        String mappedPath = checkoutRules.map(f.getPath());
        if (mappedPath == null) continue; // skip
        final File virtualFile = new File(mappedPath);
        if (f.getStatus() == 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 {
      deleteDir(parentDir, Loggers.VCS);
    }
  }

  // builds patch by exporting files using specified version
  private void buildFullPatch(@NotNull final HgVcsRoot root,
                              @NotNull final ChangeSet toVer,
                              @NotNull final PatchBuilder builder,
                              @NotNull final CheckoutRules checkoutRules) throws IOException, VcsException {
    File tempDir = HgFileUtil.createTempDir();
    try {
      HgRepo repo = createRepo(root);
      if (repo.hasSubreposAtRevision(toVer)) {
        Loggers.VCS.debug("Repository '" + root.getRepository() + "' has submodules at revision " + toVer.getId() + ", use 'hg clone' to build clean patch");
        File mirrorDir = getWorkingDir(root);
        HgRepo cloneOfTheMirror = createRepo(root, tempDir);
        cloneOfTheMirror.doClone().fromRepository(mirrorDir)
                .setUpdateWorkingDir(false)
                .setUsePullProtocol(false)
                .useUncompressedTransfer(false)
                .call();
        cloneOfTheMirror.setDefaultPath(root.getRepository());
        cloneOfTheMirror.update().toRevision(toVer).call();
        buildPatchFromDirectory(builder, tempDir, checkoutRules, myIgnoreDotHgFilter);
      } else {
        Loggers.VCS.debug("Repository '" + root.getRepository() + "' doesn't have submodules at revision " + toVer.getId() + ", use 'hg archive' to build clean patch");
        repo.archive().revision(toVer).toDir(tempDir).call();
        buildPatchFromDirectory(builder, tempDir, checkoutRules, myAcceptAllFilter);
      }
    } finally {
      deleteDir(tempDir, Loggers.VCS);
    }
  }

  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(@NotNull final HgVcsRoot root, @NotNull final ChangeSet cset) throws VcsException {
    File workingDir = getWorkingDir(root);
    lockWorkDir(workingDir);
    HgRepo repo = createRepo(root);
    try {
      if (repo.isValidRepository()) {
        if (repo.containsRevision(cset))
          return;
        try {
          repo.pull().fromRepository(root.getRepository())
                  .withTimeout(myConfig.getPullTimeout())
                  .call();
        } catch (UnrelatedRepositoryException e) {
          Loggers.VCS.warn("Repository at " + root.getRepository() + " is unrelated, clone it again");
          myMirrorManager.forgetDir(workingDir);
          syncRepository(root, cset);
        }
      } else {
        repo.doClone().fromRepository(root.getRepository())
                .useUncompressedTransfer(root.isUncompressedTransfer())
                .setUpdateWorkingDir(false)
                .call();
        repo.setDefaultPath(root.getRepository());
      }
    } finally {
      unlockWorkDir(workingDir);
    }
  }

  public void syncRepository(@NotNull final HgVcsRoot root) throws VcsException {
    File workingDir = getWorkingDir(root);
    lockWorkDir(workingDir);
    HgRepo repo = createRepo(root);
    try {
      if (repo.isValidRepository()) {
        try {
          resetBookmarks(repo);
          repo.pull().fromRepository(root.getRepository())
                  .withTimeout(myConfig.getPullTimeout())
                  .call();
        } catch (UnrelatedRepositoryException e) {
          Loggers.VCS.warn("Repository at " + root.getRepository() + " is unrelated, clone it again");
          myMirrorManager.forgetDir(workingDir);
          syncRepository(root);
        }
      } else {
        repo.doClone().fromRepository(root.getRepository())
                .setUpdateWorkingDir(false)
                .useUncompressedTransfer(root.isUncompressedTransfer())
                .call();
        repo.setDefaultPath(root.getRepository());
      }
    } finally {
      unlockWorkDir(workingDir);
    }
  }


  private void resetBookmarks(HgRepo repo) throws VcsException {
    if (!myConfig.bookmarksEnabled())
      return;
    HgVersion v = repo.version().call();
    if (v.isEqualsOrGreaterThan(BookmarksCommand.REQUIRED_HG_VERSION))
      repo.resetBookmarks();
  }

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

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

  @NotNull
  public RepositoryStateData getCurrentState(@NotNull VcsRoot root) throws VcsException {
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    syncRepository(hgRoot);
    Map<String, String> revisions = new HashMap<String, String>();
    revisions.putAll(getBookmarkRevisions(hgRoot));
    revisions.putAll(getBranchesRevisions(hgRoot));
    String defaultBranchName = hgRoot.getBranchName();
    if (revisions.get(defaultBranchName) == null) {
      throw new VcsException("Cannot find revision of the default branch '" +
              defaultBranchName + "' of vcs root " + LogUtil.describe(root));
    }
    return RepositoryStateData.createVersionState(defaultBranchName, revisions);
  }

  @NotNull
  private Map<String, String> getBranchesRevisions(@NotNull HgVcsRoot root) throws VcsException {
    HgRepo repo = createRepo(root);
    return repo.branches().call();
  }

  @NotNull
  private Map<String, String> getBookmarkRevisions(@NotNull HgVcsRoot root) throws VcsException {
    ServerHgRepo repo = createRepo(root);
    if (!myConfig.bookmarksEnabled())
      return emptyMap();
    HgVersion v = repo.version().call();
    if (!v.isEqualsOrGreaterThan(BookmarksCommand.REQUIRED_HG_VERSION))
      return emptyMap();
    return repo.bookmarks().call();
  }

  @NotNull
  public List<ModificationData> collectChanges(@NotNull VcsRoot fromRoot,
                                               @NotNull RepositoryStateData fromState,
                                               @NotNull VcsRoot toRoot,
                                               @NotNull RepositoryStateData toState,
                                               @NotNull CheckoutRules rules) throws VcsException {
    return collectChanges(toRoot, fromState, toState, rules);
  }

  @NotNull
  public List<ModificationData> collectChanges(@NotNull VcsRoot root,
                                               @NotNull RepositoryStateData fromState,
                                               @NotNull RepositoryStateData toState,
                                               @NotNull CheckoutRules rules) throws VcsException {
    List<ModificationData> changes = new ArrayList<ModificationData>();
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    OperationContext ctx = new OperationContext(this, myRepoFactory, myHgPathProvider, fromState, toState);
    for (Map.Entry<String, String> entry : toState.getBranchRevisions().entrySet()) {
      String branch = entry.getKey();
      String toRevision = entry.getValue();
      String fromRevision = fromState.getBranchRevisions().get(branch);
      if (fromRevision == null)
        fromRevision = fromState.getBranchRevisions().get(fromState.getDefaultBranchName());
      if (toRevision.equals(fromRevision))
        continue;

      Collection<String> fromRevisions = ctx.getFromRevisionsForBranch(hgRoot, fromRevision, toRevision);
      List<ModificationData> branchChanges = collectChanges(ctx, root, fromRevisions, toRevision, rules);
      for (ModificationData change : branchChanges) {
        if (!ctx.isReportedModification(change)) {
          changes.add(change);
          ctx.markAsReported(change);
        }
      }
    }
    changes.addAll(getSubrepoChanges(ctx, root, changes));
    return changes;
  }


  @NotNull
  public List<ModificationData> collectChanges(@NotNull VcsRoot fromRoot, @NotNull String fromRootRevision,
                                               @NotNull VcsRoot toRoot, @Nullable String toRootRevision,
                                               @NotNull CheckoutRules checkoutRules) throws VcsException {
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(toRoot);
    syncRepository(hgRoot);
    String toRevision = toRootRevision != null ? toRootRevision : getCurrentVersion(toRoot);
    String mergeBase = getMergeBase(hgRoot, fromRootRevision, toRevision);
    if (mergeBase == null)
      return Collections.emptyList();
    return collectChanges(toRoot, mergeBase, toRootRevision, checkoutRules);
  }


  @Nullable
  public String getMergeBase(@NotNull HgVcsRoot root, @NotNull String revision1, @NotNull String revision2) throws VcsException {
    String result = createRepo(root).mergeBase()
            .revision1(revision1)
            .revision2(revision2)
            .call();
    if (result == null)
      result = getMinusNthCommit(root, 10);
    return result;
  }


  @Nullable
  private String getMinusNthCommit(@NotNull HgVcsRoot root, int n) throws VcsException {
    LogCommand log = createRepo(root).log()
            .inBranch(root.getBranchName())
            .toNamedRevision(root.getBranchName());
    if (n > 0)
      log.setLimit(n);
    List<ChangeSet> changeSets = log.call();
    if (changeSets.isEmpty())
      return null;
    return changeSets.get(0).getId();
  }


  @NotNull
  public CollectChangesPolicy getCollectChangesPolicy() {
    return this;
  }

  public List<ModificationData> collectChanges(@NotNull VcsRoot root, @NotNull String fromVersion, @Nullable String currentVersion, @NotNull CheckoutRules checkoutRules) throws VcsException {
    if (currentVersion == null)
      return emptyList();
    OperationContext ctx = new OperationContext(this, myRepoFactory, myHgPathProvider, fromVersion, currentVersion);
    List<ModificationData> changes = collectChanges(ctx, root, asList(fromVersion), currentVersion, checkoutRules);
    changes.addAll(getSubrepoChanges(ctx, root, changes));
    return changes;
  }

  private List<ModificationData> collectChanges(@NotNull OperationContext ctx,
                                                @NotNull VcsRoot root,
                                                @NotNull Collection<String> fromVersion,
                                                @Nullable String currentVersion,
                                                @NotNull CheckoutRules checkoutRules) throws VcsException {
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    ctx.syncRepository(hgRoot);
    List<ModificationData> result = new ArrayList<ModificationData>();
    for (ChangeSet cset : getChangesets(ctx, hgRoot, fromVersion, currentVersion)) {
      result.add(createModificationData(ctx, cset, root, checkoutRules));
    }
    return result;
  }


  @NotNull
  private List<ModificationData> getSubrepoChanges(@NotNull OperationContext ctx, @NotNull VcsRoot root, @NotNull List<ModificationData> changes) throws VcsException {
    if (changes.isEmpty())
      return emptyList();

    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    if (!detectSubrepoChanges(hgRoot))
      return emptyList();

    List<SubrepoConfigChange> subrepoConfigChanges = getSubrepoConfigChanges(changes);
    List<ModificationData> subrepoChanges = new ArrayList<ModificationData>();

    for (SubrepoConfigChange configChange : subrepoConfigChanges) {
      VcsRootImpl subrepo = new VcsRootImpl(root.getId(), configChange.getSubrepoRootParams());
      if (ctx.isProcessedSubrepoChanges(subrepo, configChange.getPreviousSubrepoRevisions(), configChange.getCurrentSubrepoRevision()))
        continue;
      List<ModificationData> subChanges = collectChanges(ctx, subrepo, configChange.getPreviousSubrepoRevisions(), configChange.getCurrentSubrepoRevision(), CheckoutRules.DEFAULT);
      for (ModificationData m : subChanges) {
        if (!ctx.isReportedModification(m)) {
          subrepoChanges.add(m);
          ctx.markAsReported(m);
        }
      }
      ctx.markProcessedSubrepoChanges(subrepo, configChange.getPreviousSubrepoRevisions(), configChange.getCurrentSubrepoRevision());
    }

    List<ModificationData> subSubrepoChanges = getSubrepoChanges(ctx, root, subrepoChanges);
    subrepoChanges.addAll(subSubrepoChanges);
    return subrepoChanges;
  }


  private boolean detectSubrepoChanges(@NotNull HgVcsRoot root) {
    return myConfig.detectSubrepoChanges() && root.detectSubrepoChanges();
  }


  private List<SubrepoConfigChange> getSubrepoConfigChanges(@NotNull List<ModificationData> mainRootChanges) {
    List<SubrepoConfigChange> subrepoConfigChanges = new ArrayList<SubrepoConfigChange>();
    for (ModificationData m : mainRootChanges) {
      subrepoConfigChanges.addAll(getSubrepoConfigChanges(m));
    }
    return subrepoConfigChanges;
  }


  private List<SubrepoConfigChange> getSubrepoConfigChanges(@NotNull ModificationData m) {
    List<SubrepoConfigChange> configChanges = new ArrayList<SubrepoConfigChange>();
    for (SubrepoConfigChange configChange : SubrepoConfigChangesAttributes.readSubrepoConfigChanges(m.getAttributes())) {
      if (configChange.getSubrepoRootParams().isEmpty())
        continue;
      if (configChange.getPreviousSubrepoRevisions().isEmpty())
        continue;
      configChanges.add(configChange);
    }
    return configChanges;
  }


  private ModificationData createModificationData(@NotNull OperationContext ctx, @NotNull final ChangeSet cset, @NotNull final VcsRoot root, @NotNull final CheckoutRules checkoutRules) throws VcsException {
    List<ChangeSetRevision> parents = cset.getParents();
    if (parents.isEmpty())
      throw new IllegalStateException("Commit " + cset.getId() + " has no parents");
    List<VcsChange> files = toVcsChanges(cset.getModifiedFiles(), parents.get(0).getId(), cset.getId(), checkoutRules);
    final ModificationData result = new ModificationData(cset.getTimestamp(), files, cset.getDescription(), cset.getUser(), root, cset.getId(), cset.getId());
    for (ChangeSetRevision parent : parents) {
      result.addParentRevision(parent.getId());
    }
    setCanBeIgnored(result, cset);
    result.setAttributes(getAttributes(ctx, root, cset));
    return result;
  }


  private void setCanBeIgnored(@NotNull ModificationData md, @NotNull ChangeSet cset) {
    if (md.getParentRevisions().size() > 1) {
      //don't ignore merge commits
      md.setCanBeIgnored(false);
    } else if (cset.getModifiedFiles().isEmpty()) {
      //don't ignore empty commits
      md.setCanBeIgnored(false);
    }
  }


  @NotNull
  private List<ChangeSet> getChangesets(@NotNull OperationContext ctx, @NotNull final HgVcsRoot root, @NotNull final Collection<String> fromVersions, @Nullable final String toVersion) throws VcsException {
    if (toVersion == null)
      return Collections.emptyList();
    List<String> fromCommits = new ArrayList<String>();
    for (String fromVersion : fromVersions) {
      fromCommits.add(new ChangeSetRevision(fromVersion).getId());
    }
    String toCommit = new ChangeSetRevision(toVersion).getId();
    try {
      ServerHgRepo repo = createRepo(ctx, root);
      List<ChangeSet> csets = repo.collectChanges(root)
              .fromRevision(fromCommits)
              .toRevision(toCommit)
              .call();
      Iterator<ChangeSet> iter = csets.iterator();
      while (iter.hasNext()) {
        ChangeSet cset = iter.next();
        if (fromVersions.contains(cset.getId()))
          iter.remove();
      }
      return csets;
    } catch (UnknownRevisionException e) {
      Loggers.VCS.warn("Revision '" + e.getRevision() + "' is unknown, will return no changes");
      return Collections.emptyList();
    }
  }

  @NotNull
  private Map<String, String> getAttributes(@NotNull OperationContext ctx, @NotNull VcsRoot mainRoot, @NotNull ChangeSet cset) throws VcsException {
    Map<String, String> attributes = new HashMap<String, String>();
    HgVcsRoot root = myHgVcsRootFactory.createHgRoot(mainRoot);
    if (detectSubrepoChanges(root)) {
      try {
        ServerHgRepo repo = createRepo(ctx, root);
        SubrepoConfigChangesAttributes builder = new SubrepoConfigChangesAttributes();
        for (HgSubrepoConfigChange c : repo.getSubrepoConfigChanges(cset)) {
          fillSubrepoConfigChanges(builder, root, c);
        }
        attributes.putAll(builder.buildAttributes());
      } catch (Exception e) {
        Loggers.VCS.warn("Error while reporting subrepo config changes", e);
        if (e instanceof VcsExtension)
          throw (VcsException) e;
        throw new VcsException(e);
      }
    }
    return attributes;
  }

  private void fillSubrepoConfigChanges(@NotNull SubrepoConfigChangesAttributes builder,
                                        @NotNull HgVcsRoot mainRoot,
                                        @NotNull HgSubrepoConfigChange c) throws URISyntaxException, VcsException {
    List<String> prevRevisions = new ArrayList<String>();
    if (!(c.subrepoUrlChanged() || c.subrepoAdded() || c.subrepoRemoved())) {
      String subrepoUrl = c.getCurrent().resolveUrl(mainRoot.getRepository());
      String curRevision = c.getCurrent().revision();
      for (SubRepo prevSubrepo : c.getPrevious()) {
        prevRevisions.add(prevSubrepo.revision());
      }
      builder.addSubrepoConfigChange(new SubrepoConfigChange(mainRoot)
              .setSubrepoPath(c.getPath())
              .setSubrepoRootParamDiff(Constants.REPOSITORY_PROP, subrepoUrl)
              .setCurrentSubrepoRevision(curRevision)
              .setPreviousSubrepoRevisions(prevRevisions));
    }
  }

  @NotNull
  public BuildPatchPolicy getBuildPatchPolicy() {
    return this;
  }

  public void buildPatch(@NotNull VcsRoot root, @Nullable String fromVersion, @NotNull String toVersion, @NotNull PatchBuilder builder, @NotNull CheckoutRules checkoutRules) throws IOException, VcsException {
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    syncRepository(hgRoot);
    ChangeSet to = new ChangeSet(toVersion);
    if (fromVersion == null) {
      buildFullPatch(hgRoot, to, builder, checkoutRules);
    } else {
      ChangeSet from = new ChangeSet(fromVersion);
      HgRepo repo = createRepo(hgRoot);
      if (!repo.containsRevision(from)) {
        Loggers.VCS.info("Cannot find revision " + fromVersion + " in repository " + hgRoot.getRepository() + ", will build a full patch");
        cleanCheckoutDir(builder, checkoutRules);
        buildFullPatch(hgRoot, to, builder, checkoutRules);
      } else {
        buildIncrementalPatch(hgRoot, from, to, builder, checkoutRules);
      }
    }
  }

  private void cleanCheckoutDir(@NotNull PatchBuilder builder, @NotNull CheckoutRules checkoutRules) throws IOException {
    builder.deleteDirectory(new File(checkoutRules.map("")), true);
  }

  private void lockWorkDir(@NotNull File workDir) {
    myMirrorManager.lockDir(workDir);
  }

  private void unlockWorkDir(@NotNull File workDir) {
    myMirrorManager.unlockDir(workDir);
  }

  @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;
  }

  public String label(@NotNull String label, @NotNull String version, @NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules) throws VcsException {
    File tmpDir = null;
    try {
      tmpDir = createLabelingTmpDir();
      HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
      hgRoot.setCustomWorkingDir(tmpDir);
      syncRepository(hgRoot);
      HgRepo repo = createRepo(hgRoot);
      repo.update().branch(hgRoot.getBranchName()).call();

      String fixedTagname = fixTagName(label);
      repo.tag().revision(version)
              .tagName(fixedTagname)
              .byUser(hgRoot.getUserForTag())
              .call();

      repo.push().toRepository(hgRoot.getRepository()).call();
      return fixedTagname;
    } finally {
      deleteDir(tmpDir, Loggers.VCS);
    }
  }

  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', '_');
  }

  File getWorkingDir(HgVcsRoot root) {
    File customDir = root.getCustomWorkingDir();
    return customDir != null ? customDir : myMirrorManager.getMirrorDir(root.getRepository());
  }


  public boolean isAgentSideCheckoutAvailable() {
    return true;
  }


  private File createLabelingTmpDir() throws VcsException {
    try {
      return HgFileUtil.createTempDir();
    } catch (IOException e) {
      throw new VcsException("Unable to create temporary directory");
    }
  }


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


  @Override
  public boolean isDAGBasedVcs() {
    return true;
  }

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

  public ServerHgRepo createRepo(@NotNull HgVcsRoot root) throws VcsException {
    return myRepoFactory.create(getWorkingDir(root), myHgPathProvider.getHgPath(root), root.getAuthSettings());
  }

  public ServerHgRepo createRepo(@NotNull OperationContext ctx, @NotNull HgVcsRoot root) throws VcsException {
    return ctx.createRepo(getWorkingDir(root), myHgPathProvider.getHgPath(root), root.getAuthSettings());
  }


  private HgRepo createRepo(@NotNull HgVcsRoot root, @NotNull File customDir) throws VcsException {
    return myRepoFactory.create(customDir, myHgPathProvider.getHgPath(root), root.getAuthSettings());
  }

  @NotNull
  public String getBranchName(@NotNull final VcsRoot root) {
    try {
      HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
      return hgRoot.getBranchName();
    } catch (VcsException e) {
      return "default";
    }
  }

  @NotNull
  @Override
  public Map<String, String> getCheckoutProperties(@NotNull VcsRoot root) {
    Map<String, String> rootProperties = root.getProperties();
    Map<String, String> repositoryProperties = new HashMap<String, String>();
    repositoryProperties.put(Constants.REPOSITORY_PROP, rootProperties.get(Constants.REPOSITORY_PROP));
    return repositoryProperties;
  }


  @Override
  public ListFilesPolicy getListFilesPolicy() {
    return new ListFilesSupport(this, myHgVcsRootFactory);
  }


  @Override
  @Nullable
  protected <T extends VcsExtension> T getVcsCustomExtension(@NotNull final Class<T> extensionClass) {
    if (myExtensions == null)
      return null;
    for (MercurialServerExtension e : myExtensions) {
      if (extensionClass.isInstance(e))
        return extensionClass.cast(e);
    }
    return null;
  }
}