1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.api;
11
12 import static org.junit.Assert.assertEquals;
13
14 import java.io.File;
15 import java.io.RandomAccessFile;
16 import java.util.Arrays;
17 import java.util.Collection;
18
19 import org.eclipse.jgit.api.ResetCommand.ResetType;
20 import org.eclipse.jgit.junit.RepositoryTestCase;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Ignore;
24 import org.junit.Test;
25
26 public class HugeFileTest extends RepositoryTestCase {
27
28 private long t = System.currentTimeMillis();
29
30 private long lastt = t;
31
32 private Git git;
33
34 private void measure(String name) {
35 long c = System.currentTimeMillis();
36 System.out.println(name + ", dt=" + (c - lastt) / 1000.0 + "s");
37 lastt = c;
38 }
39
40 @Before
41 public void before() {
42 git = new Git(db);
43 }
44
45 @After
46 public void after() {
47 if (git != null) {
48 git.close();
49 }
50 }
51
52 @Ignore("Test takes way too long (~10 minutes) to be part of the standard suite")
53 @Test
54 public void testAddHugeFile() throws Exception {
55 measure("Commencing test");
56 File file = new File(db.getWorkTree(), "a.txt");
57 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
58 rf.setLength(4429185024L);
59 }
60 measure("Created file");
61
62 git.add().addFilepattern("a.txt").call();
63 measure("Added file");
64 assertEquals(
65 "[a.txt, mode:100644, length:134217728, sha1:b8cfba97c2b962a44f080b3ca4e03b3204b6a350]",
66 indexState(LENGTH | CONTENT_ID));
67
68 Status status = git.status().call();
69 measure("Status after add");
70 assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
71 assertEquals(0, status.getChanged().size());
72 assertEquals(0, status.getConflicting().size());
73 assertEquals(0, status.getMissing().size());
74 assertEquals(0, status.getModified().size());
75 assertEquals(0, status.getRemoved().size());
76 assertEquals(0, status.getUntracked().size());
77
78
79 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
80 rf.write(0);
81 }
82
83 status = git.status().call();
84 measure("Status after non-modifying update");
85
86 assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
87 assertEquals(0, status.getChanged().size());
88 assertEquals(0, status.getConflicting().size());
89 assertEquals(0, status.getMissing().size());
90 assertEquals(0, status.getModified().size());
91 assertEquals(0, status.getRemoved().size());
92 assertEquals(0, status.getUntracked().size());
93
94
95 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
96 rf.write('a');
97 }
98
99 status = git.status().call();
100 measure("Status after modifying update");
101
102 assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
103 assertEquals(0, status.getChanged().size());
104 assertEquals(0, status.getConflicting().size());
105 assertEquals(0, status.getMissing().size());
106 assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
107 assertEquals(0, status.getRemoved().size());
108 assertEquals(0, status.getUntracked().size());
109
110
111 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
112 rf.setLength(134217728L);
113 rf.write(0);
114 }
115
116 status = git.status().call();
117 measure("Status after truncating 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 assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
124 assertEquals(0, status.getRemoved().size());
125 assertEquals(0, status.getUntracked().size());
126
127
128 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
129 rf.write('a');
130 }
131
132 status = git.status().call();
133 measure("Status after modifying and truncating 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
144 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
145 rf.setLength(3429185024L);
146 rf.write(0);
147 }
148
149 git.add().addFilepattern("a.txt").call();
150 measure("Added truncated file");
151 assertEquals(
152 "[a.txt, mode:100644, length:-865782272, sha1:59b3282f8f59f22d953df956ad3511bf2dc660fd]",
153 indexState(LENGTH | CONTENT_ID));
154
155 status = git.status().call();
156 measure("Status after status on truncated file");
157
158 assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
159 assertEquals(0, status.getChanged().size());
160 assertEquals(0, status.getConflicting().size());
161 assertEquals(0, status.getMissing().size());
162 assertEquals(0, status.getModified().size());
163 assertEquals(0, status.getRemoved().size());
164 assertEquals(0, status.getUntracked().size());
165
166
167 try (RandomAccessFile rf = new RandomAccessFile(file, "rw")) {
168 rf.write('a');
169 }
170
171 status = git.status().call();
172 measure("Status after modifying and truncating update");
173
174 assertCollectionEquals(Arrays.asList("a.txt"), status.getAdded());
175 assertEquals(0, status.getChanged().size());
176 assertEquals(0, status.getConflicting().size());
177 assertEquals(0, status.getMissing().size());
178 assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
179 assertEquals(0, status.getRemoved().size());
180 assertEquals(0, status.getUntracked().size());
181
182 git.commit().setMessage("make a commit").call();
183 measure("After commit");
184 status = git.status().call();
185 measure("After status after commit");
186
187 assertEquals(0, status.getAdded().size());
188 assertEquals(0, status.getChanged().size());
189 assertEquals(0, status.getConflicting().size());
190 assertEquals(0, status.getMissing().size());
191 assertCollectionEquals(Arrays.asList("a.txt"), status.getModified());
192 assertEquals(0, status.getRemoved().size());
193 assertEquals(0, status.getUntracked().size());
194
195 git.reset().setMode(ResetType.HARD).call();
196 measure("After reset --hard");
197 assertEquals(
198 "[a.txt, mode:100644, length:-865782272, sha1:59b3282f8f59f22d953df956ad3511bf2dc660fd]",
199 indexState(LENGTH | CONTENT_ID));
200
201 status = git.status().call();
202 measure("Status after hard reset");
203
204 assertEquals(0, status.getAdded().size());
205 assertEquals(0, status.getChanged().size());
206 assertEquals(0, status.getConflicting().size());
207 assertEquals(0, status.getMissing().size());
208 assertEquals(0, status.getModified().size());
209 assertEquals(0, status.getRemoved().size());
210 assertEquals(0, status.getUntracked().size());
211 }
212
213 private static void assertCollectionEquals(Collection<?> asList,
214 Collection<?> added) {
215 assertEquals(asList.toString(), added.toString());
216 }
217
218 }