changeset 489:493f78cb64dd

Move MercurialBranchSupport to separate module
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Wed, 03 Oct 2012 18:08:22 +0400
parents 60097d9c9606
children f77000a89e97
files .idea/modules.xml mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java mercurial-server-tc/mercurial-server-tc.iml mercurial-server-tc/src/META-INF/build-server-plugin-mercurial-tc.xml mercurial-server-tc/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialBranchSupport.java mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialServerExtension.java mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java mercurial-tests/mercurial-tests.iml mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialSupportBuilder.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/Util.java
diffstat 11 files changed, 223 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/.idea/modules.xml	Wed Oct 03 16:12:28 2012 +0400
+++ b/.idea/modules.xml	Wed Oct 03 18:08:22 2012 +0400
@@ -6,6 +6,7 @@
       <module fileurl="file://$PROJECT_DIR$/mercurial-agent/mercurial-agent.iml" filepath="$PROJECT_DIR$/mercurial-agent/mercurial-agent.iml" />
       <module fileurl="file://$PROJECT_DIR$/mercurial-common/mercurial-common.iml" filepath="$PROJECT_DIR$/mercurial-common/mercurial-common.iml" />
       <module fileurl="file://$PROJECT_DIR$/mercurial-server/mercurial-server.iml" filepath="$PROJECT_DIR$/mercurial-server/mercurial-server.iml" />
+      <module fileurl="file://$PROJECT_DIR$/mercurial-server-tc/mercurial-server-tc.iml" filepath="$PROJECT_DIR$/mercurial-server-tc/mercurial-server-tc.iml" />
       <module fileurl="file://$PROJECT_DIR$/mercurial-tests/mercurial-tests.iml" filepath="$PROJECT_DIR$/mercurial-tests/mercurial-tests.iml" />
     </modules>
   </component>
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Wed Oct 03 16:12:28 2012 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java	Wed Oct 03 18:08:22 2012 +0400
@@ -41,7 +41,7 @@
                                    @NotNull final String command,
                                    @NotNull final Set<String> privateData) {
     final long start = System.currentTimeMillis();
-    ExecResult res = SimpleCommandLineProcessRunner.runCommand(cli, null, new SimpleCommandLineProcessRunner.RunCommandEventsAdapter() {
+    ExecResult res = SimpleCommandLineProcessRunner.runCommand(cli, null, new SimpleCommandLineProcessRunner.ProcessRunCallbackAdapter() {
       @Override
       public Integer getOutputIdleSecondsTimeout() {
         return executionTimeout;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-server-tc/mercurial-server-tc.iml	Wed Oct 03 18:08:22 2012 +0400
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="false">
+    <output url="file://$MODULE_DIR$/classes" />
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="library" name="TeamCity Vcs Api" level="project" />
+    <orderEntry type="library" name="TeamCityAPI-server" level="project" />
+    <orderEntry type="module" module-name="mercurial-server" />
+    <orderEntry type="library" name="TeamCityAPI-common" level="project" />
+    <orderEntry type="module" module-name="mercurial-common" />
+  </component>
+</module>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-server-tc/src/META-INF/build-server-plugin-mercurial-tc.xml	Wed Oct 03 18:08:22 2012 +0400
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans default-autowire="constructor">
+    <bean class="jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialBranchSupport"/>
+</beans>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-server-tc/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialBranchSupport.java	Wed Oct 03 18:08:22 2012 +0400
@@ -0,0 +1,78 @@
+package jetbrains.buildServer.buildTriggers.vcs.mercurial;
+
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.ChangeSet;
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
+import jetbrains.buildServer.vcs.*;
+import jetbrains.buildServer.vcs.impl.VcsRootImpl;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class MercurialBranchSupport implements BranchSupport, MercurialServerExtension {
+
+  private HgVcsRootFactory myHgRootFactory;
+  private MercurialVcsSupport myMercurialSupport;
+
+  public MercurialBranchSupport() {
+  }
+
+  public void setMercurialSupport(@NotNull MercurialVcsSupport mercurialSupport) {
+    myMercurialSupport = mercurialSupport;
+  }
+
+  public void setHgRootFactory(@NotNull HgVcsRootFactory hgRootFactory) {
+    myHgRootFactory = hgRootFactory;
+  }
+
+  @NotNull
+  public String getRemoteRunOnBranchPattern() {
+    return "remote-run/*";
+  }
+
+  @NotNull
+  public RepositoryStateData getCurrentState(@NotNull VcsRoot repository) throws VcsException {
+    return myMercurialSupport.getCurrentState(repository);
+  }
+
+  @NotNull
+  public Map<String, String> getBranchRootOptions(@NotNull VcsRoot original, @NotNull String branchName) {
+    final Map<String, String> options = new HashMap<String, String>(original.getProperties());
+    options.put(Constants.BRANCH_NAME_PROP, branchName);
+    return options;
+  }
+
+  @Nullable
+  public PersonalBranchDescription getPersonalBranchDescription(@NotNull VcsRoot original, @NotNull String branchName) throws VcsException {
+    HgVcsRoot hgRoot = myHgRootFactory.createHgRoot(original);
+    VcsRoot branchRoot = createBranchRoot(original, branchName);
+    String baseVersion = myMercurialSupport.getCurrentVersion(original);
+    String branchVersion = myMercurialSupport.getCurrentVersion(branchRoot);
+    String mergeBase = myMercurialSupport.getMergeBase(hgRoot, baseVersion, branchVersion);
+
+    if (mergeBase == null)
+      return null;
+
+    List<ChangeSet> changeSets = myMercurialSupport.createRepo(hgRoot)
+            .log()
+            .fromRevision(mergeBase)
+            .toRevision(branchVersion)
+            .showCommitsFromAllBranches()
+            .call();
+    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;
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialServerExtension.java	Wed Oct 03 18:08:22 2012 +0400
@@ -0,0 +1,4 @@
+package jetbrains.buildServer.buildTriggers.vcs.mercurial;
+
+public interface MercurialServerExtension {
+}
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java	Wed Oct 03 16:12:28 2012 +0400
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java	Wed Oct 03 18:08:22 2012 +0400
@@ -52,7 +52,7 @@
  * <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,
+public class MercurialVcsSupport extends ServerVcsSupport implements LabelingSupport, VcsFileContentProvider,
         CollectChangesBetweenRoots, CollectChangesBetweenRepositories, BuildPatchByCheckoutRules {
   private final VcsManager myVcsManager;
   private final MirrorManager myMirrorManager;
@@ -63,6 +63,7 @@
   private final FileFilter myIgnoreDotHgFilter = new IgnoreDotHgFilter();
   private final FileFilter myAcceptAllFilter = new AcceptAllFilter();
   private final HgTestConnectionSupport myTestConnection;
+  private final Collection<MercurialServerExtension> myExtensions;
 
   public MercurialVcsSupport(@NotNull final VcsManager vcsManager,
                              @NotNull final SBuildServer server,
@@ -73,7 +74,8 @@
                              @NotNull final RepoFactory repoFactory,
                              @NotNull final MirrorManager mirrorManager,
                              @NotNull final HgVcsRootFactory hgVcsRootFactory,
-                             @NotNull final HgTestConnectionSupport testConnection) {
+                             @NotNull final HgTestConnectionSupport testConnection,
+                             @NotNull final Collection<MercurialServerExtension> extensions) {
     myVcsManager = vcsManager;
     myConfig = config;
     myMirrorManager = mirrorManager;
@@ -81,6 +83,7 @@
     myRepoFactory = repoFactory;
     myHgVcsRootFactory = hgVcsRootFactory;
     myTestConnection = testConnection;
+    myExtensions = extensions;
     resetCacheHandlerManager.registerHandler(new MercurialResetCacheHandler(myMirrorManager));
     dispatcher.addListener(new BuildServerAdapter() {
       @Override
@@ -442,11 +445,6 @@
   }
 
   @NotNull
-  public String getRemoteRunOnBranchPattern() {
-    return "remote-run/*";
-  }
-
-  @NotNull
   public RepositoryStateData getCurrentState(@NotNull VcsRoot root) throws VcsException {
     HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
     Map<String, String> branchRevisions = getBranchesRevisions(hgRoot);
@@ -472,46 +470,6 @@
   }
 
   @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 {
-    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
-    VcsRoot branchRoot = createBranchRoot(root, branchName);
-    String baseVersion = getCurrentVersion(root);
-    String branchVersion = getCurrentVersion(branchRoot);
-    String mergeBase = getMergeBase(hgRoot, baseVersion, branchVersion);
-
-    if (mergeBase == null)
-      return null;
-
-    List<ChangeSet> changeSets = createRepo(hgRoot).log()
-            .fromRevision(mergeBase)
-            .toRevision(branchVersion)
-            .showCommitsFromAllBranches()
-            .call();
-    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 RepositoryStateData fromState,
                                                @NotNull VcsRoot toRoot,
@@ -559,7 +517,7 @@
 
 
   @Nullable
-  private String getMergeBase(@NotNull HgVcsRoot root, @NotNull String revision1, @NotNull String revision2) throws VcsException {
+  public String getMergeBase(@NotNull HgVcsRoot root, @NotNull String revision1, @NotNull String revision2) throws VcsException {
     String result = createRepo(root).mergeBase()
             .revision1(revision1)
             .revision2(revision2)
@@ -769,7 +727,7 @@
     }
   }
 
-  ServerHgRepo createRepo(@NotNull HgVcsRoot root) throws VcsException {
+  public ServerHgRepo createRepo(@NotNull HgVcsRoot root) throws VcsException {
     return myRepoFactory.create(getWorkingDir(root), myHgPathProvider.getHgPath(root), root.getAuthSettings());
   }
 
@@ -801,4 +759,14 @@
   public ListFilesPolicy getListFilesPolicy() {
     return new ListFilesSupport(this, myHgVcsRootFactory);
   }
+
+
+  @Nullable
+  public <T extends VcsExtension> T getVcsExtension(@NotNull Class<T> klass) {
+    for (MercurialServerExtension e : myExtensions) {
+      if (klass.isInstance(e))
+        return klass.cast(e);
+    }
+    return null;
+  }
 }
--- a/mercurial-tests/mercurial-tests.iml	Wed Oct 03 16:12:28 2012 +0400
+++ b/mercurial-tests/mercurial-tests.iml	Wed Oct 03 18:08:22 2012 +0400
@@ -19,6 +19,7 @@
     <orderEntry type="library" name="TeamCity-impl" level="project" />
     <orderEntry type="library" name="TeamCityAPI-agent" level="project" />
     <orderEntry type="library" name="TeamCity Vcs Api" level="project" />
+    <orderEntry type="module" module-name="mercurial-server-tc" />
   </component>
 </module>
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialSupportBuilder.java	Wed Oct 03 18:08:22 2012 +0400
@@ -0,0 +1,81 @@
+package jetbrains.buildServer.buildTriggers.vcs.mercurial;
+
+import jetbrains.buildServer.serverSide.BuildServerListener;
+import jetbrains.buildServer.serverSide.SBuildServer;
+import jetbrains.buildServer.util.EventDispatcher;
+import jetbrains.buildServer.util.cache.ResetCacheHandler;
+import jetbrains.buildServer.util.cache.ResetCacheRegister;
+import jetbrains.buildServer.vcs.VcsManager;
+import org.jetbrains.annotations.NotNull;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+public class MercurialSupportBuilder {
+
+  private Mockery myContext;
+  private ServerPluginConfigBuilder myConfigBuilder;
+  private ServerPluginConfig myConfig;
+  private List<MercurialServerExtension> myExtensions = new ArrayList<MercurialServerExtension>();
+  private HgVcsRootFactory myHgRootFactory;
+
+  public static MercurialSupportBuilder mercurialSupport() {
+    return new MercurialSupportBuilder();
+  }
+
+  @NotNull
+  public MercurialVcsSupport build() throws IOException {
+    if (myContext == null)
+      myContext = new Mockery();
+    VcsManager vcsManager = myContext.mock(VcsManager.class);
+    final SBuildServer server = myContext.mock(SBuildServer.class);
+    final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
+    myContext.checking(new Expectations() {{
+      allowing(server).getExecutor(); will(returnValue(executor));
+    }});
+    EventDispatcher<BuildServerListener> dispatcher = EventDispatcher.create(BuildServerListener.class);
+    if (myConfig == null)
+      myConfig = myConfigBuilder.build();
+    myHgRootFactory = new HgVcsRootFactory(myConfig);
+    MirrorManagerImpl mirrorManager = new MirrorManagerImpl(myConfig);
+    ServerHgPathProvider hgPathProvider = new ServerHgPathProvider(myConfig);
+    RepoFactory repoFactory = new RepoFactory(myConfig, new ServerCommandSettingsFactory());
+    HgTestConnectionSupport testConnection = new HgTestConnectionSupport(myHgRootFactory, repoFactory, mirrorManager, hgPathProvider);
+    final ResetCacheRegister resetCacheManager = myContext.mock(ResetCacheRegister.class);
+    myContext.checking(new Expectations() {{
+      allowing(resetCacheManager).registerHandler(with(any(ResetCacheHandler.class)));
+    }});
+    return new MercurialVcsSupport(vcsManager, server, dispatcher, resetCacheManager, myConfig, hgPathProvider,
+            repoFactory, mirrorManager, myHgRootFactory, testConnection, myExtensions);
+  }
+
+
+  public MercurialSupportBuilder withContext(@NotNull Mockery context) {
+    myContext = context;
+    return this;
+  }
+
+  public MercurialSupportBuilder withConfig(@NotNull ServerPluginConfigBuilder config) {
+    myConfigBuilder = config;
+    return this;
+  }
+
+  public MercurialSupportBuilder withConfig(@NotNull ServerPluginConfig config) {
+    myConfig = config;
+    return this;
+  }
+
+  public MercurialSupportBuilder withExtension(@NotNull MercurialServerExtension extension) {
+    myExtensions.add(extension);
+    return this;
+  }
+
+  public HgVcsRootFactory getHgRootFactory() {
+    return myHgRootFactory;
+  }
+}
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java	Wed Oct 03 16:12:28 2012 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupportTest.java	Wed Oct 03 18:08:22 2012 +0400
@@ -23,7 +23,6 @@
 import org.hamcrest.Description;
 import org.hamcrest.TypeSafeMatcher;
 import org.jetbrains.annotations.NotNull;
-import org.jmock.Mockery;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
@@ -34,6 +33,7 @@
 import java.util.*;
 
 import static com.intellij.openapi.util.io.FileUtil.*;
+import static jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialSupportBuilder.mercurialSupport;
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.Util.buildPatch;
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.Util.copyRepository;
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot;
@@ -51,11 +51,10 @@
   @BeforeMethod
   protected void setUp() throws Exception {
     super.setUp();
-    Mockery context = new Mockery();
     myPluginConfig = new ServerPluginConfigBuilder()
             .cachesDir(myTempFiles.createTempDir())
             .build();
-    myVcs = Util.createMercurialServerSupport(context, myPluginConfig);
+    myVcs = mercurialSupport().withConfig(myPluginConfig).build();
   }
 
   protected String getTestDataPath() {
@@ -526,8 +525,13 @@
 
 
   public void test_getPersonalBranchDescription_when_branch_contains_commits() throws Exception {
+    MercurialBranchSupport branchSupport = new MercurialBranchSupport();
+    MercurialSupportBuilder vcsBuilder = new MercurialSupportBuilder();
+    MercurialVcsSupport vcs = vcsBuilder.withConfig(myPluginConfig).withExtension(branchSupport).build();
+    branchSupport.setMercurialSupport(vcs);
+    branchSupport.setHgRootFactory(vcsBuilder.getHgRootFactory());
     VcsRootImpl originalRoot = createVcsRoot(simpleRepo());
-    PersonalBranchDescription description = myVcs.getPersonalBranchDescription(originalRoot, "name with space");
+    PersonalBranchDescription description = vcs.getVcsExtension(BranchSupport.class).getPersonalBranchDescription(originalRoot, "name with space");
     assertNotNull(description);
     assertEquals(description.getBranchId(), "376dcf05cd2a");
     assertEquals(description.getUsername(), "Pavel.Sher");
@@ -535,8 +539,13 @@
 
 
   public void test_getPersonalBranchDescription_when_branch_doesnot_contain_commits() throws Exception {
+    MercurialBranchSupport branchSupport = new MercurialBranchSupport();
+    MercurialSupportBuilder vcsBuilder = new MercurialSupportBuilder();
+    MercurialVcsSupport vcs = vcsBuilder.withConfig(myPluginConfig).withExtension(branchSupport).build();
+    branchSupport.setMercurialSupport(vcs);
+    branchSupport.setHgRootFactory(vcsBuilder.getHgRootFactory());
     VcsRootImpl originalRoot = createVcsRoot(simpleRepo());
-    PersonalBranchDescription description = myVcs.getPersonalBranchDescription(originalRoot, "default");
+    PersonalBranchDescription description = vcs.getVcsExtension(BranchSupport.class).getPersonalBranchDescription(originalRoot, "default");
     assertNull(description);
   }
 
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/Util.java	Wed Oct 03 16:12:28 2012 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/Util.java	Wed Oct 03 18:08:22 2012 +0400
@@ -20,6 +20,7 @@
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 
@@ -62,7 +63,7 @@
       allowing(resetCacheManager).registerHandler(with(any(ResetCacheHandler.class)));
     }});
     return new MercurialVcsSupport(vcsManager, server, dispatcher, resetCacheManager, config, hgPathProvider,
-            repoFactory, mirrorManager, hgVcsRootFactory, testConnection);
+            repoFactory, mirrorManager, hgVcsRootFactory, testConnection, Collections.<MercurialServerExtension>emptyList());
   }