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.util.List;
48
49 import org.eclipse.jgit.diff.DiffConfig;
50 import org.eclipse.jgit.diff.DiffEntry;
51 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
52 import org.eclipse.jgit.diff.RenameDetector;
53 import org.eclipse.jgit.errors.CorruptObjectException;
54 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
55 import org.eclipse.jgit.errors.MissingObjectException;
56 import org.eclipse.jgit.errors.StopWalkException;
57 import org.eclipse.jgit.lib.ObjectId;
58 import org.eclipse.jgit.revwalk.filter.RevFilter;
59 import org.eclipse.jgit.treewalk.TreeWalk;
60 import org.eclipse.jgit.treewalk.filter.TreeFilter;
61
62 /**
63 * Filter applying a {@link TreeFilter} against changed paths in each commit.
64 * <p>
65 * Each commit is differenced concurrently against all of its parents to look
66 * for tree entries that are interesting to the {@link TreeFilter}.
67 *
68 * @since 3.5
69 */
70 public class TreeRevFilter extends RevFilter {
71 private static final int PARSED = RevWalk.PARSED;
72
73 private static final int UNINTERESTING = RevWalk.UNINTERESTING;
74
75 private final int rewriteFlag;
76 private final TreeWalk pathFilter;
77
78 /**
79 * Create a {@link RevFilter} from a {@link TreeFilter}.
80 *
81 * @param walker
82 * walker used for reading trees.
83 * @param t
84 * filter to compare against any changed paths in each commit. If a
85 * {@link FollowFilter}, will be replaced with a new filter
86 * following new paths after a rename.
87 * @since 3.5
88 */
89 public TreeRevFilter(final RevWalk walker, final TreeFilter t) {
90 this(walker, t, 0);
91 }
92
93
94 /**
95 * Create a filter for the first phase of a parent-rewriting limited revision
96 * walk.
97 * <p>
98 * This filter is ANDed to evaluate before all other filters and ties the
99 * configured {@link TreeFilter} into the revision walking process.
100 * <p>
101 * If no interesting tree entries are found the commit is colored with
102 * {@code rewriteFlag}, allowing a later pass implemented by
103 * {@link RewriteGenerator} to remove those colored commits from the DAG.
104 *
105 * @see RewriteGenerator
106 *
107 * @param walker
108 * walker used for reading trees.
109 * @param t
110 * filter to compare against any changed paths in each commit. If a
111 * {@link FollowFilter}, will be replaced with a new filter
112 * following new paths after a rename.
113 * @param rewriteFlag
114 * flag to color commits to be removed from the simplified DAT.
115 */
116 TreeRevFilter(final RevWalk walker, final TreeFilter t,
117 final int rewriteFlag) {
118 pathFilter = new TreeWalk(walker.reader);
119 pathFilter.setFilter(t);
120 pathFilter.setRecursive(t.shouldBeRecursive());
121 this.rewriteFlag = rewriteFlag;
122 }
123
124 @Override
125 public RevFilter clone() {
126 throw new UnsupportedOperationException();
127 }
128
129 @Override
130 public boolean include(final RevWalk walker, final RevCommit c)
131 throws StopWalkException, MissingObjectException,
132 IncorrectObjectTypeException, IOException {
133 // Reset the tree filter to scan this commit and parents.
134 //
135 final RevCommit[] pList = c.parents;
136 final int nParents = pList.length;
137 final TreeWalk tw = pathFilter;
138 final ObjectId[] trees = new ObjectId[nParents + 1];
139 for (int i = 0; i < nParents; i++) {
140 final RevCommit p = c.parents[i];
141 if ((p.flags & PARSED) == 0)
142 p.parseHeaders(walker);
143 trees[i] = p.getTree();
144 }
145 trees[nParents] = c.getTree();
146 tw.reset(trees);
147
148 if (nParents == 1) {
149 // We have exactly one parent. This is a very common case.
150 //
151 int chgs = 0, adds = 0;
152 while (tw.next()) {
153 chgs++;
154 if (tw.getRawMode(0) == 0 && tw.getRawMode(1) != 0)
155 adds++;
156 else
157 break; // no point in looking at this further.
158 }
159
160 if (chgs == 0) {
161 // No changes, so our tree is effectively the same as
162 // our parent tree. We pass the buck to our parent.
163 //
164 c.flags |= rewriteFlag;
165 return false;
166 } else {
167 // We have interesting items, but neither of the special
168 // cases denoted above.
169 //
170 if (adds > 0 && tw.getFilter() instanceof FollowFilter) {
171 // One of the paths we care about was added in this
172 // commit. We need to update our filter to its older
173 // name, if we can discover it. Find out what that is.
174 //
175 updateFollowFilter(trees, ((FollowFilter) tw.getFilter()).cfg);
176 }
177 return true;
178 }
179 } else if (nParents == 0) {
180 // We have no parents to compare against. Consider us to be
181 // REWRITE only if we have no paths matching our filter.
182 //
183 if (tw.next())
184 return true;
185 c.flags |= rewriteFlag;
186 return false;
187 }
188
189 // We are a merge commit. We can only be REWRITE if we are same
190 // to _all_ parents. We may also be able to eliminate a parent if
191 // it does not contribute changes to us. Such a parent may be an
192 // uninteresting side branch.
193 //
194 final int[] chgs = new int[nParents];
195 final int[] adds = new int[nParents];
196 while (tw.next()) {
197 final int myMode = tw.getRawMode(nParents);
198 for (int i = 0; i < nParents; i++) {
199 final int pMode = tw.getRawMode(i);
200 if (myMode == pMode && tw.idEqual(i, nParents))
201 continue;
202
203 chgs[i]++;
204 if (pMode == 0 && myMode != 0)
205 adds[i]++;
206 }
207 }
208
209 boolean same = false;
210 boolean diff = false;
211 for (int i = 0; i < nParents; i++) {
212 if (chgs[i] == 0) {
213 // No changes, so our tree is effectively the same as
214 // this parent tree. We pass the buck to only this one
215 // parent commit.
216 //
217
218 final RevCommit p = pList[i];
219 if ((p.flags & UNINTERESTING) != 0) {
220 // This parent was marked as not interesting by the
221 // application. We should look for another parent
222 // that is interesting.
223 //
224 same = true;
225 continue;
226 }
227
228 c.flags |= rewriteFlag;
229 c.parents = new RevCommit[] { p };
230 return false;
231 }
232
233 if (chgs[i] == adds[i]) {
234 // All of the differences from this parent were because we
235 // added files that they did not have. This parent is our
236 // "empty tree root" and thus their history is not relevant.
237 // Cut our grandparents to be an empty list.
238 //
239 pList[i].parents = RevCommit.NO_PARENTS;
240 }
241
242 // We have an interesting difference relative to this parent.
243 //
244 diff = true;
245 }
246
247 if (diff && !same) {
248 // We did not abort above, so we are different in at least one
249 // way from all of our parents. We have to take the blame for
250 // that difference.
251 //
252 return true;
253 }
254
255 // We are the same as all of our parents. We must keep them
256 // as they are and allow those parents to flow into pending
257 // for further scanning.
258 //
259 c.flags |= rewriteFlag;
260 return false;
261 }
262
263 @Override
264 public boolean requiresCommitBody() {
265 return false;
266 }
267
268 private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg)
269 throws MissingObjectException, IncorrectObjectTypeException,
270 CorruptObjectException, IOException {
271 TreeWalk tw = pathFilter;
272 FollowFilter oldFilter = (FollowFilter) tw.getFilter();
273 tw.setFilter(TreeFilter.ANY_DIFF);
274 tw.reset(trees);
275
276 List<DiffEntry> files = DiffEntry.scan(tw);
277 RenameDetector rd = new RenameDetector(tw.getObjectReader(), cfg);
278 rd.addAll(files);
279 files = rd.compute();
280
281 TreeFilter newFilter = oldFilter;
282 for (DiffEntry ent : files) {
283 if (isRename(ent) && ent.getNewPath().equals(oldFilter.getPath())) {
284 newFilter = FollowFilter.create(ent.getOldPath(), cfg);
285 RenameCallback callback = oldFilter.getRenameCallback();
286 if (callback != null) {
287 callback.renamed(ent);
288 // forward the callback to the new follow filter
289 ((FollowFilter) newFilter).setRenameCallback(callback);
290 }
291 break;
292 }
293 }
294 tw.setFilter(newFilter);
295 }
296
297 private static boolean isRename(DiffEntry ent) {
298 return ent.getChangeType() == ChangeType.RENAME
299 || ent.getChangeType() == ChangeType.COPY;
300 }
301 }