View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.revwalk;
12  
13  import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
14  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
15  import org.eclipse.jgit.lib.Constants;
16  import org.eclipse.jgit.revwalk.filter.RevFilter;
17  import org.eclipse.jgit.revwalk.RevWalk;
18  import org.eclipse.jgit.revwalk.RevCommit;
19  import org.eclipse.jgit.revwalk.RevFlag;
20  
21  /**
22   * A RevFilter that adds the visited commits to {@code bitmap} as a side
23   * effect.
24   * <p>
25   * When the walk hits a commit that is part of {@code bitmap}'s
26   * BitmapIndex, that entire bitmap is ORed into {@code bitmap} and the
27   * commit and its parents are marked as SEEN so that the walk does not
28   * have to visit its ancestors.  This ensures the walk is very short if
29   * there is good bitmap coverage.
30   */
31  public class AddToBitmapFilter extends RevFilter {
32  	private final BitmapBuilder bitmap;
33  
34  	/**
35  	 * Create a filter that adds visited commits to the given bitmap.
36  	 *
37  	 * @param bitmap bitmap to write visited commits to
38  	 */
39  	public AddToBitmapFilter(BitmapBuilder bitmap) {
40  		this.bitmap = bitmap;
41  	}
42  
43  	/** {@inheritDoc} */
44  	@Override
45  	public final boolean include(RevWalk walker, RevCommit cmit) {
46  		Bitmap visitedBitmap;
47  
48  		if (bitmap.contains(cmit)) {
49  			// already included
50  		} else if ((visitedBitmap = bitmap.getBitmapIndex()
51  				.getBitmap(cmit)) != null) {
52  			bitmap.or(visitedBitmap);
53  		} else {
54  			bitmap.addObject(cmit, Constants.OBJ_COMMIT);
55  			return true;
56  		}
57  
58  		for (RevCommit p : cmit.getParents()) {
59  			p.add(RevFlag.SEEN);
60  		}
61  		return false;
62  	}
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	public final RevFilter clone() {
67  		throw new UnsupportedOperationException();
68  	}
69  
70  	/** {@inheritDoc} */
71  	@Override
72  	public final boolean requiresCommitBody() {
73  		return false;
74  	}
75  }