View Javadoc
1   /*
2    * Copyright (C) 2012, Robin Rosenberg <robin.rosenberg@dewire.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  package org.eclipse.jgit.api;
44  
45  import static org.junit.Assert.assertEquals;
46  
47  import java.io.File;
48  import java.io.RandomAccessFile;
49  import java.util.Arrays;
50  import java.util.Collection;
51  
52  import org.eclipse.jgit.api.ResetCommand.ResetType;
53  import org.eclipse.jgit.junit.RepositoryTestCase;
54  import org.junit.After;
55  import org.junit.Before;
56  import org.junit.Ignore;
57  import org.junit.Test;
58  
59  public class HugeFileTest extends RepositoryTestCase {
60  
61  	private long t = System.currentTimeMillis();
62  
63  	private long lastt = t;
64  
65  	private Git git;
66  
67  	private void measure(String name) {
68  		long c = System.currentTimeMillis();
69  		System.out.println(name + ", dt=" + (c - lastt) / 1000.0 + "s");
70  		lastt = c;
71  	}
72  
73  	@Before
74  	public void before() {
75  		git = new Git(db);
76  	}
77  
78  	@After
79  	public void after() {
80  		if (git != null) {
81  			git.close();
82  		}
83  	}
84  
85  	@Ignore("Test takes way too long (~10 minutes) to be part of the standard suite")
86  	@Test
87  	public void testAddHugeFile() throws Exception {
88  		measure("Commencing test");
89  		File file = new File(db.getWorkTree(), "a.txt");
90  		RandomAccessFile rf = new RandomAccessFile(file, "rw");
91  		rf.setLength(4429185024L);
92  		rf.close();
93  		measure("Created file");
94  
95  		git.add().addFilepattern("a.txt").call();
96  		measure("Added file");
97  		assertEquals(
98  				"[a.txt, mode:100644, length:134217728, sha1:b8cfba97c2b962a44f080b3ca4e03b3204b6a350]",
99  				indexState(LENGTH | CONTENT_ID));
100 
101 		Status status = git.status().call();
102 		measure("Status after add");
103 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
104 		assertEquals(0, status.getChanged().size());
105 		assertEquals(0, status.getConflicting().size());
106 		assertEquals(0, status.getMissing().size());
107 		assertEquals(0, status.getModified().size());
108 		assertEquals(0, status.getRemoved().size());
109 		assertEquals(0, status.getUntracked().size());
110 
111 		// Does not change anything, but modified timestamp
112 		rf = new RandomAccessFile(file, "rw");
113 		rf.write(0);
114 		rf.close();
115 
116 		status = git.status().call();
117 		measure("Status after non-modifying update");
118 
119 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
120 		assertEquals(0, status.getChanged().size());
121 		assertEquals(0, status.getConflicting().size());
122 		assertEquals(0, status.getMissing().size());
123 		assertEquals(0, status.getModified().size());
124 		assertEquals(0, status.getRemoved().size());
125 		assertEquals(0, status.getUntracked().size());
126 
127 		// Change something
128 		rf = new RandomAccessFile(file, "rw");
129 		rf.write('a');
130 		rf.close();
131 
132 		status = git.status().call();
133 		measure("Status after modifying update");
134 
135 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
136 		assertEquals(0, status.getChanged().size());
137 		assertEquals(0, status.getConflicting().size());
138 		assertEquals(0, status.getMissing().size());
139 		assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
140 		assertEquals(0, status.getRemoved().size());
141 		assertEquals(0, status.getUntracked().size());
142 
143 		// Truncate mod 4G and re-establish equality
144 		rf = new RandomAccessFile(file, "rw");
145 		rf.setLength(134217728L);
146 		rf.write(0);
147 		rf.close();
148 
149 		status = git.status().call();
150 		measure("Status after truncating update");
151 
152 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
153 		assertEquals(0, status.getChanged().size());
154 		assertEquals(0, status.getConflicting().size());
155 		assertEquals(0, status.getMissing().size());
156 		assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
157 		assertEquals(0, status.getRemoved().size());
158 		assertEquals(0, status.getUntracked().size());
159 
160 		// Change something
161 		rf = new RandomAccessFile(file, "rw");
162 		rf.write('a');
163 		rf.close();
164 
165 		status = git.status().call();
166 		measure("Status after modifying and truncating update");
167 
168 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
169 		assertEquals(0, status.getChanged().size());
170 		assertEquals(0, status.getConflicting().size());
171 		assertEquals(0, status.getMissing().size());
172 		assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
173 		assertEquals(0, status.getRemoved().size());
174 		assertEquals(0, status.getUntracked().size());
175 
176 		// Truncate to entry length becomes negative int
177 		rf = new RandomAccessFile(file, "rw");
178 		rf.setLength(3429185024L);
179 		rf.write(0);
180 		rf.close();
181 
182 		git.add().addFilepattern("a.txt").call();
183 		measure("Added truncated file");
184 		assertEquals(
185 				"[a.txt, mode:100644, length:-865782272, sha1:59b3282f8f59f22d953df956ad3511bf2dc660fd]",
186 				indexState(LENGTH | CONTENT_ID));
187 
188 		status = git.status().call();
189 		measure("Status after status on truncated file");
190 
191 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
192 		assertEquals(0, status.getChanged().size());
193 		assertEquals(0, status.getConflicting().size());
194 		assertEquals(0, status.getMissing().size());
195 		assertEquals(0, status.getModified().size());
196 		assertEquals(0, status.getRemoved().size());
197 		assertEquals(0, status.getUntracked().size());
198 
199 		// Change something
200 		rf = new RandomAccessFile(file, "rw");
201 		rf.write('a');
202 		rf.close();
203 
204 		status = git.status().call();
205 		measure("Status after modifying and truncating update");
206 
207 		assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
208 		assertEquals(0, status.getChanged().size());
209 		assertEquals(0, status.getConflicting().size());
210 		assertEquals(0, status.getMissing().size());
211 		assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
212 		assertEquals(0, status.getRemoved().size());
213 		assertEquals(0, status.getUntracked().size());
214 
215 		git.commit().setMessage("make a commit").call();
216 		measure("After commit");
217 		status = git.status().call();
218 		measure("After status after commit");
219 
220 		assertEquals(0, status.getAdded().size());
221 		assertEquals(0, status.getChanged().size());
222 		assertEquals(0, status.getConflicting().size());
223 		assertEquals(0, status.getMissing().size());
224 		assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
225 		assertEquals(0, status.getRemoved().size());
226 		assertEquals(0, status.getUntracked().size());
227 
228 		git.reset().setMode(ResetType.HARD).call();
229 		measure("After reset --hard");
230 		assertEquals(
231 				"[a.txt, mode:100644, length:-865782272, sha1:59b3282f8f59f22d953df956ad3511bf2dc660fd]",
232 				indexState(LENGTH | CONTENT_ID));
233 
234 		status = git.status().call();
235 		measure("Status after hard reset");
236 
237 		assertEquals(0, status.getAdded().size());
238 		assertEquals(0, status.getChanged().size());
239 		assertEquals(0, status.getConflicting().size());
240 		assertEquals(0, status.getMissing().size());
241 		assertEquals(0, status.getModified().size());
242 		assertEquals(0, status.getRemoved().size());
243 		assertEquals(0, status.getUntracked().size());
244 	}
245 
246 	private static void assertCollectionEquals(Collection<?> asList,
247 			Collection<?> added) {
248 		assertEquals(asList.toString(), added.toString());
249 	}
250 
251 }