Skip to content

Commit 8c500a5

Browse files
author
Andrew Bayer
committed
Added DiggBuildChooser - which I should probably rename.
1 parent 57ed0a2 commit 8c500a5

File tree

3 files changed

+212
-1
lines changed

3 files changed

+212
-1
lines changed

src/main/java/hudson/plugins/git/GitSCM.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public class GitSCM extends SCM implements Serializable {
8080
private String choosingStrategy = DEFAULT;
8181
public static final String DEFAULT = "Default";
8282
public static final String GERRIT = "Gerrit";
83-
83+
public static final String DIGG = "Digg";
84+
8485
public String gitTool = null;
8586

8687
private GitWeb browser;
@@ -297,6 +298,8 @@ public Boolean invoke(File localWorkspace, VirtualChannel channel) throws IOExce
297298
private IBuildChooser createBuildChooser(IGitAPI git, TaskListener listener, BuildData buildData) {
298299
if(this.choosingStrategy != null && GERRIT.equals(this.choosingStrategy)) {
299300
return new GerritBuildChooser(this,git,new GitUtils(listener,git), buildData);
301+
} else if(this.choosingStrategy != null && DIGG.equals(this.choosingStrategy)) {
302+
return new DiggBuildChooser(this,git,new GitUtils(listener,git), buildData);
300303
} else {
301304
return new BuildChooser(this, git, new GitUtils(listener, git), buildData);
302305
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
package hudson.plugins.git.util;
2+
3+
4+
import hudson.Extension;
5+
import hudson.model.AbstractProject;
6+
import hudson.model.Action;
7+
import hudson.model.Result;
8+
import hudson.plugins.git.*;
9+
import hudson.plugins.git.util.Build;
10+
import hudson.plugins.git.util.BuildData;
11+
import hudson.plugins.git.util.GitUtils;
12+
import hudson.util.DescribableList;
13+
import org.joda.time.DateTime;
14+
import org.spearce.jgit.lib.ObjectId;
15+
16+
import java.io.IOException;
17+
import java.util.*;
18+
19+
public class DiggBuildChooser implements IBuildChooser {
20+
21+
private final String separator = "#";
22+
private IGitAPI git;
23+
private GitUtils utils;
24+
private GitSCM gitSCM;
25+
26+
//-------- Data -----------
27+
private BuildData data;
28+
29+
public DiggBuildChooser() {
30+
this.gitSCM = null;
31+
this.git = null;
32+
this.utils = null;
33+
this.data = null;
34+
35+
}
36+
37+
public DiggBuildChooser(GitSCM gitSCM, IGitAPI git, GitUtils utils, BuildData data)
38+
{
39+
this.gitSCM = gitSCM;
40+
this.git = git;
41+
this.utils = utils;
42+
this.data = data == null ? new BuildData() : data;
43+
}
44+
45+
public void setUtilities(GitSCM gitSCM, IGitAPI git, GitUtils gitUtils) {
46+
this.gitSCM = gitSCM;
47+
this.git = git;
48+
this.utils = gitUtils;
49+
this.data = data == null ? new BuildData() : data;
50+
}
51+
52+
/**
53+
* Determines which Revisions to build.
54+
*
55+
* Uses git log --all to get every commit in repository. Then orders commits by commit time
56+
* and determines what to build next.
57+
*
58+
* Doesn't care about branches.
59+
* @throws IOException
60+
* @throws GitException
61+
*/
62+
@Override
63+
public Collection<Revision> getCandidateRevisions(boolean isPollCall, String singleBranch)
64+
throws GitException, IOException {
65+
66+
Build lastTimeBased = data.getLastBuildOfBranch("timebased");
67+
68+
Revision last = null;
69+
if(lastTimeBased != null) {
70+
last = data.getLastBuildOfBranch("timebased").getRevision();
71+
if(!last.getSha1String().equals(data.getLastBuiltRevision().getSha1String())) {
72+
//previous build wasn't timebased, so consider this as a new start
73+
last = null;
74+
}
75+
}
76+
77+
String result = git.getAllLogEntries(singleBranch);
78+
Collection<TimedCommit> commits = sortRevList(result);
79+
Iterator<TimedCommit> i = commits.iterator();
80+
ArrayList<Revision> revs = new ArrayList<Revision>();
81+
DateTime lastBuilt = null;
82+
83+
TimedCommit first = null;
84+
85+
while(i.hasNext()) {
86+
TimedCommit tc = i.next();
87+
88+
//When encountered last build, break
89+
if(last != null && tc.commit.name().equals(last.getSha1String())) {
90+
break;
91+
}
92+
93+
if (first == null) {
94+
first = tc;
95+
}
96+
else {
97+
// i.e., if first is newer than tc - we want the oldest possible
98+
// commit.
99+
if (first.when.compareTo(tc.when) > 0) {
100+
first = tc;
101+
}
102+
}
103+
}
104+
105+
if (first != null) {
106+
revs.add(getRevFromTimedCommit(first));
107+
}
108+
109+
if(last == null) {
110+
return revs;
111+
}
112+
if(revs.size() == 0 && !isPollCall) {
113+
return Collections.singletonList(last);
114+
}
115+
//reverse order
116+
ArrayList<Revision> finalRevs = new ArrayList<Revision>();
117+
for(int j = revs.size() - 1 ; j >= 0 ; j--) {
118+
finalRevs.add(revs.get(j));
119+
120+
}
121+
return finalRevs;
122+
123+
}
124+
125+
private Revision getRevFromTimedCommit(TimedCommit tc) {
126+
Revision rev = new Revision(tc.commit);
127+
rev.getBranches().add(new Branch("timebased", rev.getSha1()));
128+
return rev;
129+
}
130+
131+
private void addToRevs(ArrayList<Revision> revs, TimedCommit tc) {
132+
revs.add(getRevFromTimedCommit(tc));
133+
}
134+
135+
/* This returns commits that are always in same order.
136+
*
137+
*/
138+
private Collection<TimedCommit> sortRevList(String logOutput) {
139+
SortedSet<TimedCommit> timedCommits = new TreeSet<TimedCommit>();
140+
String[] lines = logOutput.split("\n");
141+
for (String s : lines ) {
142+
timedCommits.add(parseCommit(s));
143+
}
144+
145+
return timedCommits;
146+
}
147+
148+
private TimedCommit parseCommit(String line) {
149+
150+
String[] lines = line.split(separator);
151+
/*Line has ' in the beginning and in the end */
152+
String id = lines[0].substring(1);
153+
String date = lines[1].substring(0, lines[1].length() - 1 );
154+
//From seconds to milliseconds
155+
return new TimedCommit(ObjectId.fromString(id),
156+
new DateTime(Long.parseLong(date) * 1000));
157+
}
158+
159+
private class TimedCommit implements Comparable<TimedCommit> {
160+
161+
private ObjectId commit;
162+
public DateTime when;
163+
164+
public TimedCommit(ObjectId c, DateTime when) {
165+
this.commit = c;
166+
this.when = when;
167+
}
168+
169+
public ObjectId getCommit() {
170+
return commit;
171+
}
172+
173+
@Override
174+
public int compareTo(TimedCommit o) {
175+
//I want newest to be first
176+
int result = -(when.compareTo(o.when));
177+
//If time is equal, keep order from log.
178+
if(result == 0) {
179+
return -1;
180+
}
181+
return result;
182+
}
183+
}
184+
185+
@Override
186+
public Build revisionBuilt(Revision revision, int buildNumber, Result result )
187+
{
188+
Build build = new Build(revision, buildNumber, result);
189+
data.saveBuild(build);
190+
return build;
191+
}
192+
193+
194+
@Override
195+
public Action getData()
196+
{
197+
return data;
198+
}
199+
200+
}

src/main/resources/hudson/plugins/git/GitSCM/config.jelly

+8
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,23 @@
129129
<j:when test="${empty(scm.choosingStrategy)}">
130130
<option value="${scm.DEFAULT}" selected="SELECTED">Default</option>
131131
<option value="${scm.GERRIT}">Gerrit</option>
132+
<option value="${scm.DIGG}">Digg</option>
132133
</j:when>
133134
<j:otherwise>
134135
<j:if test="${scm.choosingStrategy==scm.DEFAULT}">
135136
<option value="${scm.DEFAULT}" selected="SELECTED" >Default</option>
136137
<option value="${scm.GERRIT}">Gerrit</option>
138+
<option value="${scm.DIGG}">Digg</option>
137139
</j:if>
138140
<j:if test="${scm.choosingStrategy==scm.GERRIT}">
139141
<option value="${scm.DEFAULT}">Default</option>
140142
<option value="${scm.GERRIT}" selected="SELECTED">Gerrit</option>
143+
<option value="${scm.DIGG}">Digg</option>
144+
</j:if>
145+
<j:if test="${scm.choosingStrategy==scm.DIGG}">
146+
<option value="${scm.DEFAULT}">Default</option>
147+
<option value="${scm.GERRIT}">Gerrit</option>
148+
<option value="${scm.DIGG}" selected="SELECTED">Digg</option>
141149
</j:if>
142150
</j:otherwise>
143151
</j:choose>

0 commit comments

Comments
 (0)