changeset 912:c386464d3bdc

Merge branch Hajipur-9.0.x
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Tue, 16 Dec 2014 20:55:08 +0100
parents b39494a5ff96 (current diff) 15fce0c26e18 (diff)
children 66cc56a53d8a
files
diffstat 6 files changed, 134 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgRepo.java	Fri Dec 05 14:59:15 2014 +0100
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgRepo.java	Tue Dec 16 20:55:08 2014 +0100
@@ -89,6 +89,10 @@
     return new UpdateCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings);
   }
 
+  public BranchCommand branch() {
+    return new BranchCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir);
+  }
+
   public BranchesCommand branches() {
     return new BranchesCommand(myCommandSettingsFactory.create(), myHgPath, myWorkingDir, myAuthSettings);
   }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BranchCommand.java	Tue Dec 16 20:55:08 2014 +0100
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2000-2014 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.command;
+
+import jetbrains.buildServer.vcs.VcsException;
+import org.jetbrains.annotations.NotNull;
+
+import java.io.File;
+
+public class BranchCommand extends BaseCommand {
+
+  private String myBranch;
+
+  public BranchCommand(@NotNull CommandSettings commandSettings,
+                       @NotNull String hgPath,
+                       @NotNull File workingDir) {
+    super(commandSettings, hgPath, workingDir);
+  }
+
+  @NotNull
+  public BranchCommand name(String branch) {
+    myBranch = branch;
+    return this;
+  }
+
+  public void call() throws VcsException {
+    if (myBranch == null)
+      return;
+    MercurialCommandLine cmd = createCommandLine();
+    cmd.addParameter("branch");
+    cmd.addParameter(myBranch);
+    runCommand(cmd);
+  }
+
+  @NotNull
+  @Override
+  protected String getDescription() {
+    if (myBranch == null)
+      return "";
+    return "hg branch " + myBranch;
+  }
+}
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCollectChangesPolicy.java	Fri Dec 05 14:59:15 2014 +0100
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCollectChangesPolicy.java	Tue Dec 16 20:55:08 2014 +0100
@@ -56,8 +56,12 @@
 
   @NotNull
   public RepositoryStateData getCurrentState(@NotNull VcsRoot root) throws VcsException {
+    return getCurrentState(myHgVcsRootFactory.createHgRoot(root));
+  }
+
+  @NotNull
+  public RepositoryStateData getCurrentState(@NotNull final HgVcsRoot hgRoot) throws VcsException {
     final OperationContext context = new OperationContext(myVcs, myRepoFactory, createMercurialProgess());
-    final HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
     VcsCallable<Map<String, String>> cmd = new VcsCallable<Map<String, String>>() {
       public Map<String, String> call() throws VcsException {
         return getHeads(hgRoot, context);
@@ -67,7 +71,7 @@
     String defaultBranchName = hgRoot.getBranchName();
     if (revisions.get(defaultBranchName) == null && !hgRoot.isIgnoreMissingDefaultBranch()) {
       throw new VcsException("Cannot find revision of the default branch '" +
-              defaultBranchName + "' of vcs root " + myVcs.describeVcsRoot(root));
+              defaultBranchName + "' of vcs root " + myVcs.describeVcsRoot(hgRoot));
     }
     return RepositoryStateData.createVersionState(defaultBranchName, revisions);
   }
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupport.java	Fri Dec 05 14:59:15 2014 +0100
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupport.java	Tue Dec 16 20:55:08 2014 +0100
@@ -25,7 +25,9 @@
 import org.jetbrains.annotations.NotNull;
 
 import java.io.*;
+import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 public class MercurialCommitSupport implements CommitSupport, MercurialServerExtension {
@@ -130,14 +132,20 @@
     @NotNull
     public CommitResult commit(@NotNull CommitSettings commitSettings) throws VcsException {
       try {
-        RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(myRoot);
+        RepositoryStateData state = getCurrentState();
         String defaultBranch = state.getDefaultBranchName();
         String defaultBranchRevision = state.getBranchRevisions().get(defaultBranch);
+        if (!state.getBranchRevisions().isEmpty() && defaultBranchRevision == null)
+          throw new VcsException("The '" + defaultBranch + "' destination branch doesn't exist");
 
         new CheckoutRepository(myMirrorManager, myHgRepoFactory, myConfig.getPullTimeout(), myConfig.useTagsAsBranches(),
                 myHgRoot, myCheckoutDir).setRevision(defaultBranchRevision).checkout();
 
         HgRepo repo = myHgRepoFactory.createRepo(myHgRoot, myCheckoutDir);
+        boolean bookmark = repo.isBookmark(defaultBranch);
+        if (!bookmark)
+          repo.branch().name(defaultBranch).call();
+
         for (String dir : myDeletedDirs) {
           FileUtil.delete(new File(myCheckoutDir, dir));
         }
@@ -153,7 +161,6 @@
         if (exitCode == 1)
           return CommitResult.createCommitNotPerformedResult("repository is up-to-date");
 
-        boolean bookmark = repo.isBookmark(defaultBranch);
         if (bookmark)
           repo.updateBookmark().name(defaultBranch).call();
 
@@ -164,10 +171,19 @@
 
         return CommitResult.createSuccessResult(repo.getWorkingDirRevision());
       } catch (Exception e) {
+        if (e instanceof VcsException)
+          throw (VcsException) e;
         throw new VcsException(e);
       }
     }
 
+    @NotNull
+    private RepositoryStateData getCurrentState() throws VcsException {
+      Map<String, String> props = new HashMap<String, String>(myHgRoot.getProperties());
+      props.put(Constants.IGNORE_MISSING_DEFAULT_BRANCH, "true");
+      return myVcs.getCollectChangesPolicy().getCurrentState(new HgVcsRoot(props));
+    }
+
     private String nonEmptyMessage(@NotNull CommitSettings commitSettings) {
       String msg = commitSettings.getDescription();
       if (!StringUtil.isEmpty(msg))
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java	Fri Dec 05 14:59:15 2014 +0100
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialVcsSupport.java	Tue Dec 16 20:55:08 2014 +0100
@@ -180,6 +180,11 @@
     return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP);
   }
 
+  @NotNull
+  public String describeVcsRoot(@NotNull final HgVcsRoot vcsRoot) {
+    return "mercurial: " + vcsRoot.getProperty(Constants.REPOSITORY_PROP);
+  }
+
   @Override
   @NotNull
   public TestConnectionSupport getTestConnectionSupport() {
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupportTest.java	Fri Dec 05 14:59:15 2014 +0100
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupportTest.java	Tue Dec 16 20:55:08 2014 +0100
@@ -16,6 +16,8 @@
 
 package jetbrains.buildServer.buildTriggers.vcs.mercurial;
 
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.AuthSettings;
+import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.TestCommandSettingsFactory;
 import jetbrains.buildServer.util.TestFor;
 import jetbrains.buildServer.vcs.*;
 import org.jetbrains.annotations.NotNull;
@@ -28,16 +30,19 @@
 import java.util.List;
 
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialSupportBuilder.mercurialSupport;
+import static jetbrains.buildServer.buildTriggers.vcs.mercurial.Util.getHgPath;
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsChangeMatcher.vcsChange;
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasItem;
 import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertNotNull;
 
 @RequiredHgVersion(min = "1.7.0")
 @Test(dataProviderClass = HgVersionConstraint.class, dataProvider = "installedHgVersion")
 public class MercurialCommitSupportTest extends BaseMercurialTestCase {
 
+  private ServerPluginConfig myPluginConfig;
   private MercurialVcsSupport myVcs;
   private MercurialCommitSupport myCommitSupport;
   private File myRepo;
@@ -48,12 +53,12 @@
     super.setUp();
 
     myRepo = createRepo("mercurial-tests/testData/commit");
-    ServerPluginConfig pluginConfig = new ServerPluginConfigBuilder()
+    myPluginConfig = new ServerPluginConfigBuilder()
             .cachesDir(myTempFiles.createTempDir())
             .build();
-    MercurialSupportBuilder mercurialBuilder = mercurialSupport().withConfig(pluginConfig);
+    MercurialSupportBuilder mercurialBuilder = mercurialSupport().withConfig(myPluginConfig);
     myVcs = mercurialBuilder.build();
-    myCommitSupport = new MercurialCommitSupport(myVcs, myVcs.getMirrorManager(), pluginConfig,
+    myCommitSupport = new MercurialCommitSupport(myVcs, myVcs.getMirrorManager(), myPluginConfig,
             mercurialBuilder.getHgRootFactory(), mercurialBuilder.getHgRepoFactory());
   }
 
@@ -100,6 +105,43 @@
   }
 
 
+  @TestFor(issues = "TW-39321")
+  public void should_throw_meaningful_error_if_destination_branch_doesnt_exist(@NotNull HgVersion _) throws Exception {
+    String nonExistingBranch = "nonExisting";
+    VcsRoot root = vcsRoot().withUrl(myRepo).withBranch(nonExistingBranch).build();
+    String description = "msg";
+    String author = "Joe Doe <joe@some.org>";
+    try {
+      CommitPatchBuilder patchBuilder = myCommitSupport.getCommitPatchBuilder(root);
+      patchBuilder.createFile("a/b/c", stream("test"));
+      patchBuilder.commit(new CommitSettingsImpl(author, description));
+      patchBuilder.dispose();
+    } catch (VcsException e) {
+      assertEquals("The '" + nonExistingBranch + "' destination branch doesn't exist", e.getMessage());
+    }
+  }
+
+
+  @TestFor(issues = "TW-39321")
+  public void should_create_branch_if_repository_has_no_branches(@NotNull HgVersion _) throws Exception {
+    File emptyRemoteRepo = myTempFiles.createTempDir();
+    ServerHgRepo repo = new ServerHgRepo(new TestCommandSettingsFactory(), myPluginConfig, emptyRemoteRepo, getHgPath(), new AuthSettings());
+    repo.init().call();
+
+    String nonExistingBranch = "nonExisting";
+    VcsRoot root = vcsRoot().withUrl(emptyRemoteRepo).withBranch(nonExistingBranch).build();
+    String description = "msg";
+    String author = "Joe Doe <joe@some.org>";
+    CommitPatchBuilder patchBuilder = myCommitSupport.getCommitPatchBuilder(root);
+    patchBuilder.createFile("a/b/c", stream("test"));
+    patchBuilder.commit(new CommitSettingsImpl(author, description));
+    patchBuilder.dispose();
+
+    RepositoryStateData state = myVcs.getCollectChangesPolicy().getCurrentState(root);
+    assertNotNull(state.getBranchRevisions().get(nonExistingBranch));
+  }
+
+
   @NotNull
   private InputStream stream(@NotNull String content) {
     return new ByteArrayInputStream(content.getBytes());