changeset 668:5abe150c9187 Gaya-8.0.x

TW-32540 ensure a custom clone dir has only valid symbols
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Sat, 12 Oct 2013 19:28:29 +0400
parents 55cf557a14de
children c32869bd757b 03a544f9eae1
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgFileUtil.java mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactory.java mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactoryTest.java
diffstat 3 files changed, 40 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgFileUtil.java	Tue Oct 01 12:51:13 2013 +0400
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgFileUtil.java	Sat Oct 12 19:28:29 2013 +0400
@@ -30,6 +30,10 @@
    */
   public static File createTempDir() throws IOException {
     File parentDir = new File(FileUtil.getTempDirectory());
+    return createTempDir(parentDir);
+  }
+
+  public static File createTempDir(@NotNull File parentDir) throws IOException {
     parentDir.mkdirs();
 
     int suffix = 0;
--- a/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactory.java	Tue Oct 01 12:51:13 2013 +0400
+++ b/mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactory.java	Sat Oct 12 19:28:29 2013 +0400
@@ -1,6 +1,7 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial;
 
 import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
+import jetbrains.buildServer.util.FileUtil;
 import jetbrains.buildServer.util.StringUtil;
 import jetbrains.buildServer.vcs.VcsException;
 import jetbrains.buildServer.vcs.VcsRoot;
@@ -40,12 +41,30 @@
       repPath = splitted[splitted.length-1];
     }
 
-    File customWorkingDir = new File(canonicalCustomCloneDir, repPath);
+    File customWorkingDir = getCustomWorkingDir(canonicalCustomCloneDir, repPath);
     hgRoot.setCustomWorkingDir(customWorkingDir);
 
     return hgRoot;
   }
 
+  private File getCustomWorkingDir(@NotNull File parentCloneDir, @NotNull String repoPath) throws VcsException {
+    File customWorkingDir = new File(parentCloneDir, repoPath);
+    customWorkingDir.mkdirs();
+    if (customWorkingDir.isDirectory())
+      return customWorkingDir;
+
+    customWorkingDir = new File(parentCloneDir, FileUtil.fixDirectoryName(repoPath));
+    customWorkingDir.mkdirs();
+    if (customWorkingDir.isDirectory())
+      return customWorkingDir;
+
+    try {
+      return HgFileUtil.createTempDir(parentCloneDir);
+    } catch (IOException e) {
+      throw new VcsException(e);
+    }
+  }
+
   private File getCanonicalCustomCloneDir(@NotNull String customClonePath) throws VcsException {
     try {
       return new File(customClonePath).getCanonicalFile();
--- a/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactoryTest.java	Tue Oct 01 12:51:13 2013 +0400
+++ b/mercurial-tests/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgVcsRootFactoryTest.java	Sat Oct 12 19:28:29 2013 +0400
@@ -14,6 +14,7 @@
 import static jetbrains.buildServer.buildTriggers.vcs.mercurial.VcsRootBuilder.vcsRoot;
 import static org.testng.AssertJUnit.assertFalse;
 import static org.testng.AssertJUnit.assertNull;
+import static org.testng.AssertJUnit.assertTrue;
 
 @Test
 public class HgVcsRootFactoryTest {
@@ -47,6 +48,21 @@
   }
 
 
+  @TestFor(issues = "TW-32540")
+  public void custom_clone_dir_should_contain_only_valid_characters() throws Exception {
+    TempFiles tmp = new TempFiles();
+    String urlWithoutPath = "http://acme:8000/";
+    VcsRoot root = vcsRoot()
+            .withUrl(urlWithoutPath)
+            .withCloneRepositoryTo(tmp.createTempDir())
+            .build();
+
+    HgVcsRoot hgRoot = new HgVcsRootFactory(serverPluginConfig().cachesDir(tmp.createTempDir()).build()).createHgRoot(root);
+    hgRoot.getCustomWorkingDir().mkdirs();
+    assertTrue(hgRoot.getCustomWorkingDir().exists());
+  }
+
+
   @TestFor(issues = "TW-28974")
   public void should_use_default_dirs() throws Exception {
     TempFiles tmp = new TempFiles();