/
SubversionChangeLogBuilder.java
246 lines (218 loc) · 9.46 KB
/
SubversionChangeLogBuilder.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Daniel Dyer, Jean-Baptiste Quenot, Luca Domenico Milanesio, Renaud Bruyeron
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.scm;
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Hudson;
import hudson.scm.SubversionSCM.ModuleLocation;
import hudson.FilePath;
import hudson.util.IOException2;
import hudson.remoting.VirtualChannel;
import hudson.FilePath.FileCallable;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationProvider;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNLogClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNWCClient;
import org.tmatesoft.svn.core.wc.SVNInfo;
import org.tmatesoft.svn.core.wc.xml.SVNXMLLogHandler;
import org.xml.sax.helpers.LocatorImpl;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import java.io.IOException;
import java.io.PrintStream;
import java.io.File;
import java.util.Map;
import java.util.Collection;
/**
* Builds <tt>changelog.xml</tt> for {@link SubversionSCM}.
*
* @author Kohsuke Kawaguchi
*/
public final class SubversionChangeLogBuilder {
/**
* Revisions of the workspace before the update/checkout.
*/
private final Map<String,Long> previousRevisions;
/**
* Revisions of the workspace after the update/checkout.
*/
private final Map<String,Long> thisRevisions;
private final BuildListener listener;
private final SubversionSCM scm;
private final AbstractBuild<?,?> build;
private final EnvVars env;
/**
* @deprecated 1.34
*/
public SubversionChangeLogBuilder(AbstractBuild<?,?> build, BuildListener listener, SubversionSCM scm) throws IOException {
this(build, null,listener, scm);
}
/**
* @since 1.34
*/
public SubversionChangeLogBuilder(AbstractBuild<?,?> build, EnvVars env, BuildListener listener, SubversionSCM scm) throws IOException {
previousRevisions = SubversionSCM.parseRevisionFile(build.getPreviousBuild());
thisRevisions = SubversionSCM.parseRevisionFile(build);
this.listener = listener;
this.scm = scm;
this.build = build;
this.env = env;
}
public boolean run(Collection<SubversionSCM.External> externals, Result changeLog) throws IOException, InterruptedException {
boolean changelogFileCreated = false;
final SVNClientManager manager = SubversionSCM.createSvnClientManager(build.getProject());
try {
SVNLogClient svnlc = manager.getLogClient();
TransformerHandler th = createTransformerHandler();
th.setResult(changeLog);
SVNLogFilter logFilter = scm.isFilterChangelog()? scm.createSVNLogFilter() : new NullSVNLogFilter();
SVNXMLLogHandler logHandler = new DirAwareSVNXMLLogHandler(th, logFilter);
// work around for http://svnkit.com/tracker/view.php?id=175
th.setDocumentLocator(DUMMY_LOCATOR);
logHandler.startDocument();
for (ModuleLocation l : scm.getLocations(env, build)) {
changelogFileCreated |= buildModule(l.getURL(), svnlc, logHandler);
}
for(SubversionSCM.External ext : externals) {
changelogFileCreated |= buildModule(
getUrlForPath(build.getWorkspace().child(ext.path)), svnlc, logHandler);
}
if(changelogFileCreated) {
logHandler.endDocument();
}
return changelogFileCreated;
} finally {
manager.dispose();
}
}
private String getUrlForPath(FilePath path) throws IOException, InterruptedException {
return path.act(new GetUrlForPath(createAuthenticationProvider(build.getProject())));
}
private ISVNAuthenticationProvider createAuthenticationProvider(AbstractProject context) {
return Hudson.getInstance().getDescriptorByType(SubversionSCM.DescriptorImpl.class).createAuthenticationProvider(context);
}
private boolean buildModule(String url, SVNLogClient svnlc, SVNXMLLogHandler logHandler) throws IOException2 {
PrintStream logger = listener.getLogger();
Long prevRev = previousRevisions.get(url);
if(prevRev==null) {
logger.println("no revision recorded for "+url+" in the previous build");
return false;
}
Long thisRev = thisRevisions.get(url);
if (thisRev == null) {
listener.error("No revision found for URL: " + url + " in " + SubversionSCM.getRevisionFile(build) + ". Revision file contains: " + thisRevisions.keySet());
return false;
}
if(thisRev.equals(prevRev)) {
logger.println("no change for "+url+" since the previous build");
return false;
}
// handle case where previous workspace revision is newer than this revision
if (prevRev.compareTo(thisRev) > 0) {
long temp = thisRev.longValue();
thisRev = new Long(prevRev.longValue());
prevRev = new Long(temp);
}
try {
if(debug)
listener.getLogger().printf("Computing changelog of %1s from %2s to %3s\n",
SVNURL.parseURIEncoded(url), prevRev+1, thisRev);
svnlc.doLog(SVNURL.parseURIEncoded(url),
null,
SVNRevision.UNDEFINED,
SVNRevision.create(prevRev+1),
SVNRevision.create(thisRev),
false, // Don't stop on copy.
true, // Report paths.
0, // Retrieve log entries for unlimited number of revisions.
debug ? new DebugSVNLogHandler(logHandler) : logHandler);
if(debug)
listener.getLogger().println("done");
} catch (SVNException e) {
throw new IOException2("revision check failed on "+url,e);
}
return true;
}
/**
* Filter {@link ISVNLogEntryHandler} that dumps information. Used only for debugging.
*/
private class DebugSVNLogHandler implements ISVNLogEntryHandler {
private final ISVNLogEntryHandler core;
private DebugSVNLogHandler(ISVNLogEntryHandler core) {
this.core = core;
}
public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
listener.getLogger().println("SVNLogEntry="+logEntry);
core.handleLogEntry(logEntry);
}
}
/**
* Creates an identity transformer.
*/
private static TransformerHandler createTransformerHandler() {
try {
return ((SAXTransformerFactory) SAXTransformerFactory.newInstance()).newTransformerHandler();
} catch (TransformerConfigurationException e) {
throw new Error(e); // impossible
}
}
private static final LocatorImpl DUMMY_LOCATOR = new LocatorImpl();
public static boolean debug = false;
static {
DUMMY_LOCATOR.setLineNumber(-1);
DUMMY_LOCATOR.setColumnNumber(-1);
}
private static class GetUrlForPath implements FileCallable<String> {
private final ISVNAuthenticationProvider authProvider;
public GetUrlForPath(ISVNAuthenticationProvider authProvider) {
this.authProvider = authProvider;
}
public String invoke(File p, VirtualChannel channel) throws IOException {
final SvnClientManager manager = SubversionSCM.createClientManager(authProvider);
try {
final SVNWCClient svnwc = manager.getWCClient();
SVNInfo info;
try {
info = svnwc.doInfo(p, SVNRevision.WORKING);
return info.getURL().toDecodedString();
} catch (SVNException e) {
e.printStackTrace();
return null;
}
} finally {
manager.dispose();
}
}
private static final long serialVersionUID = 1L;
}
}