Author: [email protected]
Date: Thu Mar 12 21:39:42 2009
New Revision: 5008

Modified:
     
trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/CommandRunner.java
    trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/SvnInfo.java
     
trunk/build-tools/ant-gwt/test/com/google/gwt/ant/taskdefs/SvnInfoTest.java

Log:
Adding git support to the ant SvnInfo task.

git svn users should now be able to correctly label a build with accurate  
svn information.

Review by: fabbott

Modified:  
trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/CommandRunner.java
==============================================================================
---  
trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/CommandRunner.java    
 
(original)
+++  
trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/CommandRunner.java    
 
Thu Mar 12 21:39:42 2009
@@ -30,9 +30,11 @@

    /**
     * Returns the output of running a command as a string. Will fail if the
-   * invoked process returns a non-zero status code.
+   * invoked process returns a non-zero status code and
+   * <code>checkStatusCode</code> is <code>true</code>.
     */
-  public static String getCommandOutput(File workDir, String... cmd) {
+  public static String getCommandOutput(boolean checkStatusCode, File  
workDir,
+      String... cmd) {
      Process process = runCommandIgnoringErr(workDir, cmd);
      StringBuilder output = new StringBuilder();
      LineNumberReader lnr = new LineNumberReader(new InputStreamReader(
@@ -43,7 +45,7 @@
          output.append('\n');
        }
        int statusCode = process.waitFor();
-      if (statusCode != 0) {
+      if (checkStatusCode && statusCode != 0) {
          throw new BuildException("Non-zero status code result (" +  
statusCode
              + ") running command: " + makeCmdString(cmd));
        }
@@ -55,6 +57,14 @@
        throw new BuildException("Interrupted waiting for command: "
            + makeCmdString(cmd), e);
      }
+  }
+
+  /**
+   * Returns the output of running a command as a string. Will fail if the
+   * invoked process returns a non-zero status code.
+   */
+  public static String getCommandOutput(File workDir, String... cmd) {
+    return getCommandOutput(true, workDir, cmd);
    }

    /**

Modified:  
trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/SvnInfo.java
==============================================================================
--- trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/SvnInfo.java      
 
(original)
+++ trunk/build-tools/ant-gwt/src/com/google/gwt/ant/taskdefs/SvnInfo.java      
 
Thu Mar 12 21:39:42 2009
@@ -79,6 +79,69 @@
        + URL_REGEX + ")\\s*");

    /**
+   * Returns true if this git working copy matches the specified svn  
revision,
+   * and also has no local modifications.
+   */
+  static boolean doesGitWorkingCopyMatchSvnRevision(File dir, String  
svnRevision) {
+    String workingRev = getGitWorkingRev(dir);
+    String targetRev = getGitRevForSvnRev(dir, svnRevision);
+    if (!workingRev.equals(targetRev)) {
+      return false;
+    }
+    String status = getGitStatus(dir);
+    return status.contains("nothing to commit (working directory clean)");
+  }
+
+  /**
+   * Returns the git commit number matching the specified svn revision.
+   */
+  static String getGitRevForSvnRev(File dir, String svnRevision) {
+    String output = CommandRunner.getCommandOutput(dir, "git", "svn",
+        "find-rev", "r" + svnRevision);
+    output = output.trim();
+    if (output.length() == 0) {
+      throw new BuildException("git svn find-rev didn't give any answer");
+    }
+    return output;
+  }
+
+  /**
+   * Runs "git status" and returns the result.
+   */
+  static String getGitStatus(File dir) {
+    // git status returns 1 for a status code, so just don't check it.
+    String output = CommandRunner.getCommandOutput(false,  
dir, "git", "status");
+    if (output.length() == 0) {
+      throw new BuildException("git status didn't give any answer");
+    }
+    return output;
+  }
+
+  /**
+   * Runs "git svn info", returning the output as a string.
+   */
+  static String getGitSvnInfo(File dir) {
+    String output =  
CommandRunner.getCommandOutput(dir, "git", "svn", "info");
+    if (output.length() == 0) {
+      throw new BuildException("git svn info didn't give any answer");
+    }
+    return output;
+  }
+
+  /**
+   * Returns the current git commit number of the working copy.
+   */
+  static String getGitWorkingRev(File dir) {
+    String output = CommandRunner.getCommandOutput(dir, "git", "rev-list",
+        "--max-count=1", "HEAD");
+    output = output.trim();
+    if (output.length() == 0) {
+      throw new BuildException("git rev-list didn't give any answer");
+    }
+    return output;
+  }
+
+  /**
     * Runs "svn info", returning the output as a string.
     */
    static String getSvnInfo(File dir) {
@@ -101,6 +164,17 @@
      return output;
    }

+  static boolean looksLikeGit(File dir) {
+    dir = dir.getAbsoluteFile();
+    while (dir != null) {
+      if (new File(dir, ".git").isDirectory()) {
+        return true;
+      }
+      dir = dir.getParentFile();
+    }
+    return false;
+  }
+
    /**
     * Returns <code>true</code> if the specified directory looks like an svn
     * working copy.
@@ -189,13 +263,20 @@
      }

      Info info;
-    if (!looksLikeSvn(workDirFile)) {
-      info = new Info("unknown", "unknown");
-    } else {
+    if (looksLikeSvn(workDirFile)) {
        info = parseInfo(getSvnInfo(workDirFile));

        // Use svnversion to get a more exact revision string.
        info.revision = getSvnVersion(workDirFile);
+    } else if (looksLikeGit(workDirFile)) {
+      info = parseInfo(getGitSvnInfo(workDirFile));
+
+      // Add a 'M' tag if this working copy is not pristine.
+      if (!doesGitWorkingCopyMatchSvnRevision(workDirFile, info.revision))  
{
+        info.revision += "M";
+      }
+    } else {
+      info = new Info("unknown", "unknown");
      }

      getProject().setNewProperty(outprop, info.branch + "@" +  
info.revision);

Modified:  
trunk/build-tools/ant-gwt/test/com/google/gwt/ant/taskdefs/SvnInfoTest.java
==============================================================================
---  
trunk/build-tools/ant-gwt/test/com/google/gwt/ant/taskdefs/SvnInfoTest.java     
 
(original)
+++  
trunk/build-tools/ant-gwt/test/com/google/gwt/ant/taskdefs/SvnInfoTest.java     
 
Thu Mar 12 21:39:42 2009
@@ -32,6 +32,79 @@
    private static final File dir = new File(".");

    /**
+   * A cached copy of 'git svn info' so we don't have to keep running it  
(makes
+   * the tests run faster).
+   */
+  private static String gitSvnInfo = null;
+
+  /**
+   * Check that this is a valid git rev.
+   */
+  private static void assertIsValidGitRev(String rev) {
+    assertNotNull(rev);
+    assertEquals(rev, 40, rev.length());
+    for (char ch : rev.toCharArray()) {
+      assertTrue(isHexDigit(ch));
+    }
+  }
+
+  private static String getGitSvnInfo() {
+    if (gitSvnInfo == null) {
+      gitSvnInfo = SvnInfo.getGitSvnInfo(dir);
+    }
+    return gitSvnInfo;
+  }
+
+  private static boolean isHexDigit(char ch) {
+    return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')
+        || (ch >= 'a' && ch <= 'f');
+  }
+
+  /**
+   * Test that doesGitWorkingCopyMatchSvnRevision finishes.
+   */
+  public void testDoesGitWorkingCopyMatchSvnRevision() {
+    if (SvnInfo.looksLikeGit(dir)) {
+      Info info = SvnInfo.parseInfo(getGitSvnInfo());
+      SvnInfo.doesGitWorkingCopyMatchSvnRevision(dir, info.revision);
+    }
+  }
+
+  /**
+   * Test that getGitRevForSvnRev returns a 40-character git hash.
+   */
+  public void testGetGitRevForSvnRev() {
+    if (SvnInfo.looksLikeGit(dir)) {
+      Info info = SvnInfo.parseInfo(getGitSvnInfo());
+      String rev = SvnInfo.getGitRevForSvnRev(dir, info.revision);
+      assertIsValidGitRev(rev);
+    }
+  }
+
+  public void testGetGitStatus() {
+    if (SvnInfo.looksLikeGit(dir)) {
+      String status = SvnInfo.getGitStatus(dir);
+      assertNotNull(status);
+      assertTrue(!"".equals(status));
+    }
+  }
+
+  public void testGetGitSvnInfo() {
+    if (SvnInfo.looksLikeGit(dir)) {
+      String info = getGitSvnInfo();
+      assertNotNull(info);
+      assertTrue(!"".equals(info));
+    }
+  }
+
+  public void testGetGitSvnWorkingRev() {
+    if (SvnInfo.looksLikeGit(dir)) {
+      String rev = SvnInfo.getGitWorkingRev(dir);
+      assertIsValidGitRev(rev);
+    }
+  }
+
+  /**
     * If this is an svn working copy, just verify that "svn info" succeeds  
and
     * returns something.
     */

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to