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