View Javadoc
1   /*
2    * Copyright (C) 2012, Marc Strapetz <marc.strapetz@syntevo.com> 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  
15  import java.io.File;
16  import java.io.IOException;
17  
18  import org.eclipse.jgit.junit.JGitTestUtil;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.junit.Test;
21  
22  public class RevWalkShallowTest extends RevWalkTestCase {
23  
24  	// Accessing ==============================================================
25  
26  	@Test
27  	public void testDepth1() throws Exception {
28  		RevCommit[] commits = setupLinearChain();
29  
30  		createShallowFile(commits[3]);
31  		updateCommits(commits);
32  
33  		rw.markStart(commits[3]);
34  		assertCommit(commits[3], rw.next());
35  		assertNull(rw.next());
36  	}
37  
38  	@Test
39  	public void testDepth2() throws Exception {
40  		RevCommit[] commits = setupLinearChain();
41  
42  		createShallowFile(commits[2]);
43  		updateCommits(commits);
44  
45  		rw.markStart(commits[3]);
46  		assertCommit(commits[3], rw.next());
47  		assertCommit(commits[2], rw.next());
48  		assertNull(rw.next());
49  	}
50  
51  	@Test
52  	public void testDepth3() throws Exception {
53  		RevCommit[] commits = setupLinearChain();
54  
55  		createShallowFile(commits[1]);
56  		updateCommits(commits);
57  
58  		rw.markStart(commits[3]);
59  		assertCommit(commits[3], rw.next());
60  		assertCommit(commits[2], rw.next());
61  		assertCommit(commits[1], rw.next());
62  		assertNull(rw.next());
63  	}
64  
65  	@Test
66  	public void testObjectDirectorySnapshot() throws Exception {
67  		RevCommit[] commits = setupLinearChain();
68  
69  		createShallowFile(commits[3]);
70  		updateCommits(commits);
71  
72  		markStart(commits[3]);
73  		assertCommit(commits[3], rw.next());
74  		assertNull(rw.next());
75  
76  		createShallowFile(commits[2]);
77  		updateCommits(commits);
78  
79  		markStart(commits[3]);
80  		assertCommit(commits[3], rw.next());
81  		assertCommit(commits[2], rw.next());
82  		assertNull(rw.next());
83  	}
84  
85  	private RevCommit[] setupLinearChain() throws Exception {
86  		RevCommit[] commits = new RevCommit[4];
87  		RevCommit parent = null;
88  		for (int i = 0; i < commits.length; i++) {
89  			commits[i] = parent != null ? commit(parent) : commit();
90  			parent = commits[i];
91  		}
92  		return commits;
93  	}
94  
95  	@Test
96  	public void testMergeCommitOneParentShallow() throws Exception {
97  		RevCommit[] commits = setupMergeChain();
98  
99  		createShallowFile(commits[4]);
100 		updateCommits(commits);
101 
102 		markStart(commits[5]);
103 		assertCommit(commits[5], rw.next());
104 		assertCommit(commits[4], rw.next());
105 		assertCommit(commits[2], rw.next());
106 		assertCommit(commits[1], rw.next());
107 		assertCommit(commits[0], rw.next());
108 		assertNull(rw.next());
109 	}
110 
111 	@Test
112 	public void testMergeCommitEntirelyShallow() throws Exception {
113 		RevCommit[] commits = setupMergeChain();
114 
115 		createShallowFile(commits[2], commits[4]);
116 		updateCommits(commits);
117 
118 		markStart(commits[5]);
119 		assertCommit(commits[5], rw.next());
120 		assertCommit(commits[4], rw.next());
121 		assertCommit(commits[2], rw.next());
122 		assertNull(rw.next());
123 	}
124 
125 	private RevCommit[] setupMergeChain() throws Exception {
126 		/*-
127 		 * Create a history like this, diverging at 1 and merging at 5:
128 		 *
129 		 *      ---o--o       commits 3,4
130 		 *     /       \
131 		 * o--o--o------o   commits 0,1,2,5
132 		 */
133 		RevCommit[] commits = new RevCommit[6];
134 		commits[0] = commit();
135 		commits[1] = commit(commits[0]);
136 		commits[2] = commit(commits[1]);
137 		commits[3] = commit(commits[1]);
138 		commits[4] = commit(commits[3]);
139 		commits[5] = commit(commits[2], commits[4]);
140 		return commits;
141 	}
142 
143 	private void updateCommits(RevCommit[] commits) {
144 		// Relookup commits using the new RevWalk
145 		for (int i = 0; i < commits.length; i++) {
146 			commits[i] = rw.lookupCommit(commits[i].getId());
147 		}
148 	}
149 
150 	private void createShallowFile(ObjectId... shallowCommits)
151 			throws IOException {
152 		// Reset the RevWalk since the new shallow file invalidates the existing
153 		// RevWalk's shallow state.
154 		rw.close();
155 		rw = createRevWalk();
156 		StringBuilder builder = new StringBuilder();
157 		for (ObjectId commit : shallowCommits) {
158 			builder.append(commit.getName() + "\n");
159 		}
160 		JGitTestUtil.write(new File(db.getDirectory(), "shallow"),
161 				builder.toString());
162 	}
163 
164 	@Test
165 	public void testShallowCommitParse() throws Exception {
166 		RevCommit a = commit();
167 		RevCommit b = commit(a);
168 
169 		createShallowFile(b);
170 
171 		rw.close();
172 		rw = createRevWalk();
173 		b = rw.parseCommit(b);
174 
175 		markStart(b);
176 		assertCommit(b, rw.next());
177 		assertNull(rw.next());
178 	}
179 }