Improve versioning driver
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 12 Feb 2012 17:39:31 +0000 (17:39 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 12 Feb 2012 17:39:31 +0000 (17:39 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@5054 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/deploy/VersionedDirSync.java
runtime/org.argeo.slc.specs/src/main/java/org/argeo/slc/deploy/VersioningDriver.java
runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/lib/svn/SvnKitDriver.java

index 7199b0ef3fa3dbc29ab226cce4441602b967da0d..ee84cf9ecc0632bc09062a051d14e0222b6956c5 100644 (file)
@@ -37,7 +37,10 @@ public class VersionedDirSync implements Runnable {
        private String url;
        private Boolean clean = false;
 
+       private Boolean changed = null;
+
        public void run() {
+               changed = null;
                if (clean) {
                        try {
                                FileUtils.deleteDirectory(dir);
@@ -47,7 +50,7 @@ public class VersionedDirSync implements Runnable {
                        }
                        dir.mkdirs();
                }
-               versioningDriver.checkout(url, dir, true);
+               changed = versioningDriver.checkout(url, dir, true);
                if (log.isDebugEnabled())
                        log.debug("Synchronized " + url + " to " + dir);
        }
@@ -69,4 +72,11 @@ public class VersionedDirSync implements Runnable {
                this.clean = clean;
        }
 
+       /** Whether last call has changed the directory */
+       public Boolean getChanged() {
+               if (changed == null)
+                       throw new SlcException("Sync has not run");
+               return changed;
+       }
+
 }
index a48b35543cc348954d3324f72a536fc5540ed3c6..6779a3eca709eadf8f532bbb38341b109e00f3d9 100644 (file)
@@ -20,6 +20,7 @@ import java.io.File;
 import java.io.OutputStream;\r
 import java.util.List;\r
 \r
+/** Abstracts common versioning operations */\r
 public interface VersioningDriver {\r
        public void getFileFromRepository(String repositoryBaseUrl,\r
                        String location, OutputStream out);\r
@@ -34,7 +35,13 @@ public interface VersioningDriver {
 \r
        public void importFileOrDir(String repositoryUrl, File fileOrDir);\r
 \r
-       public void checkout(String repositoryUrl, File destDir, Boolean recursive);\r
+       /**\r
+        * Checks out or update this versioned directory\r
+        * \r
+        * @return true if the content has changed, false otherwise\r
+        */\r
+       public Boolean checkout(String repositoryUrl, File destDir,\r
+                       Boolean recursive);\r
 \r
        public void createRepository(String filePath);\r
 \r
index f1361ec69fbb1f7ca403b4afa2e3cf2a0dbac424..e6ef52c49961ce6ebf0da7149032b8f5945a35fb 100644 (file)
@@ -21,8 +21,11 @@ import java.io.OutputStream;
 import java.util.List;\r
 import java.util.Vector;\r
 \r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
 import org.argeo.slc.SlcException;\r
 import org.argeo.slc.deploy.VersioningDriver;\r
+import org.tmatesoft.svn.core.SVNDepth;\r
 import org.tmatesoft.svn.core.SVNException;\r
 import org.tmatesoft.svn.core.SVNURL;\r
 import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;\r
@@ -30,15 +33,19 @@ import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
 import org.tmatesoft.svn.core.io.SVNRepository;\r
 import org.tmatesoft.svn.core.wc.SVNClientManager;\r
 import org.tmatesoft.svn.core.wc.SVNRevision;\r
+import org.tmatesoft.svn.core.wc.SVNWCUtil;\r
 import org.tmatesoft.svn.core.wc.admin.ISVNChangeEntryHandler;\r
 import org.tmatesoft.svn.core.wc.admin.SVNChangeEntry;\r
 \r
+/** Versioning driver with a Subversion backen, based on SVNKit */\r
 public class SvnKitDriver implements VersioningDriver {\r
+       private final static Log log = LogFactory.getLog(SvnKitDriver.class);\r
+\r
        private final SVNClientManager manager;\r
 \r
        public SvnKitDriver() {\r
-                DAVRepositoryFactory.setup();\r
-                FSRepositoryFactory.setup();\r
+               DAVRepositoryFactory.setup();\r
+               FSRepositoryFactory.setup();\r
                manager = SVNClientManager.newInstance();\r
        }\r
 \r
@@ -64,12 +71,37 @@ public class SvnKitDriver implements VersioningDriver {
                }\r
        }\r
 \r
-       @SuppressWarnings("deprecation")\r
-       public void checkout(String repositoryUrl, File destDir, Boolean recursive) {\r
+       public Boolean checkout(String repositoryUrl, File destDir,\r
+                       Boolean recursive) {\r
                try {\r
-                       manager.getUpdateClient().doCheckout(\r
+                       SVNRevision previousRevision = null;\r
+                       if (destDir.exists() && SVNWCUtil.isVersionedDirectory(destDir)) {\r
+                               previousRevision = manager.getWCClient().doInfo(destDir, null)\r
+                                               .getRevision();\r
+                       }\r
+                       if (previousRevision == null && log.isDebugEnabled())\r
+                               log.debug("Checking out " + repositoryUrl + " to " + destDir\r
+                                               + "...");\r
+                       long revision = manager.getUpdateClient().doCheckout(\r
                                        SVNURL.parseURIDecoded(repositoryUrl), destDir,\r
-                                       SVNRevision.UNDEFINED, SVNRevision.HEAD, recursive);\r
+                                       SVNRevision.UNDEFINED, SVNRevision.HEAD, SVNDepth.INFINITY,\r
+                                       recursive);\r
+                       if (previousRevision != null\r
+                                       && previousRevision.getNumber() == revision) {\r
+                               if (log.isTraceEnabled())\r
+                                       log.trace(destDir + " already at revision " + revision);\r
+                               return false;\r
+\r
+                       } else {\r
+                               if (log.isDebugEnabled())\r
+                                       if (previousRevision != null)\r
+                                               log.debug(destDir + " updated to revision " + revision\r
+                                                               + " from " + previousRevision.getNumber());\r
+                                       else\r
+                                               log.debug(destDir + " checked out to revision "\r
+                                                               + revision);\r
+                               return true;\r
+                       }\r
                } catch (Exception e) {\r
                        throw new SlcException("Cannot checkout " + repositoryUrl + " to "\r
                                        + destDir, e);\r