2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.lib
.svn
;
19 import java
.io
.OutputStream
;
20 import java
.util
.List
;
21 import java
.util
.Vector
;
23 import org
.apache
.commons
.logging
.Log
;
24 import org
.apache
.commons
.logging
.LogFactory
;
25 import org
.argeo
.slc
.SlcException
;
26 import org
.argeo
.slc
.deploy
.VersioningDriver
;
27 import org
.tmatesoft
.svn
.core
.SVNDepth
;
28 import org
.tmatesoft
.svn
.core
.SVNException
;
29 import org
.tmatesoft
.svn
.core
.SVNURL
;
30 import org
.tmatesoft
.svn
.core
.internal
.io
.dav
.DAVRepositoryFactory
;
31 import org
.tmatesoft
.svn
.core
.internal
.io
.fs
.FSRepositoryFactory
;
32 import org
.tmatesoft
.svn
.core
.io
.SVNRepository
;
33 import org
.tmatesoft
.svn
.core
.wc
.SVNClientManager
;
34 import org
.tmatesoft
.svn
.core
.wc
.SVNRevision
;
35 import org
.tmatesoft
.svn
.core
.wc
.SVNWCUtil
;
36 import org
.tmatesoft
.svn
.core
.wc
.admin
.ISVNChangeEntryHandler
;
37 import org
.tmatesoft
.svn
.core
.wc
.admin
.SVNChangeEntry
;
39 /** Versioning driver with a Subversion backen, based on SVNKit */
40 public class SvnKitDriver
implements VersioningDriver
{
41 private final static Log log
= LogFactory
.getLog(SvnKitDriver
.class);
43 private final SVNClientManager manager
;
45 public SvnKitDriver() {
46 DAVRepositoryFactory
.setup();
47 FSRepositoryFactory
.setup();
48 manager
= SVNClientManager
.newInstance();
51 @SuppressWarnings("deprecation")
52 public void updateToHead(File fileOrDir
) {
54 manager
.getUpdateClient().doUpdate(fileOrDir
, SVNRevision
.HEAD
,
56 } catch (Exception e
) {
57 throw new SlcException("Cannot update " + fileOrDir
, e
);
61 @SuppressWarnings("deprecation")
62 public void importFileOrDir(String repositoryUrl
, File fileOrDir
) {
64 manager
.getCommitClient().doImport(fileOrDir
,
65 SVNURL
.parseURIDecoded(repositoryUrl
),
66 "Import " + fileOrDir
, true);
67 } catch (Exception e
) {
68 throw new SlcException("Cannot import " + repositoryUrl
+ " to "
73 public Boolean
checkout(String repositoryUrl
, File destDir
,
76 SVNRevision previousRevision
= null;
77 if (destDir
.exists() && SVNWCUtil
.isVersionedDirectory(destDir
)) {
78 previousRevision
= manager
.getWCClient().doInfo(destDir
, null)
81 if (previousRevision
== null && log
.isDebugEnabled())
82 log
.debug("Checking out " + repositoryUrl
+ " to " + destDir
84 long revision
= manager
.getUpdateClient().doCheckout(
85 SVNURL
.parseURIDecoded(repositoryUrl
), destDir
,
86 SVNRevision
.UNDEFINED
, SVNRevision
.HEAD
, SVNDepth
.INFINITY
,
88 if (previousRevision
!= null
89 && previousRevision
.getNumber() == revision
) {
90 if (log
.isTraceEnabled())
91 log
.trace(destDir
+ " already at revision " + revision
);
95 if (log
.isDebugEnabled())
96 if (previousRevision
!= null)
97 log
.debug(destDir
+ " updated to revision " + revision
98 + " from " + previousRevision
.getNumber());
100 log
.debug(destDir
+ " checked out to revision "
104 } catch (Exception e
) {
105 throw new SlcException("Cannot checkout " + repositoryUrl
+ " to "
110 public void getFileFromRepository(String repositoryBaseUrl
,
111 String location
, OutputStream out
) {
113 SVNURL url
= SVNURL
.parseURIDecoded(repositoryBaseUrl
);
114 SVNRepository repo
= manager
.createRepository(url
, true);
115 repo
.getFile(location
, -1, null, out
);
116 } catch (Exception e
) {
117 throw new SlcException("Cannot retrieve file " + location
118 + " from " + repositoryBaseUrl
, e
);
123 public String
getRelativePath(String repositoryUrl
) {
125 SVNURL url
= SVNURL
.parseURIDecoded(repositoryUrl
);
126 SVNRepository repo
= manager
.createRepository(url
, true);
127 return repo
.getRepositoryPath("");
128 } catch (Exception e
) {
129 throw new SlcException("Cannot get relative path for "
134 public String
getRepositoryRoot(String repositoryUrl
) {
136 SVNURL url
= SVNURL
.parseURIDecoded(repositoryUrl
);
137 SVNRepository repo
= manager
.createRepository(url
, true);
138 return repo
.getRepositoryRoot(true).toDecodedString();
139 } catch (Exception e
) {
140 throw new SlcException("Cannot get repository root for "
145 public List
<String
> getChangedPaths(File repositoryRoot
, Long revision
) {
147 final List
<String
> paths
= new Vector
<String
>();
148 ISVNChangeEntryHandler handler
= new ISVNChangeEntryHandler() {
149 public void handleEntry(SVNChangeEntry entry
)
150 throws SVNException
{
151 paths
.add(entry
.getPath());
154 manager
.getLookClient().doGetChanged(repositoryRoot
,
155 SVNRevision
.create(revision
), handler
, false);
157 } catch (Exception e
) {
158 throw new SlcException("Cannot get changed paths at "
159 + repositoryRoot
+ " for revision " + revision
, e
);
163 public void createRepository(String filePath
) {
165 manager
.getAdminClient().doCreateRepository(new File(filePath
),
167 } catch (Exception e
) {
168 throw new SlcException("Cannot create repository " + filePath
, e
);
172 @SuppressWarnings("deprecation")
173 public void commit(File fileOrDir
, String commitMessage
) {
175 manager
.getCommitClient().doCommit(new File
[] { fileOrDir
}, true,
176 commitMessage
, false, true);
177 } catch (Exception e
) {
178 throw new SlcException("Cannot commit " + fileOrDir
, e
);