Skip to content

Commit 4f963ad

Browse files
hajoeichlerpnyheim
authored andcommitted
cover more edge cases: - if there is already some git information, but there was no actual checkout before - if last build data is not set - if the list of all revisions can not be received - if the revision of the last build could not received - if the revision can not be found in the list of all revisions - must be branch switch.
1 parent b55df6b commit 4f963ad

1 file changed

Lines changed: 26 additions & 15 deletions

File tree

src/main/java/hudson/plugins/git/util/DefaultBuildChooser.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
import hudson.plugins.git.IGitAPI;
1010
import hudson.plugins.git.Revision;
1111
import org.kohsuke.stapler.DataBoundConstructor;
12-
import org.eclipse.jgit.lib.ObjectId;
12+
import org.spearce.jgit.lib.ObjectId;
1313

1414
import java.io.IOException;
1515
import java.text.MessageFormat;
16+
import java.util.ArrayList;
1617
import java.util.Collection;
1718
import java.util.Collections;
1819
import java.util.Iterator;
20+
import java.util.List;
1921
import java.util.Set;
2022

2123
import 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

Comments
 (0)