Switch jgit library to the EDL (3-clause BSD)
[egit/zawir.git] / org.spearce.jgit / src / org / spearce / jgit / revwalk / StartGenerator.java
blobdebd168f3e5747fdc76f8fa2b06d9d1276162b8a
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.revwalk;
40 import java.io.IOException;
41 import java.util.EnumSet;
43 import org.spearce.jgit.errors.IncorrectObjectTypeException;
44 import org.spearce.jgit.errors.MissingObjectException;
45 import org.spearce.jgit.revwalk.filter.AndRevFilter;
46 import org.spearce.jgit.revwalk.filter.RevFilter;
47 import org.spearce.jgit.treewalk.filter.TreeFilter;
49 /**
50 * Initial RevWalk generator that bootstraps a new walk.
51 * <p>
52 * Initially RevWalk starts with this generator as its chosen implementation.
53 * The first request for a RevCommit from the RevWalk instance calls to our
54 * {@link #next()} method, and we replace ourselves with the best Generator
55 * implementation available based upon the current RevWalk configuration.
57 class StartGenerator extends Generator {
58 private final RevWalk walker;
60 StartGenerator(final RevWalk w) {
61 walker = w;
64 @Override
65 int outputType() {
66 return 0;
69 @Override
70 RevCommit next() throws MissingObjectException,
71 IncorrectObjectTypeException, IOException {
72 Generator g;
74 final RevWalk w = walker;
75 RevFilter rf = w.getRevFilter();
76 final TreeFilter tf = w.getTreeFilter();
77 AbstractRevQueue q = walker.queue;
79 if (rf == RevFilter.MERGE_BASE) {
80 // Computing for merge bases is a special case and does not
81 // use the bulk of the generator pipeline.
83 if (tf != TreeFilter.ALL)
84 throw new IllegalStateException("Cannot combine TreeFilter "
85 + tf + " with RevFilter " + rf + ".");
87 final MergeBaseGenerator mbg = new MergeBaseGenerator(w);
88 walker.pending = mbg;
89 walker.queue = AbstractRevQueue.EMPTY_QUEUE;
90 mbg.init(q);
91 return mbg.next();
94 final EnumSet<RevSort> sort = w.getRevSort();
95 boolean boundary = sort.contains(RevSort.BOUNDARY);
97 if (!boundary && walker instanceof ObjectWalk) {
98 // The object walker requires boundary support to color
99 // trees and blobs at the boundary uninteresting so it
100 // does not produce those in the result.
102 boundary = true;
104 if (boundary && !q.anybodyHasFlag(RevWalk.UNINTERESTING)) {
105 // If we were not fed uninteresting commits we will never
106 // construct a boundary. There is no reason to include the
107 // extra overhead associated with that in our pipeline.
109 boundary = false;
112 int pendingOutputType = 0;
113 if (sort.contains(RevSort.START_ORDER) && !(q instanceof FIFORevQueue))
114 q = new FIFORevQueue(q);
115 if (sort.contains(RevSort.COMMIT_TIME_DESC)
116 && !(q instanceof DateRevQueue))
117 q = new DateRevQueue(q);
118 if (tf != TreeFilter.ALL) {
119 rf = AndRevFilter.create(rf, new RewriteTreeFilter(w, tf));
120 pendingOutputType |= HAS_REWRITE | NEEDS_REWRITE;
123 walker.queue = q;
124 g = new PendingGenerator(w, q, rf, pendingOutputType);
126 if (boundary) {
127 // Because the boundary generator may produce uninteresting
128 // commits we cannot allow the pending generator to dispose
129 // of them early.
131 ((PendingGenerator) g).canDispose = false;
134 if ((g.outputType() & NEEDS_REWRITE) != 0) {
135 // Correction for an upstream NEEDS_REWRITE is to buffer
136 // fully and then apply a rewrite generator that can
137 // pull through the rewrite chain and produce a dense
138 // output graph.
140 g = new FIFORevQueue(g);
141 g = new RewriteGenerator(g);
144 if (sort.contains(RevSort.TOPO) && (g.outputType() & SORT_TOPO) == 0)
145 g = new TopoSortGenerator(g);
146 if (sort.contains(RevSort.REVERSE))
147 g = new LIFORevQueue(q);
148 if (boundary)
149 g = new BoundaryGenerator(w, g);
151 w.pending = g;
152 return g.next();