View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import java.io.IOException;
47  import java.text.MessageFormat;
48  import java.util.LinkedList;
49  
50  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
51  import org.eclipse.jgit.errors.MissingObjectException;
52  import org.eclipse.jgit.internal.JGitText;
53  
54  /**
55   * Computes the merge base(s) of the starting commits.
56   * <p>
57   * This generator is selected if the RevFilter is only
58   * {@link org.eclipse.jgit.revwalk.filter.RevFilter#MERGE_BASE}.
59   * <p>
60   * To compute the merge base we assign a temporary flag to each of the starting
61   * commits. The maximum number of starting commits is bounded by the number of
62   * free flags available in the RevWalk when the generator is initialized. These
63   * flags will be automatically released on the next reset of the RevWalk, but
64   * not until then, as they are assigned to commits throughout the history.
65   * <p>
66   * Several internal flags are reused here for a different purpose, but this
67   * should not have any impact as this generator should be run alone, and without
68   * any other generators wrapped around it.
69   */
70  class MergeBaseGenerator extends Generator {
71  	private static final int PARSED = RevWalk.PARSED;
72  	private static final int IN_PENDING = RevWalk.SEEN;
73  	private static final int POPPED = RevWalk.TEMP_MARK;
74  	private static final int MERGE_BASE = RevWalk.REWRITE;
75  
76  	private final RevWalk walker;
77  	private final DateRevQueue pending;
78  
79  	private int branchMask;
80  	private int recarryTest;
81  	private int recarryMask;
82  	private int mergeBaseAncestor = -1;
83  	private LinkedList<RevCommit> ret = new LinkedList<>();
84  
85  	private CarryStack stack;
86  
87  	MergeBaseGenerator(RevWalk w) {
88  		super(w.isFirstParent());
89  		walker = w;
90  		pending = new DateRevQueue(firstParent);
91  	}
92  
93  	void init(AbstractRevQueue p) throws IOException {
94  		try {
95  			for (;;) {
96  				final RevCommit c = p.next();
97  				if (c == null)
98  					break;
99  				add(c);
100 			}
101 			// Setup the condition used by carryOntoOne to detect a late
102 			// merge base and produce it on the next round.
103 			//
104 			recarryTest = branchMask | POPPED;
105 			recarryMask = branchMask | POPPED | MERGE_BASE;
106 			mergeBaseAncestor = walker.allocFlag();
107 
108 			for (;;) {
109 				RevCommit c = _next();
110 				if (c == null) {
111 					break;
112 				}
113 				ret.add(c);
114 			}
115 		} finally {
116 			// Always free the flags immediately. This ensures the flags
117 			// will be available for reuse when the walk resets.
118 			//
119 			walker.freeFlag(branchMask | mergeBaseAncestor);
120 		}
121 	}
122 
123 	private void add(RevCommit c) {
124 		final int flag = walker.allocFlag();
125 		branchMask |= flag;
126 		if ((c.flags & branchMask) != 0) {
127 			// This should never happen. RevWalk ensures we get a
128 			// commit admitted to the initial queue only once. If
129 			// we see this marks aren't correctly erased.
130 			//
131 			throw new IllegalStateException(MessageFormat.format(JGitText.get().staleRevFlagsOn, c.name()));
132 		}
133 		c.flags |= flag;
134 		pending.add(c);
135 	}
136 
137 	@Override
138 	int outputType() {
139 		return 0;
140 	}
141 
142 	private RevCommit _next() throws MissingObjectException,
143 			IncorrectObjectTypeException, IOException {
144 		for (;;) {
145 			final RevCommit c = pending.next();
146 			if (c == null) {
147 				return null;
148 			}
149 
150 			for (RevCommit p : c.parents) {
151 				if ((p.flags & IN_PENDING) != 0)
152 					continue;
153 				if ((p.flags & PARSED) == 0)
154 					p.parseHeaders(walker);
155 				p.flags |= IN_PENDING;
156 				pending.add(p);
157 			}
158 
159 			int carry = c.flags & branchMask;
160 			boolean mb = carry == branchMask;
161 			if (mb) {
162 				// If we are a merge base make sure our ancestors are
163 				// also flagged as being popped, so that they do not
164 				// generate to the caller.
165 				//
166 				carry |= MERGE_BASE | mergeBaseAncestor;
167 			}
168 			carryOntoHistory(c, carry);
169 
170 			if ((c.flags & MERGE_BASE) != 0) {
171 				// This commit is an ancestor of a merge base we already
172 				// popped back to the caller. If everyone in pending is
173 				// that way we are done traversing; if not we just need
174 				// to move to the next available commit and try again.
175 				//
176 				if (pending.everbodyHasFlag(MERGE_BASE))
177 					return null;
178 				continue;
179 			}
180 			c.flags |= POPPED;
181 
182 			if (mb) {
183 				c.flags |= MERGE_BASE;
184 				return c;
185 			}
186 		}
187 	}
188 
189 	@Override
190 	RevCommit next() throws MissingObjectException,
191 			IncorrectObjectTypeException, IOException {
192 		while (!ret.isEmpty()) {
193 			RevCommit commit = ret.remove();
194 			if ((commit.flags & mergeBaseAncestor) == 0) {
195 				return commit;
196 			}
197 		}
198 		return null;
199 	}
200 
201 	private void carryOntoHistory(RevCommit c, int carry) {
202 		stack = null;
203 		for (;;) {
204 			carryOntoHistoryInnerLoop(c, carry);
205 			if (stack == null) {
206 				break;
207 			}
208 			c = stack.c;
209 			carry = stack.carry;
210 			stack = stack.prev;
211 		}
212 	}
213 
214 	private void carryOntoHistoryInnerLoop(RevCommit c, int carry) {
215 		for (;;) {
216 			RevCommit[] parents = c.parents;
217 			if (parents == null || parents.length == 0) {
218 				break;
219 			}
220 
221 			int e = parents.length - 1;
222 			for (int i = 0; i < e; i++) {
223 				RevCommit p = parents[i];
224 				if (carryOntoOne(p, carry) == CONTINUE) {
225 					// Walking p will be required, buffer p on stack.
226 					stack = new CarryStack(stack, p, carry);
227 				}
228 				// For other results from carryOntoOne:
229 				// HAVE_ALL: p has all bits, do nothing to skip that path.
230 				// CONTINUE_ON_STACK: callee pushed StackElement for p.
231 			}
232 
233 			c = parents[e];
234 			if (carryOntoOne(c, carry) != CONTINUE) {
235 				break;
236 			}
237 		}
238 	}
239 
240 	private static final int CONTINUE = 0;
241 	private static final int HAVE_ALL = 1;
242 	private static final int CONTINUE_ON_STACK = 2;
243 
244 	private int carryOntoOne(RevCommit p, int carry) {
245 		// If we already had all carried flags, our parents do too.
246 		// Return HAVE_ALL to stop caller from running down this leg
247 		// of the revision graph any further.
248 		//
249 		// Otherwise return CONTINUE to ask the caller to walk history.
250 		int rc = (p.flags & carry) == carry ? HAVE_ALL : CONTINUE;
251 		p.flags |= carry;
252 
253 		if ((p.flags & recarryMask) == recarryTest) {
254 			// We were popped without being a merge base, but we just got
255 			// voted to be one. Inject ourselves back at the front of the
256 			// pending queue and tell all of our ancestors they are within
257 			// the merge base now.
258 			p.flags &= ~POPPED;
259 			pending.add(p);
260 			stack = new CarryStack(stack, p, branchMask | MERGE_BASE);
261 			return CONTINUE_ON_STACK;
262 		}
263 		return rc;
264 	}
265 
266 	private static class CarryStack {
267 		final CarryStack prev;
268 		final RevCommit c;
269 		final int carry;
270 
271 		CarryStack(CarryStack prev, RevCommit c, int carry) {
272 			this.prev = prev;
273 			this.c = c;
274 			this.carry = carry;
275 		}
276 	}
277 }