1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>,
3 * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
4 * Copyright (C) 2014, Konrad Kügler
5 * and other copyright owners as documented in the project's IP log.
6 *
7 * This program and the accompanying materials are made available
8 * under the terms of the Eclipse Distribution License v1.0 which
9 * accompanies this distribution, is reproduced below, and is
10 * available at http://www.eclipse.org/org/documents/edl-v10.php
11 *
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * - Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimer in the documentation and/or other materials provided
24 * with the distribution.
25 *
26 * - Neither the name of the Eclipse Foundation, Inc. nor the
27 * names of its contributors may be used to endorse or promote
28 * products derived from this software without specific prior
29 * written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
32 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
33 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
42 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
43 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45
46 package org.eclipse.jgit.revplot;
47
48 import java.text.MessageFormat;
49 import java.util.BitSet;
50 import java.util.Collection;
51 import java.util.HashMap;
52 import java.util.HashSet;
53 import java.util.TreeSet;
54
55 import org.eclipse.jgit.internal.JGitText;
56 import org.eclipse.jgit.revwalk.RevCommitList;
57 import org.eclipse.jgit.revwalk.RevWalk;
58
59 /**
60 * An ordered list of {@link org.eclipse.jgit.revplot.PlotCommit} subclasses.
61 * <p>
62 * Commits are allocated into lanes as they enter the list, based upon their
63 * connections between descendant (child) commits and ancestor (parent) commits.
64 * <p>
65 * The source of the list must be a {@link org.eclipse.jgit.revplot.PlotWalk}
66 * and {@link #fillTo(int)} must be used to populate the list.
67 *
68 * @param <L>
69 * type of lane used by the application.
70 */
71 public class PlotCommitList<L extends PlotLane> extends
72 RevCommitList<PlotCommit<L>> {
73 static final int MAX_LENGTH = 25;
74
75 private int positionsAllocated;
76
77 private final TreeSet<Integer> freePositions = new TreeSet<>();
78
79 private final HashSet<PlotLane> activeLanes = new HashSet<>(32);
80
81 /** number of (child) commits on a lane */
82 private final HashMap<PlotLane, Integer> laneLength = new HashMap<>(
83 32);
84
85 /** {@inheritDoc} */
86 @Override
87 public void clear() {
88 super.clear();
89 positionsAllocated = 0;
90 freePositions.clear();
91 activeLanes.clear();
92 laneLength.clear();
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public void source(RevWalk w) {
98 if (!(w instanceof PlotWalk))
99 throw new ClassCastException(MessageFormat.format(JGitText.get().classCastNotA, PlotWalk.class.getName()));
100 super.source(w);
101 }
102
103 /**
104 * Find the set of lanes passing through a commit's row.
105 * <p>
106 * Lanes passing through a commit are lanes that the commit is not directly
107 * on, but that need to travel through this commit to connect a descendant
108 * (child) commit to an ancestor (parent) commit. Typically these lanes will
109 * be drawn as lines in the passed commit's box, and the passed commit won't
110 * appear to be connected to those lines.
111 * <p>
112 * This method modifies the passed collection by adding the lanes in any
113 * order.
114 *
115 * @param currCommit
116 * the commit the caller needs to get the lanes from.
117 * @param result
118 * collection to add the passing lanes into.
119 */
120 @SuppressWarnings("unchecked")
121 public void findPassingThrough(final PlotCommit<L> currCommit,
122 final Collection<L> result) {
123 for (PlotLane p : currCommit.passingLanes)
124 result.add((L) p);
125 }
126
127 /** {@inheritDoc} */
128 @Override
129 protected void enter(int index, PlotCommit<L> currCommit) {
130 setupChildren(currCommit);
131
132 final int nChildren = currCommit.getChildCount();
133 if (nChildren == 0) {
134 currCommit.lane = nextFreeLane();
135 } else if (nChildren == 1
136 && currCommit.children[0].getParentCount() < 2) {
137 // Only one child, child has only us as their parent.
138 // Stay in the same lane as the child.
139
140 @SuppressWarnings("unchecked")
141 final PlotCommit<L> c = currCommit.children[0];
142 currCommit.lane = c.lane;
143 Integer len = laneLength.get(currCommit.lane);
144 len = len != null ? Integer.valueOf(len.intValue() + 1)
145 : Integer.valueOf(0);
146 laneLength.put(currCommit.lane, len);
147 } else {
148 // More than one child, or our child is a merge.
149
150 // We look for the child lane the current commit should continue.
151 // Candidate lanes for this are those with children, that have the
152 // current commit as their first parent.
153 // There can be multiple candidate lanes. In that case the longest
154 // lane is chosen, as this is usually the lane representing the
155 // branch the commit actually was made on.
156
157 // When there are no candidate lanes (i.e. the current commit has
158 // only children whose non-first parent it is) we place the current
159 // commit on a new lane.
160
161 // The lane the current commit will be placed on:
162 PlotLane reservedLane = null;
163 PlotCommit childOnReservedLane = null;
164 int lengthOfReservedLane = -1;
165
166 for (int i = 0; i < nChildren; i++) {
167 @SuppressWarnings("unchecked")
168 final PlotCommit<L> c = currCommit.children[i];
169 if (c.getParent(0) == currCommit) {
170 Integer len = laneLength.get(c.lane);
171 // we may be the first parent for multiple lines of
172 // development, try to continue the longest one
173 if (len.intValue() > lengthOfReservedLane) {
174 reservedLane = c.lane;
175 childOnReservedLane = c;
176 lengthOfReservedLane = len.intValue();
177 }
178 }
179 }
180
181 if (reservedLane != null) {
182 currCommit.lane = reservedLane;
183 laneLength.put(reservedLane,
184 Integer.valueOf(lengthOfReservedLane + 1));
185 handleBlockedLanes(index, currCommit, childOnReservedLane);
186 } else {
187 currCommit.lane = nextFreeLane();
188 handleBlockedLanes(index, currCommit, null);
189 }
190
191 // close lanes of children, if there are no first parents that might
192 // want to continue the child lanes
193 for (int i = 0; i < nChildren; i++) {
194 final PlotCommit c = currCommit.children[i];
195 PlotCommit firstParent = (PlotCommit) c.getParent(0);
196 if (firstParent.lane != null && firstParent.lane != c.lane)
197 closeLane(c.lane);
198 }
199 }
200
201 continueActiveLanes(currCommit);
202 if (currCommit.getParentCount() == 0)
203 closeLane(currCommit.lane);
204 }
205
206 private void continueActiveLanes(PlotCommit currCommit) {
207 for (PlotLane lane : activeLanes)
208 if (lane != currCommit.lane)
209 currCommit.addPassingLane(lane);
210 }
211
212 /**
213 * Sets up fork and merge information in the involved PlotCommits.
214 * Recognizes and handles blockades that involve forking or merging arcs.
215 *
216 * @param index
217 * the index of <code>currCommit</code> in the list
218 * @param currCommit
219 * @param childOnLane
220 * the direct child on the same lane as <code>currCommit</code>,
221 * may be null if <code>currCommit</code> is the first commit on
222 * the lane
223 */
224 private void handleBlockedLanes(final int index, final PlotCommit currCommit,
225 final PlotCommit childOnLane) {
226 for (PlotCommit child : currCommit.children) {
227 if (child == childOnLane)
228 continue; // simple continuations of lanes are handled by
229 // continueActiveLanes() calls in enter()
230
231 // Is the child a merge or is it forking off?
232 boolean childIsMerge = child.getParent(0) != currCommit;
233 if (childIsMerge) {
234 PlotLane laneToUse = currCommit.lane;
235 laneToUse = handleMerge(index, currCommit, childOnLane, child,
236 laneToUse);
237 child.addMergingLane(laneToUse);
238 } else {
239 // We want to draw a forking arc in the child's lane.
240 // As an active lane, the child lane already continues
241 // (unblocked) up to this commit, we only need to mark it as
242 // forking off from the current commit.
243 PlotLane laneToUse = child.lane;
244 currCommit.addForkingOffLane(laneToUse);
245 }
246 }
247 }
248
249 // Handles the case where currCommit is a non-first parent of the child
250 private PlotLane handleMerge(final int index, final PlotCommit currCommit,
251 final PlotCommit./../org/eclipse/jgit/revplot/PlotCommit.html#PlotCommit">PlotCommit childOnLane, PlotCommit child, PlotLane laneToUse) {
252
253 // find all blocked positions between currCommit and this child
254
255 int childIndex = index; // useless initialization, should
256 // always be set in the loop below
257 BitSet blockedPositions = new BitSet();
258 for (int r = index - 1; r >= 0; r--) {
259 final PlotCommit rObj = get(r);
260 if (rObj == child) {
261 childIndex = r;
262 break;
263 }
264 addBlockedPosition(blockedPositions, rObj);
265 }
266
267 // handle blockades
268
269 if (blockedPositions.get(laneToUse.getPosition())) {
270 // We want to draw a merging arc in our lane to the child,
271 // which is on another lane, but our lane is blocked.
272
273 // Check if childOnLane is beetween commit and the child we
274 // are currently processing
275 boolean needDetour = false;
276 if (childOnLane != null) {
277 for (int r = index - 1; r > childIndex; r--) {
278 final PlotCommit rObj = get(r);
279 if (rObj == childOnLane) {
280 needDetour = true;
281 break;
282 }
283 }
284 }
285
286 if (needDetour) {
287 // It is childOnLane which is blocking us. Repositioning
288 // our lane would not help, because this repositions the
289 // child too, keeping the blockade.
290 // Instead, we create a "detour lane" which gets us
291 // around the blockade. That lane has no commits on it.
292 laneToUse = nextFreeLane(blockedPositions);
293 currCommit.addForkingOffLane(laneToUse);
294 closeLane(laneToUse);
295 } else {
296 // The blockade is (only) due to other (already closed)
297 // lanes at the current lane's position. In this case we
298 // reposition the current lane.
299 // We are the first commit on this lane, because
300 // otherwise the child commit on this lane would have
301 // kept other lanes from blocking us. Since we are the
302 // first commit, we can freely reposition.
303 int newPos = getFreePosition(blockedPositions);
304 freePositions.add(Integer.valueOf(laneToUse
305 .getPosition()));
306 laneToUse.position = newPos;
307 }
308 }
309
310 // Actually connect currCommit to the merge child
311 drawLaneToChild(index, child, laneToUse);
312 return laneToUse;
313 }
314
315 /**
316 * Connects the commit at commitIndex to the child, using the given lane.
317 * All blockades on the lane must be resolved before calling this method.
318 *
319 * @param commitIndex
320 * @param child
321 * @param laneToContinue
322 */
323 private void drawLaneToChild(final int commitIndex, PlotCommit child,
324 PlotLane laneToContinue) {
325 for (int r = commitIndex - 1; r >= 0; r--) {
326 final PlotCommit rObj = get(r);
327 if (rObj == child)
328 break;
329 if (rObj != null)
330 rObj.addPassingLane(laneToContinue);
331 }
332 }
333
334 private static void addBlockedPosition(BitSet blockedPositions,
335 final PlotCommit rObj) {
336 if (rObj != null) {
337 PlotLane lane = rObj.getLane();
338 // Positions may be blocked by a commit on a lane.
339 if (lane != null)
340 blockedPositions.set(lane.getPosition());
341 // Positions may also be blocked by forking off and merging lanes.
342 // We don't consider passing lanes, because every passing lane forks
343 // off and merges at it ends.
344 for (PlotLane l : rObj.forkingOffLanes)
345 blockedPositions.set(l.getPosition());
346 for (PlotLane l : rObj.mergingLanes)
347 blockedPositions.set(l.getPosition());
348 }
349 }
350
351 @SuppressWarnings("unchecked")
352 private void closeLane(PlotLane lane) {
353 if (activeLanes.remove(lane)) {
354 recycleLane((L) lane);
355 laneLength.remove(lane);
356 freePositions.add(Integer.valueOf(lane.getPosition()));
357 }
358 }
359
360 private void setupChildren(PlotCommit<L> currCommit) {
361 final int nParents = currCommit.getParentCount();
362 for (int i = 0; i < nParents; i++)
363 ((PlotCommit) currCommit.getParent(i)).addChild(currCommit);
364 }
365
366 private PlotLane nextFreeLane() {
367 return nextFreeLane(null);
368 }
369
370 private PlotLane nextFreeLane(BitSet blockedPositions) {
371 final PlotLane p = createLane();
372 p.position = getFreePosition(blockedPositions);
373 activeLanes.add(p);
374 laneLength.put(p, Integer.valueOf(1));
375 return p;
376 }
377
378 /**
379 * @param blockedPositions
380 * may be null
381 * @return a free lane position
382 */
383 private int getFreePosition(BitSet blockedPositions) {
384 if (freePositions.isEmpty())
385 return positionsAllocated++;
386
387 if (blockedPositions != null) {
388 for (Integer pos : freePositions)
389 if (!blockedPositions.get(pos.intValue())) {
390 freePositions.remove(pos);
391 return pos.intValue();
392 }
393 return positionsAllocated++;
394 } else {
395 final Integer min = freePositions.first();
396 freePositions.remove(min);
397 return min.intValue();
398 }
399 }
400
401 /**
402 * Create a new {@link PlotLane} appropriate for this particular
403 * {@link PlotCommitList}.
404 *
405 * @return a new {@link PlotLane} appropriate for this particular
406 * {@link PlotCommitList}.
407 */
408 @SuppressWarnings("unchecked")
409 protected L createLane() {
410 return (L) new PlotLane();
411 }
412
413 /**
414 * Return colors and other reusable information to the plotter when a lane
415 * is no longer needed.
416 *
417 * @param lane
418 * a lane
419 */
420 protected void recycleLane(L lane) {
421 // Nothing.
422 }
423 }