Skip to content

Commit f126dd9

Browse files
author
Andrew Bayer
committed
Merging with latest
2 parents 670f235 + 8c500a5 commit f126dd9

File tree

4 files changed

+239
-1
lines changed

4 files changed

+239
-1
lines changed

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

+25-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import hudson.scm.SCM;
1515
import hudson.scm.SCMDescriptor;
1616
import hudson.util.FormValidation;
17+
import hudson.Util;
1718

1819
import java.io.ByteArrayOutputStream;
1920
import java.io.File;
@@ -75,10 +76,13 @@ public class GitSCM extends SCM implements Serializable {
7576

7677
private boolean clean;
7778

79+
private boolean wipeOutWorkspace;
80+
7881
private String choosingStrategy = DEFAULT;
7982
public static final String DEFAULT = "Default";
8083
public static final String GERRIT = "Gerrit";
81-
84+
public static final String DIGG = "Digg";
85+
8286
public String gitTool = null;
8387

8488
private GitWeb browser;
@@ -104,6 +108,7 @@ public GitSCM(
104108
boolean doGenerateSubmoduleConfigurations,
105109
Collection<SubmoduleConfig> submoduleCfg,
106110
boolean clean,
111+
boolean wipeOutWorkspace,
107112
String choosingStrategy, GitWeb browser,
108113
String gitTool) {
109114

@@ -120,6 +125,7 @@ public GitSCM(
120125
this.submoduleCfg = submoduleCfg;
121126

122127
this.clean = clean;
128+
this.wipeOutWorkspace = wipeOutWorkspace;
123129
this.choosingStrategy = choosingStrategy;
124130
this.configVersion = 1L;
125131
this.gitTool = gitTool;
@@ -173,6 +179,10 @@ public GitWeb getBrowser() {
173179
return browser;
174180
}
175181

182+
public boolean getWipeOutWorkspace() {
183+
return this.wipeOutWorkspace;
184+
}
185+
176186
public boolean getClean() {
177187
return this.clean;
178188
}
@@ -289,6 +299,8 @@ public Boolean invoke(File localWorkspace, VirtualChannel channel) throws IOExce
289299
private IBuildChooser createBuildChooser(IGitAPI git, TaskListener listener, BuildData buildData) {
290300
if(this.choosingStrategy != null && GERRIT.equals(this.choosingStrategy)) {
291301
return new GerritBuildChooser(this,git,new GitUtils(listener,git), buildData);
302+
} else if(this.choosingStrategy != null && DIGG.equals(this.choosingStrategy)) {
303+
return new DiggBuildChooser(this,git,new GitUtils(listener,git), buildData);
292304
} else {
293305
return new BuildChooser(this, git, new GitUtils(listener, git), buildData);
294306
}
@@ -480,6 +492,16 @@ public Revision invoke(File localWorkspace, VirtualChannel channel)
480492
listener.getLogger().println("Checkout:" + ws.getName() + " / " + ws.getRemote() + " - " + ws.getChannel());
481493
IGitAPI git = new GitAPI(gitExe, ws, listener, environment);
482494

495+
if (wipeOutWorkspace) {
496+
listener.getLogger().println("Wiping out workspace first");
497+
try {
498+
ws.deleteContents();
499+
} catch (InterruptedException e) {
500+
// I don't really care if this fails.
501+
}
502+
503+
}
504+
483505
if (git.hasGitRepo()) {
484506
// It's an update
485507

@@ -490,6 +512,7 @@ public Revision invoke(File localWorkspace, VirtualChannel channel)
490512
}
491513

492514
} else {
515+
493516
listener.getLogger().println("Cloning the remote Git repository");
494517

495518
// Go through the repositories, trying to clone from one
@@ -843,6 +866,7 @@ public SCM newInstance(StaplerRequest req, JSONObject formData) throws FormExcep
843866
req.getParameter("git.generate") != null,
844867
submoduleCfg,
845868
req.getParameter("git.clean") != null,
869+
req.getParameter("git.wipeOutWorkspace") != null,
846870
req.getParameter("git.choosing_strategy"),
847871
gitWeb,
848872
gitTool);
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

+11
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,32 @@
122122
<f:entry title="Use commit author in changelog" help="/plugin/git/help-authorCommitter.html">
123123
<f:checkbox name="authorOrCommitter" checked="${scm.authorOrCommitter}"/>
124124
</f:entry>
125+
<f:entry title="Wipe out workspace" help="/plugin/git/wipeOutWorkspace.html">
126+
<f:checkbox name="git.wipeOutWorkspace" checked="${scm.wipeOutWorkspace}" />
127+
</f:entry>
125128
<f:entry title="Choosing strategy" help="/plugin/git/choosingStrategy.html" field="git.choosing_strategy">
126129
<select name="git.choosing_strategy">
127130
<j:choose>
128131
<j:when test="${empty(scm.choosingStrategy)}">
129132
<option value="${scm.DEFAULT}" selected="SELECTED">Default</option>
130133
<option value="${scm.GERRIT}">Gerrit</option>
134+
<option value="${scm.DIGG}">Digg</option>
131135
</j:when>
132136
<j:otherwise>
133137
<j:if test="${scm.choosingStrategy==scm.DEFAULT}">
134138
<option value="${scm.DEFAULT}" selected="SELECTED" >Default</option>
135139
<option value="${scm.GERRIT}">Gerrit</option>
140+
<option value="${scm.DIGG}">Digg</option>
136141
</j:if>
137142
<j:if test="${scm.choosingStrategy==scm.GERRIT}">
138143
<option value="${scm.DEFAULT}">Default</option>
139144
<option value="${scm.GERRIT}" selected="SELECTED">Gerrit</option>
145+
<option value="${scm.DIGG}">Digg</option>
146+
</j:if>
147+
<j:if test="${scm.choosingStrategy==scm.DIGG}">
148+
<option value="${scm.DEFAULT}">Default</option>
149+
<option value="${scm.GERRIT}">Gerrit</option>
150+
<option value="${scm.DIGG}" selected="SELECTED">Digg</option>
140151
</j:if>
141152
</j:otherwise>
142153
</j:choose>

src/main/webapp/wipeOutWorkspace.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
Delete the contents of the workspace before building, ensuring a fully fresh workspace.
3+
</div>

0 commit comments

Comments
 (0)