1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 package org.eclipse.jgit.internal.storage.file;
44
45 import static java.nio.charset.StandardCharsets.UTF_8;
46 import static org.junit.Assert.assertFalse;
47 import static org.junit.Assert.assertTrue;
48
49 import java.io.File;
50 import java.io.FilenameFilter;
51 import java.io.IOException;
52 import java.io.PrintWriter;
53 import java.text.MessageFormat;
54 import java.util.Collection;
55 import java.util.Collections;
56 import java.util.Set;
57 import java.util.concurrent.Callable;
58 import java.util.concurrent.ExecutorService;
59 import java.util.concurrent.Executors;
60 import java.util.concurrent.Future;
61
62 import org.eclipse.jgit.internal.JGitText;
63 import org.eclipse.jgit.junit.RepositoryTestCase;
64 import org.eclipse.jgit.lib.ConfigConstants;
65 import org.eclipse.jgit.lib.Constants;
66 import org.eclipse.jgit.lib.ObjectId;
67 import org.eclipse.jgit.revwalk.RevCommit;
68 import org.eclipse.jgit.storage.file.FileBasedConfig;
69 import org.eclipse.jgit.util.FS;
70 import org.junit.Assume;
71 import org.junit.Rule;
72 import org.junit.Test;
73 import org.junit.rules.ExpectedException;
74
75 public class ObjectDirectoryTest extends RepositoryTestCase {
76
77 @Rule
78 public ExpectedException expectedEx = ExpectedException.none();
79
80 @Test
81 public void testConcurrentInsertionOfBlobsToTheSameNewFanOutDirectory()
82 throws Exception {
83 ExecutorService e = Executors.newCachedThreadPool();
84 for (int i=0; i < 100; ++i) {
85 ObjectDirectory dir = createBareRepository().getObjectDatabase();
86 for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(dir))) {
87 f.get();
88 }
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107 @Test
108 public void testScanningForPackfiles() throws Exception {
109 ObjectId unknownID = ObjectId
110 .fromString("c0ffee09d0b63d694bf49bc1e6847473f42d4a8c");
111 GC gc = new GC(db);
112 gc.setExpireAgeMillis(0);
113 gc.setPackExpireAgeMillis(0);
114
115
116
117 try (FileRepository receivingDB = new FileRepository(
118 db.getDirectory())) {
119
120
121 FileBasedConfig cfg = receivingDB.getConfig();
122 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
123 ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT, true);
124 cfg.save();
125
126
127
128 ObjectId id = commitFile("file.txt", "test", "master").getId();
129 gc.gc();
130 assertFalse(receivingDB.getObjectDatabase().has(unknownID));
131 assertTrue(receivingDB.getObjectDatabase().hasPackedObject(id));
132
133
134 File packsFolder = receivingDB.getObjectDatabase()
135 .getPackDirectory();
136
137
138
139 File tmpFile = new File(packsFolder, "1.tmp");
140 RevCommit id2 = commitFile("file.txt", "test2", "master");
141
142
143
144 fsTick(null);
145
146
147
148
149
150
151
152
153 assertTrue(tmpFile.createNewFile());
154 assertFalse(receivingDB.getObjectDatabase().has(unknownID));
155
156
157
158 gc.gc();
159
160
161
162
163
164
165
166
167
168
169
170 Thread.sleep(2600);
171
172 File[] ret = packsFolder.listFiles(new FilenameFilter() {
173 @Override
174 public boolean accept(File dir, String name) {
175 return name.endsWith(".pack");
176 }
177 });
178 assertTrue(ret != null && ret.length == 1);
179 FS fs = db.getFS();
180 Assume.assumeTrue(fs.lastModifiedInstant(tmpFile)
181 .equals(fs.lastModifiedInstant(ret[0])));
182
183
184 assertFalse(receivingDB.getObjectDatabase().has(unknownID));
185 assertTrue(receivingDB.getObjectDatabase().has(id2));
186 }
187 }
188
189 @Test
190 public void testShallowFile()
191 throws Exception {
192 FileRepository repository = createBareRepository();
193 ObjectDirectory dir = repository.getObjectDatabase();
194
195 String commit = "d3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
196 try (PrintWriter writer = new PrintWriter(
197 new File(repository.getDirectory(), Constants.SHALLOW),
198 UTF_8.name())) {
199 writer.println(commit);
200 }
201 Set<ObjectId> shallowCommits = dir.getShallowCommits();
202 assertTrue(shallowCommits.remove(ObjectId.fromString(commit)));
203 assertTrue(shallowCommits.isEmpty());
204 }
205
206 @Test
207 public void testShallowFileCorrupt()
208 throws Exception {
209 FileRepository repository = createBareRepository();
210 ObjectDirectory dir = repository.getObjectDatabase();
211
212 String commit = "X3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
213 try (PrintWriter writer = new PrintWriter(
214 new File(repository.getDirectory(), Constants.SHALLOW),
215 UTF_8.name())) {
216 writer.println(commit);
217 }
218
219 expectedEx.expect(IOException.class);
220 expectedEx.expectMessage(MessageFormat
221 .format(JGitText.get().badShallowLine, commit));
222 dir.getShallowCommits();
223 }
224
225 private Collection<Callable<ObjectId>> blobInsertersForTheSameFanOutDir(
226 final ObjectDirectory dir) {
227 Callable<ObjectId> callable = new Callable<ObjectId>() {
228 @Override
229 public ObjectId call() throws Exception {
230 return dir.newInserter().insert(Constants.OBJ_BLOB, new byte[0]);
231 }
232 };
233 return Collections.nCopies(4, callable);
234 }
235
236 }