changeset 911:15fce0c26e18 Hajipur-9.0.x

TW-39321 create branch for commit if repository has no branches
author Dmitry Neverov <dmitry.neverov@gmail.com>
date Tue, 16 Dec 2014 20:45:00 +0100
parents 26de7a765875
children c386464d3bdc ed4ae4bfd691
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgRepo.java mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/BranchCommand.java mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupport.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupportTest.java
diffstat 4 files changed, 93 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgRepo.java	Tue Dec 16 20:11:05 2014 +0100
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgRepo.java	Tue Dec 16 20:45:00 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:45:00 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/MercurialCommitSupport.java	Tue Dec 16 20:11:05 2014 +0100
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupport.java	Tue Dec 16 20:45:00 2014 +0100
@@ -135,13 +135,17 @@
         RepositoryStateData state = getCurrentState();
         String defaultBranch = state.getDefaultBranchName();
         String defaultBranchRevision = state.getBranchRevisions().get(defaultBranch);
-        if (defaultBranchRevision == null)
+        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));
         }
@@ -157,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();
 
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupportTest.java	Tue Dec 16 20:11:05 2014 +0100
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupportTest.java	Tue Dec 16 20:45:00 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());
   }
 
@@ -117,6 +122,26 @@
   }
 
 
+  @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());