|
| 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 | +} |
0 commit comments