11package hudson .plugins .git ;
22
33import static hudson .Util .fixEmptyAndTrim ;
4-
54import hudson .AbortException ;
65import hudson .EnvVars ;
76import hudson .Extension ;
1110import hudson .Util ;
1211import hudson .matrix .MatrixRun ;
1312import hudson .matrix .MatrixBuild ;
14- import hudson .model .Action ;
1513import hudson .model .BuildListener ;
1614import hudson .model .Descriptor .FormException ;
1715import hudson .model .Items ;
3028import hudson .plugins .git .util .Build ;
3129import hudson .plugins .git .util .BuildChooser ;
3230import hudson .plugins .git .util .BuildChooserDescriptor ;
33- import hudson .plugins .git .util .BuildData ;
3431import hudson .plugins .git .util .DefaultBuildChooser ;
3532import hudson .plugins .git .util .GitUtils ;
33+ import hudson .plugins .git .util .BuildData ;
3634import hudson .remoting .VirtualChannel ;
3735import hudson .scm .ChangeLogParser ;
3836import hudson .scm .PollingResult ;
3937import hudson .scm .SCMDescriptor ;
4038import hudson .scm .SCMRevisionState ;
4139import hudson .scm .SCM ;
4240import hudson .util .FormValidation ;
41+ import hudson .util .IOUtils ;
4342
4443import java .io .File ;
4544import java .io .FileOutputStream ;
6160
6261import javax .servlet .ServletException ;
6362
64- import hudson .util .IOUtils ;
6563import net .sf .json .JSONObject ;
6664
6765import org .eclipse .jgit .lib .Config ;
@@ -119,6 +117,8 @@ public class GitSCM extends SCM implements Serializable {
119117 private boolean clean ;
120118 private boolean wipeOutWorkspace ;
121119 private boolean pruneBranches ;
120+ private boolean remotePoll ;
121+
122122 /**
123123 * @deprecated
124124 * Replaced by {@link #buildChooser} instead.
@@ -166,7 +166,7 @@ public GitSCM(String repositoryUrl) {
166166 null ,
167167 false , Collections .<SubmoduleConfig >emptyList (), false ,
168168 false , new DefaultBuildChooser (), null , null , false , null ,
169- null , null , null , false , false , null , null , false );
169+ null , null , null , false , false , false , null , null , false );
170170 }
171171
172172 @ DataBoundConstructor
@@ -188,6 +188,7 @@ public GitSCM(
188188 String localBranch ,
189189 boolean recursiveSubmodules ,
190190 boolean pruneBranches ,
191+ boolean remotePoll ,
191192 String gitConfigName ,
192193 String gitConfigEmail ,
193194 boolean skipTag ) {
@@ -235,6 +236,19 @@ public GitSCM(
235236 this .excludedUsers = excludedUsers ;
236237 this .recursiveSubmodules = recursiveSubmodules ;
237238 this .pruneBranches = pruneBranches ;
239+ if (remotePoll
240+ && (branches .size () != 1
241+ || branches .get (0 ).getName ().contains ("*" )
242+ || repo .size () != 1
243+ || (excludedRegions != null && excludedRegions .length () > 0 )
244+ || (submoduleCfg .size () != 0 )
245+ || (excludedUsers != null && excludedUsers .length () > 0 ))) {
246+ LOGGER .log (Level .WARNING , "Cannot poll remotely with current configuration." );
247+ this .remotePoll = false ;
248+ } else {
249+ this .remotePoll = remotePoll ;
250+ }
251+
238252 this .gitConfigName = gitConfigName ;
239253 this .gitConfigEmail = gitConfigEmail ;
240254 this .skipTag = skipTag ;
@@ -450,6 +464,10 @@ public boolean getPruneBranches() {
450464 return this .pruneBranches ;
451465 }
452466
467+ public boolean getRemotePoll () {
468+ return this .remotePoll ;
469+ }
470+
453471 public boolean getWipeOutWorkspace () {
454472 return this .wipeOutWorkspace ;
455473 }
@@ -560,6 +578,13 @@ public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> abstractBuild
560578 return SCMRevisionState .NONE ;
561579 }
562580
581+ @ Override
582+ public boolean requiresWorkspaceForPolling () {
583+ if (remotePoll )
584+ return false ;
585+ return true ;
586+ }
587+
563588 @ Override
564589 protected PollingResult compareRemoteRevisionWith (AbstractProject <?, ?> project , Launcher launcher , FilePath workspace , final TaskListener listener , SCMRevisionState baseline ) throws IOException , InterruptedException {
565590 // Poll for changes. Are there any unbuilt revisions that Hudson ought to build ?
@@ -582,6 +607,30 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,
582607 listener .getLogger ().println ("[poll] Last Built Revision: " + buildData .lastBuild .revision );
583608 }
584609
610+ final String singleBranch = getSingleBranch (lastBuild );
611+
612+ if (singleBranch != null && this .remotePoll ) {
613+ String gitExe = "" ;
614+ GitTool [] installations = ((hudson .plugins .git .GitTool .DescriptorImpl )Hudson .getInstance ().getDescriptorByType (GitTool .DescriptorImpl .class )).getInstallations ();
615+ for (GitTool i : installations ) {
616+ if (i .getName ().equals (gitTool )) {
617+ gitExe = i .getGitExe ();
618+ break ;
619+ }
620+ }
621+ final EnvVars environment = GitUtils .getPollEnvironment (project , workspace , launcher , listener );
622+ IGitAPI git = new GitAPI (gitExe , workspace , listener , environment );
623+ String gitRepo = getParamExpandedRepos (lastBuild ).get (0 ).getURIs ().get (0 ).toString ();
624+ String headRevision = git .getHeadRev (gitRepo , getBranches ().get (0 ).getName ());
625+
626+ if (buildData .lastBuild .getRevision ().getSha1String ().equals (headRevision )) {
627+ return PollingResult .NO_CHANGES ;
628+ } else {
629+ return PollingResult .BUILD_NOW ;
630+ }
631+
632+ }
633+
585634 final String gitExe ;
586635 {
587636 //If this project is tied onto a node, it's built always there. On other cases,
@@ -611,7 +660,7 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project,
611660
612661 final EnvVars environment = GitUtils .getPollEnvironment (project , workspace , launcher , listener );
613662 final List <RemoteConfig > paramRepos = getParamExpandedRepos (lastBuild );
614- final String singleBranch = getSingleBranch (lastBuild );
663+ // final String singleBranch = getSingleBranch(lastBuild);
615664
616665 boolean pollChangesResult = workingDirectory .act (new FileCallable <Boolean >() {
617666
@@ -1581,7 +1630,7 @@ public BuildData getBuildData(Run build, boolean clone) {
15811630 /**
15821631 * Given the workspace, gets the working directory, which will be the workspace
15831632 * if no relative target dir is specified. Otherwise, it'll be "workspace/relativeTargetDir".
1584- *
1633+ *
15851634 * @param workspace
15861635 * @return working directory
15871636 */
0 commit comments