View Javadoc
1   /*
2    * Copyright (C) 2010, Garmin International
3    * Copyright (C) 2010, Matt Fischer <matt.fischer@garmin.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.revwalk;
46  
47  import java.io.IOException;
48  
49  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
50  import org.eclipse.jgit.errors.MissingObjectException;
51  import org.eclipse.jgit.lib.ObjectId;
52  
53  /**
54   * Only produce commits which are below a specified depth.
55   *
56   * @see DepthWalk
57   */
58  class DepthGenerator extends Generator {
59  	private final FIFORevQueue pending;
60  
61  	private final int depth;
62  
63  	private final int deepenSince;
64  
65  	private final RevWalk walk;
66  
67  	/**
68  	 * Commits which used to be shallow in the client, but which are
69  	 * being extended as part of this fetch.  These commits should be
70  	 * returned to the caller as UNINTERESTING so that their blobs/trees
71  	 * can be marked appropriately in the pack writer.
72  	 */
73  	private final RevFlag UNSHALLOW;
74  
75  	/**
76  	 * Commits which the normal framework has marked as UNINTERESTING,
77  	 * but which we now care about again.  This happens if a client is
78  	 * extending a shallow checkout to become deeper--the new commits at
79  	 * the bottom of the graph need to be sent, even though they are
80  	 * below other commits which the client already has.
81  	 */
82  	private final RevFlag REINTERESTING;
83  
84  	/**
85  	 * Commits reachable from commits that the client specified using --shallow-exclude.
86  	 */
87  	private final RevFlag DEEPEN_NOT;
88  
89  	/**
90  	 * @param w
91  	 * @param s Parent generator
92  	 * @throws MissingObjectException
93  	 * @throws IncorrectObjectTypeException
94  	 * @throws IOException
95  	 */
96  	DepthGenerator(DepthWalk w, Generator s) throws MissingObjectException,
97  			IncorrectObjectTypeException, IOException {
98  		super(s.firstParent);
99  		pending = new FIFORevQueue(firstParent);
100 		walk = (RevWalk)w;
101 
102 		this.depth = w.getDepth();
103 		this.deepenSince = w.getDeepenSince();
104 		this.UNSHALLOW = w.getUnshallowFlag();
105 		this.REINTERESTING = w.getReinterestingFlag();
106 		this.DEEPEN_NOT = w.getDeepenNotFlag();
107 
108 		s.shareFreeList(pending);
109 
110 		// Begin by sucking out all of the source's commits, and
111 		// adding them to the pending queue
112 		FIFORevQueue unshallowCommits = new FIFORevQueue();
113 		for (;;) {
114 			RevCommit c = s.next();
115 			if (c == null)
116 				break;
117 			if (c.has(UNSHALLOW)) {
118 				unshallowCommits.add(c);
119 			} else if (((DepthWalk.Commit) c).getDepth() == 0) {
120 				pending.add(c);
121 			}
122 		}
123 		// Move unshallow commits to the front so that the REINTERESTING flag
124 		// carry over code is executed first.
125 		for (;;) {
126 			RevCommit c = unshallowCommits.next();
127 			if (c == null) {
128 				break;
129 			}
130 			pending.unpop(c);
131 		}
132 
133 		// Mark DEEPEN_NOT on all deepen-not commits and their ancestors.
134 		// TODO(jonathantanmy): This implementation is somewhat
135 		// inefficient in that any "deepen-not <ref>" in the request
136 		// results in all commits reachable from that ref being parsed
137 		// and marked, even if the commit topology is such that it is
138 		// not necessary.
139 		for (ObjectId oid : w.getDeepenNots()) {
140 			RevCommit c;
141 			try {
142 				c = walk.parseCommit(oid);
143 			} catch (IncorrectObjectTypeException notCommit) {
144 				// The C Git implementation silently tolerates
145 				// non-commits, so do the same here.
146 				continue;
147 			}
148 
149 			FIFORevQueue queue = new FIFORevQueue();
150 			queue.add(c);
151 			while ((c = queue.next()) != null) {
152 				if (c.has(DEEPEN_NOT)) {
153 					continue;
154 				}
155 
156 				walk.parseHeaders(c);
157 				c.add(DEEPEN_NOT);
158 				for (RevCommit p : c.getParents()) {
159 					queue.add(p);
160 				}
161 			}
162 		}
163 	}
164 
165 	@Override
166 	int outputType() {
167 		return pending.outputType() | HAS_UNINTERESTING;
168 	}
169 
170 	@Override
171 	void shareFreeList(BlockRevQueue q) {
172 		pending.shareFreeList(q);
173 	}
174 
175 	@Override
176 	RevCommit next() throws MissingObjectException,
177 			IncorrectObjectTypeException, IOException {
178 		// Perform a breadth-first descent into the commit graph,
179 		// marking depths as we go.  This means that if a commit is
180 		// reachable by more than one route, we are guaranteed to
181 		// arrive by the shortest route first.
182 		for (;;) {
183 			final DepthWalk.Commit c = (DepthWalk.Commit) pending.next();
184 			if (c == null)
185 				return null;
186 
187 			if ((c.flags & RevWalk.PARSED) == 0)
188 				c.parseHeaders(walk);
189 
190 			if (c.getCommitTime() < deepenSince) {
191 				continue;
192 			}
193 
194 			if (c.has(DEEPEN_NOT)) {
195 				continue;
196 			}
197 
198 			int newDepth = c.depth + 1;
199 
200 			for (int i = 0; i < c.parents.length; i++) {
201 				if (firstParent && i > 0) {
202 					break;
203 				}
204 				RevCommit p = c.parents[i];
205 				DepthWalk.Commit dp = (DepthWalk.Commit) p;
206 
207 				// If no depth has been assigned to this commit, assign
208 				// it now.  Since we arrive by the shortest route first,
209 				// this depth is guaranteed to be the smallest value that
210 				// any path could produce.
211 				if (dp.depth == -1) {
212 					boolean failsDeepenSince = false;
213 					if (deepenSince != 0) {
214 						if ((p.flags & RevWalk.PARSED) == 0) {
215 							p.parseHeaders(walk);
216 						}
217 						failsDeepenSince =
218 							p.getCommitTime() < deepenSince;
219 					}
220 
221 					dp.depth = newDepth;
222 
223 					// If the parent is not too deep and was not excluded, add
224 					// it to the queue so that we can produce it later
225 					if (newDepth <= depth && !failsDeepenSince &&
226 							!p.has(DEEPEN_NOT)) {
227 						pending.add(p);
228 					} else {
229 						dp.makesChildBoundary = true;
230 					}
231 				}
232 
233 				if (dp.makesChildBoundary) {
234 					c.isBoundary = true;
235 				}
236 
237 				// If the current commit has become unshallowed, everything
238 				// below us is new to the client.  Mark its parent as
239 				// re-interesting, and carry that flag downward to all
240 				// of its ancestors.
241 				if(c.has(UNSHALLOW) || c.has(REINTERESTING)) {
242 					p.add(REINTERESTING);
243 					p.flags &= ~RevWalk.UNINTERESTING;
244 				}
245 			}
246 
247 			boolean produce = true;
248 
249 			// Unshallow commits are uninteresting, but still need to be sent
250 			// up to the PackWriter so that it will exclude objects correctly.
251 			// All other uninteresting commits should be omitted.
252 			if ((c.flags & RevWalk.UNINTERESTING) != 0 && !c.has(UNSHALLOW))
253 				produce = false;
254 
255 			if (c.getCommitTime() < deepenSince) {
256 				produce = false;
257 			}
258 
259 			if (produce)
260 				return c;
261 		}
262 	}
263 }