+/*\r
+ * Copyright (C) 2007-2012 Argeo GmbH\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package org.argeo.slc.lib.svn;\r
+\r
+import java.io.File;\r
+import java.io.OutputStream;\r
+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
+import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;\r
+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
+ manager = SVNClientManager.newInstance();\r
+ }\r
+\r
+ @SuppressWarnings("deprecation")\r
+ public void updateToHead(File fileOrDir) {\r
+ try {\r
+ manager.getUpdateClient().doUpdate(fileOrDir, SVNRevision.HEAD,\r
+ true);\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot update " + fileOrDir, e);\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("deprecation")\r
+ public void importFileOrDir(String repositoryUrl, File fileOrDir) {\r
+ try {\r
+ manager.getCommitClient().doImport(fileOrDir,\r
+ SVNURL.parseURIDecoded(repositoryUrl),\r
+ "Import " + fileOrDir, true);\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot import " + repositoryUrl + " to "\r
+ + fileOrDir, e);\r
+ }\r
+ }\r
+\r
+ public Boolean checkout(String repositoryUrl, File destDir,\r
+ Boolean recursive) {\r
+ try {\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, 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
+ }\r
+ }\r
+\r
+ public void getFileFromRepository(String repositoryBaseUrl,\r
+ String location, OutputStream out) {\r
+ try {\r
+ SVNURL url = SVNURL.parseURIDecoded(repositoryBaseUrl);\r
+ SVNRepository repo = manager.createRepository(url, true);\r
+ repo.getFile(location, -1, null, out);\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot retrieve file " + location\r
+ + " from " + repositoryBaseUrl, e);\r
+ }\r
+\r
+ }\r
+\r
+ public String getRelativePath(String repositoryUrl) {\r
+ try {\r
+ SVNURL url = SVNURL.parseURIDecoded(repositoryUrl);\r
+ SVNRepository repo = manager.createRepository(url, true);\r
+ return repo.getRepositoryPath("");\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot get relative path for "\r
+ + repositoryUrl, e);\r
+ }\r
+ }\r
+\r
+ public String getRepositoryRoot(String repositoryUrl) {\r
+ try {\r
+ SVNURL url = SVNURL.parseURIDecoded(repositoryUrl);\r
+ SVNRepository repo = manager.createRepository(url, true);\r
+ return repo.getRepositoryRoot(true).toDecodedString();\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot get repository root for "\r
+ + repositoryUrl, e);\r
+ }\r
+ }\r
+\r
+ public List<String> getChangedPaths(File repositoryRoot, Long revision) {\r
+ try {\r
+ final List<String> paths = new Vector<String>();\r
+ ISVNChangeEntryHandler handler = new ISVNChangeEntryHandler() {\r
+ public void handleEntry(SVNChangeEntry entry)\r
+ throws SVNException {\r
+ paths.add(entry.getPath());\r
+ }\r
+ };\r
+ manager.getLookClient().doGetChanged(repositoryRoot,\r
+ SVNRevision.create(revision), handler, false);\r
+ return paths;\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot get changed paths at "\r
+ + repositoryRoot + " for revision " + revision, e);\r
+ }\r
+ }\r
+\r
+ public void createRepository(String filePath) {\r
+ try {\r
+ manager.getAdminClient().doCreateRepository(new File(filePath),\r
+ null, false, false);\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot create repository " + filePath, e);\r
+ }\r
+ }\r
+\r
+ @SuppressWarnings("deprecation")\r
+ public void commit(File fileOrDir, String commitMessage) {\r
+ try {\r
+ manager.getCommitClient().doCommit(new File[] { fileOrDir }, true,\r
+ commitMessage, false, true);\r
+ } catch (Exception e) {\r
+ throw new SlcException("Cannot commit " + fileOrDir, e);\r
+ }\r
+ }\r
+\r
+}\r