View Javadoc
1   /*
2    * Copyright (C) 2012, Marc Strapetz <marc.strapetz@syntevo.com>
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  
48  import java.io.File;
49  import java.io.IOException;
50  
51  import org.eclipse.jgit.junit.JGitTestUtil;
52  import org.eclipse.jgit.lib.ObjectId;
53  import org.junit.Test;
54  
55  public class RevWalkShallowTest extends RevWalkTestCase {
56  
57  	// Accessing ==============================================================
58  
59  	@Test
60  	public void testDepth1() throws Exception {
61  		final RevCommit a = commit();
62  		final RevCommit b = commit(a);
63  		final RevCommit c = commit(b);
64  		final RevCommit d = commit(c);
65  
66  		createShallowFile(d);
67  
68  		rw.reset();
69  		markStart(d);
70  		assertCommit(d, rw.next());
71  		assertNull(rw.next());
72  	}
73  
74  	@Test
75  	public void testDepth2() throws Exception {
76  		final RevCommit a = commit();
77  		final RevCommit b = commit(a);
78  		final RevCommit c = commit(b);
79  		final RevCommit d = commit(c);
80  
81  		createShallowFile(c);
82  
83  		rw.reset();
84  		markStart(d);
85  		assertCommit(d, rw.next());
86  		assertCommit(c, rw.next());
87  		assertNull(rw.next());
88  	}
89  
90  	@Test
91  	public void testDepth3() throws Exception {
92  		final RevCommit a = commit();
93  		final RevCommit b = commit(a);
94  		final RevCommit c = commit(b);
95  		final RevCommit d = commit(c);
96  
97  		createShallowFile(b);
98  
99  		rw.reset();
100 		markStart(d);
101 		assertCommit(d, rw.next());
102 		assertCommit(c, rw.next());
103 		assertCommit(b, rw.next());
104 		assertNull(rw.next());
105 	}
106 
107 	@Test
108 	public void testMergeCommitOneParentShallow() throws Exception {
109 		final RevCommit a = commit();
110 		final RevCommit b = commit(a);
111 		final RevCommit c = commit(b);
112 		final RevCommit d = commit(b);
113 		final RevCommit e = commit(d);
114 		final RevCommit merge = commit(c, e);
115 
116 		createShallowFile(e);
117 
118 		rw.reset();
119 		markStart(merge);
120 		assertCommit(merge, rw.next());
121 		assertCommit(e, rw.next());
122 		assertCommit(c, rw.next());
123 		assertCommit(b, rw.next());
124 		assertCommit(a, rw.next());
125 		assertNull(rw.next());
126 	}
127 
128 	@Test
129 	public void testMergeCommitEntirelyShallow() throws Exception {
130 		final RevCommit a = commit();
131 		final RevCommit b = commit(a);
132 		final RevCommit c = commit(b);
133 		final RevCommit d = commit(b);
134 		final RevCommit e = commit(d);
135 		final RevCommit merge = commit(c, e);
136 
137 		createShallowFile(c, e);
138 
139 		rw.reset();
140 		markStart(merge);
141 		assertCommit(merge, rw.next());
142 		assertCommit(e, rw.next());
143 		assertCommit(c, rw.next());
144 		assertNull(rw.next());
145 	}
146 
147 	@Test
148 	public void testObjectDirectorySnapshot() throws Exception {
149 		RevCommit a = commit();
150 		RevCommit b = commit(a);
151 		RevCommit c = commit(b);
152 		RevCommit d = commit(c);
153 
154 		createShallowFile(d);
155 
156 		rw.reset();
157 		markStart(d);
158 		assertCommit(d, rw.next());
159 		assertNull(rw.next());
160 
161 		rw = createRevWalk();
162 		a = rw.lookupCommit(a);
163 		b = rw.lookupCommit(b);
164 		c = rw.lookupCommit(c);
165 		d = rw.lookupCommit(d);
166 
167 		rw.reset();
168 		markStart(d);
169 		assertCommit(d, rw.next());
170 		assertNull(rw.next());
171 
172 		createShallowFile(c);
173 
174 		rw = createRevWalk();
175 		a = rw.lookupCommit(a);
176 		b = rw.lookupCommit(b);
177 		c = rw.lookupCommit(c);
178 		d = rw.lookupCommit(d);
179 
180 		rw.reset();
181 		markStart(d);
182 		assertCommit(d, rw.next());
183 		assertCommit(c, rw.next());
184 		assertNull(rw.next());
185 	}
186 
187 	private void createShallowFile(ObjectId... shallowCommits)
188 			throws IOException {
189 		final StringBuilder builder = new StringBuilder();
190 		for (ObjectId commit : shallowCommits)
191 			builder.append(commit.getName() + "\n");
192 		JGitTestUtil.write(new File(db.getDirectory(), "shallow"),
193 				builder.toString());
194 	}
195 }