99import hudson .plugins .git .IGitAPI ;
1010import hudson .plugins .git .Revision ;
1111import org .kohsuke .stapler .DataBoundConstructor ;
12- import org .eclipse .jgit .lib .ObjectId ;
12+ import org .spearce .jgit .lib .ObjectId ;
1313
1414import java .io .IOException ;
1515import java .text .MessageFormat ;
16+ import java .util .ArrayList ;
1617import java .util .Collection ;
1718import java .util .Collections ;
1819import java .util .Iterator ;
20+ import java .util .List ;
1921import java .util .Set ;
2022
2123import static java .util .Collections .emptyList ;
@@ -92,23 +94,26 @@ public Collection<Revision> getCandidateRevisions(boolean isPollCall, String sin
9294 verbose (listener , "Found a new commit {0} to be built on {1}" , sha1 , singleBranch );
9395
9496 // calculate the revisions that are new compared to the last build
95- List <ObjectId > allRevs = git .revListAll (); // index 0 contains the newest revision
96- Revision lastBuiltRev = data .getLastBuiltRevision ();
97- int indexOfLastBuildRev = allRevs .indexOf (lastBuiltRev .getSha1 ());
98- List <ObjectId > newRevisionsSinceLastBuild = allRevs .subList (0 , indexOfLastBuildRev );
99-
100- // translate list of ObjectIds into list of Revisions
10197 List <Revision > candidateRevs = new ArrayList <Revision >();
102- for (ObjectId objectId : newRevisionsSinceLastBuild ) {
103- Revision revision = new Revision (objectId );
104- revision .getBranches ().add (new Branch (singleBranch , sha1 ));
105- candidateRevs .add (revision );
98+ List <ObjectId > allRevs = git .revListAll (); // index 0 contains the newest revision
99+ if (data != null && allRevs != null ) {
100+ Revision lastBuiltRev = data .getLastBuiltRevision ();
101+ if (lastBuiltRev == null ) {
102+ return Collections .singletonList (objectId2Revision (singleBranch , sha1 ));
103+ }
104+ int indexOfLastBuildRev = allRevs .indexOf (lastBuiltRev .getSha1 ());
105+ if (indexOfLastBuildRev == -1 ) {
106+ // mhmmm ... can happen when branches are switched.
107+ return Collections .singletonList (objectId2Revision (singleBranch , sha1 ));
108+ }
109+ List <ObjectId > newRevisionsSinceLastBuild = allRevs .subList (0 , indexOfLastBuildRev );
110+ // translate list of ObjectIds into list of Revisions
111+ for (ObjectId objectId : newRevisionsSinceLastBuild ) {
112+ candidateRevs .add (objectId2Revision (singleBranch , objectId ));
113+ }
106114 }
107- // fallback to return given sha1 if the list of candidate revisions is empty
108115 if (candidateRevs .isEmpty ()) {
109- Revision revision = new Revision (sha1 );
110- revision .getBranches ().add (new Branch (singleBranch , sha1 ));
111- return Collections .singletonList (revision );
116+ return Collections .singletonList (objectId2Revision (singleBranch , sha1 ));
112117 }
113118 return candidateRevs ;
114119 } catch (GitException e ) {
@@ -118,6 +123,12 @@ public Collection<Revision> getCandidateRevisions(boolean isPollCall, String sin
118123 }
119124 }
120125
126+ private Revision objectId2Revision (String singleBranch , ObjectId sha1 ) {
127+ Revision revision = new Revision (sha1 );
128+ revision .getBranches ().add (new Branch (singleBranch , sha1 ));
129+ return revision ;
130+ }
131+
121132 /**
122133 * In order to determine which Revisions to build.
123134 *
0 commit comments