changeset 866:3795b579d310

Report version of detected hg on agent
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Mon, 15 Sep 2014 20:16:55 +0200
parents 3d700493d033
children 80d695d15cc0
files mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgDetector.java
diffstat 1 files changed, 58 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgDetector.java	Mon Sep 15 19:39:59 2014 +0200
+++ b/mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgDetector.java	Mon Sep 15 20:16:55 2014 +0200
@@ -38,6 +38,7 @@
 public class HgDetector extends AgentLifeCycleAdapter {
 
   final static String AGENT_HG_PATH_PROPERTY = "teamcity.hg.agent.path";
+  private final static String AGENT_HG_VERSION = "teamcity.hg.version";
   private final static Logger LOG = Logger.getInstance(HgDetector.class.getName());
   private final static HgVersion LEAST_SUPPORTED_VERSION = new HgVersion(1, 5, 2);
   private final CommandSettingsFactory myCommandSettingsFactory;
@@ -55,50 +56,62 @@
   public void beforeAgentConfigurationLoaded(@NotNull final BuildAgent agent) {
     BuildAgentConfiguration config = agent.getConfiguration();
     String agentHgPath = config.getConfigurationParameters().get(AGENT_HG_PATH_PROPERTY);
-    File workDir = config.getTempDirectory();
+    File tmpDir = config.getTempDirectory();
     if (agentHgPath == null) {
-      String detectedHg = detectHg(workDir);
+      HgExec detectedHg = detectHg(tmpDir);
       if (detectedHg != null) {
-        LOG.info("Detect installed mercurial at path " + detectedHg + ", provide it as a property " + AGENT_HG_PATH_PROPERTY);
+        LOG.info("Detect installed mercurial at path " + detectedHg.getPath() + ", provide it as a property " + AGENT_HG_PATH_PROPERTY);
         config.addConfigurationParameter(AGENT_HG_PATH_PROPERTY, "hg");
+        config.addConfigurationParameter(AGENT_HG_VERSION, detectedHg.getVersion().toString());
       } else {
         LOG.info("Cannot detect installed mercurial");
       }
     } else {
-      if (!canRunHg(agentHgPath, workDir, true))
-        LOG.warn("Mercurial executable at path " + agentHgPath + " cannot be run or not compatible with TeamCity");
+      HgExec hg = detectHgAtPath(tmpDir, agentHgPath, true);
+      if (hg == null) {
+        LOG.warn("Cannot run mercurial at path " + agentHgPath);
+      } else {
+        if (isCompatible(hg.getVersion())) {
+          config.addConfigurationParameter(AGENT_HG_VERSION, hg.getVersion().toString());
+        } else {
+          LOG.warn("Mercurial at path " + agentHgPath + " is not compatible with TeamCity");
+        }
+      }
     }
   }
 
 
   @Nullable
-  private String detectHg(@NotNull final File workDir) {
+  private HgExec detectHg(@NotNull File tmpDir) {
     for (String path : myHgPaths) {
-      if (canRunHg(path, workDir))
-        return path;
+      HgExec exec = detectHgAtPath(tmpDir, path, false);
+      if (exec == null)
+        continue;
+      if (isCompatible(exec.getVersion())) {
+        return exec;
+      } else {
+        warn("Mercurial version at path " + path + " is " + exec.getVersion() + ", required version is " + LEAST_SUPPORTED_VERSION + "+", false);
+      }
     }
     return null;
   }
 
 
-  private boolean canRunHg(@NotNull final String hgPath, @NotNull final File workDir) {
-    return canRunHg(hgPath, workDir, false);
+  @Nullable
+  private HgExec detectHgAtPath(@NotNull File tmpDir, @NotNull String hgPath, boolean logWarnings) {
+    try {
+      HgVersion version = getVersion(hgPath, tmpDir);
+      return new HgExec(hgPath, version);
+    } catch (VcsException e) {
+      warn("Error while trying to get hg version, hg path: " + hgPath, e, logWarnings);
+      return null;
+    }
   }
 
-  private boolean canRunHg(@NotNull final String hgPath, @NotNull final File workDir, boolean logWarnings) {
-    VersionCommand versionCommand = new VersionCommand(myCommandSettingsFactory.create(), hgPath, workDir);
-    try {
-      HgVersion version = versionCommand.call();
-      if (isCompatible(version)) {
-        return true;
-      } else {
-        warn("Mercurial version at path " + hgPath + " is " + version + ", required version is " + LEAST_SUPPORTED_VERSION + "+", logWarnings);
-        return false;
-      }
-    } catch (VcsException e) {
-      warn("Error while trying to get hg version, hg path: " + hgPath, e, logWarnings);
-      return false;
-    }
+
+  @NotNull
+  private HgVersion getVersion(@NotNull String hgPath, @NotNull File workDir) throws VcsException {
+    return new VersionCommand(myCommandSettingsFactory.create(), hgPath, workDir).call();
   }
 
 
@@ -119,4 +132,25 @@
   private boolean isCompatible(@NotNull final HgVersion version) {
     return version.isEqualsOrGreaterThan(LEAST_SUPPORTED_VERSION);
   }
+
+
+  private class HgExec {
+    private final String myPath;
+    private final HgVersion myVersion;
+
+    public HgExec(@NotNull String path, @NotNull HgVersion version) {
+      myPath = path;
+      myVersion = version;
+    }
+
+    @NotNull
+    public String getPath() {
+      return myPath;
+    }
+
+    @NotNull
+    public HgVersion getVersion() {
+      return myVersion;
+    }
+  }
 }