View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.revwalk;
45  
46  import static org.junit.Assert.assertNull;
47  import static org.junit.Assert.assertSame;
48  import static org.junit.Assert.assertTrue;
49  
50  import org.eclipse.jgit.lib.FileMode;
51  import org.eclipse.jgit.lib.ObjectId;
52  import org.eclipse.jgit.lib.ObjectInserter;
53  import org.eclipse.jgit.lib.TreeFormatter;
54  import org.junit.Test;
55  
56  public class ObjectWalkTest extends RevWalkTestCase {
57  	protected ObjectWalk objw;
58  
59  	@Override
60  	protected RevWalk createRevWalk() {
61  		return objw = new ObjectWalk(db);
62  	}
63  
64  	@Test
65  	public void testNoCommits() throws Exception {
66  		assertNull(objw.next());
67  		assertNull(objw.nextObject());
68  	}
69  
70  	@Test
71  	public void testTwoCommitsEmptyTree() throws Exception {
72  		final RevCommit a = commit();
73  		final RevCommit b = commit(a);
74  		markStart(b);
75  
76  		assertCommit(b, objw.next());
77  		assertCommit(a, objw.next());
78  		assertNull(objw.next());
79  
80  		assertSame(tree(), objw.nextObject());
81  		assertNull(objw.nextObject());
82  	}
83  
84  	@Test
85  	public void testOneCommitOneTreeTwoBlob() throws Exception {
86  		final RevBlob f0 = blob("0");
87  		final RevBlob f1 = blob("1");
88  		final RevTree t = tree(file("0", f0), file("1", f1), file("2", f1));
89  		final RevCommit a = commit(t);
90  		markStart(a);
91  
92  		assertCommit(a, objw.next());
93  		assertNull(objw.next());
94  
95  		assertSame(t, objw.nextObject());
96  		assertSame(f0, objw.nextObject());
97  		assertSame(f1, objw.nextObject());
98  		assertNull(objw.nextObject());
99  	}
100 
101 	@Test
102 	public void testTwoCommitTwoTreeTwoBlob() throws Exception {
103 		final RevBlob f0 = blob("0");
104 		final RevBlob f1 = blob("1");
105 		final RevBlob f2 = blob("0v2");
106 		final RevTree ta = tree(file("0", f0), file("1", f1), file("2", f1));
107 		final RevTree tb = tree(file("0", f2), file("1", f1), file("2", f1));
108 		final RevCommit a = commit(ta);
109 		final RevCommit b = commit(tb, a);
110 		markStart(b);
111 
112 		assertCommit(b, objw.next());
113 		assertCommit(a, objw.next());
114 		assertNull(objw.next());
115 
116 		assertSame(tb, objw.nextObject());
117 		assertSame(f2, objw.nextObject());
118 		assertSame(f1, objw.nextObject());
119 
120 		assertSame(ta, objw.nextObject());
121 		assertSame(f0, objw.nextObject());
122 
123 		assertNull(objw.nextObject());
124 	}
125 
126 	@Test
127 	public void testTwoCommitDeepTree1() throws Exception {
128 		final RevBlob f0 = blob("0");
129 		final RevBlob f1 = blob("0v2");
130 		final RevTree ta = tree(file("a/b/0", f0));
131 		final RevTree tb = tree(file("a/b/1", f1));
132 		final RevCommit a = commit(ta);
133 		final RevCommit b = commit(tb, a);
134 		markStart(b);
135 
136 		assertCommit(b, objw.next());
137 		assertCommit(a, objw.next());
138 		assertNull(objw.next());
139 
140 		assertSame(tb, objw.nextObject());
141 		assertSame(get(tb, "a"), objw.nextObject());
142 		assertSame(get(tb, "a/b"), objw.nextObject());
143 		assertSame(f1, objw.nextObject());
144 
145 		assertSame(ta, objw.nextObject());
146 		assertSame(get(ta, "a"), objw.nextObject());
147 		assertSame(get(ta, "a/b"), objw.nextObject());
148 		assertSame(f0, objw.nextObject());
149 
150 		assertNull(objw.nextObject());
151 	}
152 
153 	@Test
154 	public void testTwoCommitDeepTree2() throws Exception {
155 		final RevBlob f1 = blob("1");
156 		final RevTree ta = tree(file("a/b/0", f1), file("a/c/q", f1));
157 		final RevTree tb = tree(file("a/b/1", f1), file("a/c/q", f1));
158 		final RevCommit a = commit(ta);
159 		final RevCommit b = commit(tb, a);
160 		markStart(b);
161 
162 		assertCommit(b, objw.next());
163 		assertCommit(a, objw.next());
164 		assertNull(objw.next());
165 
166 		assertSame(tb, objw.nextObject());
167 		assertSame(get(tb, "a"), objw.nextObject());
168 		assertSame(get(tb, "a/b"), objw.nextObject());
169 		assertSame(f1, objw.nextObject());
170 		assertSame(get(tb, "a/c"), objw.nextObject());
171 
172 		assertSame(ta, objw.nextObject());
173 		assertSame(get(ta, "a"), objw.nextObject());
174 		assertSame(get(ta, "a/b"), objw.nextObject());
175 
176 		assertNull(objw.nextObject());
177 	}
178 
179 	@Test
180 	public void testCull() throws Exception {
181 		final RevBlob f1 = blob("1");
182 		final RevBlob f2 = blob("2");
183 		final RevBlob f3 = blob("3");
184 		final RevBlob f4 = blob("4");
185 
186 		final RevTree ta = tree(file("a/1", f1), file("c/3", f3));
187 		final RevCommit a = commit(ta);
188 
189 		final RevTree tb = tree(file("a/1", f2), file("c/3", f3));
190 		final RevCommit b1 = commit(tb, a);
191 		final RevCommit b2 = commit(tb, b1);
192 
193 		final RevTree tc = tree(file("a/1", f4));
194 		final RevCommit c1 = commit(tc, a);
195 		final RevCommit c2 = commit(tc, c1);
196 
197 		markStart(b2);
198 		markUninteresting(c2);
199 
200 		assertCommit(b2, objw.next());
201 		assertCommit(b1, objw.next());
202 		assertNull(objw.next());
203 
204 		assertTrue(a.has(RevFlag.UNINTERESTING));
205 		assertTrue(ta.has(RevFlag.UNINTERESTING));
206 		assertTrue(f1.has(RevFlag.UNINTERESTING));
207 		assertTrue(f3.has(RevFlag.UNINTERESTING));
208 
209 		assertSame(tb, objw.nextObject());
210 		assertSame(get(tb, "a"), objw.nextObject());
211 		assertSame(f2, objw.nextObject());
212 		assertNull(objw.nextObject());
213 	}
214 
215 	@Test
216 	public void testEmptyTreeCorruption() throws Exception {
217 		ObjectId bId = ObjectId
218 				.fromString("abbbfafe3129f85747aba7bfac992af77134c607");
219 		final RevTree tree_root, tree_A, tree_AB;
220 		final RevCommit b;
221 		try (ObjectInserter inserter = db.newObjectInserter()) {
222 			ObjectId empty = inserter.insert(new TreeFormatter());
223 
224 			TreeFormatter A = new TreeFormatter();
225 			A.append("A", FileMode.TREE, empty);
226 			A.append("B", FileMode.TREE, empty);
227 			ObjectId idA = inserter.insert(A);
228 
229 			TreeFormatter root = new TreeFormatter();
230 			root.append("A", FileMode.TREE, idA);
231 			root.append("B", FileMode.REGULAR_FILE, bId);
232 			ObjectId idRoot = inserter.insert(root);
233 			inserter.flush();
234 
235 			tree_root = objw.parseTree(idRoot);
236 			tree_A = objw.parseTree(idA);
237 			tree_AB = objw.parseTree(empty);
238 			b = commit(tree_root);
239 		}
240 
241 		markStart(b);
242 
243 		assertCommit(b, objw.next());
244 		assertNull(objw.next());
245 
246 		assertSame(tree_root, objw.nextObject());
247 		assertSame(tree_A, objw.nextObject());
248 		assertSame(tree_AB, objw.nextObject());
249 		assertSame(rw.lookupBlob(bId), objw.nextObject());
250 		assertNull(objw.nextObject());
251 	}
252 }