View Javadoc
1   /*
2    * Copyright (C) 2010, Garmin International
3    * Copyright (C) 2010, Matt Fischer <matt.fischer@garmin.com> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.revwalk;
13  
14  import java.io.IOException;
15  import java.util.Collections;
16  import java.util.List;
17  import java.util.Objects;
18  
19  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
20  import org.eclipse.jgit.errors.MissingObjectException;
21  import org.eclipse.jgit.lib.AnyObjectId;
22  import org.eclipse.jgit.lib.ObjectId;
23  import org.eclipse.jgit.lib.ObjectReader;
24  import org.eclipse.jgit.lib.Repository;
25  
26  /**
27   * Interface for revision walkers that perform depth filtering.
28   */
29  public interface DepthWalk {
30  	/**
31  	 * Get depth to filter to.
32  	 *
33  	 * @return Depth to filter to.
34  	 */
35  	int getDepth();
36  
37  	/**
38  	 * @return the deepen-since value; if not 0, this walk only returns commits
39  	 *         whose commit time is at or after this limit
40  	 * @since 5.2
41  	 */
42  	default int getDeepenSince() {
43  		return 0;
44  	}
45  
46  	/**
47  	 * @return the objects specified by the client using --shallow-exclude
48  	 * @since 5.2
49  	 */
50  	default List<ObjectId> getDeepenNots() {
51  		return Collections.emptyList();
52  	}
53  
54  	/** @return flag marking commits that should become unshallow. */
55  	/**
56  	 * Get flag marking commits that should become unshallow.
57  	 *
58  	 * @return flag marking commits that should become unshallow.
59  	 */
60  	RevFlag getUnshallowFlag();
61  
62  	/**
63  	 * Get flag marking commits that are interesting again.
64  	 *
65  	 * @return flag marking commits that are interesting again.
66  	 */
67  	RevFlag getReinterestingFlag();
68  
69  	/**
70  	 * @return flag marking commits that are to be excluded because of --shallow-exclude
71  	 * @since 5.2
72  	 */
73  	RevFlag getDeepenNotFlag();
74  
75  	/** RevCommit with a depth (in commits) from a root. */
76  	public static class Commit extends RevCommit {
77  		/** Depth of this commit in the graph, via shortest path. */
78  		int depth;
79  
80  		boolean isBoundary;
81  
82  		/**
83  		 * True if this commit was excluded due to a shallow fetch
84  		 * setting. All its children are thus boundary commits.
85  		 */
86  		boolean makesChildBoundary;
87  
88  		/** @return depth of this commit, as found by the shortest path. */
89  		public int getDepth() {
90  			return depth;
91  		}
92  
93  		/**
94  		 * @return true if at least one of this commit's parents was excluded
95  		 *         due to a shallow fetch setting, false otherwise
96  		 * @since 5.2
97  		 */
98  		public boolean isBoundary() {
99  			return isBoundary;
100 		}
101 
102 		/**
103 		 * Initialize a new commit.
104 		 *
105 		 * @param id
106 		 *            object name for the commit.
107 		 */
108 		protected Commit(AnyObjectId id) {
109 			super(id);
110 			depth = -1;
111 		}
112 	}
113 
114 	/** Subclass of RevWalk that performs depth filtering. */
115 	public class RevWalk extends org.eclipse.jgit.revwalk.RevWalk implements DepthWalk {
116 		private final int depth;
117 
118 		private int deepenSince;
119 
120 		private List<ObjectId> deepenNots;
121 
122 		private final RevFlag UNSHALLOW;
123 
124 		private final RevFlag REINTERESTING;
125 
126 		private final RevFlag DEEPEN_NOT;
127 
128 		/**
129 		 * @param repo Repository to walk
130 		 * @param depth Maximum depth to return
131 		 */
132 		public RevWalk(Repository repo, int depth) {
133 			super(repo);
134 
135 			this.depth = depth;
136 			this.deepenNots = Collections.emptyList();
137 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
138 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
139 			this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
140 		}
141 
142 		/**
143 		 * @param or ObjectReader to use
144 		 * @param depth Maximum depth to return
145 		 */
146 		public RevWalk(ObjectReader or, int depth) {
147 			super(or);
148 
149 			this.depth = depth;
150 			this.deepenNots = Collections.emptyList();
151 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
152 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
153 			this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
154 		}
155 
156 		/**
157 		 * Mark a root commit (i.e., one whose depth should be considered 0.)
158 		 *
159 		 * @param c
160 		 *            Commit to mark
161 		 * @throws IOException
162 		 * @throws IncorrectObjectTypeException
163 		 * @throws MissingObjectException
164 		 */
165 		public void markRoot(RevCommit c) throws MissingObjectException,
166 				IncorrectObjectTypeException, IOException {
167 			if (c instanceof Commit)
168 				((Commit) c).depth = 0;
169 			super.markStart(c);
170 		}
171 
172 		@Override
173 		protected RevCommit createCommit(AnyObjectId id) {
174 			return new Commit(id);
175 		}
176 
177 		@Override
178 		public int getDepth() {
179 			return depth;
180 		}
181 
182 		@Override
183 		public int getDeepenSince() {
184 			return deepenSince;
185 		}
186 
187 		/**
188 		 * Sets the deepen-since value.
189 		 *
190 		 * @param limit
191 		 *            new deepen-since value
192 		 * @since 5.2
193 		 */
194 		public void setDeepenSince(int limit) {
195 			deepenSince = limit;
196 		}
197 
198 		@Override
199 		public List<ObjectId> getDeepenNots() {
200 			return deepenNots;
201 		}
202 
203 		/**
204 		 * Mark objects that the client specified using
205 		 * --shallow-exclude. Objects that are not commits have no
206 		 * effect.
207 		 *
208 		 * @param deepenNots specified objects
209 		 * @since 5.2
210 		 */
211 		public void setDeepenNots(List<ObjectId> deepenNots) {
212 			this.deepenNots = Objects.requireNonNull(deepenNots);
213 		}
214 
215 		@Override
216 		public RevFlag getUnshallowFlag() {
217 			return UNSHALLOW;
218 		}
219 
220 		@Override
221 		public RevFlag getReinterestingFlag() {
222 			return REINTERESTING;
223 		}
224 
225 		@Override
226 		public RevFlag getDeepenNotFlag() {
227 			return DEEPEN_NOT;
228 		}
229 
230 		/**
231 		 * @since 4.5
232 		 */
233 		@Override
234 		public ObjectWalk toObjectWalkWithSameObjects() {
235 			ObjectWalk ow = new ObjectWalk(reader, depth);
236 			ow.deepenSince = deepenSince;
237 			ow.deepenNots = deepenNots;
238 			ow.objects = objects;
239 			ow.freeFlags = freeFlags;
240 			return ow;
241 		}
242 	}
243 
244 	/** Subclass of ObjectWalk that performs depth filtering. */
245 	public class ObjectWalk extends org.eclipse.jgit.revwalk.ObjectWalk implements DepthWalk {
246 		private final int depth;
247 
248 		private int deepenSince;
249 
250 		private List<ObjectId> deepenNots;
251 
252 		private final RevFlag UNSHALLOW;
253 
254 		private final RevFlag REINTERESTING;
255 
256 		private final RevFlag DEEPEN_NOT;
257 
258 		/**
259 		 * @param repo Repository to walk
260 		 * @param depth Maximum depth to return
261 		 */
262 		public ObjectWalk(Repository repo, int depth) {
263 			super(repo);
264 
265 			this.depth = depth;
266 			this.deepenNots = Collections.emptyList();
267 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
268 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
269 			this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
270 		}
271 
272 		/**
273 		 * @param or Object Reader
274 		 * @param depth Maximum depth to return
275 		 */
276 		public ObjectWalk(ObjectReader or, int depth) {
277 			super(or);
278 
279 			this.depth = depth;
280 			this.deepenNots = Collections.emptyList();
281 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
282 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
283 			this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
284 		}
285 
286 		/**
287 		 * Mark a root commit (i.e., one whose depth should be considered 0.)
288 		 *
289 		 * @param o
290 		 *            Commit to mark
291 		 * @throws IOException
292 		 * @throws IncorrectObjectTypeException
293 		 * @throws MissingObjectException
294 		 */
295 		public void markRoot(RevObject o) throws MissingObjectException,
296 				IncorrectObjectTypeException, IOException {
297 			RevObject c = o;
298 			while (c instanceof RevTag) {
299 				c = ((RevTag) c).getObject();
300 				parseHeaders(c);
301 			}
302 			if (c instanceof Commit)
303 				((Commit) c).depth = 0;
304 			super.markStart(o);
305 		}
306 
307 		/**
308 		 * Mark an element which used to be shallow in the client, but which
309 		 * should now be considered a full commit. Any ancestors of this commit
310 		 * should be included in the walk, even if they are the ancestor of an
311 		 * uninteresting commit.
312 		 *
313 		 * @param c
314 		 *            Commit to mark
315 		 * @throws MissingObjectException
316 		 * @throws IncorrectObjectTypeException
317 		 * @throws IOException
318 		 */
319 		public void markUnshallow(RevObject c) throws MissingObjectException,
320 				IncorrectObjectTypeException, IOException {
321 			if (c instanceof RevCommit)
322 				c.add(UNSHALLOW);
323 			super.markStart(c);
324 		}
325 
326 		@Override
327 		protected RevCommit createCommit(AnyObjectId id) {
328 			return new Commit(id);
329 		}
330 
331 		@Override
332 		public int getDepth() {
333 			return depth;
334 		}
335 
336 		@Override
337 		public int getDeepenSince() {
338 			return deepenSince;
339 		}
340 
341 		@Override
342 		public List<ObjectId> getDeepenNots() {
343 			return deepenNots;
344 		}
345 
346 		@Override
347 		public RevFlag getUnshallowFlag() {
348 			return UNSHALLOW;
349 		}
350 
351 		@Override
352 		public RevFlag getReinterestingFlag() {
353 			return REINTERESTING;
354 		}
355 
356 		@Override
357 		public RevFlag getDeepenNotFlag() {
358 			return DEEPEN_NOT;
359 		}
360 	}
361 }