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.assertThrows;
48 import static org.junit.Assert.assertTrue;
49
50 import java.io.File;
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.Test;
72
73 public class ObjectDirectoryTest extends RepositoryTestCase {
74
75 @Test
76 public void testConcurrentInsertionOfBlobsToTheSameNewFanOutDirectory()
77 throws Exception {
78 ExecutorService e = Executors.newCachedThreadPool();
79 for (int i=0; i < 100; ++i) {
80 ObjectDirectory dir = createBareRepository().getObjectDatabase();
81 for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(dir))) {
82 f.get();
83 }
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 @Test
103 public void testScanningForPackfiles() throws Exception {
104 ObjectId unknownID = ObjectId
105 .fromString("c0ffee09d0b63d694bf49bc1e6847473f42d4a8c");
106 GC gc = new GC(db);
107 gc.setExpireAgeMillis(0);
108 gc.setPackExpireAgeMillis(0);
109
110
111
112 try (FileRepository receivingDB = new FileRepository(
113 db.getDirectory())) {
114
115
116 FileBasedConfig cfg = receivingDB.getConfig();
117 cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
118 ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT, true);
119 cfg.save();
120
121
122
123 ObjectId id = commitFile("file.txt", "test", "master").getId();
124 gc.gc();
125 assertFalse(receivingDB.getObjectDatabase().has(unknownID));
126 assertTrue(receivingDB.getObjectDatabase().hasPackedObject(id));
127
128
129 File packsFolder = receivingDB.getObjectDatabase()
130 .getPackDirectory();
131
132
133
134 File tmpFile = new File(packsFolder, "1.tmp");
135 RevCommit id2 = commitFile("file.txt", "test2", "master");
136
137
138
139 fsTick(null);
140
141
142
143
144
145
146
147
148 assertTrue(tmpFile.createNewFile());
149 assertFalse(receivingDB.getObjectDatabase().has(unknownID));
150
151
152
153 gc.gc();
154
155
156
157
158
159
160
161
162
163
164
165 Thread.sleep(2600);
166
167 File[] ret = packsFolder.listFiles(
168 (File dir, String name) -> name.endsWith(".pack"));
169 assertTrue(ret != null && ret.length == 1);
170 FS fs = db.getFS();
171 Assume.assumeTrue(fs.lastModifiedInstant(tmpFile)
172 .equals(fs.lastModifiedInstant(ret[0])));
173
174
175 assertFalse(receivingDB.getObjectDatabase().has(unknownID));
176 assertTrue(receivingDB.getObjectDatabase().has(id2));
177 }
178 }
179
180 @Test
181 public void testShallowFile()
182 throws Exception {
183 FileRepository repository = createBareRepository();
184 ObjectDirectory dir = repository.getObjectDatabase();
185
186 String commit = "d3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
187 try (PrintWriter writer = new PrintWriter(
188 new File(repository.getDirectory(), Constants.SHALLOW),
189 UTF_8.name())) {
190 writer.println(commit);
191 }
192 Set<ObjectId> shallowCommits = dir.getShallowCommits();
193 assertTrue(shallowCommits.remove(ObjectId.fromString(commit)));
194 assertTrue(shallowCommits.isEmpty());
195 }
196
197 @Test
198 public void testShallowFileCorrupt() throws Exception {
199 FileRepository repository = createBareRepository();
200 ObjectDirectory dir = repository.getObjectDatabase();
201
202 String commit = "X3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
203 try (PrintWriter writer = new PrintWriter(
204 new File(repository.getDirectory(), Constants.SHALLOW),
205 UTF_8.name())) {
206 writer.println(commit);
207 }
208 assertThrows(
209 MessageFormat.format(JGitText.get().badShallowLine, commit),
210 IOException.class, () -> dir.getShallowCommits());
211 }
212
213 private Collection<Callable<ObjectId>> blobInsertersForTheSameFanOutDir(
214 final ObjectDirectory dir) {
215 Callable<ObjectId> callable = () -> dir.newInserter()
216 .insert(Constants.OBJ_BLOB, new byte[0]);
217 return Collections.nCopies(4, callable);
218 }
219
220 }