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 pending = new FIFORevQueue();
99 walk = (RevWalk)w;
100
101 this.depth = w.getDepth();
102 this.deepenSince = w.getDeepenSince();
103 this.UNSHALLOW = w.getUnshallowFlag();
104 this.REINTERESTING = w.getReinterestingFlag();
105 this.DEEPEN_NOT = w.getDeepenNotFlag();
106
107 s.shareFreeList(pending);
108
109 // Begin by sucking out all of the source's commits, and
110 // adding them to the pending queue
111 for (;;) {
112 RevCommit c = s.next();
113 if (c == null)
114 break;
115 if (((DepthWalk.Commit) c).getDepth() == 0)
116 pending.add(c);
117 }
118
119 // Mark DEEPEN_NOT on all deepen-not commits and their ancestors.
120 // TODO(jonathantanmy): This implementation is somewhat
121 // inefficient in that any "deepen-not <ref>" in the request
122 // results in all commits reachable from that ref being parsed
123 // and marked, even if the commit topology is such that it is
124 // not necessary.
125 for (ObjectId oid : w.getDeepenNots()) {
126 RevCommit c;
127 try {
128 c = walk.parseCommit(oid);
129 } catch (IncorrectObjectTypeException notCommit) {
130 // The C Git implementation silently tolerates
131 // non-commits, so do the same here.
132 continue;
133 }
134
135 FIFORevQueue queue = new FIFORevQueue();
136 queue.add(c);
137 while ((c = queue.next()) != null) {
138 if (c.has(DEEPEN_NOT)) {
139 continue;
140 }
141
142 walk.parseHeaders(c);
143 c.add(DEEPEN_NOT);
144 for (RevCommit p : c.getParents()) {
145 queue.add(p);
146 }
147 }
148 }
149 }
150
151 @Override
152 int outputType() {
153 return pending.outputType() | HAS_UNINTERESTING;
154 }
155
156 @Override
157 void shareFreeList(BlockRevQueue q) {
158 pending.shareFreeList(q);
159 }
160
161 @Override
162 RevCommit next() throws MissingObjectException,
163 IncorrectObjectTypeException, IOException {
164 // Perform a breadth-first descent into the commit graph,
165 // marking depths as we go. This means that if a commit is
166 // reachable by more than one route, we are guaranteed to
167 // arrive by the shortest route first.
168 for (;;) {
169 final DepthWalk.Commit c = (DepthWalk.Commit) pending.next();
170 if (c == null)
171 return null;
172
173 if ((c.flags & RevWalk.PARSED) == 0)
174 c.parseHeaders(walk);
175
176 if (c.getCommitTime() < deepenSince) {
177 continue;
178 }
179
180 if (c.has(DEEPEN_NOT)) {
181 continue;
182 }
183
184 int newDepth = c.depth + 1;
185
186 for (RevCommit p : c.parents) {
187 DepthWalk.Commit dp = (DepthWalk.Commit) p;
188
189 // If no depth has been assigned to this commit, assign
190 // it now. Since we arrive by the shortest route first,
191 // this depth is guaranteed to be the smallest value that
192 // any path could produce.
193 if (dp.depth == -1) {
194 boolean failsDeepenSince = false;
195 if (deepenSince != 0) {
196 if ((p.flags & RevWalk.PARSED) == 0) {
197 p.parseHeaders(walk);
198 }
199 failsDeepenSince =
200 p.getCommitTime() < deepenSince;
201 }
202
203 dp.depth = newDepth;
204
205 // If the parent is not too deep and was not excluded, add
206 // it to the queue so that we can produce it later
207 if (newDepth <= depth && !failsDeepenSince &&
208 !p.has(DEEPEN_NOT)) {
209 pending.add(p);
210 } else {
211 dp.makesChildBoundary = true;
212 }
213 }
214
215 if (dp.makesChildBoundary) {
216 c.isBoundary = true;
217 }
218
219 // If the current commit has become unshallowed, everything
220 // below us is new to the client. Mark its parent as
221 // re-interesting, and carry that flag downward to all
222 // of its ancestors.
223 if(c.has(UNSHALLOW) || c.has(REINTERESTING)) {
224 p.add(REINTERESTING);
225 p.flags &= ~RevWalk.UNINTERESTING;
226 }
227 }
228
229 boolean produce = true;
230
231 // Unshallow commits are uninteresting, but still need to be sent
232 // up to the PackWriter so that it will exclude objects correctly.
233 // All other uninteresting commits should be omitted.
234 if ((c.flags & RevWalk.UNINTERESTING) != 0 && !c.has(UNSHALLOW))
235 produce = false;
236
237 if (c.getCommitTime() < deepenSince) {
238 produce = false;
239 }
240
241 if (produce)
242 return c;
243 }
244 }
245 }