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