diff --git a/core/src/main/java/hudson/scm/SubversionSCM.java b/core/src/main/java/hudson/scm/SubversionSCM.java index 7e7661ca4b..5e7511b6ab 100644 --- a/core/src/main/java/hudson/scm/SubversionSCM.java +++ b/core/src/main/java/hudson/scm/SubversionSCM.java @@ -32,6 +32,7 @@ import hudson.Launcher; import hudson.Util; import hudson.XmlFile; import hudson.Functions; +import static hudson.Util.fixEmptyAndTrim; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; @@ -64,6 +65,9 @@ import org.tmatesoft.svn.core.SVNException; import org.tmatesoft.svn.core.SVNNodeKind; import org.tmatesoft.svn.core.SVNURL; import org.tmatesoft.svn.core.SVNCancelException; +import org.tmatesoft.svn.core.ISVNLogEntryHandler; +import org.tmatesoft.svn.core.SVNLogEntry; +import org.tmatesoft.svn.core.SVNLogEntryPath; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.auth.ISVNAuthenticationProvider; import org.tmatesoft.svn.core.auth.SVNAuthentication; @@ -88,6 +92,7 @@ import org.tmatesoft.svn.core.wc.SVNRevision; import org.tmatesoft.svn.core.wc.SVNUpdateClient; import org.tmatesoft.svn.core.wc.SVNWCClient; import org.tmatesoft.svn.core.wc.SVNWCUtil; +import org.tmatesoft.svn.core.wc.SVNLogClient; import javax.servlet.ServletException; import javax.xml.transform.stream.StreamResult; @@ -118,6 +123,7 @@ import java.util.StringTokenizer; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import net.sf.json.JSONObject; @@ -154,13 +160,14 @@ public class SubversionSCM extends SCM implements Serializable { private boolean useUpdate; private final SubversionRepositoryBrowser browser; + private String excludedRegions; // No longer in use but left for serialization compatibility. @Deprecated private String modules; public SubversionSCM(String[] remoteLocations, String[] localLocations, - boolean useUpdate, SubversionRepositoryBrowser browser) { + boolean useUpdate, SubversionRepositoryBrowser browser, String excludedRegions) { List modules = new ArrayList(); if (remoteLocations != null && localLocations != null) { @@ -180,13 +187,14 @@ public class SubversionSCM extends SCM implements Serializable { this.useUpdate = useUpdate; this.browser = browser; + this.excludedRegions = excludedRegions; } /** * Convenience constructor, especially during testing. */ public SubversionSCM(String svnUrl) { - this(new String[]{svnUrl},new String[]{null},true,null); + this(new String[]{svnUrl},new String[]{null},true,null,null); } /** @@ -253,6 +261,32 @@ public class SubversionSCM extends SCM implements Serializable { return browser; } + public String getExcludedRegions() { + return excludedRegions; + } + + public String[] getExcludedRegionsNormalized() { + return excludedRegions == null ? null : excludedRegions.split("[\\r\\n]+"); + } + + private Pattern[] getExcludedRegionsPatterns() { + String[] excludedRegions = getExcludedRegionsNormalized(); + if (excludedRegions != null) + { + Pattern[] patterns = new Pattern[excludedRegions.length]; + + int i = 0; + for (String excludedRegion : excludedRegions) + { + patterns[i++] = Pattern.compile(excludedRegion); + } + + return patterns; + } + + return null; + } + /** * Sets the SVN_REVISION environment variable during the build. */ @@ -860,22 +894,105 @@ public class SubversionSCM extends SCM implements Serializable { if(ext.url.equals(url) && ext.isRevisionFixed()) continue OUTER; - try { - SvnInfo remoteInfo = new SvnInfo(parseSvnInfo(SVNURL.parseURIDecoded(url),authProvider)); - listener.getLogger().println(Messages.SubversionSCM_pollChanges_remoteRevisionAt(url,remoteInfo.revision)); - if(remoteInfo.revision > localInfo.getValue()) { - listener.getLogger().println(Messages.SubversionSCM_pollChanges_changedFrom(localInfo.getValue())); - return true; // change found - } - } catch (SVNException e) { - e.printStackTrace(listener.error("Failed to check repository revision for "+ url)); - return false; - } + boolean changesFound = false; + try { + final SVNURL decodedURL = SVNURL.parseURIDecoded(url); + SvnInfo remoteInfo = new SvnInfo(parseSvnInfo(decodedURL,authProvider)); + listener.getLogger().println(Messages.SubversionSCM_pollChanges_remoteRevisionAt(url,remoteInfo.revision)); + if(remoteInfo.revision > localInfo.getValue()) { + Pattern[] excludedPatterns = getExcludedRegionsPatterns(); + if (excludedPatterns == null) { + changesFound = true; // change found + } else { + try { + SVNLogHandler handler = new SVNLogHandler(excludedPatterns); + final SVNClientManager manager = createSvnClientManager(authProvider); + try { + final SVNLogClient svnlc = manager.getLogClient(); + svnlc.doLog(decodedURL, null, SVNRevision.UNDEFINED, + SVNRevision.create(localInfo.getValue()+1), // get log entries from the local revision + 1 + SVNRevision.create(remoteInfo.revision), // to the remote revision + false, // Don't stop on copy. + true, // Report paths. + 0, // Retrieve log entries for unlimited number of revisions. + handler); + } finally { + manager.dispose(); + } + + changesFound = handler.isChangesFound(); + } catch (SVNException e) { + e.printStackTrace(listener.error("Failed to check repository revision for "+ url)); + changesFound = false; + } + } + + if (changesFound) { + listener.getLogger().println(Messages.SubversionSCM_pollChanges_changedFrom(localInfo.getValue())); + } + } + } catch (SVNException e) { + e.printStackTrace(listener.error("Failed to check repository revision for "+ url)); + changesFound = false; + } + + return changesFound; } return false; // no change } + private final class SVNLogHandler implements ISVNLogEntryHandler { + private boolean changesFound = false; + + private Pattern[] excludedPatterns; + + private SVNLogHandler(Pattern[] excludedPatterns) { + this.excludedPatterns = excludedPatterns; + } + + public boolean isChangesFound() { + return changesFound; + } + + /** + * Handles a log entry passed. + * Check for paths that should be excluded from triggering a build. + * If a path is not a path that should be excluded, set changesFound to true + * + * @param logEntry an {@link org.tmatesoft.svn.core.SVNLogEntry} object + * that represents per revision information + * (committed paths, log message, etc.) + * @throws org.tmatesoft.svn.core.SVNException + */ + public void handleLogEntry(SVNLogEntry logEntry) throws SVNException { + if (excludedPatterns != null && excludedPatterns.length > 0) { + Map changedPaths = logEntry.getChangedPaths(); + for (Object paths : changedPaths.values()) { + SVNLogEntryPath logEntryPath = (SVNLogEntryPath) paths; + String path = logEntryPath.getPath(); + + boolean patternMatched = false; + + for (Pattern pattern : excludedPatterns) { + if (pattern.matcher(path).matches()) { + patternMatched = true; + break; + } + } + + if (!patternMatched) { + changesFound = true; + break; + } + } + } else if (!logEntry.getChangedPaths().isEmpty()) { + // no excluded patterns and paths have changed, so just return true + changesFound = true; + } + } + } + public ChangeLogParser createChangeLogParser() { return new SubversionChangeLogParser(); } @@ -1137,7 +1254,8 @@ public class SubversionSCM extends SCM implements Serializable { req.getParameterValues("svn.location_remote"), req.getParameterValues("svn.location_local"), req.getParameter("svn_use_update") != null, - RepositoryBrowsers.createInstance(SubversionRepositoryBrowser.class, req, formData, "browser")); + RepositoryBrowsers.createInstance(SubversionRepositoryBrowser.class, req, formData, "browser"), + req.getParameter("svn.excludedRegions")); } /** @@ -1457,6 +1575,30 @@ public class SubversionSCM extends SCM implements Serializable { }.process(); } + /** + * Validates the excludeRegions Regex + */ + public void doExcludeRegionsCheck(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { + new FormFieldValidator(req,rsp,false) { + protected void check() throws IOException, ServletException { + String v = fixEmptyAndTrim(request.getParameter("value")); + + if(v != null) { + String[] regions = v.split("\\r\\n"); + for (String region : regions) { + try { + Pattern.compile(region); + } + catch (PatternSyntaxException e) { + error("Invalid regular expression. " + e.getMessage()); + } + } + } + ok(); + } + }.process(); + } + static { new Initializer(); } diff --git a/core/src/main/resources/hudson/scm/SubversionSCM/config.jelly b/core/src/main/resources/hudson/scm/SubversionSCM/config.jelly index da5b2a4c9a..c8345ccbdf 100644 --- a/core/src/main/resources/hudson/scm/SubversionSCM/config.jelly +++ b/core/src/main/resources/hudson/scm/SubversionSCM/config.jelly @@ -47,5 +47,10 @@ THE SOFTWARE. - + + + + + + \ No newline at end of file diff --git a/war/resources/help/subversion/excludedRegions.html b/war/resources/help/subversion/excludedRegions.html new file mode 100644 index 0000000000..7cfae8125f --- /dev/null +++ b/war/resources/help/subversion/excludedRegions.html @@ -0,0 +1,12 @@ +
+ If set, and Hudson is set to poll for changes, Hudson will ignore any files and/or folders in this list when determining if a build needs to be triggered. +

Each exclusion uses regular expression pattern matching, and must be separated by a new line. +

+

+	 src/main/web/.*\.html
+	 src/main/web/.*\.jpeg
+	 src/main/web/.*\.gif
+  
+ The example above illustrates that if only html/jpeg/gif files have been committed to the SCM a build will not occur. +

More information on regular expressions can be found here. +

\ No newline at end of file