1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scm.svn;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNCopyClient;
import org.tmatesoft.svn.core.wc.SVNCopySource;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc2.SvnList;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
import org.tmatesoft.svn.core.wc2.SvnRemoteMkDir;
import org.tmatesoft.svn.core.wc2.SvnTarget;
public class SvnTester {
private final SVNClientManager manager = SVNClientManager.newInstance(new SvnOperationFactory());
private final SVNURL localRepository;
public SvnTester(Path root) throws SVNException, IOException {
localRepository = SVNRepositoryFactory.createLocalRepository(root.toFile(), false, false);
mkdir("trunk");
mkdir("branches");
}
private void mkdir(String relpath) throws IOException, SVNException {
SvnRemoteMkDir remoteMkDir = manager.getOperationFactory().createRemoteMkDir();
remoteMkDir.addTarget(SvnTarget.fromURL(localRepository.appendPath(relpath, false)));
remoteMkDir.run();
}
public void createBranch(String branchName) throws IOException, SVNException {
SVNCopyClient copyClient = manager.getCopyClient();
SVNCopySource source = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, localRepository.appendPath("trunk", false));
copyClient.doCopy(new SVNCopySource[] {source}, localRepository.appendPath("branches/" + branchName, false), false, false, true, "Create branch", null);
}
public void checkout(Path worktree, String path) throws SVNException {
SVNUpdateClient updateClient = manager.getUpdateClient();
updateClient.doCheckout(localRepository.appendPath(path, false),
worktree.toFile(), null, null, SVNDepth.INFINITY, false);
}
public void add(Path worktree, String filename) throws SVNException {
manager.getWCClient().doAdd(worktree.resolve(filename).toFile(), true, false, false, SVNDepth.INFINITY, false, false, true);
}
public void copy(Path worktree, String src, String dst) throws SVNException {
SVNCopyClient copyClient = manager.getCopyClient();
SVNCopySource source = new SVNCopySource(SVNRevision.HEAD, SVNRevision.HEAD, worktree.resolve(src).toFile());
copyClient.doCopy(new SVNCopySource[]{source}, worktree.resolve(dst).toFile(), false, false, true);
}
public void commit(Path worktree) throws SVNException {
manager.getCommitClient().doCommit(new File[] {worktree.toFile()}, false, "commit " + worktree, null, null, false, false, SVNDepth.INFINITY);
}
public void update(Path worktree) throws SVNException {
manager.getUpdateClient().doUpdate(new File[] {worktree.toFile()}, SVNRevision.HEAD, SVNDepth.INFINITY, false, false);
}
public Collection<String> list(String... paths) throws SVNException {
Set<String> results = new HashSet<>();
SvnList list = manager.getOperationFactory().createList();
if (paths.length == 0) {
list.addTarget(SvnTarget.fromURL(localRepository));
} else {
for (String path : paths) {
list.addTarget(SvnTarget.fromURL(localRepository.appendPath(path, false)));
}
}
list.setDepth(SVNDepth.INFINITY);
list.setReceiver((svnTarget, svnDirEntry) -> {
String path = svnDirEntry.getRelativePath();
if (!path.isEmpty()) {
results.add(path);
}
});
list.run();
return results;
}
public void createFile(Path worktree, String filename, String content) throws IOException {
Files.write(worktree.resolve(filename), content.getBytes());
}
public void createFile(Path worktree, String filename) throws IOException {
createFile(worktree, filename, filename + "\n");
}
public void appendToFile(Path worktree, String filename) throws IOException {
Files.write(worktree.resolve(filename), (filename + "\n").getBytes(), StandardOpenOption.APPEND);
}
public void deleteFile(Path worktree, String filename) throws SVNException {
manager.getWCClient().doDelete(worktree.resolve(filename).toFile(), false, false);
}
}
|