View Javadoc
1   /*
2    * Copyright (c) 2020, Google LLC  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    * http://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.internal.revwalk;
11  
12  import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
13  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
14  import org.eclipse.jgit.lib.Constants;
15  import org.eclipse.jgit.lib.AnyObjectId;
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 effect.
23   * <p>
24   * When the walk hits a commit that is the same as {@code cachedCommit} or is
25   * part of {@code bitmap}'s BitmapIndex, that entire bitmap is ORed into
26   * {@code bitmap} and the commit and its parents are marked as SEEN so that the
27   * walk does not have to visit its ancestors. This ensures the walk is very
28   * short if there is good bitmap coverage.
29   */
30  public class AddToBitmapWithCacheFilter extends RevFilter {
31  	private final AnyObjectId cachedCommit;
32  
33  	private final Bitmap cachedBitmap;
34  
35  	private final BitmapBuilder bitmap;
36  
37  	/**
38  	 * Create a filter with a cached BitmapCommit that adds visited commits to
39  	 * the given bitmap.
40  	 *
41  	 * @param cachedCommit
42  	 *            the cached commit
43  	 * @param cachedBitmap
44  	 *            the bitmap corresponds to {@code cachedCommit}}
45  	 * @param bitmap
46  	 *            bitmap to write visited commits to
47  	 */
48  	public AddToBitmapWithCacheFilter(AnyObjectId cachedCommit,
49  			Bitmap cachedBitmap,
50  			BitmapBuilder bitmap) {
51  		this.cachedCommit = cachedCommit;
52  		this.cachedBitmap = cachedBitmap;
53  		this.bitmap = bitmap;
54  	}
55  
56  	/** {@inheritDoc} */
57  	@Override
58  	public final boolean include(RevWalk rw, RevCommit c) {
59  		Bitmap visitedBitmap;
60  
61  		if (bitmap.contains(c)) {
62  			// already included
63  		} else if ((visitedBitmap = bitmap.getBitmapIndex()
64  				.getBitmap(c)) != null) {
65  			bitmap.or(visitedBitmap);
66  		} else if (cachedCommit.equals(c)) {
67  			bitmap.or(cachedBitmap);
68  		} else {
69  			bitmap.addObject(c, Constants.OBJ_COMMIT);
70  			return true;
71  		}
72  
73  		for (RevCommit p : c.getParents()) {
74  			p.add(RevFlag.SEEN);
75  		}
76  		return false;
77  	}
78  
79  	/** {@inheritDoc} */
80  	@Override
81  	public final RevFilter clone() {
82  		throw new UnsupportedOperationException();
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	public final boolean requiresCommitBody() {
88  		return false;
89  	}
90  }
91