changeset 724:f1945b327008

parse subrepos from string
author eugene.petrenko@jetbrains.com
date Mon, 13 Jan 2014 20:00:37 +0100
parents 860021731792
children 9e4c8cd92838
files mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgSubs.java
diffstat 1 files changed, 58 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgSubs.java	Mon Jan 13 19:52:52 2014 +0100
+++ b/mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgSubs.java	Mon Jan 13 20:00:37 2014 +0100
@@ -1,12 +1,13 @@
 package jetbrains.buildServer.buildTriggers.vcs.mercurial;
 
 import jetbrains.buildServer.util.FileUtil;
+import jetbrains.buildServer.util.StringUtil;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 import static java.util.Collections.emptyMap;
 
@@ -18,33 +19,61 @@
 public class HgSubs {
 
   @NotNull
-  public static Map<String, SubRepo> readSubrepositories(@NotNull final File hgsub, @NotNull final File hgsubstate) {
-    if (hgsub.exists() && hgsubstate.exists()) {
-      try {
-        Map<String, String> path2repo = readHgsub(hgsub);
-        Map<String, String> path2revision = readHgsubstate(hgsubstate);
-        Map<String, SubRepo> result = new HashMap<String, SubRepo>();
-        for (Map.Entry<String, String> entry : path2repo.entrySet()) {
-          String path = entry.getKey();
-          String url = entry.getValue();
-          String revision = path2revision.get(path);
-          if (revision != null)
-            result.put(path, new SubRepo(path, url, revision));
-        }
-        return result;
-      } catch (IOException e) {
-        return emptyMap();
-      }
-    } else {
+  public static Map<String, SubRepo> readSubrepositories(@NotNull final File hgsub,
+                                                         @NotNull final File hgsubstate) {
+    if (!hgsub.exists() || !hgsubstate.exists()) {
       return emptyMap();
     }
+
+    final Map<String, String> path2repo;
+    final Map<String, String> path2revision;
+    try {
+      path2repo = readHgsub(hgsub);
+      path2revision = readHgsubstate(hgsubstate);
+    } catch (IOException e) {
+      return emptyMap();
+    }
+
+    return readSubrepositories(path2repo, path2revision);
+  }
+
+  @NotNull
+  public static Map<String, SubRepo> readSubrepositories(@Nullable final String hgsubText,
+                                                         @Nullable final String hgsubstateText) {
+
+    if (hgsubstateText == null || hgsubText == null) return emptyMap();
+
+    return readSubrepositories(
+            readHgsub(Arrays.asList(StringUtil.splitByLines(hgsubText))),
+            readHgsub(Arrays.asList(StringUtil.splitByLines(hgsubstateText)))
+    );
+  }
+
+  @NotNull
+  private static Map<String, SubRepo> readSubrepositories(@NotNull final Map<String, String> path2repo,
+                                                          @NotNull final Map<String, String> path2revision) {
+    final Map<String, SubRepo> result = new HashMap<String, SubRepo>();
+    for (Map.Entry<String, String> entry : path2repo.entrySet()) {
+      final String path = entry.getKey();
+      final String url = entry.getValue();
+      final String revision = path2revision.get(path);
+      if (revision != null)
+        result.put(path, new SubRepo(path, url, revision));
+    }
+    return result;
   }
 
   @NotNull
   /*returns map: relative path -> repository url */
   private static Map<String, String> readHgsub(@NotNull final File hgsub) throws IOException {
+    return readHgsub(FileUtil.readFile(hgsub));
+  }
+
+  @NotNull
+  /*returns map: relative path -> repository url */
+  private static Map<String, String> readHgsub(@NotNull final Collection<String> lines) {
     Map<String, String> result = new HashMap<String, String>();
-    for (String line : FileUtil.readFile(hgsub)) {
+    for (String line : lines) {
       String[] parts = line.split(" = ");
       if (parts.length == 2)
         result.put(parts[0], parts[1]);
@@ -56,8 +85,14 @@
   @NotNull
   /*returns map: relative path -> revision */
   private static Map<String, String> readHgsubstate(@NotNull final File hgsubstate) throws IOException {
-    Map<String, String> result = new HashMap<String, String>();
-    for (String line : FileUtil.readFile(hgsubstate)) {
+    return readHgsubstate(FileUtil.readFile(hgsubstate));
+  }
+
+  @NotNull
+  /*returns map: relative path -> revision */
+  private static Map<String, String> readHgsubstate(@NotNull final Collection<String> lines) {
+    final Map<String, String> result = new HashMap<String, String>();
+    for (String line : lines) {
       String[] parts = line.split(" ");
       if (parts.length == 2)
         result.put(parts[1], parts[0]);