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
52 /**
53 * Only produce commits which are below a specified depth.
54 *
55 * @see DepthWalk
56 */
57 class DepthGenerator extends Generator {
58 private final FIFORevQueue pending;
59
60 private final int depth;
61
62 private final RevWalk walk;
63
64 /**
65 * Commits which used to be shallow in the client, but which are
66 * being extended as part of this fetch. These commits should be
67 * returned to the caller as UNINTERESTING so that their blobs/trees
68 * can be marked appropriately in the pack writer.
69 */
70 private final RevFlag UNSHALLOW;
71
72 /**
73 * Commits which the normal framework has marked as UNINTERESTING,
74 * but which we now care about again. This happens if a client is
75 * extending a shallow checkout to become deeper--the new commits at
76 * the bottom of the graph need to be sent, even though they are
77 * below other commits which the client already has.
78 */
79 private final RevFlag REINTERESTING;
80
81 /**
82 * @param w
83 * @param s Parent generator
84 * @throws MissingObjectException
85 * @throws IncorrectObjectTypeException
86 * @throws IOException
87 */
88 DepthGenerator(DepthWalk w, Generator s) throws MissingObjectException,
89 IncorrectObjectTypeException, IOException {
90 pending = new FIFORevQueue();
91 walk = (RevWalk)w;
92
93 this.depth = w.getDepth();
94 this.UNSHALLOW = w.getUnshallowFlag();
95 this.REINTERESTING = w.getReinterestingFlag();
96
97 s.shareFreeList(pending);
98
99 // Begin by sucking out all of the source's commits, and
100 // adding them to the pending queue
101 for (;;) {
102 RevCommit c = s.next();
103 if (c == null)
104 break;
105 if (((DepthWalk.Commit) c).getDepth() == 0)
106 pending.add(c);
107 }
108 }
109
110 @Override
111 int outputType() {
112 return pending.outputType() | HAS_UNINTERESTING;
113 }
114
115 @Override
116 void shareFreeList(final BlockRevQueue q) {
117 pending.shareFreeList(q);
118 }
119
120 @Override
121 RevCommit next() throws MissingObjectException,
122 IncorrectObjectTypeException, IOException {
123 // Perform a breadth-first descent into the commit graph,
124 // marking depths as we go. This means that if a commit is
125 // reachable by more than one route, we are guaranteed to
126 // arrive by the shortest route first.
127 for (;;) {
128 final DepthWalk.Commit c = (DepthWalk.Commit) pending.next();
129 if (c == null)
130 return null;
131
132 if ((c.flags & RevWalk.PARSED) == 0)
133 c.parseHeaders(walk);
134
135 int newDepth = c.depth + 1;
136
137 for (final RevCommit p : c.parents) {
138 DepthWalk.Commit dp = (DepthWalk.Commit) p;
139
140 // If no depth has been assigned to this commit, assign
141 // it now. Since we arrive by the shortest route first,
142 // this depth is guaranteed to be the smallest value that
143 // any path could produce.
144 if (dp.depth == -1) {
145 dp.depth = newDepth;
146
147 // If the parent is not too deep, add it to the queue
148 // so that we can produce it later
149 if (newDepth <= depth)
150 pending.add(p);
151 }
152
153 // If the current commit has become unshallowed, everything
154 // below us is new to the client. Mark its parent as
155 // re-interesting, and carry that flag downward to all
156 // of its ancestors.
157 if(c.has(UNSHALLOW) || c.has(REINTERESTING)) {
158 p.add(REINTERESTING);
159 p.flags &= ~RevWalk.UNINTERESTING;
160 }
161 }
162
163 // Produce all commits less than the depth cutoff
164 boolean produce = c.depth <= depth;
165
166 // Unshallow commits are uninteresting, but still need to be sent
167 // up to the PackWriter so that it will exclude objects correctly.
168 // All other uninteresting commits should be omitted.
169 if ((c.flags & RevWalk.UNINTERESTING) != 0 && !c.has(UNSHALLOW))
170 produce = false;
171
172 if (produce)
173 return c;
174 }
175 }
176 }