View Javadoc
1   /*
2    * Copyright (C) 2009-2010, 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 static org.junit.Assert.assertNull;
14  import static org.junit.Assert.assertSame;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.eclipse.jgit.lib.FileMode;
18  import org.eclipse.jgit.lib.ObjectId;
19  import org.eclipse.jgit.lib.ObjectInserter;
20  import org.eclipse.jgit.lib.TreeFormatter;
21  import org.junit.Test;
22  
23  public class ObjectWalkTest extends RevWalkTestCase {
24  	protected ObjectWalk objw;
25  
26  	@Override
27  	protected RevWalk createRevWalk() {
28  		return objw = new ObjectWalk(db);
29  	}
30  
31  	@Test
32  	public void testNoCommits() throws Exception {
33  		assertNull(objw.next());
34  		assertNull(objw.nextObject());
35  	}
36  
37  	@Test
38  	public void testTwoCommitsEmptyTree() throws Exception {
39  		final RevCommit a = commit();
40  		final RevCommit b = commit(a);
41  		markStart(b);
42  
43  		assertCommit(b, objw.next());
44  		assertCommit(a, objw.next());
45  		assertNull(objw.next());
46  
47  		assertSame(tree(), objw.nextObject());
48  		assertNull(objw.nextObject());
49  	}
50  
51  	@Test
52  	public void testOneCommitOneTreeTwoBlob() throws Exception {
53  		final RevBlob f0 = blob("0");
54  		final RevBlob f1 = blob("1");
55  		final RevTree t = tree(file("0", f0), file("1", f1), file("2", f1));
56  		final RevCommit a = commit(t);
57  		markStart(a);
58  
59  		assertCommit(a, objw.next());
60  		assertNull(objw.next());
61  
62  		assertSame(t, objw.nextObject());
63  		assertSame(f0, objw.nextObject());
64  		assertSame(f1, objw.nextObject());
65  		assertNull(objw.nextObject());
66  	}
67  
68  	@Test
69  	public void testTwoCommitTwoTreeTwoBlob() throws Exception {
70  		final RevBlob f0 = blob("0");
71  		final RevBlob f1 = blob("1");
72  		final RevBlob f2 = blob("0v2");
73  		final RevTree ta = tree(file("0", f0), file("1", f1), file("2", f1));
74  		final RevTree tb = tree(file("0", f2), file("1", f1), file("2", f1));
75  		final RevCommit a = commit(ta);
76  		final RevCommit b = commit(tb, a);
77  		markStart(b);
78  
79  		assertCommit(b, objw.next());
80  		assertCommit(a, objw.next());
81  		assertNull(objw.next());
82  
83  		assertSame(tb, objw.nextObject());
84  		assertSame(f2, objw.nextObject());
85  		assertSame(f1, objw.nextObject());
86  
87  		assertSame(ta, objw.nextObject());
88  		assertSame(f0, objw.nextObject());
89  
90  		assertNull(objw.nextObject());
91  	}
92  
93  	@Test
94  	public void testTwoCommitDeepTree1() throws Exception {
95  		final RevBlob f0 = blob("0");
96  		final RevBlob f1 = blob("0v2");
97  		final RevTree ta = tree(file("a/b/0", f0));
98  		final RevTree tb = tree(file("a/b/1", f1));
99  		final RevCommit a = commit(ta);
100 		final RevCommit b = commit(tb, a);
101 		markStart(b);
102 
103 		assertCommit(b, objw.next());
104 		assertCommit(a, objw.next());
105 		assertNull(objw.next());
106 
107 		assertSame(tb, objw.nextObject());
108 		assertSame(get(tb, "a"), objw.nextObject());
109 		assertSame(get(tb, "a/b"), objw.nextObject());
110 		assertSame(f1, objw.nextObject());
111 
112 		assertSame(ta, objw.nextObject());
113 		assertSame(get(ta, "a"), objw.nextObject());
114 		assertSame(get(ta, "a/b"), objw.nextObject());
115 		assertSame(f0, objw.nextObject());
116 
117 		assertNull(objw.nextObject());
118 	}
119 
120 	@Test
121 	public void testTwoCommitDeepTree2() throws Exception {
122 		final RevBlob f1 = blob("1");
123 		final RevTree ta = tree(file("a/b/0", f1), file("a/c/q", f1));
124 		final RevTree tb = tree(file("a/b/1", f1), file("a/c/q", f1));
125 		final RevCommit a = commit(ta);
126 		final RevCommit b = commit(tb, a);
127 		markStart(b);
128 
129 		assertCommit(b, objw.next());
130 		assertCommit(a, objw.next());
131 		assertNull(objw.next());
132 
133 		assertSame(tb, objw.nextObject());
134 		assertSame(get(tb, "a"), objw.nextObject());
135 		assertSame(get(tb, "a/b"), objw.nextObject());
136 		assertSame(f1, objw.nextObject());
137 		assertSame(get(tb, "a/c"), objw.nextObject());
138 
139 		assertSame(ta, objw.nextObject());
140 		assertSame(get(ta, "a"), objw.nextObject());
141 		assertSame(get(ta, "a/b"), objw.nextObject());
142 
143 		assertNull(objw.nextObject());
144 	}
145 
146 	@Test
147 	public void testCull() throws Exception {
148 		final RevBlob f1 = blob("1");
149 		final RevBlob f2 = blob("2");
150 		final RevBlob f3 = blob("3");
151 		final RevBlob f4 = blob("4");
152 
153 		final RevTree ta = tree(file("a/1", f1), file("c/3", f3));
154 		final RevCommit a = commit(ta);
155 
156 		final RevTree tb = tree(file("a/1", f2), file("c/3", f3));
157 		final RevCommit b1 = commit(tb, a);
158 		final RevCommit b2 = commit(tb, b1);
159 
160 		final RevTree tc = tree(file("a/1", f4));
161 		final RevCommit c1 = commit(tc, a);
162 		final RevCommit c2 = commit(tc, c1);
163 
164 		markStart(b2);
165 		markUninteresting(c2);
166 
167 		assertCommit(b2, objw.next());
168 		assertCommit(b1, objw.next());
169 		assertNull(objw.next());
170 
171 		assertTrue(a.has(RevFlag.UNINTERESTING));
172 		assertTrue(ta.has(RevFlag.UNINTERESTING));
173 		assertTrue(f1.has(RevFlag.UNINTERESTING));
174 		assertTrue(f3.has(RevFlag.UNINTERESTING));
175 
176 		assertSame(tb, objw.nextObject());
177 		assertSame(get(tb, "a"), objw.nextObject());
178 		assertSame(f2, objw.nextObject());
179 		assertNull(objw.nextObject());
180 	}
181 
182 	@Test
183 	public void testEmptyTreeCorruption() throws Exception {
184 		ObjectId bId = ObjectId
185 				.fromString("abbbfafe3129f85747aba7bfac992af77134c607");
186 		final RevTree tree_root, tree_A, tree_AB;
187 		final RevCommit b;
188 		try (ObjectInserter inserter = db.newObjectInserter()) {
189 			ObjectId empty = inserter.insert(new TreeFormatter());
190 
191 			TreeFormatter A = new TreeFormatter();
192 			A.append("A", FileMode.TREE, empty);
193 			A.append("B", FileMode.TREE, empty);
194 			ObjectId idA = inserter.insert(A);
195 
196 			TreeFormatter root = new TreeFormatter();
197 			root.append("A", FileMode.TREE, idA);
198 			root.append("B", FileMode.REGULAR_FILE, bId);
199 			ObjectId idRoot = inserter.insert(root);
200 			inserter.flush();
201 
202 			tree_root = objw.parseTree(idRoot);
203 			tree_A = objw.parseTree(idA);
204 			tree_AB = objw.parseTree(empty);
205 			b = commit(tree_root);
206 		}
207 
208 		markStart(b);
209 
210 		assertCommit(b, objw.next());
211 		assertNull(objw.next());
212 
213 		assertSame(tree_root, objw.nextObject());
214 		assertSame(tree_A, objw.nextObject());
215 		assertSame(tree_AB, objw.nextObject());
216 		assertSame(rw.lookupBlob(bId), objw.nextObject());
217 		assertNull(objw.nextObject());
218 	}
219 
220 	@Test
221 	public void testSkipTreeWhenStartFromBlob() throws Exception {
222 		final RevBlob f1 = blob("1");
223 		objw.markStart(f1);
224 		assertSame(f1, objw.nextObject());
225 		objw.skipTree();
226 	}
227 }