diff --git a/core/src/main/java/hudson/Functions.java b/core/src/main/java/hudson/Functions.java index 28b3674af0..6817667361 100644 --- a/core/src/main/java/hudson/Functions.java +++ b/core/src/main/java/hudson/Functions.java @@ -59,6 +59,8 @@ import hudson.tasks.Builder; import hudson.tasks.Publisher; import hudson.util.Area; import hudson.util.Iterators; +import hudson.scm.SCM; +import hudson.scm.SCMDescriptor; import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken; import org.apache.commons.jelly.JellyContext; import org.apache.commons.jelly.JellyTagException; @@ -610,6 +612,10 @@ public class Functions { return BuildStepDescriptor.filter(Publisher.all(), project.getClass()); } + public static List> getSCMDescriptors(AbstractProject project) { + return SCM._for(project); + } + public static List> getComputerLauncherDescriptors() { return Hudson.getInstance().getDescriptorList(ComputerLauncher.class); } diff --git a/core/src/main/java/hudson/model/AbstractProject.java b/core/src/main/java/hudson/model/AbstractProject.java index 71eecd7747..0e996abc33 100644 --- a/core/src/main/java/hudson/model/AbstractProject.java +++ b/core/src/main/java/hudson/model/AbstractProject.java @@ -1169,7 +1169,7 @@ public abstract class AbstractProject

,R extends A authToken = BuildAuthorizationToken.create(req); - setScm(SCMS.parseSCM(req)); + setScm(SCMS.parseSCM(req,this)); for (Trigger t : triggers) t.stop(); diff --git a/core/src/main/java/hudson/scm/SCM.java b/core/src/main/java/hudson/scm/SCM.java index 1eff6dc37f..cd38c8dd4b 100644 --- a/core/src/main/java/hudson/scm/SCM.java +++ b/core/src/main/java/hudson/scm/SCM.java @@ -39,11 +39,15 @@ import hudson.model.TaskListener; import hudson.model.Node; import hudson.model.WorkspaceCleanupThread; import hudson.model.Hudson; +import hudson.model.Descriptor; +import hudson.model.AbstractProject.AbstractProjectDescriptor; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Map; +import java.util.List; +import java.util.ArrayList; /** * Captures the configuration information in it. @@ -361,4 +365,26 @@ public abstract class SCM implements Describable, ExtensionPoint { public static DescriptorExtensionList> all() { return Hudson.getInstance().getDescriptorList(SCM.class); } + + /** + * Returns the list of {@link SCMDescriptor}s that are applicable to the given project. + */ + public static List> _for(final AbstractProject project) { + if(project==null) return all(); + + final Descriptor pd = Hudson.getInstance().getDescriptor((Class) project.getClass()); + List> r = new ArrayList>(); + for (SCMDescriptor scmDescriptor : all()) { + if(!scmDescriptor.isApplicable(project)) continue; + + if (pd instanceof AbstractProjectDescriptor) { + AbstractProjectDescriptor apd = (AbstractProjectDescriptor) pd; + if(!apd.isApplicable(scmDescriptor)) continue; + } + + r.add(scmDescriptor); + } + + return r; + } } diff --git a/core/src/main/java/hudson/scm/SCMDescriptor.java b/core/src/main/java/hudson/scm/SCMDescriptor.java index ae1c3c681b..9845d54035 100644 --- a/core/src/main/java/hudson/scm/SCMDescriptor.java +++ b/core/src/main/java/hudson/scm/SCMDescriptor.java @@ -25,6 +25,7 @@ package hudson.scm; import hudson.model.Descriptor; import hudson.model.Describable; +import hudson.model.AbstractProject; import java.util.List; import java.util.Collections; @@ -83,6 +84,19 @@ public abstract class SCMDescriptor extends Descriptor { return false; } + /** + * Allows {@link SCMDescriptor}s to choose which projects it wants to be configurable against. + * + *

+ * When this method returns false, this {@link SCM} will not appear in the configuration screen + * for the given project. The default method always return true. + * + * @since 1.294 + */ + public boolean isApplicable(AbstractProject project) { + return true; + } + /** * Returns the list of {@link RepositoryBrowser} {@link Descriptor} * that can be used with this SCM. diff --git a/core/src/main/java/hudson/scm/SCMS.java b/core/src/main/java/hudson/scm/SCMS.java index 005746bc98..aec68f43d8 100644 --- a/core/src/main/java/hudson/scm/SCMS.java +++ b/core/src/main/java/hudson/scm/SCMS.java @@ -25,6 +25,7 @@ package hudson.scm; import hudson.model.Descriptor; import hudson.model.Hudson; +import hudson.model.AbstractProject; import hudson.model.Descriptor.FormException; import hudson.util.DescriptorList; import hudson.DescriptorExtensionList; @@ -51,14 +52,26 @@ public class SCMS { /** * Parses {@link SCM} configuration from the submitted form. + * + * @param target + * The project for which this SCM is configured to. */ - public static SCM parseSCM(StaplerRequest req) throws FormException, ServletException { + public static SCM parseSCM(StaplerRequest req, AbstractProject target) throws FormException, ServletException { String scm = req.getParameter("scm"); if(scm==null) return new NullSCM(); int scmidx = Integer.parseInt(scm); - SCMDescriptor d = SCM.all().get(scmidx); + SCMDescriptor d = SCM._for(target).get(scmidx); d.generation++; return d.newInstance(req, req.getSubmittedForm().getJSONObject("scm")); } + + /** + * @deprecated as of 1.294 + * Use {@link #parseSCM(StaplerRequest, AbstractProject)} and pass in the caller's project type. + */ + public static SCM parseSCM(StaplerRequest req) throws FormException, ServletException { + return parseSCM(req,null); + } + } diff --git a/core/src/main/resources/lib/hudson/project/config-scm.jelly b/core/src/main/resources/lib/hudson/project/config-scm.jelly index a287927e40..140cb6d2a3 100644 --- a/core/src/main/resources/lib/hudson/project/config-scm.jelly +++ b/core/src/main/resources/lib/hudson/project/config-scm.jelly @@ -25,7 +25,7 @@ THE SOFTWARE. - +