View Javadoc
1   /*
2    * Copyright (C) 2012, 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.revwalk;
12  
13  import java.io.IOException;
14  import java.util.Arrays;
15  
16  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
17  import org.eclipse.jgit.errors.MissingObjectException;
18  import org.eclipse.jgit.internal.revwalk.AddToBitmapFilter;
19  import org.eclipse.jgit.internal.revwalk.AddToBitmapWithCacheFilter;
20  import org.eclipse.jgit.internal.revwalk.AddUnseenToBitmapFilter;
21  import org.eclipse.jgit.lib.AnyObjectId;
22  import org.eclipse.jgit.lib.BitmapIndex;
23  import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
24  import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
25  import org.eclipse.jgit.lib.NullProgressMonitor;
26  import org.eclipse.jgit.lib.ObjectId;
27  import org.eclipse.jgit.lib.ProgressMonitor;
28  import org.eclipse.jgit.revwalk.filter.ObjectFilter;
29  
30  /**
31   * Helper class to do ObjectWalks with pack index bitmaps.
32   *
33   * @since 4.10
34   */
35  public final class BitmapWalker {
36  
37  	private final ObjectWalk walker;
38  
39  	private final BitmapIndex bitmapIndex;
40  
41  	private final ProgressMonitor pm;
42  
43  	private long countOfBitmapIndexMisses;
44  
45  	// Cached bitmap and commit to save walk time.
46  	private AnyObjectId prevCommit;
47  
48  	private Bitmap prevBitmap;
49  
50  	/**
51  	 * Create a BitmapWalker.
52  	 *
53  	 * @param walker walker to use when traversing the object graph.
54  	 * @param bitmapIndex index to obtain bitmaps from.
55  	 * @param pm progress monitor to report progress on.
56  	 */
57  	public BitmapWalker(
58  			ObjectWalk walker, BitmapIndex bitmapIndex, ProgressMonitor pm) {
59  		this.walker = walker;
60  		this.bitmapIndex = bitmapIndex;
61  		this.pm = (pm == null) ? NullProgressMonitor.INSTANCE : pm;
62  	}
63  
64  	/**
65  	 * Set the cached commit for the walker.
66  	 *
67  	 * @param prevCommit
68  	 *            the cached commit.
69  	 * @since 5.8
70  	 */
71  	public void setPrevCommit(AnyObjectId prevCommit) {
72  		this.prevCommit = prevCommit;
73  	}
74  
75  	/**
76  	 * Set the bitmap associated with the cached commit for the walker.
77  	 *
78  	 * @param prevBitmap
79  	 *            the bitmap associated with the cached commit.
80  	 * @since 5.8
81  	 */
82  	public void setPrevBitmap(Bitmap prevBitmap) {
83  		this.prevBitmap = prevBitmap;
84  	}
85  
86  	/**
87  	 * Return the number of objects that had to be walked because they were not covered by a
88  	 * bitmap.
89  	 *
90  	 * @return the number of objects that had to be walked because they were not covered by a
91  	 *     bitmap.
92  	 */
93  	public long getCountOfBitmapIndexMisses() {
94  		return countOfBitmapIndexMisses;
95  	}
96  
97  	/**
98  	 * Return, as a bitmap, the objects reachable from the objects in start.
99  	 *
100 	 * @param start
101 	 *            the objects to start the object traversal from.
102 	 * @param seen
103 	 *            the objects to skip if encountered during traversal.
104 	 * @param ignoreMissing
105 	 *            true to ignore missing objects, false otherwise.
106 	 * @return as a bitmap, the objects reachable from the objects in start.
107 	 * @throws org.eclipse.jgit.errors.MissingObjectException
108 	 *             the object supplied is not available from the object
109 	 *             database. This usually indicates the supplied object is
110 	 *             invalid, but the reference was constructed during an earlier
111 	 *             invocation to
112 	 *             {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}.
113 	 * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
114 	 *             the object was not parsed yet and it was discovered during
115 	 *             parsing that it is not actually the type of the instance
116 	 *             passed in. This usually indicates the caller used the wrong
117 	 *             type in a
118 	 *             {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}
119 	 *             call.
120 	 * @throws java.io.IOException
121 	 *             a pack file or loose object could not be read.
122 	 */
123 	public BitmapBuilder findObjects(Iterable<? extends ObjectId> start, BitmapBuilder seen,
124 			boolean ignoreMissing)
125 			throws MissingObjectException, IncorrectObjectTypeException,
126 				   IOException {
127 		if (!ignoreMissing) {
128 			return findObjectsWalk(start, seen, false);
129 		}
130 
131 		try {
132 			return findObjectsWalk(start, seen, true);
133 		} catch (MissingObjectException ignore) {
134 			// An object reachable from one of the "start"s is missing.
135 			// Walk from the "start"s one at a time so it can be excluded.
136 		}
137 
138 		final BitmapBuilder result = bitmapIndex.newBitmapBuilder();
139 		for (ObjectId obj : start) {
140 			Bitmap bitmap = bitmapIndex.getBitmap(obj);
141 			if (bitmap != null) {
142 				result.or(bitmap);
143 			}
144 		}
145 
146 		for (ObjectId obj : start) {
147 			if (result.contains(obj)) {
148 				continue;
149 			}
150 			try {
151 				result.or(findObjectsWalk(Arrays.asList(obj), result, false));
152 			} catch (MissingObjectException ignore) {
153 				// An object reachable from this "start" is missing.
154 				//
155 				// This can happen when the client specified a "have" line
156 				// pointing to an object that is present but unreachable:
157 				// "git prune" and "git fsck" only guarantee that the object
158 				// database will continue to contain all objects reachable
159 				// from a ref and does not guarantee connectivity for other
160 				// objects in the object database.
161 				//
162 				// In this situation, skip the relevant "start" and move on
163 				// to the next one.
164 				//
165 				// TODO(czhen): Make findObjectsWalk resume the walk instead
166 				// once RevWalk and ObjectWalk support that.
167 			}
168 		}
169 		return result;
170 	}
171 
172 	private BitmapBuilder findObjectsWalk(Iterable<? extends ObjectId> start, BitmapBuilder seen,
173 			boolean ignoreMissingStart)
174 			throws MissingObjectException, IncorrectObjectTypeException,
175 			IOException {
176 		walker.reset();
177 		final BitmapBuilder bitmapResult = bitmapIndex.newBitmapBuilder();
178 
179 		for (ObjectId obj : start) {
180 			Bitmap bitmap = bitmapIndex.getBitmap(obj);
181 			if (bitmap != null)
182 				bitmapResult.or(bitmap);
183 		}
184 
185 		boolean marked = false;
186 		for (ObjectId obj : start) {
187 			try {
188 				if (!bitmapResult.contains(obj)) {
189 					walker.markStart(walker.parseAny(obj));
190 					marked = true;
191 				}
192 			} catch (MissingObjectException e) {
193 				if (ignoreMissingStart)
194 					continue;
195 				throw e;
196 			}
197 		}
198 
199 		if (marked) {
200 			if (prevCommit != null) {
201 				walker.setRevFilter(new AddToBitmapWithCacheFilter(prevCommit,
202 						prevBitmap, bitmapResult));
203 			} else if (seen == null) {
204 				walker.setRevFilter(new AddToBitmapFilter(bitmapResult));
205 			} else {
206 				walker.setRevFilter(
207 						new AddUnseenToBitmapFilter(seen, bitmapResult));
208 			}
209 			walker.setObjectFilter(new BitmapObjectFilter(bitmapResult));
210 
211 			while (walker.next() != null) {
212 				// Iterate through all of the commits. The BitmapRevFilter does
213 				// the work.
214 				//
215 				// filter.include returns true for commits that do not have
216 				// a bitmap in bitmapIndex and are not reachable from a
217 				// bitmap in bitmapIndex encountered earlier in the walk.
218 				// Thus the number of commits returned by next() measures how
219 				// much history was traversed without being able to make use
220 				// of bitmaps.
221 				pm.update(1);
222 				countOfBitmapIndexMisses++;
223 			}
224 
225 			RevObject ro;
226 			while ((ro = walker.nextObject()) != null) {
227 				bitmapResult.addObject(ro, ro.getType());
228 				pm.update(1);
229 			}
230 		}
231 
232 		return bitmapResult;
233 	}
234 
235 	/**
236 	 * Filter that excludes objects already in the given bitmap.
237 	 */
238 	static class BitmapObjectFilter extends ObjectFilter {
239 		private final BitmapBuilder bitmap;
240 
241 		BitmapObjectFilter(BitmapBuilder bitmap) {
242 			this.bitmap = bitmap;
243 		}
244 
245 		@Override
246 		public final boolean include(ObjectWalk walker, AnyObjectId objid)
247 			throws MissingObjectException, IncorrectObjectTypeException,
248 			       IOException {
249 			return !bitmap.contains(objid);
250 		}
251 	}
252 }