View Javadoc
1   /*
2    * Copyright (C) 2010, Garmin International
3    * Copyright (C) 2010, Matt Fischer <matt.fischer@garmin.com>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.revwalk;
46  
47  import java.io.IOException;
48  
49  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
50  import org.eclipse.jgit.errors.MissingObjectException;
51  import org.eclipse.jgit.lib.AnyObjectId;
52  import org.eclipse.jgit.lib.ObjectReader;
53  import org.eclipse.jgit.lib.Repository;
54  
55  /**
56   * Interface for revision walkers that perform depth filtering.
57   */
58  public interface DepthWalk {
59  	/**
60  	 * Get depth to filter to.
61  	 *
62  	 * @return Depth to filter to.
63  	 */
64  	public int getDepth();
65  
66  	/** @return flag marking commits that should become unshallow. */
67  	/**
68  	 * Get flag marking commits that should become unshallow.
69  	 *
70  	 * @return flag marking commits that should become unshallow.
71  	 */
72  	public RevFlag getUnshallowFlag();
73  
74  	/**
75  	 * Get flag marking commits that are interesting again.
76  	 *
77  	 * @return flag marking commits that are interesting again.
78  	 */
79  	public RevFlag getReinterestingFlag();
80  
81  	/** RevCommit with a depth (in commits) from a root. */
82  	public static class Commit extends RevCommit {
83  		/** Depth of this commit in the graph, via shortest path. */
84  		int depth;
85  
86  		/** @return depth of this commit, as found by the shortest path. */
87  		public int getDepth() {
88  			return depth;
89  		}
90  
91  		/**
92  		 * Initialize a new commit.
93  		 *
94  		 * @param id
95  		 *            object name for the commit.
96  		 */
97  		protected Commit(AnyObjectId id) {
98  			super(id);
99  			depth = -1;
100 		}
101 	}
102 
103 	/** Subclass of RevWalk that performs depth filtering. */
104 	public class RevWalk extends org.eclipse.jgit.revwalk.RevWalk implements DepthWalk {
105 		private final int depth;
106 
107 		private final RevFlag UNSHALLOW;
108 
109 		private final RevFlag REINTERESTING;
110 
111 		/**
112 		 * @param repo Repository to walk
113 		 * @param depth Maximum depth to return
114 		 */
115 		public RevWalk(Repository repo, int depth) {
116 			super(repo);
117 
118 			this.depth = depth;
119 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
120 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
121 		}
122 
123 		/**
124 		 * @param or ObjectReader to use
125 		 * @param depth Maximum depth to return
126 		 */
127 		public RevWalk(ObjectReader or, int depth) {
128 			super(or);
129 
130 			this.depth = depth;
131 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
132 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
133 		}
134 
135 		/**
136 		 * Mark a root commit (i.e., one whose depth should be considered 0.)
137 		 *
138 		 * @param c
139 		 *            Commit to mark
140 		 * @throws IOException
141 		 * @throws IncorrectObjectTypeException
142 		 * @throws MissingObjectException
143 		 */
144 		public void markRoot(RevCommit c) throws MissingObjectException,
145 				IncorrectObjectTypeException, IOException {
146 			if (c instanceof Commit)
147 				((Commit) c).depth = 0;
148 			super.markStart(c);
149 		}
150 
151 		@Override
152 		protected RevCommit createCommit(AnyObjectId id) {
153 			return new Commit(id);
154 		}
155 
156 		@Override
157 		public int getDepth() {
158 			return depth;
159 		}
160 
161 		@Override
162 		public RevFlag getUnshallowFlag() {
163 			return UNSHALLOW;
164 		}
165 
166 		@Override
167 		public RevFlag getReinterestingFlag() {
168 			return REINTERESTING;
169 		}
170 
171 		/**
172 		 * @since 4.5
173 		 */
174 		@Override
175 		public ObjectWalk toObjectWalkWithSameObjects() {
176 			ObjectWalk ow = new ObjectWalk(reader, depth);
177 			ow.objects = objects;
178 			ow.freeFlags = freeFlags;
179 			return ow;
180 		}
181 	}
182 
183 	/** Subclass of ObjectWalk that performs depth filtering. */
184 	public class ObjectWalk extends org.eclipse.jgit.revwalk.ObjectWalk implements DepthWalk {
185 		private final int depth;
186 
187 		private final RevFlag UNSHALLOW;
188 
189 		private final RevFlag REINTERESTING;
190 
191 		/**
192 		 * @param repo Repository to walk
193 		 * @param depth Maximum depth to return
194 		 */
195 		public ObjectWalk(Repository repo, int depth) {
196 			super(repo);
197 
198 			this.depth = depth;
199 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
200 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
201 		}
202 
203 		/**
204 		 * @param or Object Reader
205 		 * @param depth Maximum depth to return
206 		 */
207 		public ObjectWalk(ObjectReader or, int depth) {
208 			super(or);
209 
210 			this.depth = depth;
211 			this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
212 			this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
213 		}
214 
215 		/**
216 		 * Mark a root commit (i.e., one whose depth should be considered 0.)
217 		 *
218 		 * @param o
219 		 *            Commit to mark
220 		 * @throws IOException
221 		 * @throws IncorrectObjectTypeException
222 		 * @throws MissingObjectException
223 		 */
224 		public void markRoot(RevObject o) throws MissingObjectException,
225 				IncorrectObjectTypeException, IOException {
226 			RevObject c = o;
227 			while (c instanceof RevTag) {
228 				c = ((RevTag) c).getObject();
229 				parseHeaders(c);
230 			}
231 			if (c instanceof Commit)
232 				((Commit) c).depth = 0;
233 			super.markStart(o);
234 		}
235 
236 		/**
237 		 * Mark an element which used to be shallow in the client, but which
238 		 * should now be considered a full commit. Any ancestors of this commit
239 		 * should be included in the walk, even if they are the ancestor of an
240 		 * uninteresting commit.
241 		 *
242 		 * @param c
243 		 *            Commit to mark
244 		 * @throws MissingObjectException
245 		 * @throws IncorrectObjectTypeException
246 		 * @throws IOException
247 		 */
248 		public void markUnshallow(RevObject c) throws MissingObjectException,
249 				IncorrectObjectTypeException, IOException {
250 			if (c instanceof RevCommit)
251 				c.add(UNSHALLOW);
252 			super.markStart(c);
253 		}
254 
255 		@Override
256 		protected RevCommit createCommit(AnyObjectId id) {
257 			return new Commit(id);
258 		}
259 
260 		@Override
261 		public int getDepth() {
262 			return depth;
263 		}
264 
265 		@Override
266 		public RevFlag getUnshallowFlag() {
267 			return UNSHALLOW;
268 		}
269 
270 		@Override
271 		public RevFlag getReinterestingFlag() {
272 			return REINTERESTING;
273 		}
274 	}
275 }